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

@@ -175,8 +175,7 @@ abstract class IOSApp extends ApplicationPackage {
bundleDir = fs.directory(applicationBinary);
} else {
// Try to unpack as an ipa.
final Directory tempDir = fs.systemTempDirectory.createTempSync(
'flutter_app_');
final Directory tempDir = fs.systemTempDirectory.createTempSync('flutter_app.');
addShutdownHook(() async {
await tempDir.delete(recursive: true);
}, ShutdownStage.STILL_RECORDING);

View File

@@ -215,13 +215,13 @@ class UpdatePackagesCommand extends FlutterCommand {
// pub tool will attempt to bring these dependencies up to the most recent
// possible versions while honoring all their constraints.
final PubDependencyTree tree = new PubDependencyTree(); // object to collect results
final Directory temporaryDirectory = fs.systemTempDirectory.createTempSync('flutter_update_packages_');
final Directory tempDir = fs.systemTempDirectory.createTempSync('flutter_update_packages.');
try {
final File fakePackage = _pubspecFor(temporaryDirectory);
final File fakePackage = _pubspecFor(tempDir);
fakePackage.createSync();
fakePackage.writeAsStringSync(_generateFakePubspec(dependencies.values));
// First we run "pub upgrade" on this generated package:
await pubGet(context: PubContext.updatePackages, directory: temporaryDirectory.path, upgrade: true, checkLastModified: false);
await pubGet(context: PubContext.updatePackages, directory: tempDir.path, upgrade: true, checkLastModified: false);
// Then we run "pub deps --style=compact" on the result. We pipe all the
// output to tree.fill(), which parses it so that it can create a graph
// of all the dependencies so that we can figure out the transitive
@@ -230,12 +230,12 @@ class UpdatePackagesCommand extends FlutterCommand {
await pub(
<String>['deps', '--style=compact'],
context: PubContext.updatePackages,
directory: temporaryDirectory.path,
directory: tempDir.path,
filter: tree.fill,
retry: false, // errors here are usually fatal since we're not hitting the network
);
} finally {
temporaryDirectory.deleteSync(recursive: true);
tempDir.deleteSync(recursive: true);
}
// The transitive dependency tree for the fake package does not contain

View File

@@ -318,13 +318,11 @@ Future<XcodeBuildResult> buildXcodeProject({
Status buildSubStatus;
Status initialBuildStatus;
Directory scriptOutputPipeTempDirectory;
Directory tempDir;
if (logger.supportsColor) {
scriptOutputPipeTempDirectory = fs.systemTempDirectory
.createTempSync('flutter_build_log_pipe');
final File scriptOutputPipeFile =
scriptOutputPipeTempDirectory.childFile('pipe_to_stdout');
tempDir = fs.systemTempDirectory.createTempSync('flutter_build_log_pipe.');
final File scriptOutputPipeFile = tempDir.childFile('pipe_to_stdout');
os.makePipe(scriptOutputPipeFile.path);
Future<void> listenToScriptOutputLine() async {
@@ -362,7 +360,7 @@ Future<XcodeBuildResult> buildXcodeProject({
initialBuildStatus?.cancel();
buildStopwatch.stop();
// Free pipe file.
scriptOutputPipeTempDirectory?.deleteSync(recursive: true);
tempDir?.deleteSync(recursive: true);
printStatus(
'Xcode build done.',
ansiAlternative: 'Xcode build done.'.padRight(kDefaultStatusPadding + 1)

View File

@@ -233,13 +233,13 @@ class FlutterCommandRunner extends CommandRunner<Null> {
if (topLevelResults['bug-report']) {
// --bug-report implies --record-to=<tmp_path>
final Directory tmp = await const LocalFileSystem()
final Directory tempDir = const LocalFileSystem()
.systemTempDirectory
.createTemp('flutter_tools_');
recordTo = tmp.path;
.createTempSync('flutter_tools_bug_report.');
recordTo = tempDir.path;
// Record the arguments that were used to invoke this runner.
final File manifest = tmp.childFile('MANIFEST.txt');
final File manifest = tempDir.childFile('MANIFEST.txt');
final StringBuffer buffer = new StringBuffer()
..writeln('# arguments')
..writeln(topLevelResults.arguments)
@@ -251,13 +251,14 @@ class FlutterCommandRunner extends CommandRunner<Null> {
// ZIP the recording up once the recording has been serialized.
addShutdownHook(() async {
final File zipFile = getUniqueFile(fs.currentDirectory, 'bugreport', 'zip');
os.zip(tmp, zipFile);
os.zip(tempDir, zipFile);
printStatus(
'Bug report written to ${zipFile.basename}.\n'
'Note that this bug report contains local paths, device '
'identifiers, and log snippets.');
'Bug report written to ${zipFile.basename}.\n'
'Note that this bug report contains local paths, device '
'identifiers, and log snippets.'
);
}, ShutdownStage.POST_PROCESS_RECORDING);
addShutdownHook(() => tmp.delete(recursive: true), ShutdownStage.CLEANUP);
addShutdownHook(() => tempDir.delete(recursive: true), ShutdownStage.CLEANUP);
}
assert(recordTo == null || replayFrom == null);

View File

@@ -136,7 +136,7 @@ class CoverageCollector extends TestWatcher {
return false;
}
final Directory tempDir = fs.systemTempDirectory.createTempSync('flutter_tools');
final Directory tempDir = fs.systemTempDirectory.createTempSync('flutter_tools_test_coverage.');
try {
final File sourceFile = coverageFile.copySync(fs.path.join(tempDir.path, 'lcov.source.info'));
final ProcessResult result = processManager.runSync(<String>[

View File

@@ -203,7 +203,7 @@ class _Compiler {
// Incremental compilation requests done for each test copy that file away
// for independent execution.
final Directory outputDillDirectory = fs.systemTempDirectory
.createTempSync('output_dill');
.createTempSync('flutter_test_compiler.');
final File outputDill = outputDillDirectory.childFile('output.dill');
bool suppressOutput = false;
@@ -656,15 +656,15 @@ class _FlutterPlatform extends PlatformPlugin {
String _createListenerDart(List<_Finalizer> finalizers, int ourTestCount,
String testPath, HttpServer server) {
// Prepare a temporary directory to store the Dart file that will talk to us.
final Directory temporaryDirectory = fs.systemTempDirectory
.createTempSync('dart_test_listener');
final Directory tempDir = fs.systemTempDirectory
.createTempSync('flutter_test_listener.');
finalizers.add(() async {
printTrace('test $ourTestCount: deleting temporary directory');
temporaryDirectory.deleteSync(recursive: true);
tempDir.deleteSync(recursive: true);
});
// Prepare the Dart file that will talk to us and start the test.
final File listenerFile = fs.file('${temporaryDirectory.path}/listener.dart');
final File listenerFile = fs.file('${tempDir.path}/listener.dart');
listenerFile.createSync();
listenerFile.writeAsStringSync(_generateTestMain(
testUrl: fs.path.toUri(fs.path.absolute(testPath)),
@@ -683,7 +683,7 @@ class _FlutterPlatform extends PlatformPlugin {
// bundlePath needs to point to a folder with `platform.dill` file.
final Directory tempBundleDirectory = fs.systemTempDirectory
.createTempSync('flutter_bundle_directory');
.createTempSync('flutter_test_bundle.');
finalizers.add(() async {
printTrace(
'test $ourTestCount: deleting temporary bundle directory');
@@ -757,7 +757,7 @@ class _FlutterPlatform extends PlatformPlugin {
sb.writeln(' <cachedir>/var/cache/fontconfig</cachedir>');
sb.writeln('</fontconfig>');
final Directory fontsDir = fs.systemTempDirectory.createTempSync('flutter_fonts');
final Directory fontsDir = fs.systemTempDirectory.createTempSync('flutter_test_fonts.');
_cachedFontConfig = fs.file('${fontsDir.path}/fonts.conf');
_cachedFontConfig.createSync();
_cachedFontConfig.writeAsStringSync(sb.toString());