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

@@ -14,25 +14,25 @@ String errorMessage;
/// Runs the given [testFunction] on a freshly generated Flutter project.
Future<void> runProjectTest(Future<void> testFunction(FlutterProject project)) async {
final Directory tmp = await Directory.systemTemp.createTemp('gradle');
final FlutterProject project = await FlutterProject.create(tmp, 'hello');
final Directory tempDir = Directory.systemTemp.createTempSync('flutter_devicelab_gradle_plugin_test.');
final FlutterProject project = await FlutterProject.create(tempDir, 'hello');
try {
await testFunction(project);
} finally {
project.parent.deleteSync(recursive: true);
rmTree(tempDir);
}
}
/// Runs the given [testFunction] on a freshly generated Flutter plugin project.
Future<void> runPluginProjectTest(Future<void> testFunction(FlutterPluginProject pluginProject)) async {
final Directory tmp = await Directory.systemTemp.createTemp('gradle');
final FlutterPluginProject pluginProject = await FlutterPluginProject.create(tmp, 'aaa');
final Directory tempDir = Directory.systemTemp.createTempSync('flutter_devicelab_gradle_plugin_test.');
final FlutterPluginProject pluginProject = await FlutterPluginProject.create(tempDir, 'aaa');
try {
await testFunction(pluginProject);
} finally {
pluginProject.parent.deleteSync(recursive: true);
rmTree(tempDir);
}
}

View File

@@ -4,6 +4,7 @@
import 'dart:async';
import 'dart:io';
import 'package:flutter_devicelab/framework/framework.dart';
import 'package:flutter_devicelab/framework/utils.dart';
import 'package:path/path.dart' as path;
@@ -22,9 +23,9 @@ Future<Null> main() async {
section('Create Flutter module project');
final Directory directory = await Directory.systemTemp.createTemp('module');
final Directory tempDir = Directory.systemTemp.createTempSync('flutter_module_test.');
try {
await inDirectory(directory, () async {
await inDirectory(tempDir, () async {
await flutter(
'create',
options: <String>['--org', 'io.flutter.devicelab', '-t', 'module', 'hello'],
@@ -33,14 +34,14 @@ Future<Null> main() async {
section('Add plugins');
final File pubspec = new File(path.join(directory.path, 'hello', 'pubspec.yaml'));
final File pubspec = new File(path.join(tempDir.path, 'hello', 'pubspec.yaml'));
String content = await pubspec.readAsString();
content = content.replaceFirst(
'\ndependencies:\n',
'\ndependencies:\n battery:\n package_info:\n',
);
await pubspec.writeAsString(content, flush: true);
await inDirectory(new Directory(path.join(directory.path, 'hello')), () async {
await inDirectory(new Directory(path.join(tempDir.path, 'hello')), () async {
await flutter(
'packages',
options: <String>['get'],
@@ -49,7 +50,7 @@ Future<Null> main() async {
section('Build Flutter module library archive');
await inDirectory(new Directory(path.join(directory.path, 'hello', '.android')), () async {
await inDirectory(new Directory(path.join(tempDir.path, 'hello', '.android')), () async {
await exec(
'./gradlew',
<String>['flutter:assembleDebug'],
@@ -58,7 +59,7 @@ Future<Null> main() async {
});
final bool aarBuilt = exists(new File(path.join(
directory.path,
tempDir.path,
'hello',
'.android',
'Flutter',
@@ -74,7 +75,7 @@ Future<Null> main() async {
section('Build ephemeral host app');
await inDirectory(new Directory(path.join(directory.path, 'hello')), () async {
await inDirectory(new Directory(path.join(tempDir.path, 'hello')), () async {
await flutter(
'build',
options: <String>['apk'],
@@ -82,7 +83,7 @@ Future<Null> main() async {
});
final bool ephemeralHostApkBuilt = exists(new File(path.join(
directory.path,
tempDir.path,
'hello',
'build',
'host',
@@ -98,13 +99,13 @@ Future<Null> main() async {
section('Clean build');
await inDirectory(new Directory(path.join(directory.path, 'hello')), () async {
await inDirectory(new Directory(path.join(tempDir.path, 'hello')), () async {
await flutter('clean');
});
section('Materialize host app');
await inDirectory(new Directory(path.join(directory.path, 'hello')), () async {
await inDirectory(new Directory(path.join(tempDir.path, 'hello')), () async {
await flutter(
'materialize',
options: <String>['android'],
@@ -113,7 +114,7 @@ Future<Null> main() async {
section('Build materialized host app');
await inDirectory(new Directory(path.join(directory.path, 'hello')), () async {
await inDirectory(new Directory(path.join(tempDir.path, 'hello')), () async {
await flutter(
'build',
options: <String>['apk'],
@@ -121,7 +122,7 @@ Future<Null> main() async {
});
final bool materializedHostApkBuilt = exists(new File(path.join(
directory.path,
tempDir.path,
'hello',
'build',
'host',
@@ -137,18 +138,18 @@ Future<Null> main() async {
section('Add to Android app');
final Directory hostApp = new Directory(path.join(directory.path, 'hello_host_app'));
final Directory hostApp = new Directory(path.join(tempDir.path, 'hello_host_app'));
mkdir(hostApp);
recursiveCopy(
new Directory(path.join(flutterDirectory.path, 'dev', 'integration_tests', 'android_host_app')),
hostApp,
);
copy(
new File(path.join(directory.path, 'hello', '.android', 'gradlew')),
new File(path.join(tempDir.path, 'hello', '.android', 'gradlew')),
hostApp,
);
copy(
new File(path.join(directory.path, 'hello', '.android', 'gradle', 'wrapper', 'gradle-wrapper.jar')),
new File(path.join(tempDir.path, 'hello', '.android', 'gradle', 'wrapper', 'gradle-wrapper.jar')),
new Directory(path.join(hostApp.path, 'gradle', 'wrapper')),
);
@@ -177,7 +178,7 @@ Future<Null> main() async {
} catch (e) {
return new TaskResult.failure(e.toString());
} finally {
rmTree(directory);
rmTree(tempDir);
}
});
}

View File

@@ -77,15 +77,23 @@ void fail(String message) {
throw new BuildFailedError(message);
}
void rm(FileSystemEntity entity) {
if (entity.existsSync())
entity.deleteSync();
// Remove the given file or directory.
void rm(FileSystemEntity entity, { bool recursive = false}) {
if (entity.existsSync()) {
// This should not be necessary, but it turns out that
// on Windows it's common for deletions to fail due to
// bogus (we think) "access denied" errors.
try {
entity.deleteSync(recursive: recursive);
} on FileSystemException catch (error) {
print('Failed to delete ${entity.path}: $error');
}
}
}
/// Remove recursively.
void rmTree(FileSystemEntity entity) {
if (entity.existsSync())
entity.deleteSync(recursive: true);
rm(entity, recursive: true);
}
List<FileSystemEntity> ls(Directory directory) => directory.listSync();
@@ -414,7 +422,7 @@ Future<Null> getFlutter(String revision) async {
section('Get Flutter!');
if (exists(flutterDirectory)) {
rmTree(flutterDirectory);
flutterDirectory.deleteSync(recursive: true);
}
await inDirectory(flutterDirectory.parent, () async {

View File

@@ -73,8 +73,7 @@ TaskFunction createBasicMaterialCompileTest() {
const String sampleAppName = 'sample_flutter_app';
final Directory sampleDir = dir('${Directory.systemTemp.path}/$sampleAppName');
if (await sampleDir.exists())
rmTree(sampleDir);
rmTree(sampleDir);
await inDirectory(Directory.systemTemp, () async {
await flutter('create', options: <String>[sampleAppName]);

View File

@@ -33,23 +33,24 @@ class PluginTest {
Future<TaskResult> call() async {
section('Create Flutter project');
final Directory tmp = await Directory.systemTemp.createTemp('plugin');
final FlutterProject project = await FlutterProject.create(tmp, options);
if (buildTarget == 'ios') {
await prepareProvisioningCertificates(project.rootPath);
}
final Directory tempDir = Directory.systemTemp.createTempSync('flutter_devicelab_plugin_test.');
try {
section('Add plugin');
await project.addPlugin('path_provider');
section('Build');
await project.build(buildTarget);
final FlutterProject project = await FlutterProject.create(tempDir, options);
try {
if (buildTarget == 'ios')
await prepareProvisioningCertificates(project.rootPath);
section('Add plugin');
await project.addPlugin('path_provider');
section('Build');
await project.build(buildTarget);
} finally {
await project.delete();
}
return new TaskResult.success(null);
} catch (e) {
return new TaskResult.failure(e.toString());
} finally {
await project.delete();
rmTree(tempDir);
}
}
}
@@ -97,9 +98,9 @@ class FlutterProject {
<String>['--stop'],
canFail: true,
);
// TODO(mravn): Investigating if flakiness is timing dependent.
// TODO(ianh): Investigating if flakiness is timing dependent.
await new Future<Null>.delayed(const Duration(seconds: 10));
}
await parent.delete(recursive: true);
rmTree(parent);
}
}