Skip to content

Commit 423479c

Browse files
dcharkesCommit Queue
authored andcommitted
[native assets] Fix pub workspaces in dart test
Bug: #60489 We can land a fix in `package:test` as well. However, projects referring to an older version of test will not see such fixes. So, land a workaround here that is picked up by older versions of test. Change-Id: Ib26be2c0edc1bccf58b7231e05896cd55ac0cf19 Cq-Include-Trybots: luci.dart.try:pkg-linux-debug-try,pkg-linux-release-arm64-try,pkg-linux-release-try,pkg-mac-release-arm64-try,pkg-win-release-arm64-try,pkg-mac-release-try,pkg-win-release-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/421741 Reviewed-by: Hossein Yousefi <[email protected]>
1 parent f0eeb22 commit 423479c

File tree

3 files changed

+52
-20
lines changed

3 files changed

+52
-20
lines changed

pkg/dartdev/lib/src/commands/test.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,16 @@ Run "${runner!.executableName} help" to see global options.''');
9090
log.stderr('Error: Compiling native assets failed.');
9191
return DartdevCommand.errorExitCode;
9292
}
93-
nativeAssets = assetsYamlFileUri.toFilePath();
93+
// TODO(https://github.com/dart-lang/sdk/issues/60489): Add a way to
94+
// package:test to explicitly provide the native_assets.yaml path
95+
// instead of copying to the workspace .dart_tool.
96+
final expectedPackageTestLocation =
97+
packageConfig.resolve('native_assets.yaml');
98+
if (expectedPackageTestLocation != assetsYamlFileUri) {
99+
await File.fromUri(assetsYamlFileUri)
100+
.copy(expectedPackageTestLocation.toFilePath());
101+
}
102+
nativeAssets = expectedPackageTestLocation.toFilePath();
94103
}
95104
}
96105
}

pkg/dartdev/test/native_assets/helpers.dart

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ Future<run_process.RunProcessResult> runProcess({
6767
filesystem: const LocalFileSystem(),
6868
);
6969

70-
Future<void> copyTestProjects(
71-
Uri copyTargetUri, Logger logger, Uri packageLocation, Uri sdkRoot) async {
70+
Future<void> copyTestProjects(Uri copyTargetUri, Logger logger,
71+
Uri packageLocation, Uri sdkRoot, bool usePubWorkspace) async {
7272
// Reuse the test projects from `pkg:native`.
7373
final testProjectsUri = packageLocation.resolve('test_data/');
7474
final manifestUri = testProjectsUri.resolve('manifest.yaml');
@@ -82,7 +82,7 @@ Future<void> copyTestProjects(
8282
.where((e) => !(e.pathSegments.last.startsWith('pubspec') &&
8383
e.pathSegments.last.endsWith('.yaml')))
8484
.toList();
85-
final filesToModify = manifest
85+
final pubspecPaths = manifest
8686
.where((e) =>
8787
e.pathSegments.last.startsWith('pubspec') &&
8888
e.pathSegments.last.endsWith('.yaml'))
@@ -98,17 +98,7 @@ Future<void> copyTestProjects(
9898
}
9999
await sourceFile.copy(targetUri.toFilePath());
100100
}
101-
for (final pathToModify in filesToModify) {
102-
final sourceFile = File.fromUri(testProjectsUri.resolveUri(pathToModify));
103-
final targetUri = copyTargetUri.resolveUri(pathToModify);
104-
final sourceString = await sourceFile.readAsString();
105-
final pubspec = YamlEditor(sourceString);
106-
if ((loadYamlNode(sourceString) as Map)['resolution'] != null) {
107-
pubspec.remove(['resolution']);
108-
}
109-
pubspec.update([
110-
'dependency_overrides'
111-
], {
101+
final dependencyOverrides = {
112102
'native_assets_cli': {
113103
'path': sdkRoot
114104
.resolve('third_party/pkg/native/pkgs/native_assets_cli/')
@@ -125,10 +115,36 @@ Future<void> copyTestProjects(
125115
'record_use': {
126116
'path': sdkRoot.resolve('pkg/record_use/').toFilePath(),
127117
},
128-
});
118+
};
119+
for (final pubspecPath in pubspecPaths) {
120+
final sourceFile = File.fromUri(testProjectsUri.resolveUri(pubspecPath));
121+
final targetUri = copyTargetUri.resolveUri(pubspecPath);
122+
final sourceString = await sourceFile.readAsString();
123+
final pubspec = YamlEditor(sourceString);
124+
if (!usePubWorkspace) {
125+
if ((loadYamlNode(sourceString) as Map)['resolution'] != null) {
126+
pubspec.remove(['resolution']);
127+
}
128+
pubspec.update(['dependency_overrides'], dependencyOverrides);
129+
}
129130
final modifiedString = pubspec.toString();
130131
await File.fromUri(targetUri).writeAsString(modifiedString);
131132
}
133+
if (usePubWorkspace) {
134+
final workspacePubspec = YamlEditor('');
135+
workspacePubspec.update([], {
136+
'name': 'my_pub_workspace',
137+
'environment': {'sdk': '>=3.7.0 <4.0.0'},
138+
'workspace': [
139+
for (final pubspec in pubspecPaths)
140+
if (!pubspec.toFilePath().contains('version_skew'))
141+
pubspec.toFilePath().replaceAll('pubspec.yaml', ''),
142+
],
143+
'dependency_overrides': dependencyOverrides,
144+
});
145+
final pubspecUri = copyTargetUri.resolve('pubspec.yaml');
146+
await File.fromUri(pubspecUri).writeAsString(workspacePubspec.toString());
147+
}
132148

133149
// If we're copying `my_native_library/` we need to simulate that its
134150
// native assets are pre-built
@@ -198,6 +214,7 @@ Future<void> nativeAssetsTest(
198214
String packageUnderTest,
199215
Future<void> Function(Uri) fun, {
200216
bool skipPubGet = false,
217+
bool usePubWorkspace = false,
201218
}) async =>
202219
await runPackageTest(
203220
packageUnderTest,
@@ -218,6 +235,7 @@ Future<void> nativeAssetsTest(
218235
Platform.script.resolve(
219236
'../../../../third_party/pkg/native/pkgs/native_assets_builder/'),
220237
Platform.script.resolve('../../../../'),
238+
usePubWorkspace,
221239
);
222240

223241
Future<void> recordUseTest(
@@ -232,19 +250,22 @@ Future<void> recordUseTest(
232250
const ['drop_dylib_recording'],
233251
Platform.script.resolve('../../../record_use/'),
234252
Platform.script.resolve('../../../../'),
253+
false,
235254
);
236255

237256
Future<void> runPackageTest(
238257
String packageUnderTest,
239258
bool skipPubGet,
240259
Future<void> Function(Uri) fun,
241260
List<String> validPackages,
242-
Uri packageLocation,
243-
Uri sdkRoot
261+
Uri packageLocation,
262+
Uri sdkRoot,
263+
bool usePubWorkspace,
244264
) async {
245265
assert(validPackages.contains(packageUnderTest));
246266
return await inTempDir((tempUri) async {
247-
await copyTestProjects(tempUri, logger, packageLocation, sdkRoot);
267+
await copyTestProjects(
268+
tempUri, logger, packageLocation, sdkRoot, usePubWorkspace);
248269
final packageUri = tempUri.resolve('$packageUnderTest/');
249270
if (!skipPubGet) {
250271
await runPubGet(workingDirectory: packageUri, logger: logger);

pkg/dartdev/test/native_assets/test_test.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ void main(List<String> args) async {
2424
'system_library',
2525
]) {
2626
test('package:$package dart test', timeout: longTimeout, () async {
27-
await nativeAssetsTest(package, (packageUri) async {
27+
await nativeAssetsTest(package, usePubWorkspace: true,
28+
(packageUri) async {
2829
final result = await runDart(
2930
arguments: [
3031
'--enable-experiment=native-assets',
@@ -123,6 +124,7 @@ void main(List<String> args) async {
123124
);
124125
});
125126
});
127+
126128
test(
127129
'dart test with user defines',
128130
timeout: longTimeout,

0 commit comments

Comments
 (0)