[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 <bkonyi@google.com>
Reviewed-by: Jake Macdonald <jakemac@google.com>
Commit-Queue: Ben Konyi <bkonyi@google.com>
This commit is contained in:
Danny Tuppeny
2025-04-15 07:45:39 -07:00
committed by Commit Queue
parent e4676d5b56
commit f24d4d1653
9 changed files with 64 additions and 66 deletions
@@ -21,11 +21,13 @@ String crossOSNotAllowedError(String format) =>
final String hostOSMessage = 'Host OS: ${Platform.operatingSystem}';
String targetOSMessage(String targetOS) => 'Target OS: $targetOS';
void main(List<String> args) async {
void main([List<String> args = const []]) async {
if (!nativeAssetsExperimentAvailableOnCurrentChannel) {
return;
}
final dartDevEntryScriptUri = resolveDartDevUri('bin/dartdev.dart');
final bool fromDartdevSource = args.contains('--source');
final hostOS = Platform.operatingSystem;
final crossOS = Platform.isLinux ? 'macos' : 'linux';
@@ -48,8 +50,7 @@ void main(List<String> args) async {
final result = await runDart(
arguments: [
'--enable-experiment=native-assets',
if (fromDartdevSource)
Platform.script.resolve('../../bin/dartdev.dart').toFilePath(),
if (fromDartdevSource) dartDevEntryScriptUri.toFilePath(),
'build',
if (targetOS != null) ...[
'--target-os',
@@ -219,8 +220,7 @@ void main(List<String> args) {
final result = await runDart(
arguments: [
'--enable-experiment=native-assets',
if (fromDartdevSource)
Platform.script.resolve('../../bin/dartdev.dart').toFilePath(),
if (fromDartdevSource) dartDevEntryScriptUri.toFilePath(),
'build',
'bin/dart_app.dart',
'.'
+6 -5
View File
@@ -15,6 +15,8 @@ import 'package:test/test.dart';
import 'package:yaml/yaml.dart';
import 'package:yaml_edit/yaml_edit.dart';
import '../utils.dart';
extension UriExtension on Uri {
Uri get parent {
return File(toFilePath()).parent.uri;
@@ -243,9 +245,8 @@ Future<void> nativeAssetsTest(
'treeshaking_native_libs',
'user_defines',
],
Platform.script.resolve(
'../../../../third_party/pkg/native/pkgs/native_assets_builder/'),
Platform.script.resolve('../../../../'),
sdkRootUri.resolve('third_party/pkg/native/pkgs/native_assets_builder/'),
sdkRootUri,
usePubWorkspace,
);
@@ -259,8 +260,8 @@ Future<void> recordUseTest(
skipPubGet,
fun,
const ['drop_dylib_recording'],
Platform.script.resolve('../../../record_use/'),
Platform.script.resolve('../../../../'),
sdkRootUri.resolve('pkg/record_use/'),
sdkRootUri,
false,
);
+1 -1
View File
@@ -9,7 +9,7 @@ import 'package:test/test.dart';
import '../utils.dart';
import 'helpers.dart';
void main(List<String> args) async {
void main([List<String> args = const []]) async {
if (!nativeAssetsExperimentAvailableOnCurrentChannel) {
test('dart run', timeout: longTimeout, () async {
await nativeAssetsTest('dart_app', (dartAppUri) async {
@@ -9,7 +9,7 @@ import 'package:test/test.dart';
import '../utils.dart';
import 'helpers.dart';
void main(List<String> args) async {
void main([List<String> args = const []]) async {
if (!nativeAssetsExperimentAvailableOnCurrentChannel) {
return;
}
+19 -15
View File
@@ -6,6 +6,7 @@ import 'dart:io';
import 'package:expect/expect.dart';
import 'package:path/path.dart' as p;
import 'package:test/test.dart';
import 'utils.dart';
@@ -25,20 +26,23 @@ Future<void> copyPath(String from, String to) async {
}
Future<void> main() async {
ensureRunFromSdkBinDart();
test('Regression test for https://github.com/dart-lang/sdk/issues/46364',
() async {
ensureRunFromSdkBinDart();
final exePath = Platform.resolvedExecutable;
final sdkDir = p.dirname(p.dirname(exePath));
// Try to run the VM located on a path with % encoded characters. The VM
// should not try and resolve the path as a URI for SDK artifacts (e.g.,
// dartdev.dart.snapshot).
final d = Directory.systemTemp.createTempSync('dart_symlink%3A');
try {
await copyPath(sdkDir, d.path);
final path = '${d.path}/bin/dart';
final result = await Process.run(path, ['help']);
Expect.equals(result.exitCode, 0);
} finally {
await d.delete(recursive: true);
}
final exePath = Platform.resolvedExecutable;
final sdkDir = p.dirname(p.dirname(exePath));
// Try to run the VM located on a path with % encoded characters. The VM
// should not try and resolve the path as a URI for SDK artifacts (e.g.,
// dartdev.dart.snapshot).
final d = Directory.systemTemp.createTempSync('dart_symlink%3A');
try {
await copyPath(sdkDir, d.path);
final path = '${d.path}/bin/dart';
final result = await Process.run(path, ['help']);
Expect.equals(result.exitCode, 0);
} finally {
await d.delete(recursive: true);
}
});
}
+15 -11
View File
@@ -5,6 +5,7 @@
import 'dart:io';
import 'package:expect/expect.dart';
import 'package:test/test.dart';
// Passing --disable-dart-dev after a DartDev command should cause the VM to
// exit with an error, not cause a segfault.
@@ -12,15 +13,18 @@ import 'package:expect/expect.dart';
// See https://github.com/dart-lang/sdk/issues/56592 for details.
Future<void> main() async {
final result = await Process.run(
Platform.resolvedExecutable,
[
'test',
'--disable-dart-dev',
],
);
Expect.contains(
'Attempted to use --disable-dart-dev with a Dart CLI command.',
result.stderr,
);
test('Regression test for https://github.com/dart-lang/sdk/issues/56592',
() async {
final result = await Process.run(
Platform.resolvedExecutable,
[
'test',
'--disable-dart-dev',
],
);
Expect.contains(
'Attempted to use --disable-dart-dev with a Dart CLI command.',
result.stderr,
);
});
}
+5 -5
View File
@@ -8,6 +8,8 @@ import 'dart:io';
import 'package:path/path.dart' as path;
import 'package:test/test.dart';
import 'utils.dart';
// Regression test for https://github.com/dart-lang/sdk/issues/56080
void main() {
@@ -18,13 +20,11 @@ void main() {
});
test('sdk_test.dart passes when run with dart from PATH', () async {
final script = path.join(
path.dirname(Platform.script.toString()),
'sdk_test.dart',
);
final sdkTestUri = resolveDartDevUri('test/sdk_test.dart');
process = await Process.start(
'dart',
[script],
[sdkTestUri.toFilePath()],
environment: {'PATH': path.dirname(Platform.resolvedExecutable)},
);
@@ -7,7 +7,6 @@ import 'dart:io';
import 'package:test/test.dart';
const numRuns = 10;
final script = Platform.script.resolve('smoke.dart').toString();
void main() {
group(
+12 -22
View File
@@ -5,6 +5,7 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';
import 'package:cli_util/cli_logging.dart';
import 'package:dartdev/dartdev.dart';
@@ -27,6 +28,9 @@ const String dartVersionFilePrefix2_9 = '''
// @dart = 2.9
''';
/// Return the root URI of the SDK by walking up from the pkg/dartdev folder.
final sdkRootUri = resolveDartDevUri('../../');
void initGlobalState() {
log = Logger.standard();
}
@@ -273,29 +277,8 @@ class TestProject {
);
}
String? _sdkRootPath;
/// Return the root of the SDK.
String get sdkRootPath {
if (_sdkRootPath == null) {
// Assumes the script importing this one is somewhere under the SDK.
String current = path.canonicalize(Platform.script.toFilePath());
do {
String tryDir = path.dirname(current);
if (File(path.join(tryDir, 'pkg', 'dartdev', 'bin', 'dartdev.dart'))
.existsSync()) {
_sdkRootPath = tryDir;
return _sdkRootPath!;
}
current = tryDir;
} while (path.dirname(current) != current);
throw StateError('can not find SDK repository root');
}
return _sdkRootPath!;
}
String get absolutePathToDartdevFile =>
path.join(sdkRootPath, 'pkg', 'dartdev', 'bin', 'dartdev.dart');
sdkRootUri.resolve('pkg/dartdev/bin/dartdev.dart').toFilePath();
Directory? findDirectory(String name) {
var directory = Directory(path.join(dir.path, name));
@@ -356,3 +339,10 @@ String replacePathsWithMatchingCase(String input, {required String filePath}) {
filePath,
);
}
/// Resolves a relative URI from the pkg/dartdev folder.
Uri resolveDartDevUri(String path) {
final dartDevLibUri =
Isolate.resolvePackageUriSync(Uri.parse('package:dartdev/'));
return dartDevLibUri!.resolve('../$path');
}