Unverified Commit 4db845fb authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

Add capability to flutter test --platform=chrome (#33525)

parent 56940b54
targets:
$default:
builders:
build_web_compilers|entrypoint:
enabled: false
sources:
exclude:
- "test/data/**"
......@@ -12,17 +12,21 @@ import 'package:build_modules/src/platform.dart';
import 'package:build_runner_core/build_runner_core.dart' as core;
import 'package:build_runner_core/src/generate/build_impl.dart';
import 'package:build_runner_core/src/generate/options.dart';
import 'package:build_test/builder.dart';
import 'package:build_test/src/debug_test_builder.dart';
import 'package:build_web_compilers/build_web_compilers.dart';
import 'package:build_web_compilers/builders.dart';
import 'package:build_web_compilers/src/dev_compiler_bootstrap.dart';
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;
import 'package:test_core/backend.dart';
import 'package:watcher/watcher.dart';
import '../artifacts.dart';
import '../base/file_system.dart';
import '../base/logger.dart';
import '../base/platform.dart';
import '../compile.dart';
import '../dart/package_map.dart';
import '../globals.dart';
......@@ -65,6 +69,20 @@ final DartPlatform flutterWebPlatform =
/// The build application to compile a flutter application to the web.
final List<core.BuilderApplication> builders = <core.BuilderApplication>[
core.apply(
'flutter_tools|test_bootstrap',
<BuilderFactory>[
(BuilderOptions options) => const DebugTestBuilder(),
(BuilderOptions options) => const FlutterWebTestBootstrapBuilder(),
],
core.toRoot(),
hideOutput: true,
defaultGenerateFor: const InputSet(
include: <String>[
'test/**',
],
),
),
core.apply(
'flutter_tools|module_library',
<Builder Function(BuilderOptions)>[moduleLibraryBuilder],
......@@ -109,7 +127,7 @@ final List<core.BuilderApplication> builders = <core.BuilderApplication>[
'flutter_tools|entrypoint',
<BuilderFactory>[
(BuilderOptions options) => FlutterWebEntrypointBuilder(
options.config['target'] ?? 'lib/main.dart'),
options.config['targets'] ?? <String>['lib/main.dart']),
],
core.toRoot(),
hideOutput: true,
......@@ -117,6 +135,7 @@ final List<core.BuilderApplication> builders = <core.BuilderApplication>[
include: <String>[
'lib/**',
'web/**',
'test/**_test.dart.browser_test.dart',
],
),
),
......@@ -135,13 +154,14 @@ class BuildRunnerWebCompilationProxy extends WebCompilationProxy {
@override
Future<void> initialize({
@required Directory projectDirectory,
@required String target,
@required List<String> targets,
String testOutputDir,
}) async {
// Override the generated output directory so this does not conflict with
// other build_runner output.
core.overrideGeneratedOutputDirectory('flutter_web');
_packageUriMapper = PackageUriMapper(
path.absolute(target), PackageMap.globalPackagesPath, null, null);
path.absolute('lib/main.dart'), PackageMap.globalPackagesPath, null, null);
_packageGraph = core.PackageGraph.forPath(projectDirectory.path);
final core.BuildEnvironment buildEnvironment = core.OverrideableEnvironment(
core.IOEnvironment(_packageGraph), onLog: (LogRecord record) {
......@@ -163,8 +183,18 @@ class BuildRunnerWebCompilationProxy extends WebCompilationProxy {
trackPerformance: false,
deleteFilesByDefault: true,
);
final Set<core.BuildDirectory> buildDirs = <core.BuildDirectory>{
if (testOutputDir != null)
core.BuildDirectory(
'test',
outputLocation: core.OutputLocation(
testOutputDir,
useSymlinks: !platform.isWindows,
),
),
};
final Status status =
logger.startProgress('Compiling $target for the Web...', timeout: null);
logger.startProgress('Compiling ${targets.first} for the Web...', timeout: null);
try {
_builder = await BuildImpl.create(
buildOptions,
......@@ -172,12 +202,12 @@ class BuildRunnerWebCompilationProxy extends WebCompilationProxy {
builders,
<String, Map<String, dynamic>>{
'flutter_tools|entrypoint': <String, dynamic>{
'target': target,
'targets': targets,
}
},
isReleaseBuild: false,
);
await _builder.run(const <AssetId, ChangeType>{});
await _builder.run(const <AssetId, ChangeType>{}, buildDirs: buildDirs);
} finally {
status.stop();
}
......@@ -205,9 +235,9 @@ class BuildRunnerWebCompilationProxy extends WebCompilationProxy {
/// A ddc-only entrypoint builder that respects the Flutter target flag.
class FlutterWebEntrypointBuilder implements Builder {
const FlutterWebEntrypointBuilder(this.target);
const FlutterWebEntrypointBuilder(this.targets);
final String target;
final List<String> targets;
@override
Map<String, List<String>> get buildExtensions => const <String, List<String>>{
......@@ -222,10 +252,124 @@ class FlutterWebEntrypointBuilder implements Builder {
@override
Future<void> build(BuildStep buildStep) async {
if (!buildStep.inputId.path.contains(target)) {
bool matches = false;
for (String target in targets) {
if (buildStep.inputId.path.contains(target)) {
matches = true;
break;
}
}
if (!matches) {
return;
}
log.info('building for target ${buildStep.inputId.path}');
await bootstrapDdc(buildStep, platform: flutterWebPlatform);
}
}
class FlutterWebTestBootstrapBuilder implements Builder {
const FlutterWebTestBootstrapBuilder();
@override
Map<String, List<String>> get buildExtensions => const <String, List<String>>{
'_test.dart': <String>[
'_test.dart.browser_test.dart',
]
};
@override
Future<void> build(BuildStep buildStep) async {
final AssetId id = buildStep.inputId;
final String contents = await buildStep.readAsString(id);
final String assetPath = id.pathSegments.first == 'lib'
? path.url.join('packages', id.package, id.path)
: id.path;
final Metadata metadata = parseMetadata(
assetPath, contents, Runtime.builtIn.map((Runtime runtime) => runtime.name).toSet());
if (metadata.testOn.evaluate(SuitePlatform(Runtime.chrome))) {
await buildStep.writeAsString(id.addExtension('.browser_test.dart'), '''
import 'dart:ui' as ui;
import 'dart:html';
import 'dart:js';
import 'package:stream_channel/stream_channel.dart';
import 'package:test_api/src/backend/stack_trace_formatter.dart'; // ignore: implementation_imports
import 'package:test_api/src/util/stack_trace_mapper.dart'; // ignore: implementation_imports
import 'package:test_api/src/remote_listener.dart'; // ignore: implementation_imports
import 'package:test_api/src/suite_channel_manager.dart'; // ignore: implementation_imports
import "${path.url.basename(id.path)}" as test;
Future<void> main() async {
// Extra initialization for flutter_web.
// The following parameters are hard-coded in Flutter's test embedder. Since
// we don't have an embedder yet this is the lowest-most layer we can put
// this stuff in.
await ui.webOnlyTestSetup();
internalBootstrapBrowserTest(() => test.main);
}
void internalBootstrapBrowserTest(Function getMain()) {
var channel =
serializeSuite(getMain, hidePrints: false, beforeLoad: () async {
var serialized =
await suiteChannel("test.browser.mapper").stream.first as Map;
if (serialized == null) return;
});
postMessageChannel().pipe(channel);
}
StreamChannel serializeSuite(Function getMain(),
{bool hidePrints = true, Future beforeLoad()}) =>
RemoteListener.start(getMain,
hidePrints: hidePrints, beforeLoad: beforeLoad);
StreamChannel suiteChannel(String name) {
var manager = SuiteChannelManager.current;
if (manager == null) {
throw StateError('suiteChannel() may only be called within a test worker.');
}
return manager.connectOut(name);
}
StreamChannel postMessageChannel() {
var controller = StreamChannelController(sync: true);
window.onMessage.firstWhere((message) {
return message.origin == window.location.origin && message.data == "port";
}).then((message) {
var port = message.ports.first;
var portSubscription = port.onMessage.listen((message) {
controller.local.sink.add(message.data);
});
controller.local.stream.listen((data) {
port.postMessage({"data": data});
}, onDone: () {
port.postMessage({"event": "done"});
portSubscription.cancel();
});
});
context['parent'].callMethod('postMessage', [
JsObject.jsify({"href": window.location.href, "ready": true}),
window.location.origin,
]);
return controller.foreign;
}
void setStackTraceMap
per(StackTraceMapper mapper) {
var formatter = StackTraceFormatter.current;
if (formatter == null) {
throw StateError(
'setStackTraceMapper() may only be called within a test worker.');
}
formatter.configure(mapper: mapper);
}
''');
}
}
}
......@@ -99,6 +99,11 @@ class TestCommand extends FastFlutterCommand {
negatable: true,
help: 'Whether to build the assets bundle for testing.\n'
'Consider using --no-test-assets if assets are not required.',
)
..addOption('platform',
allowed: const <String>['tester', 'chrome'],
defaultsTo: 'tester',
help: 'The platform to run the unit tests on. Defaults to "tester".'
);
}
......@@ -166,6 +171,16 @@ class TestCommand extends FastFlutterCommand {
'Test files must be in that directory and end with the pattern "_test.dart".'
);
}
} else {
final List<String> fileCopy = <String>[];
for (String file in files) {
if (file.endsWith(platform.pathSeparator)) {
fileCopy.addAll(_findTests(fs.directory(file)));
} else {
fileCopy.add(file);
}
}
files = fileCopy;
}
CoverageCollector collector;
......@@ -222,6 +237,7 @@ class TestCommand extends FastFlutterCommand {
concurrency: jobs,
buildTestAssets: buildTestAssets,
flutterProject: flutterProject,
web: argResults['platform'] == 'chrome',
);
if (collector != null) {
......
......@@ -112,7 +112,7 @@ class ResidentWebRunner extends ResidentRunner {
// Start the web compiler and build the assets.
await webCompilationProxy.initialize(
projectDirectory: currentProject.directory,
target: target,
targets: <String>[target],
);
_lastCompiled = DateTime.now();
final AssetBundle assetBundle = AssetBundleFactory.instance.createBundle();
......
This diff is collapsed.
......@@ -5,7 +5,9 @@
import 'dart:async';
import 'package:meta/meta.dart';
import 'package:test_api/backend.dart';
import 'package:test_core/src/executable.dart' as test; // ignore: implementation_imports
import 'package:test_core/src/runner/hack_register_platform.dart' as hack; // ignore: implementation_imports
import '../artifacts.dart';
import '../base/common.dart';
......@@ -16,7 +18,9 @@ import '../base/terminal.dart';
import '../dart/package_map.dart';
import '../globals.dart';
import '../project.dart';
import '../web/compile.dart';
import 'flutter_platform.dart' as loader;
import 'flutter_web_platform.dart';
import 'watcher.dart';
/// Runs tests using package:test and the Flutter engine.
......@@ -40,6 +44,7 @@ Future<int> runTests(
FlutterProject flutterProject,
String icudtlPath,
Directory coverageDirectory,
bool web,
}) async {
// Compute the command-line arguments for package:test.
final List<String> testArgs = <String>[];
......@@ -62,6 +67,32 @@ Future<int> runTests(
for (String plainName in plainNames) {
testArgs..add('--plain-name')..add(plainName);
}
if (web) {
final String tempBuildDir = fs.systemTempDirectory
.createTempSync('_flutter_test')
.absolute
.uri
.toFilePath();
await webCompilationProxy.initialize(
projectDirectory: flutterProject.directory,
testOutputDir: tempBuildDir,
targets: testFiles.map((String testFile) {
return fs.path.relative(testFile, from: flutterProject.directory.path);
}).toList(),
);
testArgs.add('--platform=chrome');
testArgs.add('--precompiled=$tempBuildDir');
testArgs.add('--');
testArgs.addAll(testFiles);
hack.registerPlatformPlugin(
<Runtime>[Runtime.chrome],
() {
return FlutterWebPlatform.start(flutterProject.directory.path);
}
);
await test.main(testArgs);
return exitCode;
}
testArgs.add('--');
testArgs.addAll(testFiles);
......
......@@ -90,7 +90,8 @@ class WebCompilationProxy {
/// `projectDirectory`.
Future<void> initialize({
@required Directory projectDirectory,
@required String target,
@required List<String> targets,
String testOutputDir,
}) async {
throw UnimplementedError();
}
......
......@@ -43,6 +43,7 @@ dependencies:
# this, make sure the tests are still running correctly.
test_api: 0.2.5
test_core: 0.2.5
test: 1.6.3
# Code generation dependencies
build_runner_core: 3.0.5
......@@ -103,7 +104,6 @@ dev_dependencies:
mockito: 4.0.0
file_testing: 2.1.0
vm_service_lib: 3.17.0
test: 1.6.3
build_runner: 1.4.0
build_vm_compilers: 1.0.0
build_test: 0.10.7+3
......
<!DOCTYPE html>
<html>
<head>
<title>test Browser Host</title>
</head>
<body>
<svg id="dart" version="1.1" x="0px" y="0px" width="400px" height="400px" viewBox="0 0 400 400">
<path id="right-flank" fill="#0083C9" d="M249.379,226.486l-6.676,15.572L166.174,166h58.82c0,0,2.807-0.409,3.645,1.966L249.379,226.486z"/>
<path id="right-ear" fill="#00D2B8" d="M201.84,141.906L166.174,166h58.82c0,0,2.168-0.25,2.645,0.566l-2.694-8.848l-15.024-14.68C207.555,140.329,203.578,140.744,201.84,141.906z"/>
<path id="left-flank" fill="#00D2B8" d="M242.616,241.856l-15.022,6.799l-60.493-21.429c-1.035-0.395-1.101-3.696-1.101-3.696v-57.932L242.616,241.856z"/>
<path id="left-paw" fill="#55DECA" d="M167.003,227.098l60.636,21.558l15.064-6.799L237.224,259h-43.856c0,0-14.077-13.929-18.141-17.993C171.162,236.943,169.162,233.989,167.003,227.098z"/>
<path id="right-paw" fill="#00A4E4" d="M227.676,166.365c0.963,1.401,1.361,2.473,1.361,2.473l20.352,57.648l-6.711,15.37L259,236.463v-44.854c0,0-13.678-13.965-17.741-17.882C237.193,169.811,231.466,166.319,227.676,166.365z"/>
<path id="left-ear" fill="#0083C9" d="M166.769,227.098c0,0-0.769-1.104-0.769-4.355v-57.144l-23.115,34.877c-1.626,1.774-1.567,6.538,1.595,9.755l13.636,13.892L166.769,227.098z"/>
</svg>
<div id="dark"></div>
<svg id="play" version="1.1" x="0px" y="0px" width="80px" height="80px" viewBox="0 0 25 25">
<defs><filter id="blur"><feGaussianBlur stdDeviation="0.3" id="feGaussianBlur5097" /></filter></defs>
<path d="M 3.777014,1.3715789 A 1.1838119,1.1838119 0 0 0 2.693923,2.5488509 V 22.444746 a 1.1838119,1.1838119 0 0 0 1.765908,1.035999 l 17.235259,-9.95972 a 1.1838119,1.1838119 0 0 0 0,-2.071998 L 4.459831,1.5128519 A 1.1838119,1.1838119 0 0 0 3.777014,1.3715789 z" style="opacity:0.5;stroke:#000000;stroke-width:1;filter:url(#blur)" />
<path style="fill:#ffffff;fill-opacity:1;stroke:#000000;stroke-width:1.32722104" d="M 3.4770491,1.0714664 A 1.1838119,1.1838119 0 0 0 2.3939589,2.2487382 V 22.144633 a 1.1838119,1.1838119 0 0 0 1.7659079,1.035999 l 17.2352602,-9.95972 a 1.1838119,1.1838119 0 0 0 0,-2.071998 L 4.1598668,1.2127389 A 1.1838119,1.1838119 0 0 0 3.4770491,1.0714664 z" />
</svg>
<script src="host.dart.js"></script>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment