Skip to content

Commit c8ac438

Browse files
authored
Prepare for Dart 3.0 api removals (#1812)
Fixes #1811 I also pre-emptively bumped our dep on matcher, which I think should be fine, but please push back if you disagree. Moves all null safety related tests into a legacy_tests directory.
1 parent 73cd754 commit c8ac438

File tree

31 files changed

+832
-380
lines changed

31 files changed

+832
-380
lines changed

.github/workflows/dart.yml

+354-222
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration_tests/spawn_hybrid/test/hybrid_test.dart

-14
Original file line numberDiff line numberDiff line change
@@ -341,20 +341,6 @@ void main() {
341341
});
342342
});
343343

344-
test('can opt out of null safety', () async {
345-
expect(spawnHybridCode('''
346-
// @dart=2.9
347-
import "package:stream_channel/stream_channel.dart";
348-
349-
// Would cause an error in null safety mode.
350-
int x;
351-
352-
void hybridMain(StreamChannel channel) {
353-
channel.sink..add(1)..add(2)..add(3)..close();
354-
}
355-
''').stream.toList(), completion(equals([1, 2, 3])));
356-
});
357-
358344
test('opts in to null safety by default', () async {
359345
expect(spawnHybridCode('''
360346
import "package:stream_channel/stream_channel.dart";

integration_tests/nnbd_opted_in/mono_pkg.yaml renamed to legacy_tests/nnbd_opted_in/mono_pkg.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# See https://pub.dev/packages/mono_repo
22

33
sdk:
4-
- dev
54
- pubspec
65

76
stages:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# See https://pub.dev/packages/mono_repo
2+
3+
sdk:
4+
- pubspec
5+
6+
stages:
7+
- analyze_and_format:
8+
- group:
9+
- format
10+
- analyze: --fatal-infos
11+
sdk:
12+
- pubspec
13+
- unit_test:
14+
- test: -p chrome,vm,node
15+
os:
16+
- linux
17+
- windows
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: nnbd_opted_in_with_optout
2+
environment:
3+
sdk: '>=2.18.0 <3.0.0'
4+
dev_dependencies:
5+
lints: '>=1.0.0 <3.0.0'
6+
test: any
7+
dependency_overrides:
8+
test:
9+
path: ../../pkgs/test
10+
test_api:
11+
path: ../../pkgs/test_api
12+
test_core:
13+
path: ../../pkgs/test_core
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
final bool isOptedOut = <int?>[] is List<int>;

integration_tests/nnbd_opted_out/mono_pkg.yaml renamed to legacy_tests/nnbd_opted_out/mono_pkg.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# See https://pub.dev/packages/mono_repo
22

33
sdk:
4-
- dev
54
- 2.18.0
65

76
stages:
@@ -10,7 +9,7 @@ stages:
109
- format
1110
- analyze: --fatal-infos
1211
sdk:
13-
- dev
12+
- stable
1413
- unit_test:
1514
- test: -p chrome,vm,node
1615
os:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# See https://pub.dev/packages/mono_repo
2+
3+
sdk:
4+
- pubspec
5+
6+
stages:
7+
- analyze_and_format:
8+
- group:
9+
- format
10+
- analyze: --fatal-infos
11+
sdk:
12+
- stable
13+
- unit_test:
14+
- test: -p chrome,vm,node
15+
os:
16+
- linux
17+
- windows
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: spawn_hybrid_with_optout
2+
publish_to: none
3+
environment:
4+
sdk: '>=2.18.0 <3.0.0'
5+
dev_dependencies:
6+
lints: '>=1.0.0 <3.0.0'
7+
test: any
8+
dependency_overrides:
9+
test:
10+
path: ../../pkgs/test
11+
test_api:
12+
path: ../../pkgs/test_api
13+
test_core:
14+
path: ../../pkgs/test_core
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:test/test.dart';
6+
7+
void main() {
8+
group('spawnHybridCode()', () {
9+
test('can opt out of null safety', () async {
10+
expect(spawnHybridCode('''
11+
// @dart=2.9
12+
import "package:stream_channel/stream_channel.dart";
13+
14+
// Would cause an error in null safety mode.
15+
int x;
16+
17+
void hybridMain(StreamChannel channel) {
18+
channel.sink..add(1)..add(2)..add(3)..close();
19+
}
20+
''').stream.toList(), completion(equals([1, 2, 3])));
21+
});
22+
23+
test('opts in to null safety by default', () async {
24+
expect(spawnHybridCode('''
25+
import "package:stream_channel/stream_channel.dart";
26+
27+
// Use some null safety syntax
28+
int? x;
29+
30+
void hybridMain(StreamChannel channel) {
31+
channel.sink..add(1)..add(2)..add(3)..close();
32+
}
33+
''').stream.toList(), completion(equals([1, 2, 3])));
34+
});
35+
});
36+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# See https://pub.dev/packages/mono_repo
2+
3+
sdk:
4+
- pubspec
5+
6+
stages:
7+
- analyze_and_format:
8+
- group:
9+
- format
10+
- analyze: --fatal-infos
11+
sdk:
12+
- stable
13+
- unit_test:
14+
- test: -p chrome,vm,node
15+
os:
16+
- linux
17+
- windows

legacy_tests/unit_tests/pubspec.yaml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: legacy_unit_tests
2+
publish_to: none
3+
environment:
4+
sdk: '>=2.18.0 <3.0.0'
5+
dev_dependencies:
6+
lints: '>=1.0.0 <3.0.0'
7+
package_config: ^2.0.0
8+
path: ^1.8.0
9+
test: any
10+
test_descriptor: ^2.0.0
11+
test_process: ^2.0.0
12+
dependency_overrides:
13+
test:
14+
path: ../../pkgs/test
15+
test_api:
16+
path: ../../pkgs/test_api
17+
test_core:
18+
path: ../../pkgs/test_core

legacy_tests/unit_tests/test/io.dart

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:async';
6+
import 'dart:io';
7+
import 'dart:isolate';
8+
9+
import 'package:path/path.dart' as p;
10+
import 'package:test/test.dart';
11+
import 'package:test_descriptor/test_descriptor.dart' as d;
12+
import 'package:test_process/test_process.dart';
13+
14+
/// The path to the root directory of the `test` package.
15+
final Future<String> packageDir =
16+
Isolate.resolvePackageUri(Uri(scheme: 'package', path: 'test/'))
17+
.then((uri) {
18+
var dir = p.dirname(uri!.path);
19+
// If it starts with a `/C:` or other drive letter, remove the leading `/`.
20+
if (dir[0] == '/' && dir[2] == ':') dir = dir.substring(1);
21+
return dir;
22+
});
23+
24+
/// The path to the `pub` executable in the current Dart SDK.
25+
final _pubPath = p.absolute(p.join(p.dirname(Platform.resolvedExecutable),
26+
Platform.isWindows ? 'pub.bat' : 'pub'));
27+
28+
/// The platform-specific message emitted when a nonexistent file is loaded.
29+
final String noSuchFileMessage = Platform.isWindows
30+
? 'The system cannot find the file specified.'
31+
: 'No such file or directory';
32+
33+
/// A regular expression that matches the output of "pub serve".
34+
final _servingRegExp =
35+
RegExp(r'^Serving myapp [a-z]+ on http://localhost:(\d+)$');
36+
37+
/// An operating system name that's different than the current operating system.
38+
final otherOS = Platform.isWindows ? 'mac-os' : 'windows';
39+
40+
/// The port of a pub serve instance run via [runPubServe].
41+
///
42+
/// This is only set after [runPubServe] is called.
43+
int get pubServePort => _pubServePort!;
44+
int? _pubServePort;
45+
46+
/// Expects that the entire stdout stream of [test] equals [expected].
47+
void expectStdoutEquals(TestProcess test, String expected) =>
48+
_expectStreamEquals(test.stdoutStream(), expected);
49+
50+
/// Expects that the entire stderr stream of [test] equals [expected].
51+
void expectStderrEquals(TestProcess test, String expected) =>
52+
_expectStreamEquals(test.stderrStream(), expected);
53+
54+
/// Expects that the entirety of the line stream [stream] equals [expected].
55+
void _expectStreamEquals(Stream<String> stream, String expected) {
56+
expect((() async {
57+
var lines = await stream.toList();
58+
expect(lines.join('\n').trim(), equals(expected.trim()));
59+
})(), completes);
60+
}
61+
62+
/// Returns a [StreamMatcher] that asserts that the stream emits strings
63+
/// containing each string in [strings] in order.
64+
///
65+
/// This expects each string in [strings] to match a different string in the
66+
/// stream.
67+
StreamMatcher containsInOrder(Iterable<String> strings) =>
68+
emitsInOrder(strings.map((string) => emitsThrough(contains(string))));
69+
70+
/// Lazily compile the test package to kernel and re-use that, initialized with
71+
/// [precompileTestExecutable].
72+
String? _testExecutablePath;
73+
74+
/// Must be invoked before any call to [runTests], should be invoked in a top
75+
/// level `setUpAll` for best caching results.
76+
Future<void> precompileTestExecutable() async {
77+
if (_testExecutablePath != null) {
78+
throw StateError('Test executable already precompiled');
79+
}
80+
var tmpDirectory = await Directory.systemTemp.createTemp('test');
81+
var precompiledPath = p.join(tmpDirectory.path, 'test_runner.dill');
82+
var result = await Process.run(Platform.executable, [
83+
'compile',
84+
'kernel',
85+
p.url.join(await packageDir, 'bin', 'test.dart'),
86+
'-o',
87+
precompiledPath,
88+
]);
89+
if (result.exitCode != 0) {
90+
throw StateError(
91+
'Failed to compile test runner:\n${result.stdout}\n${result.stderr}');
92+
}
93+
94+
addTearDown(() async {
95+
await tmpDirectory.delete(recursive: true);
96+
});
97+
_testExecutablePath = precompiledPath;
98+
}
99+
100+
/// Runs the test executable with the package root set properly.
101+
///
102+
/// You must invoke [precompileTestExecutable] before invoking this function.
103+
Future<TestProcess> runTest(Iterable<String> args,
104+
{String? reporter,
105+
String? fileReporter,
106+
int? concurrency,
107+
Map<String, String>? environment,
108+
bool forwardStdio = false,
109+
String? packageConfig,
110+
Iterable<String>? vmArgs}) async {
111+
concurrency ??= 1;
112+
var testExecutablePath = _testExecutablePath;
113+
if (testExecutablePath == null) {
114+
throw StateError(
115+
'You must call `precompileTestExecutable` before calling `runTest`');
116+
}
117+
118+
var allArgs = [
119+
...?vmArgs,
120+
testExecutablePath,
121+
'--concurrency=$concurrency',
122+
if (reporter != null) '--reporter=$reporter',
123+
if (fileReporter != null) '--file-reporter=$fileReporter',
124+
...args,
125+
];
126+
127+
environment ??= {};
128+
environment.putIfAbsent('_DART_TEST_TESTING', () => 'true');
129+
130+
return await runDart(allArgs,
131+
environment: environment,
132+
description: 'dart bin/test.dart',
133+
forwardStdio: forwardStdio,
134+
packageConfig: packageConfig);
135+
}
136+
137+
/// Runs Dart.
138+
///
139+
/// If [packageConfig] is provided then that is passed for the `--packages`
140+
/// arg, otherwise the current isolate config is passed.
141+
Future<TestProcess> runDart(Iterable<String> args,
142+
{Map<String, String>? environment,
143+
String? description,
144+
bool forwardStdio = false,
145+
String? packageConfig}) async {
146+
var allArgs = <String>[
147+
...Platform.executableArguments.where((arg) =>
148+
!arg.startsWith('--package-root=') && !arg.startsWith('--packages=')),
149+
'--packages=${packageConfig ?? await Isolate.packageConfig}',
150+
...args
151+
];
152+
153+
return await TestProcess.start(
154+
p.absolute(Platform.resolvedExecutable), allArgs,
155+
workingDirectory: d.sandbox,
156+
environment: environment,
157+
description: description,
158+
forwardStdio: forwardStdio);
159+
}
160+
161+
/// Runs Pub.
162+
Future<TestProcess> runPub(Iterable<String> args,
163+
{Map<String, String>? environment}) {
164+
return TestProcess.start(_pubPath, args,
165+
workingDirectory: d.sandbox,
166+
environment: environment,
167+
description: 'pub ${args.first}');
168+
}
169+
170+
/// Runs "pub serve".
171+
///
172+
/// This returns assigns [_pubServePort] to a future that will complete to the
173+
/// port of the "pub serve" instance.
174+
Future<TestProcess> runPubServe(
175+
{Iterable<String>? args,
176+
String? workingDirectory,
177+
Map<String, String>? environment}) async {
178+
var allArgs = ['serve', '--port', '0'];
179+
if (args != null) allArgs.addAll(args);
180+
181+
var pub = await runPub(allArgs, environment: environment);
182+
183+
Match? match;
184+
while (match == null) {
185+
match = _servingRegExp.firstMatch(await pub.stdout.next);
186+
}
187+
_pubServePort = int.parse(match[1]!);
188+
189+
return pub;
190+
}

0 commit comments

Comments
 (0)