Unverified Commit f4e79e68 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] add support for dart defines to flutter test (#70791)

parent e26c7f98
// Copyright 2014 The Flutter Authors. 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:flutter_test/flutter_test.dart';
void main() {
test('Dart defines can be provided', () {
expect(const String.fromEnvironment('flutter.test.foo'), 'bar');
});
}
......@@ -31,6 +31,7 @@ class TestCommand extends FlutterCommand {
addNullSafetyModeOptions(hide: !verboseHelp);
usesTrackWidgetCreation(verboseHelp: verboseHelp);
addEnableExperimentation(hide: !verboseHelp);
usesDartDefineOption();
argParser
..addMultiOption('name',
help: 'A regular expression matching substrings of the names of tests to run.',
......@@ -184,6 +185,18 @@ class TestCommand extends FlutterCommand {
final String excludeTags = stringArg('exclude-tags');
final BuildInfo buildInfo = await getBuildInfo(forcedBuildMode: BuildMode.debug);
if (buildInfo.packageConfig['test_api'] == null) {
globals.printError(
'\n'
'Error: cannot run without a dependency on either "package:flutter_test" or "package:test". '
'Ensure the following lines are present in your pubspec.yaml:'
'\n\n'
'dev_dependencies:\n'
' flutter_test:\n'
' sdk: flutter\n',
);
}
if (buildTestAssets && flutterProject.manifest.assets.isNotEmpty) {
await _buildTestAsset();
}
......
......@@ -90,9 +90,6 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner {
}) async {
// Configure package:test to use the Flutter engine for child processes.
final String shellPath = globals.artifacts.getArtifactPath(Artifact.flutterTester);
if (!globals.processManager.canRun(shellPath)) {
throwToolExit('Cannot execute Flutter tester at $shellPath');
}
// Compute the command-line arguments for package:test.
final List<String> testArgs = <String>[
......
......@@ -5,7 +5,6 @@
import 'dart:async';
import 'package:meta/meta.dart';
import 'package:package_config/package_config.dart';
import '../artifacts.dart';
import '../base/file_system.dart';
......@@ -114,8 +113,6 @@ class TestCompiler {
return residentCompiler;
}
PackageConfig _packageConfig;
// Handle a compilation request.
Future<void> _onCompilationRequest(_CompilationRequest request) async {
final bool isEmpty = compilationQueue.isEmpty;
......@@ -126,25 +123,6 @@ class TestCompiler {
if (!isEmpty) {
return;
}
if (_packageConfig == null) {
_packageConfig ??= buildInfo.packageConfig;
// Compilation will fail if there is no flutter_test dependency, since
// this library is imported by the generated entrypoint script.
if (_packageConfig['test_api'] == null) {
globals.printError(
'\n'
'Error: cannot run without a dependency on either "package:flutter_test" or "package:test". '
'Ensure the following lines are present in your pubspec.yaml:'
'\n\n'
'dev_dependencies:\n'
' flutter_test:\n'
' sdk: flutter\n',
);
request.result.complete(null);
await compilerController.close();
return;
}
}
while (compilationQueue.isNotEmpty) {
final _CompilationRequest request = compilationQueue.first;
globals.printTrace('Compiling ${request.mainUri}');
......@@ -158,7 +136,7 @@ class TestCompiler {
request.mainUri,
<Uri>[request.mainUri],
outputPath: outputDill.path,
packageConfig: _packageConfig,
packageConfig: buildInfo.packageConfig,
);
final String outputPath = compilerOutput?.outputFilename;
......
......@@ -114,29 +114,6 @@ void main() {
ProcessManager: () => FakeProcessManager.any(),
Logger: () => BufferLogger.test(),
});
testUsingContext('TestCompiler reports an error when there is no dependency on flutter_test or test', () async {
final FakeTestCompiler testCompiler = FakeTestCompiler(
BuildInfo.debug,
FlutterProject.current(),
residentCompiler,
);
expect(await testCompiler.compile(Uri.parse('test/foo.dart')), null);
expect(testLogger.errorText, contains('Error: cannot run without a dependency on '
'either "package:flutter_test" or "package:test'));
verifyNever(residentCompiler.recompile(
any,
<Uri>[Uri.parse('test/foo.dart')],
outputPath: testCompiler.outputDill.path,
packageConfig: anyNamed('packageConfig'),
));
}, overrides: <Type, Generator>{
FileSystem: () => fileSystem,
Platform: () => linuxPlatform,
ProcessManager: () => FakeProcessManager.any(),
Logger: () => BufferLogger.test(),
});
}
/// Override the creation of the Resident Compiler to simplify testing.
......
......@@ -82,6 +82,11 @@ void main() {
return _testFile('package_assets', automatedTestsDirectory, flutterTestDirectory, exitCode: isZero);
});
testWithoutContext('flutter test should support dart defines', () async {
return _testFile('dart_defines', automatedTestsDirectory, flutterTestDirectory, exitCode: isZero,
extraArguments: <String>['--dart-define=flutter.test.foo=bar']);
});
testWithoutContext('flutter test should run a test when its name matches a regexp', () async {
final ProcessResult result = await _runFlutterTest('filtering', automatedTestsDirectory, flutterTestDirectory,
extraArguments: const <String>['--name', 'inc.*de']);
......
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