[analyzer] Move convertPath from MemoryResourceProvider to an extension

+ make the implementation of ResourceProviderMixin methods not depend on MemoryResourceProvider.

This is a step towards being able to share more code between tests that use different kinds of ResourceProviders. Although ResourceProviderMixin  currently still has a MemoryResourceProvider, all of the methods themselves can work against any ResourceProvider.

convertPath was moved to an extension in test_utilities (rather than to the base ResourceProvider) because it seems quite test-specific (it assumes any absolute paths are relative to drive C - something we'll need to update to use for non-memory tests.

Change-Id: Ibb3cfb31ebbdac6410868f0395bd19f9ce7b0e18
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/350380
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Danny Tuppeny 2024-02-05 19:16:23 +00:00 committed by Commit Queue
parent 3495a21fc8
commit 2bf9fefa90
3 changed files with 35 additions and 49 deletions

View file

@ -895,17 +895,3 @@ mixin _ContextRoot on ResourceProviderMixin {
);
}
}
extension on PhysicalResourceProvider {
/// Converts the given posix [filePath] to conform to this provider's path
/// context.
String convertPath(String filePath) {
if (pathContext.style == path.windows.style) {
if (filePath.startsWith(path.posix.separator)) {
filePath = r'C:' + filePath;
}
return filePath.replaceAll(path.posix.separator, path.windows.separator);
}
return filePath;
}
}

View file

@ -9,6 +9,7 @@ import 'dart:typed_data';
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/source/source.dart';
import 'package:analyzer/src/source/source_resource.dart';
import 'package:analyzer/src/test_utilities/resource_provider_mixin.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as pathos;
import 'package:watcher/watcher.dart' hide Watcher;
@ -43,7 +44,7 @@ class MemoryResourceProvider implements ResourceProvider {
this.delayWatcherInitialization,
}) : _pathContext = context ??= pathos.style == pathos.Style.windows
// On Windows, ensure that the current drive matches
// the drive inserted by MemoryResourceProvider.convertPath
// the drive inserted by ResourceProvider.convertPath
// so that packages are mapped to the correct drive
? pathos.Context(current: 'C:\\')
: pathos.context;
@ -55,15 +56,8 @@ class MemoryResourceProvider implements ResourceProvider {
///
/// This is a utility method for testing; paths passed in to other methods in
/// this class are never converted automatically.
String convertPath(String path) {
if (pathContext.style == pathos.windows.style) {
if (path.startsWith(pathos.posix.separator)) {
path = r'C:' + path;
}
path = path.replaceAll(pathos.posix.separator, pathos.windows.separator);
}
return path;
}
String convertPath(String filePath) =>
ResourceProviderExtensions(this).convertPath(filePath);
/// Delete the file with the given path.
void deleteFile(String path) {
@ -144,11 +138,8 @@ class MemoryResourceProvider implements ResourceProvider {
throw FileSystemException(path, 'Not a file.');
}
_pathToData[path] = _FileData(
bytes: const Utf8Encoder().convert(content),
timeStamp: nextStamp++,
);
_notifyWatchers(path, ChangeType.MODIFY);
var bytes = const Utf8Encoder().convert(content);
_setFileContent(path, bytes);
}
File newFile(String path, String content) {
@ -160,17 +151,7 @@ class MemoryResourceProvider implements ResourceProvider {
_ensureAbsoluteAndNormalized(path);
bytes = bytes is Uint8List ? bytes : Uint8List.fromList(bytes);
var parentPath = pathContext.dirname(path);
var parentData = _newFolder(parentPath);
_addToParentFolderData(parentData, path);
_pathToData[path] = _FileData(
bytes: bytes,
timeStamp: nextStamp++,
);
_notifyWatchers(path, ChangeType.ADD);
return _MemoryFile(this, path);
return _setFileContent(path, bytes);
}
Folder newFolder(String path) {
@ -303,16 +284,19 @@ class MemoryResourceProvider implements ResourceProvider {
return result;
}
void _setFileContent(String path, Uint8List bytes) {
File _setFileContent(String path, Uint8List bytes) {
var parentPath = pathContext.dirname(path);
var parentData = _newFolder(parentPath);
_addToParentFolderData(parentData, path);
var exists = _pathToData.containsKey(path);
_pathToData[path] = _FileData(
bytes: bytes,
timeStamp: nextStamp++,
);
_notifyWatchers(path, ChangeType.MODIFY);
_notifyWatchers(path, exists ? ChangeType.MODIFY : ChangeType.ADD);
return _MemoryFile(this, path);
}
}

View file

@ -8,6 +8,7 @@ import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/file_system/memory_file_system.dart';
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
import 'package:path/path.dart' as path;
import 'package:path/path.dart';
/// A mixin for test classes that adds a memory-backed [ResourceProvider] and
/// utility methods for manipulating the file system.
@ -26,7 +27,7 @@ mixin ResourceProviderMixin {
path.Context get pathContext => resourceProvider.pathContext;
String convertPath(String path) => resourceProvider.convertPath(path);
String convertPath(String filePath) => resourceProvider.convertPath(filePath);
void deleteAnalysisOptionsYamlFile(String directoryPath) {
var path = join(directoryPath, file_paths.analysisOptionsYaml);
@ -35,7 +36,7 @@ mixin ResourceProviderMixin {
void deleteFile(String path) {
String convertedPath = convertPath(path);
resourceProvider.deleteFile(convertedPath);
resourceProvider.getFile(convertedPath).delete();
}
void deleteFile2(File file) {
@ -44,7 +45,7 @@ mixin ResourceProviderMixin {
void deleteFolder(String path) {
String convertedPath = convertPath(path);
resourceProvider.deleteFolder(convertedPath);
resourceProvider.getFolder(convertedPath).delete();
}
void deletePackageConfigJsonFile(String directoryPath) {
@ -83,7 +84,7 @@ mixin ResourceProviderMixin {
void modifyFile(String path, String content) {
String convertedPath = convertPath(path);
resourceProvider.modifyFile(convertedPath, content);
resourceProvider.getFile(convertedPath).writeAsStringSync(content);
}
void modifyFile2(File file, String content) {
@ -112,18 +113,18 @@ mixin ResourceProviderMixin {
File newFile(String path, String content) {
String convertedPath = convertPath(path);
return resourceProvider.newFile(convertedPath, content);
return resourceProvider.getFile(convertedPath)..writeAsStringSync(content);
}
@Deprecated('Use newFile() instead')
File newFile2(String path, String content) {
String convertedPath = convertPath(path);
return resourceProvider.newFile(convertedPath, content);
return resourceProvider.getFile(content)..writeAsStringSync(convertedPath);
}
Folder newFolder(String path) {
String convertedPath = convertPath(path);
return resourceProvider.newFolder(convertedPath);
return resourceProvider.getFolder(convertedPath)..create();
}
File newPackageConfigJsonFile(String directoryPath, String content) {
@ -149,3 +150,18 @@ mixin ResourceProviderMixin {
return toUri(path).toString();
}
}
extension ResourceProviderExtensions on ResourceProvider {
/// Convert the given posix [path] to conform to this provider's path context.
///
/// This is a utility method for testing.
String convertPath(String filePath) {
if (pathContext.style == windows.style) {
if (filePath.startsWith(posix.separator)) {
filePath = r'C:' + filePath;
}
filePath = filePath.replaceAll(posix.separator, windows.separator);
}
return filePath;
}
}