Skip to content

Commit

Permalink
Prepare for Dart 3.0 api removals (#1812)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jakemac53 authored Dec 8, 2022
1 parent 73cd754 commit c8ac438
Show file tree
Hide file tree
Showing 31 changed files with 832 additions and 380 deletions.
576 changes: 354 additions & 222 deletions .github/workflows/dart.yml

Large diffs are not rendered by default.

14 changes: 0 additions & 14 deletions integration_tests/spawn_hybrid/test/hybrid_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -341,20 +341,6 @@ void main() {
});
});

test('can opt out of null safety', () async {
expect(spawnHybridCode('''
// @dart=2.9
import "package:stream_channel/stream_channel.dart";
// Would cause an error in null safety mode.
int x;
void hybridMain(StreamChannel channel) {
channel.sink..add(1)..add(2)..add(3)..close();
}
''').stream.toList(), completion(equals([1, 2, 3])));
});

test('opts in to null safety by default', () async {
expect(spawnHybridCode('''
import "package:stream_channel/stream_channel.dart";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# See https://pub.dev/packages/mono_repo

sdk:
- dev
- pubspec

stages:
Expand Down
File renamed without changes.
17 changes: 17 additions & 0 deletions legacy_tests/nnbd_opted_in_with_optout/mono_pkg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# See https://pub.dev/packages/mono_repo

sdk:
- pubspec

stages:
- analyze_and_format:
- group:
- format
- analyze: --fatal-infos
sdk:
- pubspec
- unit_test:
- test: -p chrome,vm,node
os:
- linux
- windows
13 changes: 13 additions & 0 deletions legacy_tests/nnbd_opted_in_with_optout/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: nnbd_opted_in_with_optout
environment:
sdk: '>=2.18.0 <3.0.0'
dev_dependencies:
lints: '>=1.0.0 <3.0.0'
test: any
dependency_overrides:
test:
path: ../../pkgs/test
test_api:
path: ../../pkgs/test_api
test_core:
path: ../../pkgs/test_core
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

final bool isOptedOut = <int?>[] is List<int>;
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# See https://pub.dev/packages/mono_repo

sdk:
- dev
- 2.18.0

stages:
Expand All @@ -10,7 +9,7 @@ stages:
- format
- analyze: --fatal-infos
sdk:
- dev
- stable
- unit_test:
- test: -p chrome,vm,node
os:
Expand Down
File renamed without changes.
17 changes: 17 additions & 0 deletions legacy_tests/spawn_hybrid_with_optout/mono_pkg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# See https://pub.dev/packages/mono_repo

sdk:
- pubspec

stages:
- analyze_and_format:
- group:
- format
- analyze: --fatal-infos
sdk:
- stable
- unit_test:
- test: -p chrome,vm,node
os:
- linux
- windows
14 changes: 14 additions & 0 deletions legacy_tests/spawn_hybrid_with_optout/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: spawn_hybrid_with_optout
publish_to: none
environment:
sdk: '>=2.18.0 <3.0.0'
dev_dependencies:
lints: '>=1.0.0 <3.0.0'
test: any
dependency_overrides:
test:
path: ../../pkgs/test
test_api:
path: ../../pkgs/test_api
test_core:
path: ../../pkgs/test_core
36 changes: 36 additions & 0 deletions legacy_tests/spawn_hybrid_with_optout/test/hybrid_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:test/test.dart';

void main() {
group('spawnHybridCode()', () {
test('can opt out of null safety', () async {
expect(spawnHybridCode('''
// @dart=2.9
import "package:stream_channel/stream_channel.dart";
// Would cause an error in null safety mode.
int x;
void hybridMain(StreamChannel channel) {
channel.sink..add(1)..add(2)..add(3)..close();
}
''').stream.toList(), completion(equals([1, 2, 3])));
});

test('opts in to null safety by default', () async {
expect(spawnHybridCode('''
import "package:stream_channel/stream_channel.dart";
// Use some null safety syntax
int? x;
void hybridMain(StreamChannel channel) {
channel.sink..add(1)..add(2)..add(3)..close();
}
''').stream.toList(), completion(equals([1, 2, 3])));
});
});
}
17 changes: 17 additions & 0 deletions legacy_tests/unit_tests/mono_repo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# See https://pub.dev/packages/mono_repo

sdk:
- pubspec

stages:
- analyze_and_format:
- group:
- format
- analyze: --fatal-infos
sdk:
- stable
- unit_test:
- test: -p chrome,vm,node
os:
- linux
- windows
18 changes: 18 additions & 0 deletions legacy_tests/unit_tests/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: legacy_unit_tests
publish_to: none
environment:
sdk: '>=2.18.0 <3.0.0'
dev_dependencies:
lints: '>=1.0.0 <3.0.0'
package_config: ^2.0.0
path: ^1.8.0
test: any
test_descriptor: ^2.0.0
test_process: ^2.0.0
dependency_overrides:
test:
path: ../../pkgs/test
test_api:
path: ../../pkgs/test_api
test_core:
path: ../../pkgs/test_core
190 changes: 190 additions & 0 deletions legacy_tests/unit_tests/test/io.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:io';
import 'dart:isolate';

import 'package:path/path.dart' as p;
import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;
import 'package:test_process/test_process.dart';

/// The path to the root directory of the `test` package.
final Future<String> packageDir =
Isolate.resolvePackageUri(Uri(scheme: 'package', path: 'test/'))
.then((uri) {
var dir = p.dirname(uri!.path);
// If it starts with a `/C:` or other drive letter, remove the leading `/`.
if (dir[0] == '/' && dir[2] == ':') dir = dir.substring(1);
return dir;
});

/// The path to the `pub` executable in the current Dart SDK.
final _pubPath = p.absolute(p.join(p.dirname(Platform.resolvedExecutable),
Platform.isWindows ? 'pub.bat' : 'pub'));

/// The platform-specific message emitted when a nonexistent file is loaded.
final String noSuchFileMessage = Platform.isWindows
? 'The system cannot find the file specified.'
: 'No such file or directory';

/// A regular expression that matches the output of "pub serve".
final _servingRegExp =
RegExp(r'^Serving myapp [a-z]+ on http://localhost:(\d+)$');

/// An operating system name that's different than the current operating system.
final otherOS = Platform.isWindows ? 'mac-os' : 'windows';

/// The port of a pub serve instance run via [runPubServe].
///
/// This is only set after [runPubServe] is called.
int get pubServePort => _pubServePort!;
int? _pubServePort;

/// Expects that the entire stdout stream of [test] equals [expected].
void expectStdoutEquals(TestProcess test, String expected) =>
_expectStreamEquals(test.stdoutStream(), expected);

/// Expects that the entire stderr stream of [test] equals [expected].
void expectStderrEquals(TestProcess test, String expected) =>
_expectStreamEquals(test.stderrStream(), expected);

/// Expects that the entirety of the line stream [stream] equals [expected].
void _expectStreamEquals(Stream<String> stream, String expected) {
expect((() async {
var lines = await stream.toList();
expect(lines.join('\n').trim(), equals(expected.trim()));
})(), completes);
}

/// Returns a [StreamMatcher] that asserts that the stream emits strings
/// containing each string in [strings] in order.
///
/// This expects each string in [strings] to match a different string in the
/// stream.
StreamMatcher containsInOrder(Iterable<String> strings) =>
emitsInOrder(strings.map((string) => emitsThrough(contains(string))));

/// Lazily compile the test package to kernel and re-use that, initialized with
/// [precompileTestExecutable].
String? _testExecutablePath;

/// Must be invoked before any call to [runTests], should be invoked in a top
/// level `setUpAll` for best caching results.
Future<void> precompileTestExecutable() async {
if (_testExecutablePath != null) {
throw StateError('Test executable already precompiled');
}
var tmpDirectory = await Directory.systemTemp.createTemp('test');
var precompiledPath = p.join(tmpDirectory.path, 'test_runner.dill');
var result = await Process.run(Platform.executable, [
'compile',
'kernel',
p.url.join(await packageDir, 'bin', 'test.dart'),
'-o',
precompiledPath,
]);
if (result.exitCode != 0) {
throw StateError(
'Failed to compile test runner:\n${result.stdout}\n${result.stderr}');
}

addTearDown(() async {
await tmpDirectory.delete(recursive: true);
});
_testExecutablePath = precompiledPath;
}

/// Runs the test executable with the package root set properly.
///
/// You must invoke [precompileTestExecutable] before invoking this function.
Future<TestProcess> runTest(Iterable<String> args,
{String? reporter,
String? fileReporter,
int? concurrency,
Map<String, String>? environment,
bool forwardStdio = false,
String? packageConfig,
Iterable<String>? vmArgs}) async {
concurrency ??= 1;
var testExecutablePath = _testExecutablePath;
if (testExecutablePath == null) {
throw StateError(
'You must call `precompileTestExecutable` before calling `runTest`');
}

var allArgs = [
...?vmArgs,
testExecutablePath,
'--concurrency=$concurrency',
if (reporter != null) '--reporter=$reporter',
if (fileReporter != null) '--file-reporter=$fileReporter',
...args,
];

environment ??= {};
environment.putIfAbsent('_DART_TEST_TESTING', () => 'true');

return await runDart(allArgs,
environment: environment,
description: 'dart bin/test.dart',
forwardStdio: forwardStdio,
packageConfig: packageConfig);
}

/// Runs Dart.
///
/// If [packageConfig] is provided then that is passed for the `--packages`
/// arg, otherwise the current isolate config is passed.
Future<TestProcess> runDart(Iterable<String> args,
{Map<String, String>? environment,
String? description,
bool forwardStdio = false,
String? packageConfig}) async {
var allArgs = <String>[
...Platform.executableArguments.where((arg) =>
!arg.startsWith('--package-root=') && !arg.startsWith('--packages=')),
'--packages=${packageConfig ?? await Isolate.packageConfig}',
...args
];

return await TestProcess.start(
p.absolute(Platform.resolvedExecutable), allArgs,
workingDirectory: d.sandbox,
environment: environment,
description: description,
forwardStdio: forwardStdio);
}

/// Runs Pub.
Future<TestProcess> runPub(Iterable<String> args,
{Map<String, String>? environment}) {
return TestProcess.start(_pubPath, args,
workingDirectory: d.sandbox,
environment: environment,
description: 'pub ${args.first}');
}

/// Runs "pub serve".
///
/// This returns assigns [_pubServePort] to a future that will complete to the
/// port of the "pub serve" instance.
Future<TestProcess> runPubServe(
{Iterable<String>? args,
String? workingDirectory,
Map<String, String>? environment}) async {
var allArgs = ['serve', '--port', '0'];
if (args != null) allArgs.addAll(args);

var pub = await runPub(allArgs, environment: environment);

Match? match;
while (match == null) {
match = _servingRegExp.firstMatch(await pub.stdout.next);
}
_pubServePort = int.parse(match[1]!);

return pub;
}
Loading

0 comments on commit c8ac438

Please sign in to comment.