@@ -9,11 +9,9 @@ import 'package:analyzer/file_system/memory_file_system.dart';
99import 'package:analyzer/src/test_utilities/package_config_file_builder.dart' ;
1010import 'package:analyzer/src/util/file_paths.dart' as file_paths;
1111import '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.
2422mixin 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
180198extension 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 }
0 commit comments