Clean up usage of temporary directories (#20682)

All temporary directory start with `flutter_` and have their random component separated from the name by a period, as in `flutter_test_bundle.YFYQMY`.

I've tried to find some of the places where we didn't cleanly delete temporary directories, too. This greatly reduces, though it does not entirely eliminate, the directories we leave behind when running tests, especially `flutter_tools` tests.

While I was at it I standardized on `tempDir` as the variable name for temporary directories, since it was the most common, removing occurrences of `temp` and `tmp`, among others.

Also I factored out some common code that used to catch exceptions that happen on Windows, and made more places use that pattern.
This commit is contained in:
Ian Hickson
2018-08-17 13:17:23 -07:00
committed by GitHub
parent d581208557
commit 3dec6a6930
51 changed files with 339 additions and 346 deletions

View File

@@ -13,24 +13,21 @@ import '../src/common.dart';
import 'test_data/basic_project.dart';
import 'test_driver.dart';
BasicProject _project = new BasicProject();
FlutterTestDriver _flutter;
void main() {
group('expression evaluation', () {
Directory tempDir;
final BasicProject _project = new BasicProject();
FlutterTestDriver _flutter;
setUp(() async {
final Directory tempDir = await fs.systemTempDirectory.createTemp('test_app');
tempDir = fs.systemTempDirectory.createTempSync('flutter_expression_test.');
await _project.setUpIn(tempDir);
_flutter = new FlutterTestDriver(tempDir);
});
tearDown(() async {
try {
await _flutter.stop();
_project.cleanup();
} catch (e) {
// Don't fail tests if we failed to clean up temp folder.
}
await _flutter.stop();
tryToDelete(tempDir);
});
Future<VMIsolate> breakInBuildMethod(FlutterTestDriver flutter) async {

View File

@@ -11,26 +11,22 @@ import '../src/context.dart';
import 'test_data/basic_project.dart';
import 'test_driver.dart';
FlutterTestDriver _flutterRun, _flutterAttach;
BasicProject _project = new BasicProject();
void main() {
FlutterTestDriver _flutterRun, _flutterAttach;
final BasicProject _project = new BasicProject();
Directory tempDir;
setUp(() async {
final Directory tempDir = await fs.systemTempDirectory.createTemp('test_app');
tempDir = fs.systemTempDirectory.createTempSync('flutter_attach_test.');
await _project.setUpIn(tempDir);
_flutterRun = new FlutterTestDriver(tempDir);
_flutterAttach = new FlutterTestDriver(tempDir);
});
tearDown(() async {
try {
await _flutterRun.stop();
await _flutterAttach.stop();
_project.cleanup();
} catch (e) {
// Don't fail tests if we failed to clean up temp folder.
}
await _flutterRun.stop();
await _flutterAttach.stop();
tryToDelete(tempDir);
});
group('attached process', () {

View File

@@ -19,19 +19,14 @@ void main() {
Directory oldCurrentDir;
setUp(() async {
tempDir = await fs.systemTempDirectory.createTemp('flutter_tester_device');
tempDir = fs.systemTempDirectory.createTempSync('flutter_tester_device_test.');
oldCurrentDir = fs.currentDirectory;
fs.currentDirectory = tempDir;
});
tearDown(() {
fs.currentDirectory = oldCurrentDir;
try {
tempDir?.deleteSync(recursive: true);
tempDir = null;
} catch (e) {
// Ignored.
}
tryToDelete(tempDir);
});
group('FlutterTesterDevice', () {

View File

@@ -12,24 +12,21 @@ import '../src/common.dart';
import 'test_data/basic_project.dart';
import 'test_driver.dart';
BasicProject _project = new BasicProject();
FlutterTestDriver _flutter;
void main() {
group('hot reload', () {
Directory tempDir;
final BasicProject _project = new BasicProject();
FlutterTestDriver _flutter;
setUp(() async {
final Directory tempDir = await fs.systemTempDirectory.createTemp('test_app');
tempDir = fs.systemTempDirectory.createTempSync('flutter_hot_reload_test_app.');
await _project.setUpIn(tempDir);
_flutter = new FlutterTestDriver(tempDir);
});
tearDown(() async {
try {
await _flutter.stop();
_project.cleanup();
} catch (e) {
// Don't fail tests if we failed to clean up temp folder.
}
await _flutter.stop();
tryToDelete(tempDir);
});
test('works without error', () async {

View File

@@ -12,9 +12,6 @@ import '../src/common.dart';
import 'test_data/basic_project.dart';
import 'test_driver.dart';
BasicProject _project = new BasicProject();
FlutterTestDriver _flutter;
/// This duration is arbitrary but is ideally:
/// a) long enough to ensure that if the app is crashing at startup, we notice
/// b) as short as possible, to avoid inflating build times
@@ -22,15 +19,19 @@ const Duration requiredLifespan = Duration(seconds: 5);
void main() {
group('flutter run', () {
final BasicProject _project = new BasicProject();
FlutterTestDriver _flutter;
Directory tempDir;
setUp(() async {
final Directory tempDir = await fs.systemTempDirectory.createTemp('test_app');
tempDir = fs.systemTempDirectory.createTempSync('flutter_lifetime_test.');
await _project.setUpIn(tempDir);
_flutter = new FlutterTestDriver(tempDir);
});
tearDown(() async {
await _flutter.stop();
_project.cleanup();
tryToDelete(tempDir);
});
test('does not terminate when a debugger is attached', () async {

View File

@@ -26,10 +26,6 @@ abstract class TestProject {
await getPackages(dir.path);
}
void cleanup() {
dir?.deleteSync(recursive: true);
}
int lineContaining(String contents, String search) {
final int index = contents.split('\n').indexWhere((String l) => l.contains(search));
if (index == -1)

View File

@@ -23,6 +23,8 @@ const Duration appStartTimeout = Duration(seconds: 60);
const Duration quitTimeout = Duration(seconds: 5);
class FlutterTestDriver {
FlutterTestDriver(this._projectFolder);
final Directory _projectFolder;
Process _proc;
int _procPid;
@@ -36,8 +38,6 @@ class FlutterTestDriver {
int _vmServicePort;
bool _hasExited = false;
FlutterTestDriver(this._projectFolder);
VMServiceClient vmService;
String get lastErrorInfo => _errorBuffer.toString();
int get vmServicePort => _vmServicePort;
@@ -318,24 +318,24 @@ class FlutterTestDriver {
int id = 1;
Future<dynamic> _sendRequest(String method, dynamic params) async {
final int requestId = id++;
final Map<String, dynamic> req = <String, dynamic>{
final Map<String, dynamic> request = <String, dynamic>{
'id': requestId,
'method': method,
'params': params
};
final String jsonEncoded = json.encode(<Map<String, dynamic>>[req]);
final String jsonEncoded = json.encode(<Map<String, dynamic>>[request]);
_debugPrint(jsonEncoded);
// Set up the response future before we send the request to avoid any
// races.
final Future<Map<String, dynamic>> responseFuture = _waitFor(id: requestId);
_proc.stdin.writeln(jsonEncoded);
final Map<String, dynamic> resp = await responseFuture;
final Map<String, dynamic> response = await responseFuture;
if (resp['error'] != null || resp['result'] == null)
if (response['error'] != null || response['result'] == null)
_throwErrorResponse('Unexpected error response');
return resp['result'];
return response['result'];
}
void _throwErrorResponse(String msg) {