[flutter_tools] Add support for URI formats like ?line=x for "flutter test" (#119740)

* [flutter_tools] Add support for URI formats like ?line=x for "flutter test"

* Remove unnecessary function

* Handle parsing absolute paths on Windows

* Use Windows-style paths when running on Windows

* Fix paths in isFile

* Remove unnecessary clear
This commit is contained in:
Danny Tuppeny
2023-02-27 19:13:00 +00:00
committed by GitHub
parent 9a4e897965
commit 06952ba254
6 changed files with 91 additions and 33 deletions

View File

@@ -236,7 +236,7 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
bool get isIntegrationTest => _isIntegrationTest;
bool _isIntegrationTest = false;
List<String> _testFiles = <String>[];
final Set<Uri> _testFileUris = <Uri>{};
@override
Future<Set<DevelopmentArtifact>> get requiredArtifacts async {
@@ -261,37 +261,43 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
@override
Future<FlutterCommandResult> verifyThenRunCommand(String? commandPath) {
_testFiles = argResults!.rest.map<String>(globals.fs.path.absolute).toList();
if (_testFiles.isEmpty) {
final List<Uri> testUris = argResults!.rest.map(_parseTestArgument).toList();
if (testUris.isEmpty) {
// We don't scan the entire package, only the test/ subdirectory, so that
// files with names like "hit_test.dart" don't get run.
final Directory testDir = globals.fs.directory('test');
if (!testDir.existsSync()) {
throwToolExit('Test directory "${testDir.path}" not found.');
}
_testFiles = _findTests(testDir).toList();
if (_testFiles.isEmpty) {
_testFileUris.addAll(_findTests(testDir).map(Uri.file));
if (_testFileUris.isEmpty) {
throwToolExit(
'Test directory "${testDir.path}" does not appear to contain any test files.\n'
'Test files must be in that directory and end with the pattern "_test.dart".'
);
}
} else {
_testFiles = <String>[
for (String path in _testFiles)
if (globals.fs.isDirectorySync(path))
..._findTests(globals.fs.directory(path))
else
globals.fs.path.normalize(globals.fs.path.absolute(path)),
];
for (final Uri uri in testUris) {
// Test files may have query strings to support name/line/col:
// flutter test test/foo.dart?name=a&line=1
String testPath = uri.replace(query: '').toFilePath();
testPath = globals.fs.path.absolute(testPath);
testPath = globals.fs.path.normalize(testPath);
if (globals.fs.isDirectorySync(testPath)) {
_testFileUris.addAll(_findTests(globals.fs.directory(testPath)).map(Uri.file));
} else {
_testFileUris.add(Uri.file(testPath).replace(query: uri.query));
}
}
}
// This needs to be set before [super.verifyThenRunCommand] so that the
// correct [requiredArtifacts] can be identified before [run] takes place.
_isIntegrationTest = _shouldRunAsIntegrationTests(globals.fs.currentDirectory.absolute.path, _testFiles);
final List<String> testFilePaths = _testFileUris.map((Uri uri) => uri.replace(query: '').toFilePath()).toList();
_isIntegrationTest = _shouldRunAsIntegrationTests(globals.fs.currentDirectory.absolute.path, testFilePaths);
globals.printTrace(
'Found ${_testFiles.length} files which will be executed as '
'Found ${_testFileUris.length} files which will be executed as '
'${_isIntegrationTest ? 'Integration' : 'Widget'} Tests.',
);
return super.verifyThenRunCommand(commandPath);
@@ -338,7 +344,7 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
}
final bool startPaused = boolArgDeprecated('start-paused');
if (startPaused && _testFiles.length != 1) {
if (startPaused && _testFileUris.length != 1) {
throwToolExit(
'When using --start-paused, you must specify a single test file to run.',
exitCode: 1,
@@ -451,7 +457,7 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
final Stopwatch? testRunnerTimeRecorderStopwatch = testTimeRecorder?.start(TestTimePhases.TestRunner);
final int result = await testRunner.runTests(
testWrapper,
_testFiles,
_testFileUris.toList(),
debuggingOptions: debuggingOptions,
names: names,
plainNames: plainNames,
@@ -500,6 +506,22 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
return FlutterCommandResult.success();
}
/// Parses a test file/directory target passed as an argument and returns it
/// as an absolute file:/// [URI] with optional querystring for name/line/col.
Uri _parseTestArgument(String arg) {
// We can't parse Windows paths as URIs if they have query strings, so
// parse the file and query parts separately.
final int queryStart = arg.indexOf('?');
String filePart = queryStart == -1 ? arg : arg.substring(0, queryStart);
final String queryPart = queryStart == -1 ? '' : arg.substring(queryStart + 1);
filePart = globals.fs.path.absolute(filePart);
filePart = globals.fs.path.normalize(filePart);
return Uri.file(filePart)
.replace(query: queryPart.isEmpty ? null : queryPart);
}
Future<void> _buildTestAsset() async {
final AssetBundle assetBundle = AssetBundleFactory.instance.createBundle();
final int build = await assetBundle.build(packagesPath: '.packages');

View File

@@ -24,7 +24,7 @@ abstract class FlutterTestRunner {
/// Runs tests using package:test and the Flutter engine.
Future<int> runTests(
TestWrapper testWrapper,
List<String> testFiles, {
List<Uri> testFiles, {
required DebuggingOptions debuggingOptions,
List<String> names = const <String>[],
List<String> plainNames = const <String>[],
@@ -62,7 +62,7 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner {
@override
Future<int> runTests(
TestWrapper testWrapper,
List<String> testFiles, {
List<Uri> testFiles, {
required DebuggingOptions debuggingOptions,
List<String> names = const <String>[],
List<String> plainNames = const <String>[],
@@ -128,6 +128,7 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner {
'--shard-index=$shardIndex',
'--chain-stack-traces',
];
if (web) {
final String tempBuildDir = globals.fs.systemTempDirectory
.createTempSync('flutter_test.')
@@ -144,13 +145,13 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner {
).initialize(
projectDirectory: flutterProject!.directory,
testOutputDir: tempBuildDir,
testFiles: testFiles,
testFiles: testFiles.map((Uri uri) => uri.toFilePath()).toList(),
buildInfo: debuggingOptions.buildInfo,
);
testArgs
..add('--platform=chrome')
..add('--')
..addAll(testFiles);
..addAll(testFiles.map((Uri uri) => uri.toString()));
testWrapper.registerPlatformPlugin(
<Runtime>[Runtime.chrome],
() {
@@ -186,7 +187,7 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner {
testArgs
..add('--')
..addAll(testFiles);
..addAll(testFiles.map((Uri uri) => uri.toString()));
final InternetAddressType serverType =
ipv6 ? InternetAddressType.IPv6 : InternetAddressType.IPv4;