diff --git a/dev/automated_tests/flutter_test/dart_defines_expectation.txt b/dev/automated_tests/flutter_test/dart_defines_expectation.txt new file mode 100644 index 0000000000..cda62d712c --- /dev/null +++ b/dev/automated_tests/flutter_test/dart_defines_expectation.txt @@ -0,0 +1 @@ +[0-9]+:[0-9]+ [+]1: All tests passed! diff --git a/dev/automated_tests/flutter_test/dart_defines_test.dart b/dev/automated_tests/flutter_test/dart_defines_test.dart new file mode 100644 index 0000000000..4c24016897 --- /dev/null +++ b/dev/automated_tests/flutter_test/dart_defines_test.dart @@ -0,0 +1,11 @@ +// 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'); + }); +} diff --git a/packages/flutter_tools/lib/src/commands/test.dart b/packages/flutter_tools/lib/src/commands/test.dart index f6ae0791dc..c32f6b633e 100644 --- a/packages/flutter_tools/lib/src/commands/test.dart +++ b/packages/flutter_tools/lib/src/commands/test.dart @@ -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(); } diff --git a/packages/flutter_tools/lib/src/test/runner.dart b/packages/flutter_tools/lib/src/test/runner.dart index 05ac3765d0..de6b98aa0d 100644 --- a/packages/flutter_tools/lib/src/test/runner.dart +++ b/packages/flutter_tools/lib/src/test/runner.dart @@ -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 testArgs = [ diff --git a/packages/flutter_tools/lib/src/test/test_compiler.dart b/packages/flutter_tools/lib/src/test/test_compiler.dart index a9d39f66d2..e3dce77f5d 100644 --- a/packages/flutter_tools/lib/src/test/test_compiler.dart +++ b/packages/flutter_tools/lib/src/test/test_compiler.dart @@ -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 _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, [request.mainUri], outputPath: outputDill.path, - packageConfig: _packageConfig, + packageConfig: buildInfo.packageConfig, ); final String outputPath = compilerOutput?.outputFilename; diff --git a/packages/flutter_tools/test/general.shard/test_compiler_test.dart b/packages/flutter_tools/test/general.shard/test_compiler_test.dart index ed38d3f295..61884b1117 100644 --- a/packages/flutter_tools/test/general.shard/test_compiler_test.dart +++ b/packages/flutter_tools/test/general.shard/test_compiler_test.dart @@ -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.parse('test/foo.dart')], - outputPath: testCompiler.outputDill.path, - packageConfig: anyNamed('packageConfig'), - )); - }, overrides: { - FileSystem: () => fileSystem, - Platform: () => linuxPlatform, - ProcessManager: () => FakeProcessManager.any(), - Logger: () => BufferLogger.test(), - }); } /// Override the creation of the Resident Compiler to simplify testing. diff --git a/packages/flutter_tools/test/integration.shard/test_test.dart b/packages/flutter_tools/test/integration.shard/test_test.dart index 15aab8af2a..4266542621 100644 --- a/packages/flutter_tools/test/integration.shard/test_test.dart +++ b/packages/flutter_tools/test/integration.shard/test_test.dart @@ -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: ['--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 ['--name', 'inc.*de']);