Unverified Commit 22f17004 authored by Jia Hao's avatar Jia Hao Committed by GitHub

[flutter_tools] Make setting of CWD consistent for flutter test (#74622)

parent 2eeeba9c
// 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 'dart:io';
import 'package:test/test.dart' hide TypeMatcher, isInstanceOf;
void main() {
test('working directory is the root of this package', () {
expect(Directory.current.path, endsWith('automated_tests'));
});
}
......@@ -10,8 +10,6 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:vitool/vitool.dart';
import 'package:path/path.dart' as path;
const String kPackagePath = '..';
void main() {
test('parsePixels', () {
......@@ -694,5 +692,5 @@ class PathAnimationMatcher extends Matcher {
}
String testAsset(String name) {
return path.join(kPackagePath, 'test_assets', name);
return path.join('test_assets', name);
}
......@@ -13,7 +13,7 @@ import 'package:flutter_test/flutter_test.dart';
import '../test_utils.dart';
final String rootDirectoryPath = Directory.current.parent.path;
final String rootDirectoryPath = Directory.current.path;
void main() {
for (final String language in kCupertinoSupportedLanguages) {
......
......@@ -13,7 +13,7 @@ import 'package:flutter_test/flutter_test.dart';
import '../test_utils.dart';
final String rootDirectoryPath = Directory.current.parent.path;
final String rootDirectoryPath = Directory.current.path;
void main() {
for (final String language in kMaterialSupportedLanguages) {
......
......@@ -140,7 +140,6 @@ Future<void> run(List<String> args) async {
exitCode = await const FlutterTestRunner().runTests(
const TestWrapper(),
tests.keys.toList(),
workDir: testDirectory,
watcher: collector,
ipv6: false,
enableObservatory: collector != null,
......
......@@ -219,18 +219,17 @@ class TestCommand extends FlutterCommand {
);
}
Directory workDir;
if (files.isEmpty) {
// We don't scan the entire package, only the test/ subdirectory, so that
// files with names like like "hit_test.dart" don't get run.
workDir = globals.fs.directory('test');
if (!workDir.existsSync()) {
throwToolExit('Test directory "${workDir.path}" not found.');
final Directory testDir = globals.fs.directory('test');
if (!testDir.existsSync()) {
throwToolExit('Test directory "${testDir.path}" not found.');
}
files = _findTests(workDir).toList();
files = _findTests(testDir).toList();
if (files.isEmpty) {
throwToolExit(
'Test directory "${workDir.path}" does not appear to contain any test files.\n'
'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".'
);
}
......@@ -269,7 +268,6 @@ class TestCommand extends FlutterCommand {
final int result = await testRunner.runTests(
testWrapper,
files,
workDir: workDir,
names: names,
plainNames: plainNames,
tags: tags,
......
......@@ -27,7 +27,6 @@ abstract class FlutterTestRunner {
Future<int> runTests(
TestWrapper testWrapper,
List<String> testFiles, {
Directory workDir,
List<String> names = const <String>[],
List<String> plainNames = const <String>[],
String tags,
......@@ -64,7 +63,6 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner {
Future<int> runTests(
TestWrapper testWrapper,
List<String> testFiles, {
Directory workDir,
List<String> names = const <String>[],
List<String> plainNames = const <String>[],
String tags,
......@@ -206,14 +204,7 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner {
additionalArguments: additionalArguments,
);
// Call package:test's main method in the appropriate directory.
final Directory saved = globals.fs.currentDirectory;
try {
if (workDir != null) {
globals.printTrace('switching to directory $workDir to run tests');
globals.fs.currentDirectory = workDir;
}
globals.printTrace('running test package with arguments: $testArgs');
await testWrapper.main(testArgs);
......@@ -222,7 +213,6 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner {
return exitCode;
} finally {
globals.fs.currentDirectory = saved.path;
await platform.close();
}
}
......
......@@ -42,6 +42,10 @@ void main() {
return _testFile('trivial_widget', automatedTestsDirectory, flutterTestDirectory, exitCode: isZero);
});
testWithoutContext('flutter test set the working directory correctly', () async {
return _testFile('working_directory', automatedTestsDirectory, flutterTestDirectory, exitCode: isZero);
});
testWithoutContext('flutter test should report nice errors for exceptions thrown within testWidgets()', () async {
return _testFile('exception_handling', automatedTestsDirectory, flutterTestDirectory);
});
......
......@@ -10,7 +10,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:path/path.dart' as path;
final String bat = Platform.isWindows ? '.bat' : '';
final String _flutterBin = path.join(Directory.current.parent.parent.parent.path, 'bin', 'flutter$bat');
final String _flutterBin = path.join(Directory.current.parent.parent.path, 'bin', 'flutter$bat');
const String _integrationResultsPrefix =
'IntegrationTestWidgetsFlutterBinding test results:';
const String _failureExcerpt = r'Expected: <false>\n Actual: <true>';
......
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