Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit cbd6712

Browse files
bwilkersoncommit-bot@chromium.org
authored andcommitted
Add a test that will fail if any of the files in analyzer_cli are not sorted by MemberSorter
Change-Id: I76d5caeef2b6525e93636f150579143a5b7e2c2b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/140561 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
1 parent 1dc03cc commit cbd6712

File tree

3 files changed

+113
-22
lines changed

3 files changed

+113
-22
lines changed

pkg/analyzer_cli/test/all.dart

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,30 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'analysis_options_test.dart' as analysis_options_test;
6-
import 'build_mode_test.dart' as build_mode_test;
7-
import 'driver_test.dart' as driver_test;
8-
import 'embedder_test.dart' as embedder_test;
9-
import 'errors_reported_once_test.dart' as errors_reported_once_test;
10-
import 'errors_upgrade_fails_cli_test.dart' as errors_upgrade_fails_cli_test;
11-
import 'options_test.dart' as options_test;
12-
import 'package_prefix_test.dart' as package_prefix_test;
13-
import 'perf_report_test.dart' as perf_report_test;
14-
import 'reporter_test.dart' as reporter_test;
15-
import 'strong_mode_test.dart' as strong_mode_test;
5+
import 'analysis_options_test.dart' as analysis_options;
6+
import 'build_mode_test.dart' as build_mode;
7+
import 'driver_test.dart' as driver;
8+
import 'embedder_test.dart' as embedder;
9+
import 'errors_reported_once_test.dart' as errors_reported_once;
10+
import 'errors_upgrade_fails_cli_test.dart' as errors_upgrade_fails_cli;
11+
import 'options_test.dart' as options;
12+
import 'package_prefix_test.dart' as package_prefix;
13+
import 'perf_report_test.dart' as perf_report;
14+
import 'reporter_test.dart' as reporter;
15+
import 'strong_mode_test.dart' as strong_mode;
16+
import 'verify_sorted_test.dart' as verify_sorted;
1617

1718
void main() {
18-
analysis_options_test.main();
19-
build_mode_test.main();
20-
driver_test.main();
21-
embedder_test.main();
22-
errors_reported_once_test.main();
23-
errors_upgrade_fails_cli_test.main();
24-
options_test.main();
25-
package_prefix_test.main();
26-
perf_report_test.main();
27-
reporter_test.main();
28-
strong_mode_test.main();
19+
analysis_options.main();
20+
build_mode.main();
21+
driver.main();
22+
embedder.main();
23+
errors_reported_once.main();
24+
errors_upgrade_fails_cli.main();
25+
options.main();
26+
package_prefix.main();
27+
perf_report.main();
28+
reporter.main();
29+
strong_mode.main();
30+
verify_sorted.main();
2931
}

pkg/analyzer_cli/test/utils.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,27 @@ import 'package:path/path.dart' as pathos;
1313
final String testDirectory = pathos.dirname(
1414
pathos.fromUri((reflectClass(_TestUtils).owner as LibraryMirror).uri));
1515

16+
/// Returns a path to the directory containing source code for packages such as
17+
/// kernel, front_end, and analyzer.
18+
String get packageRoot {
19+
// If the package root directory is specified on the command line using
20+
// -DpkgRoot=..., use it.
21+
var pkgRootVar = const String.fromEnvironment('pkgRoot');
22+
if (pkgRootVar != null) {
23+
var path = pathos.join(Directory.current.path, pkgRootVar);
24+
if (!path.endsWith(pathos.separator)) path += pathos.separator;
25+
return path;
26+
}
27+
// Otherwise try to guess based on the script path.
28+
var scriptPath = pathos.fromUri(Platform.script);
29+
var parts = pathos.split(scriptPath);
30+
var pkgIndex = parts.indexOf('pkg');
31+
if (pkgIndex != -1) {
32+
return pathos.joinAll(parts.sublist(0, pkgIndex + 1)) + pathos.separator;
33+
}
34+
throw StateError('Unable to find sdk/pkg/ in $scriptPath');
35+
}
36+
1637
/// Recursively copy the specified [src] directory (or file)
1738
/// to the specified destination path.
1839
Future<void> recursiveCopy(FileSystemEntity src, String dstPath) async {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:io';
6+
7+
import 'package:analysis_server/src/services/correction/sort_members.dart';
8+
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
9+
import 'package:analyzer/dart/analysis/results.dart';
10+
import 'package:analyzer/dart/analysis/session.dart';
11+
import 'package:analyzer/file_system/file_system.dart';
12+
import 'package:analyzer/file_system/physical_file_system.dart';
13+
import 'package:test/test.dart';
14+
15+
import 'utils.dart';
16+
17+
void main() {
18+
var provider = PhysicalResourceProvider.INSTANCE;
19+
var normalizedRoot = provider.pathContext.normalize(packageRoot);
20+
var packagePath = provider.pathContext.join(normalizedRoot, 'analyzer_cli');
21+
var testDataPath = provider.pathContext.join(packagePath, 'test', 'data');
22+
23+
var collection = AnalysisContextCollection(
24+
includedPaths: <String>[packagePath],
25+
excludedPaths: [testDataPath],
26+
resourceProvider: provider);
27+
var contexts = collection.contexts;
28+
if (contexts.length != 1) {
29+
fail('The directory $packagePath contains multiple analysis contexts.');
30+
}
31+
32+
buildTestsIn(contexts[0].currentSession, packagePath, testDataPath,
33+
provider.getFolder(packagePath));
34+
}
35+
36+
void buildTestsIn(AnalysisSession session, String testDirPath,
37+
String testDataPath, Folder directory) {
38+
var pathContext = session.resourceProvider.pathContext;
39+
var children = directory.getChildren();
40+
children.sort((first, second) => first.shortName.compareTo(second.shortName));
41+
for (var child in children) {
42+
if (child is Folder) {
43+
if (child.path != testDataPath) {
44+
buildTestsIn(session, testDirPath, testDataPath, child);
45+
}
46+
} else if (child is File && child.shortName.endsWith('.dart')) {
47+
var path = child.path;
48+
var relativePath = pathContext.relative(path, from: testDirPath);
49+
test(relativePath, () {
50+
var result = session.getParsedUnit(path);
51+
if (result.state != ResultState.VALID) {
52+
fail('Could not parse $path');
53+
}
54+
var code = result.content;
55+
var unit = result.unit;
56+
var errors = result.errors;
57+
if (errors.isNotEmpty) {
58+
fail('Errors found when parsing $path');
59+
}
60+
var sorter = MemberSorter(code, unit);
61+
var edits = sorter.sort();
62+
if (edits.isNotEmpty) {
63+
fail('Unsorted file $path');
64+
}
65+
});
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)