Skip to content

Commit e35e665

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer_testing: convert all modifyFile users to modifyFile2
This also involves tidying up blaze_watcher_test, in order to easily get back a File object from a helper method. Change-Id: If9dc812dc3a97a205dee600b89c3e8490e9efcba Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/429201 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 7882084 commit e35e665

File tree

7 files changed

+30
-39
lines changed

7 files changed

+30
-39
lines changed

pkg/analysis_server/test/analysis/notification_errors_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,15 @@ analyzer:
170170
'.dart_tool/package_config.json',
171171
);
172172
var generatedProject = join(testPackageRootPath, '.dart_tool/foo');
173-
var generatedFile = join(generatedProject, 'lib', 'foo.dart');
173+
var generatedFilePath = join(generatedProject, 'lib', 'foo.dart');
174174

175175
// Add the generated project into package_config.json.
176176
var config = PackageConfigFileBuilder();
177177
config.add(name: 'foo', rootPath: generatedProject);
178178
newFile(configPath, config.toContent(toUriStr: toUriStr));
179179

180180
// Set up project that references the class prior to initial analysis.
181-
newFile(generatedFile, 'class A {}');
181+
var generatedFile = newFile(generatedFilePath, 'class A {}');
182182
addTestFile('''
183183
import 'package:foo/foo.dart';
184184
A? a;
@@ -191,7 +191,7 @@ A? a;
191191

192192
// Remove the class, which should cause the main project to have an analysis
193193
// error.
194-
modifyFile(generatedFile, '');
194+
modifyFile2(generatedFile, '');
195195

196196
await waitForTasksFinished();
197197
await pumpEventQueue(times: 5000);

pkg/analysis_server/test/analysis_server_base.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ class PubPackageAnalysisServerTest extends ContextResolutionTest
300300
}
301301

302302
void modifyTestFile(String content) {
303-
modifyFile(testFilePath, content);
303+
modifyFile2(testFile, content);
304304
}
305305

306306
/// Returns the offset of [search] in [file].

pkg/analysis_server/test/lsp/completion_yaml_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ version: 1.0.0
516516
dependencies:
517517
one: ^2.3.4''';
518518

519-
newFile(pubspecFilePath, content);
519+
var pubspecFile = newFile(pubspecFilePath, content);
520520
await initialize();
521521
await openFile(pubspecFileUri, code.code);
522522
await pumpEventQueue(times: 500);
@@ -525,7 +525,7 @@ dependencies:
525525
// cached data.
526526
processRunner.startHandler =
527527
(executable, args, {dir, env}) => MockProcess(1, 0, updatedJson, '');
528-
modifyFile(pubspecFilePath, '$content# trailing comment');
528+
modifyFile2(pubspecFile, '$content# trailing comment');
529529
await pumpEventQueue(times: 500);
530530

531531
await verifyCompletions(

pkg/analysis_server/test/lsp/temporary_overlay_operation_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ class TemporaryOverlayOperationTest extends AbstractLspAnalysisServerTest {
6868
}
6969

7070
Future<void> test_pausesWatcherEvents() async {
71-
newFile(mainFilePath, '// ORIGINAL');
71+
var mainFile = newFile(mainFilePath, '// ORIGINAL');
7272
await initialize();
7373
await initialAnalysis;
7474

7575
await _TestTemporaryOverlayOperation(server, () async {
7676
// Modify the file to trigger watcher events
77-
modifyFile(mainFilePath, '// CHANGED');
77+
modifyFile2(mainFile, '// CHANGED');
7878

7979
// Ensure we still have the original content.
8080
await pumpEventQueue(times: 5000);

pkg/analyzer/test/src/workspace/blaze_watcher_test.dart

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async';
66
import 'dart:isolate';
77

8+
import 'package:analyzer/file_system/file_system.dart';
89
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
910
import 'package:analyzer/src/workspace/blaze.dart';
1011
import 'package:analyzer/src/workspace/blaze_watcher.dart';
@@ -25,22 +26,22 @@ class BlazeWatcherTest with ResourceProviderMixin {
2526
late final BlazeWorkspace workspace;
2627

2728
void test_blazeFileWatcher() {
28-
_addResources(['/workspace/${file_paths.blazeWorkspaceMarker}']);
29+
_addResource('/workspace/${file_paths.blazeWorkspaceMarker}');
2930
var candidates = [
3031
convertPath('/workspace/blaze-bin/my/module/test1.dart'),
3132
convertPath('/workspace/blaze-genfiles/my/module/test1.dart'),
3233
];
3334
var watcher = BlazeFilePoller(resourceProvider, candidates);
3435

3536
// First do some tests with the first candidate path.
36-
_addResources([candidates[0]]);
37+
var firstCandidate = _addResource(candidates[0]) as File;
3738

3839
var event = watcher.poll()!;
3940

4041
expect(event.type, ChangeType.ADD);
4142
expect(event.path, candidates[0]);
4243

43-
modifyFile(candidates[0], 'const foo = 42;');
44+
modifyFile2(firstCandidate, 'const foo = 42;');
4445

4546
event = watcher.poll()!;
4647

@@ -56,7 +57,7 @@ class BlazeWatcherTest with ResourceProviderMixin {
5657

5758
// Now check that if we add the *second* candidate, we'll get the
5859
// notification for it.
59-
_addResources([candidates[1]]);
60+
_addResource(candidates[1]);
6061

6162
event = watcher.poll()!;
6263

@@ -68,7 +69,7 @@ class BlazeWatcherTest with ResourceProviderMixin {
6869
}
6970

7071
void test_blazeFileWatcherIsolate() async {
71-
_addResources(['/workspace/${file_paths.blazeWorkspaceMarker}']);
72+
_addResource('/workspace/${file_paths.blazeWorkspaceMarker}');
7273
var candidates1 = [
7374
convertPath('/workspace/blaze-bin/my/module/test1.dart'),
7475
convertPath('/workspace/blaze-genfiles/my/module/test1.dart'),
@@ -113,7 +114,7 @@ class BlazeWatcherTest with ResourceProviderMixin {
113114
);
114115

115116
// First do some tests with the first candidate path.
116-
_addResources([candidates1[0]]);
117+
_addResource(candidates1[0]);
117118

118119
trigger.controller.add('');
119120
var events = (await queue.next as BlazeWatcherEvents).events;
@@ -123,7 +124,7 @@ class BlazeWatcherTest with ResourceProviderMixin {
123124
expect(events[0].type, ChangeType.ADD);
124125

125126
// Now let's take a look at the second file.
126-
_addResources([candidates2[1]]);
127+
_addResource(candidates2[1]);
127128

128129
trigger.controller.add('');
129130
events = (await queue.next as BlazeWatcherEvents).events;
@@ -162,10 +163,8 @@ class BlazeWatcherTest with ResourceProviderMixin {
162163
}
163164

164165
void test_blazeFileWatcherIsolate_multipleWorkspaces() async {
165-
_addResources([
166-
'/workspace1/${file_paths.blazeWorkspaceMarker}',
167-
'/workspace2/${file_paths.blazeWorkspaceMarker}',
168-
]);
166+
_addResource('/workspace1/${file_paths.blazeWorkspaceMarker}');
167+
_addResource('/workspace2/${file_paths.blazeWorkspaceMarker}');
169168
var candidates1 = [
170169
convertPath('/workspace1/blaze-bin/my/module/test1.dart'),
171170
convertPath('/workspace1/blaze-genfiles/my/module/test1.dart'),
@@ -223,7 +222,7 @@ class BlazeWatcherTest with ResourceProviderMixin {
223222
);
224223

225224
// First do some tests with the first candidate path.
226-
_addResources([candidates1[0]]);
225+
_addResource(candidates1[0]);
227226

228227
trigger1!.controller.add('');
229228
var events = (await queue.next as BlazeWatcherEvents).events;
@@ -233,7 +232,7 @@ class BlazeWatcherTest with ResourceProviderMixin {
233232
expect(events[0].type, ChangeType.ADD);
234233

235234
// Now let's take a look at the second file.
236-
_addResources([candidates2[1]]);
235+
_addResource(candidates2[1]);
237236

238237
trigger2!.controller.add('');
239238
events = (await queue.next as BlazeWatcherEvents).events;
@@ -272,11 +271,11 @@ class BlazeWatcherTest with ResourceProviderMixin {
272271
}
273272

274273
void test_blazeFileWatcherWithFolder() {
275-
_addResources(['/workspace/${file_paths.blazeWorkspaceMarker}']);
274+
_addResource('/workspace/${file_paths.blazeWorkspaceMarker}');
276275

277276
// The `_addResources`/`_deleteResources` functions recognize a folder by a
278277
// trailing `/`, but everywhere else we need to use normalized paths.
279-
void addFolder(path) => _addResources(['$path/']);
278+
void addFolder(path) => _addResource('$path/');
280279
void deleteFolder(path) => _deleteResources(['$path/']);
281280

282281
var candidates = [
@@ -310,14 +309,12 @@ class BlazeWatcherTest with ResourceProviderMixin {
310309
expect(watcher.poll(), isNull);
311310
}
312311

313-
/// Create new files and directories from [paths].
314-
void _addResources(List<String> paths) {
315-
for (String path in paths) {
316-
if (path.endsWith('/')) {
317-
newFolder(path.substring(0, path.length - 1));
318-
} else {
319-
newFile(path, '');
320-
}
312+
/// Creates a new file or directory from [resourcePath].
313+
Resource _addResource(String resourcePath) {
314+
if (resourcePath.endsWith('/')) {
315+
return newFolder(resourcePath.substring(0, resourcePath.length - 1));
316+
} else {
317+
return newFile(resourcePath, '');
321318
}
322319
}
323320

pkg/analyzer_testing/api.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ package:analyzer_testing/resource_provider_mixin.dart:
3535
getFile (method: File Function(String))
3636
getFolder (method: Folder Function(String))
3737
join (method: String Function(String, [String?, String?, String?, String?, String?, String?, String?]))
38-
modifyFile (method: void Function(String, String))
3938
modifyFile2 (method: void Function(File, String))
4039
newAnalysisOptionsYamlFile (method: File Function(String, String))
4140
newBazelBuildFile (method: File Function(String, String))

pkg/analyzer_testing/lib/resource_provider_mixin.dart

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,10 @@ mixin ResourceProviderMixin {
8383
part8,
8484
);
8585

86-
/// Writes [content] to the file at [path].
87-
void modifyFile(String path, String content) {
88-
String convertedPath = convertPath(path);
89-
resourceProvider.getFile(convertedPath).writeAsStringSync(content);
90-
}
91-
9286
/// Writes [content] to [file].
9387
void modifyFile2(File file, String content) {
94-
modifyFile(file.path, content);
88+
String convertedPath = convertPath(file.path);
89+
resourceProvider.getFile(convertedPath).writeAsStringSync(content);
9590
}
9691

9792
/// Writes a new `analysis_options.yaml` file at [directoryPath] with

0 commit comments

Comments
 (0)