Skip to content

Commit b378d24

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: Improve API of ResourceProviderMixin
Mostly add doc comments, before moving this to public API in the analyzer_testing package. * Remove `createResourceProvider`, could be inlined. * Rename `newBlazeBuildFile` to `newBazelBuildFile`. * Remove `deleteFile2`; only used once, with an easy replacement. Change-Id: I296a569b7f68ae9d82e8cb3808931f5590b992cd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/426903 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent aa8542e commit b378d24

File tree

4 files changed

+59
-39
lines changed

4 files changed

+59
-39
lines changed

pkg/analysis_server/test/domain_analysis_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class AnalysisDomainBlazeTest extends _AnalysisDomainTest {
5151

5252
Future<void> test_fileSystem_changeFile_buildFile_legacy() async {
5353
// Make it a Blaze package.
54-
newBlazeBuildFile(myPackageRootPath, r'''
54+
newBazelBuildFile(myPackageRootPath, r'''
5555
# foo
5656
''');
5757

@@ -70,7 +70,7 @@ AnalysisErrors
7070
''');
7171

7272
// Change BUILD file, nothing interesting.
73-
newBlazeBuildFile(myPackageRootPath, r'''
73+
newBazelBuildFile(myPackageRootPath, r'''
7474
# bar
7575
''');
7676

pkg/analyzer/lib/src/test_utilities/resource_provider_mixin.dart

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ import 'package:analyzer/file_system/memory_file_system.dart';
99
import 'package:analyzer/src/test_utilities/package_config_file_builder.dart';
1010
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
1111
import 'package:path/path.dart' as path;
12-
import 'package:path/path.dart';
1312

14-
/// A mixin for test classes that adds a memory-backed [ResourceProvider] (that
15-
/// can be overriden via [createResourceProvider]) and utility methods for
16-
/// manipulating the file system.
13+
/// A mixin for test classes that adds a memory-backed [ResourceProvider] and
14+
/// utility methods for manipulating the file system.
1715
///
1816
/// The resource provider will use paths in the same style as the current
1917
/// platform unless the `TEST_ANALYZER_WINDOWS_PATHS` environment variable is
@@ -22,37 +20,35 @@ import 'package:path/path.dart';
2220
/// The utility methods all take a posix style path and convert it as
2321
/// appropriate for the actual platform.
2422
mixin ResourceProviderMixin {
25-
late final resourceProvider = createResourceProvider();
23+
late final ResourceProvider resourceProvider =
24+
Platform.environment['TEST_ANALYZER_WINDOWS_PATHS'] == 'true'
25+
? MemoryResourceProvider(context: path.windows)
26+
: MemoryResourceProvider();
2627

28+
/// The path context of [resourceProvider].
2729
path.Context get pathContext => resourceProvider.pathContext;
2830

31+
/// Converts the given posix [filePath] to conform to [resourceProvider]'s
32+
/// path context.
2933
String convertPath(String filePath) => resourceProvider.convertPath(filePath);
3034

31-
ResourceProvider createResourceProvider() {
32-
return Platform.environment['TEST_ANALYZER_WINDOWS_PATHS'] == 'true'
33-
? MemoryResourceProvider(context: path.windows)
34-
: MemoryResourceProvider();
35-
}
36-
35+
/// Deletes the analysis options YAML file at [directoryPath].
3736
void deleteAnalysisOptionsYamlFile(String directoryPath) {
3837
var path = join(directoryPath, file_paths.analysisOptionsYaml);
3938
deleteFile(path);
4039
}
4140

41+
/// Deletes the file at [path].
4242
void deleteFile(String path) {
43-
String convertedPath = convertPath(path);
44-
resourceProvider.getFile(convertedPath).delete();
45-
}
46-
47-
void deleteFile2(File file) {
48-
deleteFile(file.path);
43+
resourceProvider.getFile(convertPath(path)).delete();
4944
}
5045

46+
/// Deletes the folder at [path].
5147
void deleteFolder(String path) {
52-
String convertedPath = convertPath(path);
53-
resourceProvider.getFolder(convertedPath).delete();
48+
resourceProvider.getFolder(convertPath(path)).delete();
5449
}
5550

51+
/// Deletes the `package_config.json` file at [directoryPath].
5652
void deletePackageConfigJsonFile(String directoryPath) {
5753
var path = join(
5854
directoryPath,
@@ -62,20 +58,24 @@ mixin ResourceProviderMixin {
6258
deleteFile(path);
6359
}
6460

61+
/// Returns [uri] as a String.
6562
String fromUri(Uri uri) {
6663
return resourceProvider.pathContext.fromUri(uri);
6764
}
6865

66+
/// Gets the [File] at [path].
6967
File getFile(String path) {
7068
String convertedPath = convertPath(path);
7169
return resourceProvider.getFile(convertedPath);
7270
}
7371

72+
/// Gets the [Folder] at [path].
7473
Folder getFolder(String path) {
7574
String convertedPath = convertPath(path);
7675
return resourceProvider.getFolder(convertedPath);
7776
}
7877

78+
/// Joins the part paths as per [path.Context.join].
7979
String join(
8080
String part1, [
8181
String? part2,
@@ -96,46 +96,57 @@ mixin ResourceProviderMixin {
9696
part8,
9797
);
9898

99+
/// Writes [content] to the file at [path].
99100
void modifyFile(String path, String content) {
100101
String convertedPath = convertPath(path);
101102
resourceProvider.getFile(convertedPath).writeAsStringSync(content);
102103
}
103104

105+
/// Writes [content] to [file].
104106
void modifyFile2(File file, String content) {
105107
modifyFile(file.path, content);
106108
}
107109

110+
/// Writes a new `analysis_options.yaml` file at [directoryPath] with
111+
/// [content].
108112
File newAnalysisOptionsYamlFile(String directoryPath, String content) {
109113
String path = join(directoryPath, file_paths.analysisOptionsYaml);
110114
return newFile(path, content);
111115
}
112116

113-
File newBlazeBuildFile(String directoryPath, String content) {
114-
String path = join(directoryPath, file_paths.blazeBuild);
115-
return newFile(path, content);
117+
/// Creates a new Bazel BUILD file at [directoryPath] with [content].
118+
File newBazelBuildFile(String directoryPath, String content) {
119+
String filePath = join(directoryPath, file_paths.blazeBuild);
120+
return newFile(filePath, content);
116121
}
117122

123+
/// Creates a new BUILD.gn file at [directoryPath] with [content].
118124
File newBuildGnFile(String directoryPath, String content) {
119125
String path = join(directoryPath, file_paths.buildGn);
120126
return newFile(path, content);
121127
}
122128

129+
/// Writes [content] to the file at [path].
123130
File newFile(String path, String content) {
124131
String convertedPath = convertPath(path);
125132
return resourceProvider.getFile(convertedPath)..writeAsStringSync(content);
126133
}
127134

135+
/// Creates and returns a new [Folder] at [path].
128136
Folder newFolder(String path) {
129137
String convertedPath = convertPath(path);
130138
return resourceProvider.getFolder(convertedPath)..create();
131139
}
132140

141+
/// Creates and returns a new [Link] at [path] to [target].
133142
Link newLink(String path, String target) {
134143
String convertedPath = convertPath(path);
135144
String convertedTarget = convertPath(target);
136145
return resourceProvider.getLink(convertedPath)..create(convertedTarget);
137146
}
138147

148+
/// Writes a `.dart_tool/package_config.json` file at [directoryPath] with
149+
/// [content].
139150
File newPackageConfigJsonFile(String directoryPath, String content) {
140151
String path = join(
141152
directoryPath,
@@ -145,6 +156,7 @@ mixin ResourceProviderMixin {
145156
return newFile(path, content);
146157
}
147158

159+
/// Writes a `.dart_tool/package_config.json` file at [directoryPath].
148160
File newPackageConfigJsonFileFromBuilder(
149161
String directoryPath,
150162
PackageConfigFileBuilder builder,
@@ -153,11 +165,14 @@ mixin ResourceProviderMixin {
153165
return newPackageConfigJsonFile(directoryPath, content);
154166
}
155167

168+
/// Writes a `pubspec.yaml` file at [directoryPath] with [content].
156169
File newPubspecYamlFile(String directoryPath, String content) {
157170
String path = join(directoryPath, file_paths.pubspecYaml);
158171
return newFile(path, content);
159172
}
160173

174+
/// Writes a new `.dart_tool/package_config.json` file for a package rooted at
175+
/// [packagePath], named [name].
161176
void newSinglePackageConfigJsonFile({
162177
required String packagePath,
163178
required String name,
@@ -167,26 +182,31 @@ mixin ResourceProviderMixin {
167182
newPackageConfigJsonFileFromBuilder(packagePath, builder);
168183
}
169184

185+
/// Converts [path] to a URI.
170186
Uri toUri(String path) {
171187
path = convertPath(path);
172188
return resourceProvider.pathContext.toUri(path);
173189
}
174190

191+
/// Converts [path] to a URI and returns the normalized String representation
192+
/// of the URI.
175193
String toUriStr(String path) {
176194
return toUri(path).toString();
177195
}
178196
}
179197

180198
extension ResourceProviderExtensions on ResourceProvider {
181-
/// Convert the given posix [path] to conform to this provider's path context.
182-
///
183-
/// This is a utility method for testing.
199+
/// Converts the given posix [filePath] to conform to this provider's path
200+
/// context.
184201
String convertPath(String filePath) {
185-
if (pathContext.style == windows.style) {
186-
if (filePath.startsWith(posix.separator)) {
202+
if (pathContext.style == path.windows.style) {
203+
if (filePath.startsWith(path.posix.separator)) {
187204
filePath = r'C:' + filePath;
188205
}
189-
filePath = filePath.replaceAll(posix.separator, windows.separator);
206+
filePath = filePath.replaceAll(
207+
path.posix.separator,
208+
path.windows.separator,
209+
);
190210
}
191211
return filePath;
192212
}

pkg/analyzer/test/src/dart/analysis/context_locator_test.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ analyzer:
333333
test_locateRoots_multiple_dirAndNestedDir_outerIsBlaze_innerConfigurationFiles() {
334334
var outerRootFolder = newFolder('/outer');
335335
newFile('$outerRootFolder/${file_paths.blazeWorkspaceMarker}', '');
336-
newBlazeBuildFile('$outerRootFolder', '');
336+
newBazelBuildFile('$outerRootFolder', '');
337337
var innerRootFolder = newFolder('/outer/examples/inner');
338338
var innerOptionsFile = newAnalysisOptionsYamlFile('$innerRootFolder', '');
339339
var innerPackagesFile = newPackageConfigJsonFile('$innerRootFolder', '');
@@ -528,8 +528,8 @@ analyzer:
528528

529529
newFile('$workspacePath1/${file_paths.blazeWorkspaceMarker}', '');
530530
newFile('$workspacePath2/${file_paths.blazeWorkspaceMarker}', '');
531-
newBlazeBuildFile(pkgPath1, '');
532-
newBlazeBuildFile(pkgPath2, '');
531+
newBazelBuildFile(pkgPath1, '');
532+
newBazelBuildFile(pkgPath2, '');
533533

534534
var folder1 = newFolder('$pkgPath1/lib/folder1');
535535
var folder2 = newFolder('$pkgPath2/lib/folder2');
@@ -740,8 +740,8 @@ analyzer:
740740

741741
newFile('$workspacePath1/${file_paths.blazeWorkspaceMarker}', '');
742742
newFile('$workspacePath2/${file_paths.blazeWorkspaceMarker}', '');
743-
newBlazeBuildFile(pkgPath1, '');
744-
newBlazeBuildFile(pkgPath2, '');
743+
newBazelBuildFile(pkgPath1, '');
744+
newBazelBuildFile(pkgPath2, '');
745745

746746
var file1 = newFile('$pkgPath1/lib/file1.dart', '');
747747
var file2 = newFile('$pkgPath2/lib/file2.dart', '');
@@ -774,8 +774,8 @@ analyzer:
774774
var barPath = '$workspacePath/bar';
775775

776776
newFile('$workspacePath/${file_paths.blazeWorkspaceMarker}', '');
777-
newBlazeBuildFile(fooPath, '');
778-
newBlazeBuildFile(barPath, '');
777+
newBazelBuildFile(fooPath, '');
778+
newBazelBuildFile(barPath, '');
779779

780780
var fooFile = newFile('$fooPath/lib/foo.dart', '');
781781
var barFile = newFile('$barPath/lib/bar.dart', '');
@@ -1663,7 +1663,7 @@ ${getFolder(outPath).path}
16631663
var myPackage = getFolder(myPackagePath);
16641664

16651665
newFile('$workspacePath/${file_paths.blazeWorkspaceMarker}', '');
1666-
var buildFile = newBlazeBuildFile(myPackagePath, '');
1666+
var buildFile = newBazelBuildFile(myPackagePath, '');
16671667
var pubspecYamlFile = newPubspecYamlFile(myPackagePath, '');
16681668
var myFile = newFile('$myPackagePath/lib/my.dart', '');
16691669

pkg/analyzer/test/src/dart/analysis/driver_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4818,7 +4818,7 @@ final a = new A();
48184818
''');
48194819

48204820
// Remove `a`, so `b` is reanalyzed and has an error.
4821-
deleteFile2(a);
4821+
deleteFile(a.path);
48224822
driver.removeFile2(a);
48234823
await assertEventsText(collector, r'''
48244824
[status] working

0 commit comments

Comments
 (0)