Skip to content

Commit d2d810a

Browse files
sigmundchCommit Bot
authored and
Commit Bot
committed
[modular_test] Remove uses of .packages altogether.
Embed in the modules.yaml any extra paths needed for package dependencies instead. This is one step to help towards #48275 Change-Id: I22ef02b2b2327a0c798f2fea73d59c758a8bb0bb Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/240651 Reviewed-by: Joshua Litt <[email protected]> Commit-Queue: Sigmund Cherem <[email protected]>
1 parent 441a762 commit d2d810a

File tree

15 files changed

+88
-81
lines changed

15 files changed

+88
-81
lines changed

pkg/modular_test/lib/src/loader.dart

Lines changed: 24 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,13 @@
1010
/// * individual .dart files, each file is considered a module. A
1111
/// `main.dart` file is required as the entry point of the test.
1212
/// * subfolders: each considered a module with multiple files
13-
/// * (optional) a .packages file:
14-
/// * if this is not specified, the test will use [defaultPackagesInput]
15-
/// instead.
16-
/// * if specified, it will be extended with the definitions in
17-
/// [defaultPackagesInput]. The list of packages provided is expected to
18-
/// be disjoint with those in [defaultPackagesInput].
1913
/// * a modules.yaml file: a specification of dependencies between modules.
2014
/// The format is described in `test_specification_parser.dart`.
2115
import 'dart:io';
22-
import 'dart:convert';
23-
import 'dart:typed_data';
2416
import 'suite.dart';
2517
import 'test_specification_parser.dart';
2618
import 'find_sdk_root.dart';
2719

28-
import 'package:package_config/src/packages_file.dart' as packages_file;
29-
3020
/// Returns the [ModularTest] associated with a folder under [uri].
3121
///
3222
/// After scanning the contents of the folder, this method creates a
@@ -38,13 +28,12 @@ Future<ModularTest> loadTest(Uri uri) async {
3828
var folder = Directory.fromUri(uri);
3929
var testUri = folder.uri; // normalized in case the trailing '/' was missing.
4030
Uri root = await findRoot();
41-
Map<String, Uri> defaultPackages =
42-
_parseDotPackagesBytesToLibMap(_defaultPackagesInput, root);
31+
final defaultTestSpecification = parseTestSpecification(_defaultPackagesSpec);
32+
Set<String> defaultPackages = defaultTestSpecification.packages.keys.toSet();
4333
Module sdkModule = await _createSdkModule(root);
4434
Map<String, Module> modules = {'sdk': sdkModule};
4535
String specString;
4636
Module mainModule;
47-
Map<String, Uri> packages = {};
4837
var entries = folder.listSync(recursive: false).toList()
4938
// Sort to avoid dependency on file system order.
5039
..sort(_compareFileSystemEntity);
@@ -59,7 +48,7 @@ Future<ModularTest> loadTest(Uri uri) async {
5948
"'$moduleName' which conflicts with the sdk module "
6049
"that is provided by default.");
6150
}
62-
if (defaultPackages.containsKey(moduleName)) {
51+
if (defaultPackages.contains(moduleName)) {
6352
return _invalidTest("The file '$fileName' defines a module called "
6453
"'$moduleName' which conflicts with a package by the same name "
6554
"that is provided by default.");
@@ -75,9 +64,6 @@ Future<ModularTest> loadTest(Uri uri) async {
7564
packageBase: Uri.parse('.'));
7665
if (isMain) mainModule = module;
7766
modules[moduleName] = module;
78-
} else if (fileName == '.packages') {
79-
List<int> packagesBytes = await entry.readAsBytes();
80-
packages = _parseDotPackagesBytesToLibMap(packagesBytes, entryUri);
8167
} else if (fileName == 'modules.yaml') {
8268
specString = await entry.readAsString();
8369
}
@@ -90,7 +76,7 @@ Future<ModularTest> loadTest(Uri uri) async {
9076
"which conflicts with the sdk module "
9177
"that is provided by default.");
9278
}
93-
if (defaultPackages.containsKey(moduleName)) {
79+
if (defaultPackages.contains(moduleName)) {
9480
return _invalidTest("The folder '$moduleName' defines a module "
9581
"which conflicts with a package by the same name "
9682
"that is provided by default.");
@@ -110,12 +96,17 @@ Future<ModularTest> loadTest(Uri uri) async {
11096
return _invalidTest("main module is missing");
11197
}
11298

113-
_addDefaultPackageEntries(packages, defaultPackages);
114-
await _addModulePerPackage(packages, modules);
11599
TestSpecification spec = parseTestSpecification(specString);
100+
for (final name in defaultPackages) {
101+
if (spec.packages.containsKey(name)) {
102+
_invalidTest(
103+
".packages file defines a conflicting entry for package '$name'.");
104+
}
105+
}
106+
await _addModulePerPackage(defaultTestSpecification.packages, root, modules);
107+
await _addModulePerPackage(spec.packages, testUri, modules);
116108
_attachDependencies(spec.dependencies, modules);
117-
_attachDependencies(
118-
parseTestSpecification(_defaultPackagesSpec).dependencies, modules);
109+
_attachDependencies(defaultTestSpecification.dependencies, modules);
119110
_addSdkDependencies(modules, sdkModule);
120111
_detectCyclesAndRemoveUnreachable(modules, mainModule);
121112
var sortedModules = modules.values.toList()
@@ -170,33 +161,21 @@ void _addSdkDependencies(Map<String, Module> modules, Module sdkModule) {
170161
}
171162
}
172163

173-
void _addDefaultPackageEntries(
174-
Map<String, Uri> packages, Map<String, Uri> defaultPackages) {
175-
for (var name in defaultPackages.keys) {
176-
var existing = packages[name];
177-
if (existing != null && existing != defaultPackages[name]) {
178-
_invalidTest(
179-
".packages file defines an conflicting entry for package '$name'.");
180-
}
181-
packages[name] = defaultPackages[name];
182-
}
183-
}
184-
185164
/// Create a module for each package dependency.
186-
Future<void> _addModulePerPackage(
187-
Map<String, Uri> packages, Map<String, Module> modules) async {
165+
Future<void> _addModulePerPackage(Map<String, String> packages, Uri configRoot,
166+
Map<String, Module> modules) async {
188167
for (var packageName in packages.keys) {
189168
var module = modules[packageName];
190169
if (module != null) {
191170
module.isPackage = true;
192171
} else {
193-
var packageLibUri = packages[packageName];
194-
var rootUri = Directory.fromUri(packageLibUri).parent.uri;
172+
var packageLibUri = configRoot.resolve(packages[packageName]);
173+
var packageRootUri = Directory.fromUri(packageLibUri).parent.uri;
195174
var sources = await _listModuleSources(packageLibUri);
196175
// TODO(sigmund): validate that we don't use a different alias for a
197176
// module that is part of the test (package name and module name should
198177
// match).
199-
modules[packageName] = Module(packageName, [], rootUri, sources,
178+
modules[packageName] = Module(packageName, [], packageRootUri, sources,
200179
isPackage: true, packageBase: Uri.parse('lib/'), isShared: true);
201180
}
202181
}
@@ -249,15 +228,6 @@ _detectCyclesAndRemoveUnreachable(Map<String, Module> modules, Module main) {
249228
toRemove.forEach(modules.remove);
250229
}
251230

252-
/// Default entries for a .packages file with paths relative to the SDK root.
253-
List<int> _defaultPackagesInput = utf8.encode('''
254-
expect:pkg/expect/lib
255-
smith:pkg/smith/lib
256-
async_helper:pkg/async_helper/lib
257-
meta:pkg/meta/lib
258-
collection:third_party/pkg/collection/lib
259-
''');
260-
261231
/// Specifies the dependencies of all packages in [_defaultPackagesInput]. This
262232
/// string needs to be updated if dependencies between those packages changes
263233
/// (which is rare).
@@ -270,6 +240,12 @@ dependencies:
270240
meta: []
271241
async_helper: []
272242
collection: []
243+
packages:
244+
expect: pkg/expect/lib
245+
smith: pkg/smith/lib
246+
async_helper: pkg/async_helper/lib
247+
meta: pkg/meta/lib
248+
collection: third_party/pkg/collection/lib
273249
''';
274250

275251
/// Report an conflict error.
@@ -316,15 +292,3 @@ int _compareFileSystemEntity(FileSystemEntity a, FileSystemEntity b) {
316292
}
317293
}
318294
}
319-
320-
/// Parse [bytes] representing a `.packages` file into the map of package names
321-
/// to URIs of their `lib` locations.
322-
Map<String, Uri> _parseDotPackagesBytesToLibMap(Uint8List bytes, Uri baseUri) {
323-
var map = <String, Uri>{};
324-
var packageConfig =
325-
packages_file.parse(bytes, baseUri, (error) => throw error);
326-
for (var package in packageConfig.packages) {
327-
map[package.name] = package.packageUriRoot;
328-
}
329-
return map;
330-
}

pkg/modular_test/lib/src/test_specification_parser.dart

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@
1010
/// main: [b, expect]
1111
/// flags:
1212
/// - constant-update-2018
13+
/// packages:
14+
/// c: .
15+
/// a: a
1316
///
14-
/// Where the dependencies section describe how modules depend on one another,
15-
/// and the flags section show what flags are needed to run that specific test.
17+
///
18+
/// Where:
19+
/// - the dependencies section describe how modules depend on one another,
20+
/// - the flags section show what flags are needed to run that specific test,
21+
/// - the packages section is used to create a package structure on top of the
22+
/// declared modules.
1623
///
1724
/// When defining dependencies:
1825
/// - Each name corresponds to a module.
@@ -21,9 +28,24 @@
2128
/// - If a module has a single dependency, it can be written as a single
2229
/// value.
2330
///
31+
/// When defining packages:
32+
/// - The name corresponds to a package name, this doesn't need to match
33+
/// the name of the module. That said, it's common for some modules
34+
/// and packages to share their name (especially for the default set of
35+
/// packages included by the framework, like package:expect).
36+
/// - The value is a path to the folder containing the libraries of that
37+
/// package.
38+
///
39+
/// The packages entry is optional. If this is not specified, the test will
40+
/// still have a default set of packages, like package:expect and package:meta.
41+
/// If the packages entry is specified, it will be extended with the definitions
42+
/// of the default set of packages as well. Thus, the list of packages provided
43+
/// is expected to be disjoint with those in the default set. The default set is
44+
/// defined directly in the code of `loader.dart`.
45+
///
2446
/// The logic in this library mostly treats these names as strings, separately
25-
/// `loader.dart` is responsible for validating and attaching this dependency
26-
/// information to a set of module definitions.
47+
/// `loader.dart` is responsible for validating, attaching dependency
48+
/// information to a set of module definitions, and resolving package paths.
2749
///
2850
/// The framework is agnostic of what the flags are, but at this time we only
2951
/// use the name of experimental language features. These are then used to
@@ -76,7 +98,18 @@ TestSpecification parseTestSpecification(String contents) {
7698
_invalidSpecification(
7799
"flags: '$flags' expected to be string or list of strings");
78100
}
79-
return new TestSpecification(normalizedFlags, normalizedMap);
101+
102+
Map<String, String> normalizedPackages = {};
103+
final packages = spec['packages'];
104+
if (packages != null) {
105+
if (packages is Map) {
106+
normalizedPackages.addAll(packages.cast<String, String>());
107+
} else {
108+
_invalidSpecification("packages is not a map");
109+
}
110+
}
111+
return new TestSpecification(
112+
normalizedFlags, normalizedMap, normalizedPackages);
80113
}
81114

82115
/// Data specifying details about a modular test including dependencies and
@@ -96,7 +129,13 @@ class TestSpecification {
96129
/// (for instance, the module of `package:expect` or the sdk itself).
97130
final Map<String, List<String>> dependencies;
98131

99-
TestSpecification(this.flags, this.dependencies);
132+
/// Map of package name to a relative path.
133+
///
134+
/// The paths in this map are meant to be resolved relative to the location
135+
/// where this test specification was defined.
136+
final Map<String, String> packages;
137+
138+
TestSpecification(this.flags, this.dependencies, this.packages);
100139
}
101140

102141
_invalidSpecification(String message) {

pkg/modular_test/test/loader/dag_with_packages/.packages

Lines changed: 0 additions & 4 deletions
This file was deleted.

pkg/modular_test/test/loader/dag_with_packages/modules.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ dependencies:
22
a: [b, c]
33
b: d
44
main: [a, b]
5+
packages:
6+
a: a/
7+
b: b/
8+
d: d/
9+
c: c/

pkg/modular_test/test/loader/invalid_packages_error/.packages

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# This expectation file is generated by loader_test.dart
22

3-
Invalid test: .packages file defines an conflicting entry for package 'expect'.
3+
Invalid test: .packages file defines a conflicting entry for package 'expect'.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
dependencies:
22
main:
33
- expect
4+
packages:
5+
expect: .

pkg/modular_test/test/loader/valid_packages/.packages

Lines changed: 0 additions & 1 deletion
This file was deleted.

pkg/modular_test/test/loader/valid_packages/modules.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ dependencies:
22
main:
33
- js
44
- expect
5+
packages:
6+
js: ../../../../js/lib

tests/modular/js_interop/.packages

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/modular/js_interop/modules.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
dependencies:
77
main: log
88
log: js
9+
packages:
10+
js: ../../../pkg/js/lib

tests/modular/package_imports/.packages

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/modular/package_imports/modules.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ dependencies:
1111
f3: a
1212
a: f1
1313
f1: f0
14+
15+
packages:
16+
f0: .
17+
f1: f1
18+
a: a

tests/modular/static_interop_erasure/.packages

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/modular/static_interop_erasure/modules.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77
dependencies:
88
main: static_interop
99
static_interop: js
10+
packages:
11+
js: ../../../pkg/js/lib

0 commit comments

Comments
 (0)