Skip to content

Commit 9f3d730

Browse files
committed
Use pubspec_parse to copy dependencies instead of relying on the project to declare it
1 parent e376078 commit 9f3d730

File tree

4 files changed

+66
-39
lines changed

4 files changed

+66
-39
lines changed

dwds/test/dart_uri_file_uri_test.dart

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ void main() {
2626
final testProject = TestProject.test;
2727
final testPackageProject = TestProject.testPackage();
2828

29-
/// The directory for the general _test package.
30-
final testDir = testProject.absolutePackageDirectory;
31-
32-
/// The directory for the _testPackage package (contained within dwds),
33-
/// which imports _test.
34-
final testPackageDir = testPackageProject.absolutePackageDirectory;
35-
3629
final context = TestContext(testPackageProject, provider);
3730

3831
for (final compilationMode in CompilationMode.values) {
@@ -71,22 +64,41 @@ void main() {
7164

7265
test('file path to org-dartlang-app', () {
7366
final webMain = Uri.file(
74-
p.join(testPackageDir, 'web', 'main.dart'),
67+
p.join(
68+
// The directory for the _testPackage package (contained within
69+
// dwds), which imports _test.
70+
testPackageProject.absolutePackageDirectory,
71+
'web',
72+
'main.dart',
73+
),
7574
);
7675
final uri = DartUri('$webMain');
7776
expect(uri.serverPath, appServerPath);
7877
});
7978

8079
test('file path to this package', () {
8180
final testPackageLib = Uri.file(
82-
p.join(testPackageDir, 'lib', 'test_library.dart'),
81+
p.join(
82+
testPackageProject.absolutePackageDirectory,
83+
'lib',
84+
'test_library.dart',
85+
),
8386
);
8487
final uri = DartUri('$testPackageLib');
8588
expect(uri.serverPath, serverPath);
8689
});
8790

8891
test('file path to another package', () {
89-
final testLib = Uri.file(p.join(testDir, 'lib', 'library.dart'));
92+
final testLib = Uri.file(
93+
p.join(
94+
// The directory for the general _test package.
95+
testPackageProject.absolutePackageDirectory,
96+
'..',
97+
testProject.packageDirectory,
98+
'lib',
99+
'library.dart',
100+
),
101+
);
90102
final dartUri = DartUri('$testLib');
91103
expect(dartUri.serverPath, anotherServerPath);
92104
});

dwds/test/fixtures/project.dart

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:io';
66

77
import 'package:io/io.dart';
88
import 'package:path/path.dart' as p;
9+
import 'package:pubspec_parse/pubspec_parse.dart';
910
import 'package:test_common/utilities.dart';
1011

1112
enum IndexBaseMode { noBase, base }
@@ -17,7 +18,6 @@ class TestProject {
1718
final String webAssetsPath;
1819
final String dartEntryFileName;
1920
final String htmlEntryFileName;
20-
final List<TestProject> dependencies;
2121
final bool editable;
2222

2323
late Directory _fixturesCopy;
@@ -74,7 +74,6 @@ class TestProject {
7474
dartEntryFileName: 'main.dart',
7575
htmlEntryFileName:
7676
baseMode == IndexBaseMode.base ? 'base_index.html' : 'index.html',
77-
dependencies: [TestProject.test],
7877
);
7978

8079
static final testCircular1 = TestProject._(
@@ -83,7 +82,6 @@ class TestProject {
8382
webAssetsPath: 'web',
8483
dartEntryFileName: 'main.dart',
8584
htmlEntryFileName: 'index.html',
86-
dependencies: [TestProject.testCircular2()],
8785
);
8886

8987
TestProject.testCircular2({IndexBaseMode baseMode = IndexBaseMode.noBase})
@@ -94,7 +92,6 @@ class TestProject {
9492
dartEntryFileName: 'main.dart',
9593
htmlEntryFileName:
9694
baseMode == IndexBaseMode.base ? 'base_index.html' : 'index.html',
97-
dependencies: [TestProject.testCircular1],
9895
);
9996

10097
static final test = TestProject._(
@@ -147,7 +144,6 @@ class TestProject {
147144
webAssetsPath: 'web',
148145
dartEntryFileName: 'main.dart',
149146
htmlEntryFileName: 'index.html',
150-
dependencies: [TestProject.testHotRestart1],
151147
editable: true,
152148
);
153149

@@ -184,18 +180,45 @@ class TestProject {
184180
required this.webAssetsPath,
185181
required this.dartEntryFileName,
186182
required this.htmlEntryFileName,
187-
this.dependencies = const <TestProject>[],
188183
this.editable = false,
189184
});
190185

191-
static void _copyPackageIntoTempDirectory(
186+
static void _copyPackageAndPathDependenciesIntoTempDirectory(
192187
Directory tempDirectory,
193188
String packageDirectory,
189+
Set<String> copiedPackageDirectories,
194190
) {
191+
// There may be cycles in dependencies, so check that we already copied this
192+
// package.
193+
if (copiedPackageDirectories.contains(packageDirectory)) return;
195194
final currentPath = absolutePath(pathFromFixtures: packageDirectory);
196195
final newPath = p.join(tempDirectory.absolute.path, packageDirectory);
197196
Directory(newPath).createSync();
198197
copyPathSync(currentPath, newPath);
198+
copiedPackageDirectories.add(packageDirectory);
199+
final pubspec = Pubspec.parse(
200+
File(p.join(currentPath, 'pubspec.yaml')).readAsStringSync(),
201+
);
202+
for (final package in pubspec.dependencies.keys) {
203+
final dependency = pubspec.dependencies[package]!;
204+
if (dependency is PathDependency) {
205+
final dependencyDirectory = Directory(dependency.path);
206+
// It may be okay to do some more complicated copying here for path
207+
// dependencies that aren't immediately under `fixtures`, but for now,
208+
// only support those that are.
209+
assert(
210+
dependencyDirectory.parent == Directory(currentPath).parent,
211+
'Path dependency of $packageDirectory: '
212+
'${dependencyDirectory.absolute.path} is not an immediate directory '
213+
'in `fixtures`.',
214+
);
215+
_copyPackageAndPathDependenciesIntoTempDirectory(
216+
tempDirectory,
217+
p.dirname(dependencyDirectory.path),
218+
copiedPackageDirectories,
219+
);
220+
}
221+
}
199222
}
200223

201224
Future<void> setUp() async {
@@ -210,19 +233,11 @@ class TestProject {
210233
Directory.systemTemp.resolveSymbolicLinksSync(),
211234
);
212235
_fixturesCopy = systemTempDir.createTempSync();
213-
_copyPackageIntoTempDirectory(_fixturesCopy, packageDirectory);
214-
// Also copy any of its dependencies, recursively.
215-
final copiedPackages = <TestProject>{this};
216-
final dependencyQueue = List<TestProject>.from(dependencies);
217-
while (dependencyQueue.isNotEmpty) {
218-
final dependency = dependencyQueue.removeAt(0);
219-
if (copiedPackages.contains(dependency)) continue;
220-
_copyPackageIntoTempDirectory(
221-
_fixturesCopy,
222-
dependency.packageDirectory,
223-
);
224-
dependencyQueue.addAll(dependency.dependencies);
225-
}
236+
_copyPackageAndPathDependenciesIntoTempDirectory(
237+
_fixturesCopy,
238+
packageDirectory,
239+
{},
240+
);
226241
}
227242

228243
// Clean up the project.

dwds/test/load_strategy_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void main() {
3131
group(
3232
'When the packageConfigLocator does not specify a package config path',
3333
() {
34-
final strategy = FakeStrategy(FakeAssetReader());
34+
late final strategy = FakeStrategy(FakeAssetReader());
3535

3636
test('defaults to "./dart_tool/package_config.json"', () {
3737
expect(
@@ -43,7 +43,7 @@ void main() {
4343
);
4444

4545
group('When a custom package config path is specified', () {
46-
final strategy = FakeStrategy(
46+
late final strategy = FakeStrategy(
4747
FakeAssetReader(),
4848
packageConfigPath: 'custom/package_config/path',
4949
);
@@ -57,7 +57,7 @@ void main() {
5757
});
5858

5959
group('When default build settings defined', () {
60-
final strategy = FakeStrategy(
60+
late final strategy = FakeStrategy(
6161
FakeAssetReader(),
6262
buildSettings: TestBuildSettings.dart(),
6363
);
@@ -85,7 +85,7 @@ void main() {
8585
final isFlutterApp = true;
8686
final experiments = ['records'];
8787

88-
final strategy = FakeStrategy(
88+
late final strategy = FakeStrategy(
8989
FakeAssetReader(),
9090
buildSettings: BuildSettings(
9191
appEntrypoint: appEntrypoint,

dwds/test/readers/frontend_server_asset_reader_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ void main() {
2222
late Directory tempFixtures;
2323
late File jsonOriginal;
2424
late File mapOriginal;
25+
late String packagesDir;
2526

2627
final testProject = TestProject.test;
27-
final packagesDir = testProject.absolutePackageDirectory;
2828

2929
Future<void> createTempFixtures() async {
3030
tempFixtures = await Directory.systemTemp.createTemp('dwds_test_fixtures');
@@ -37,15 +37,14 @@ void main() {
3737
).copy(p.join(tempFixtures.path, 'main.dart.dill.map'));
3838
}
3939

40-
setUpAll(() async {
40+
setUp(() async {
41+
await testProject.setUp();
42+
packagesDir = testProject.absolutePackageDirectory;
4143
final sdkLayout = TestSdkLayout.defaultSdkLayout;
4244
await Process.run(sdkLayout.dartPath, [
4345
'pub',
4446
'upgrade',
4547
], workingDirectory: packagesDir);
46-
});
47-
48-
setUp(() async {
4948
await createTempFixtures();
5049
assetReader = FrontendServerAssetReader(
5150
outputPath: p.join(tempFixtures.path, 'main.dart.dill'),
@@ -55,6 +54,7 @@ void main() {
5554
});
5655

5756
tearDown(() async {
57+
testProject.tearDown();
5858
if (await tempFixtures.exists()) await tempFixtures.delete(recursive: true);
5959
});
6060

0 commit comments

Comments
 (0)