Skip to content

Commit f24d4d1

Browse files
DanTupCommit Queue
authored andcommitted
[dartdev] Fix some tests to work under 'dart test'
Now that the SDK uses Pub Workspaces, using the test runner is enabled in Dart-Code. However there are some differences when using 'dart test' that caused some of these tests to fail - this change addresses them: - Don't use Platform.script because it won't be the source Dart filename - Make any `args` to `main()` optional - Add calls to `test()` around some regression tests I still have a few remaining failures locally, but I'm not yet sure if they're related to the test runner and will troubleshoot them separately. Change-Id: I01efc5517174ae7e8146892bba1d84cc69029fd4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/421162 Reviewed-by: Ben Konyi <[email protected]> Reviewed-by: Jake Macdonald <[email protected]> Commit-Queue: Ben Konyi <[email protected]>
1 parent e4676d5 commit f24d4d1

File tree

9 files changed

+64
-66
lines changed

9 files changed

+64
-66
lines changed

pkg/dartdev/test/native_assets/build_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ String crossOSNotAllowedError(String format) =>
2121
final String hostOSMessage = 'Host OS: ${Platform.operatingSystem}';
2222
String targetOSMessage(String targetOS) => 'Target OS: $targetOS';
2323

24-
void main(List<String> args) async {
24+
void main([List<String> args = const []]) async {
2525
if (!nativeAssetsExperimentAvailableOnCurrentChannel) {
2626
return;
2727
}
2828

29+
final dartDevEntryScriptUri = resolveDartDevUri('bin/dartdev.dart');
30+
2931
final bool fromDartdevSource = args.contains('--source');
3032
final hostOS = Platform.operatingSystem;
3133
final crossOS = Platform.isLinux ? 'macos' : 'linux';
@@ -48,8 +50,7 @@ void main(List<String> args) async {
4850
final result = await runDart(
4951
arguments: [
5052
'--enable-experiment=native-assets',
51-
if (fromDartdevSource)
52-
Platform.script.resolve('../../bin/dartdev.dart').toFilePath(),
53+
if (fromDartdevSource) dartDevEntryScriptUri.toFilePath(),
5354
'build',
5455
if (targetOS != null) ...[
5556
'--target-os',
@@ -219,8 +220,7 @@ void main(List<String> args) {
219220
final result = await runDart(
220221
arguments: [
221222
'--enable-experiment=native-assets',
222-
if (fromDartdevSource)
223-
Platform.script.resolve('../../bin/dartdev.dart').toFilePath(),
223+
if (fromDartdevSource) dartDevEntryScriptUri.toFilePath(),
224224
'build',
225225
'bin/dart_app.dart',
226226
'.'

pkg/dartdev/test/native_assets/helpers.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import 'package:test/test.dart';
1515
import 'package:yaml/yaml.dart';
1616
import 'package:yaml_edit/yaml_edit.dart';
1717

18+
import '../utils.dart';
19+
1820
extension UriExtension on Uri {
1921
Uri get parent {
2022
return File(toFilePath()).parent.uri;
@@ -243,9 +245,8 @@ Future<void> nativeAssetsTest(
243245
'treeshaking_native_libs',
244246
'user_defines',
245247
],
246-
Platform.script.resolve(
247-
'../../../../third_party/pkg/native/pkgs/native_assets_builder/'),
248-
Platform.script.resolve('../../../../'),
248+
sdkRootUri.resolve('third_party/pkg/native/pkgs/native_assets_builder/'),
249+
sdkRootUri,
249250
usePubWorkspace,
250251
);
251252

@@ -259,8 +260,8 @@ Future<void> recordUseTest(
259260
skipPubGet,
260261
fun,
261262
const ['drop_dylib_recording'],
262-
Platform.script.resolve('../../../record_use/'),
263-
Platform.script.resolve('../../../../'),
263+
sdkRootUri.resolve('pkg/record_use/'),
264+
sdkRootUri,
264265
false,
265266
);
266267

pkg/dartdev/test/native_assets/run_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'package:test/test.dart';
99
import '../utils.dart';
1010
import 'helpers.dart';
1111

12-
void main(List<String> args) async {
12+
void main([List<String> args = const []]) async {
1313
if (!nativeAssetsExperimentAvailableOnCurrentChannel) {
1414
test('dart run', timeout: longTimeout, () async {
1515
await nativeAssetsTest('dart_app', (dartAppUri) async {

pkg/dartdev/test/native_assets/test_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'package:test/test.dart';
99
import '../utils.dart';
1010
import 'helpers.dart';
1111

12-
void main(List<String> args) async {
12+
void main([List<String> args = const []]) async {
1313
if (!nativeAssetsExperimentAvailableOnCurrentChannel) {
1414
return;
1515
}

pkg/dartdev/test/regress_46364_test.dart

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

77
import 'package:expect/expect.dart';
88
import 'package:path/path.dart' as p;
9+
import 'package:test/test.dart';
910

1011
import 'utils.dart';
1112

@@ -25,20 +26,23 @@ Future<void> copyPath(String from, String to) async {
2526
}
2627

2728
Future<void> main() async {
28-
ensureRunFromSdkBinDart();
29+
test('Regression test for https://github.com/dart-lang/sdk/issues/46364',
30+
() async {
31+
ensureRunFromSdkBinDart();
2932

30-
final exePath = Platform.resolvedExecutable;
31-
final sdkDir = p.dirname(p.dirname(exePath));
32-
// Try to run the VM located on a path with % encoded characters. The VM
33-
// should not try and resolve the path as a URI for SDK artifacts (e.g.,
34-
// dartdev.dart.snapshot).
35-
final d = Directory.systemTemp.createTempSync('dart_symlink%3A');
36-
try {
37-
await copyPath(sdkDir, d.path);
38-
final path = '${d.path}/bin/dart';
39-
final result = await Process.run(path, ['help']);
40-
Expect.equals(result.exitCode, 0);
41-
} finally {
42-
await d.delete(recursive: true);
43-
}
33+
final exePath = Platform.resolvedExecutable;
34+
final sdkDir = p.dirname(p.dirname(exePath));
35+
// Try to run the VM located on a path with % encoded characters. The VM
36+
// should not try and resolve the path as a URI for SDK artifacts (e.g.,
37+
// dartdev.dart.snapshot).
38+
final d = Directory.systemTemp.createTempSync('dart_symlink%3A');
39+
try {
40+
await copyPath(sdkDir, d.path);
41+
final path = '${d.path}/bin/dart';
42+
final result = await Process.run(path, ['help']);
43+
Expect.equals(result.exitCode, 0);
44+
} finally {
45+
await d.delete(recursive: true);
46+
}
47+
});
4448
}

pkg/dartdev/test/regress_56592_test.dart

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,26 @@
55
import 'dart:io';
66

77
import 'package:expect/expect.dart';
8+
import 'package:test/test.dart';
89

910
// Passing --disable-dart-dev after a DartDev command should cause the VM to
1011
// exit with an error, not cause a segfault.
1112
//
1213
// See https://github.com/dart-lang/sdk/issues/56592 for details.
1314

1415
Future<void> main() async {
15-
final result = await Process.run(
16-
Platform.resolvedExecutable,
17-
[
18-
'test',
19-
'--disable-dart-dev',
20-
],
21-
);
22-
Expect.contains(
23-
'Attempted to use --disable-dart-dev with a Dart CLI command.',
24-
result.stderr,
25-
);
16+
test('Regression test for https://github.com/dart-lang/sdk/issues/56592',
17+
() async {
18+
final result = await Process.run(
19+
Platform.resolvedExecutable,
20+
[
21+
'test',
22+
'--disable-dart-dev',
23+
],
24+
);
25+
Expect.contains(
26+
'Attempted to use --disable-dart-dev with a Dart CLI command.',
27+
result.stderr,
28+
);
29+
});
2630
}

pkg/dartdev/test/sdk_from_path_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import 'dart:io';
88
import 'package:path/path.dart' as path;
99
import 'package:test/test.dart';
1010

11+
import 'utils.dart';
12+
1113
// Regression test for https://github.com/dart-lang/sdk/issues/56080
1214

1315
void main() {
@@ -18,13 +20,11 @@ void main() {
1820
});
1921

2022
test('sdk_test.dart passes when run with dart from PATH', () async {
21-
final script = path.join(
22-
path.dirname(Platform.script.toString()),
23-
'sdk_test.dart',
24-
);
23+
final sdkTestUri = resolveDartDevUri('test/sdk_test.dart');
24+
2525
process = await Process.start(
2626
'dart',
27-
[script],
27+
[sdkTestUri.toFilePath()],
2828
environment: {'PATH': path.dirname(Platform.resolvedExecutable)},
2929
);
3030

pkg/dartdev/test/smoke/invalid_smoke_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'dart:io';
77
import 'package:test/test.dart';
88

99
const numRuns = 10;
10-
final script = Platform.script.resolve('smoke.dart').toString();
1110

1211
void main() {
1312
group(

pkg/dartdev/test/utils.dart

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async';
66
import 'dart:convert';
77
import 'dart:io';
8+
import 'dart:isolate';
89

910
import 'package:cli_util/cli_logging.dart';
1011
import 'package:dartdev/dartdev.dart';
@@ -27,6 +28,9 @@ const String dartVersionFilePrefix2_9 = '''
2728
// @dart = 2.9
2829
''';
2930

31+
/// Return the root URI of the SDK by walking up from the pkg/dartdev folder.
32+
final sdkRootUri = resolveDartDevUri('../../');
33+
3034
void initGlobalState() {
3135
log = Logger.standard();
3236
}
@@ -273,29 +277,8 @@ class TestProject {
273277
);
274278
}
275279

276-
String? _sdkRootPath;
277-
278-
/// Return the root of the SDK.
279-
String get sdkRootPath {
280-
if (_sdkRootPath == null) {
281-
// Assumes the script importing this one is somewhere under the SDK.
282-
String current = path.canonicalize(Platform.script.toFilePath());
283-
do {
284-
String tryDir = path.dirname(current);
285-
if (File(path.join(tryDir, 'pkg', 'dartdev', 'bin', 'dartdev.dart'))
286-
.existsSync()) {
287-
_sdkRootPath = tryDir;
288-
return _sdkRootPath!;
289-
}
290-
current = tryDir;
291-
} while (path.dirname(current) != current);
292-
throw StateError('can not find SDK repository root');
293-
}
294-
return _sdkRootPath!;
295-
}
296-
297280
String get absolutePathToDartdevFile =>
298-
path.join(sdkRootPath, 'pkg', 'dartdev', 'bin', 'dartdev.dart');
281+
sdkRootUri.resolve('pkg/dartdev/bin/dartdev.dart').toFilePath();
299282

300283
Directory? findDirectory(String name) {
301284
var directory = Directory(path.join(dir.path, name));
@@ -356,3 +339,10 @@ String replacePathsWithMatchingCase(String input, {required String filePath}) {
356339
filePath,
357340
);
358341
}
342+
343+
/// Resolves a relative URI from the pkg/dartdev folder.
344+
Uri resolveDartDevUri(String path) {
345+
final dartDevLibUri =
346+
Isolate.resolvePackageUriSync(Uri.parse('package:dartdev/'));
347+
return dartDevLibUri!.resolve('../$path');
348+
}

0 commit comments

Comments
 (0)