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

@@ -100,13 +100,13 @@ const String kDartDocPrefix = '///';
const String kDartDocPrefixWithSpace = '$kDartDocPrefix ';
Future<Null> main() async {
final Directory temp = Directory.systemTemp.createTempSync('analyze_sample_code_');
final Directory tempDir = Directory.systemTemp.createTempSync('flutter_analyze_sample_code.');
int exitCode = 1;
bool keepMain = false;
final List<String> buffer = <String>[];
try {
final File mainDart = new File(path.join(temp.path, 'main.dart'));
final File pubSpec = new File(path.join(temp.path, 'pubspec.yaml'));
final File mainDart = new File(path.join(tempDir.path, 'main.dart'));
final File pubSpec = new File(path.join(tempDir.path, 'pubspec.yaml'));
final Directory flutterPackage = new Directory(path.join(_flutterRoot, 'packages', 'flutter', 'lib'));
final List<Section> sections = <Section>[];
int sampleCodeSections = 0;
@@ -210,7 +210,7 @@ dependencies:
final Process process = await Process.start(
_flutter,
<String>['analyze', '--no-preamble', '--no-congratulate', mainDart.parent.path],
workingDirectory: temp.path,
workingDirectory: tempDir.path,
);
stderr.addStream(process.stderr);
final List<String> errors = await process.stdout.transform<String>(utf8.decoder).transform<String>(const LineSplitter()).toList();
@@ -285,7 +285,7 @@ dependencies:
print('No errors!');
} finally {
if (keepMain) {
print('Kept ${temp.path} because it had errors (see above).');
print('Kept ${tempDir.path} because it had errors (see above).');
print('-------8<-------');
int number = 1;
for (String line in buffer) {
@@ -295,10 +295,9 @@ dependencies:
print('-------8<-------');
} else {
try {
temp.deleteSync(recursive: true);
tempDir.deleteSync(recursive: true);
} on FileSystemException catch (e) {
// ignore errors deleting the temporary directory
print('Ignored exception during tearDown: $e');
print('Failed to delete ${tempDir.path}: $e');
}
}
}

View File

@@ -640,7 +640,7 @@ Future<Null> main(List<String> argList) async {
Directory tempDir;
bool removeTempDir = false;
if (args['temp_dir'] == null || args['temp_dir'].isEmpty) {
tempDir = Directory.systemTemp.createTempSync('flutter_');
tempDir = Directory.systemTemp.createTempSync('flutter_package.');
removeTempDir = true;
} else {
tempDir = new Directory(args['temp_dir']);

View File

@@ -191,7 +191,7 @@ Future<Null> _analyzeRepo() async {
await _checkForTrailingSpaces();
// Try analysis against a big version of the gallery; generate into a temporary directory.
final String outDir = Directory.systemTemp.createTempSync('mega_gallery').path;
final Directory outDir = Directory.systemTemp.createTempSync('flutter_mega_gallery.');
try {
await _runCommand(dart,
@@ -199,17 +199,13 @@ Future<Null> _analyzeRepo() async {
'--preview-dart-2',
path.join(flutterRoot, 'dev', 'tools', 'mega_gallery.dart'),
'--out',
outDir,
outDir.path,
],
workingDirectory: flutterRoot,
);
await _runFlutterAnalyze(outDir, options: <String>['--watch', '--benchmark']);
await _runFlutterAnalyze(outDir.path, options: <String>['--watch', '--benchmark']);
} finally {
try {
new Directory(outDir).deleteSync(recursive: true);
} catch (e) {
// ignore
}
outDir.deleteSync(recursive: true);
}
print('${bold}DONE: Analysis successful.$reset');

View File

@@ -2,6 +2,8 @@
// 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;
import 'package:test/test.dart' as test_package show TypeMatcher;
@@ -12,3 +14,14 @@ export 'package:test/test.dart' hide TypeMatcher, isInstanceOf;
/// A matcher that compares the type of the actual value to the type argument T.
Matcher isInstanceOf<T>() => new test_package.TypeMatcher<T>(); // ignore: prefer_const_constructors, https://github.com/dart-lang/sdk/issues/32544
void tryToDelete(Directory directory) {
// 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 {
directory.deleteSync(recursive: true);
} on FileSystemException catch (error) {
print('Failed to delete ${directory.path}: $error');
}
}

View File

@@ -67,7 +67,7 @@ void main() {
});
group('ArchiveCreator for $platformName', () {
ArchiveCreator creator;
Directory tmpDir;
Directory tempDir;
Directory flutterDir;
FakeProcessManager processManager;
final List<List<String>> args = <List<String>>[];
@@ -82,12 +82,12 @@ void main() {
processManager = new FakeProcessManager();
args.clear();
namedArgs.clear();
tmpDir = await Directory.systemTemp.createTemp('flutter_');
flutterDir = new Directory(path.join(tmpDir.path, 'flutter'));
tempDir = Directory.systemTemp.createTempSync('flutter_prepage_package_test.');
flutterDir = new Directory(path.join(tempDir.path, 'flutter'));
flutterDir.createSync(recursive: true);
creator = new ArchiveCreator(
tmpDir,
tmpDir,
tempDir,
tempDir,
testRef,
Branch.dev,
processManager: processManager,
@@ -99,16 +99,11 @@ void main() {
});
tearDown(() async {
// On Windows, the directory is locked and not able to be deleted yet. So
// we just leave some (very small, because we're not actually building
// archives here) trash around to be deleted at the next reboot.
if (!platform.isWindows) {
await tmpDir.delete(recursive: true);
}
tryToDelete(tempDir);
});
test('sets PUB_CACHE properly', () async {
final String createBase = path.join(tmpDir.absolute.path, 'create_');
final String createBase = path.join(tempDir.absolute.path, 'create_');
final Map<String, List<ProcessResult>> calls = <String, List<ProcessResult>>{
'git clone -b dev https://chromium.googlesource.com/external/github.com/flutter/flutter': null,
'git reset --hard $testRef': null,
@@ -116,7 +111,7 @@ void main() {
'git describe --tags --abbrev=0': <ProcessResult>[new ProcessResult(0, 0, 'v1.2.3', '')],
};
if (platform.isWindows) {
calls['7za x ${path.join(tmpDir.path, 'mingit.zip')}'] = null;
calls['7za x ${path.join(tempDir.path, 'mingit.zip')}'] = null;
}
calls.addAll(<String, List<ProcessResult>>{
'$flutter doctor': null,
@@ -128,7 +123,7 @@ void main() {
'$flutter create --template=plugin ${createBase}plugin': null,
'git clean -f -X **/.packages': null,
});
final String archiveName = path.join(tmpDir.absolute.path,
final String archiveName = path.join(tempDir.absolute.path,
'flutter_${platformName}_v1.2.3-dev${platform.isLinux ? '.tar.xz' : '.zip'}');
if (platform.isWindows) {
calls['7za a -tzip -mx=9 $archiveName flutter'] = null;
@@ -151,7 +146,7 @@ void main() {
});
test('calls the right commands for archive output', () async {
final String createBase = path.join(tmpDir.absolute.path, 'create_');
final String createBase = path.join(tempDir.absolute.path, 'create_');
final Map<String, List<ProcessResult>> calls = <String, List<ProcessResult>>{
'git clone -b dev https://chromium.googlesource.com/external/github.com/flutter/flutter': null,
'git reset --hard $testRef': null,
@@ -159,7 +154,7 @@ void main() {
'git describe --tags --abbrev=0': <ProcessResult>[new ProcessResult(0, 0, 'v1.2.3', '')],
};
if (platform.isWindows) {
calls['7za x ${path.join(tmpDir.path, 'mingit.zip')}'] = null;
calls['7za x ${path.join(tempDir.path, 'mingit.zip')}'] = null;
}
calls.addAll(<String, List<ProcessResult>>{
'$flutter doctor': null,
@@ -171,7 +166,7 @@ void main() {
'$flutter create --template=plugin ${createBase}plugin': null,
'git clean -f -X **/.packages': null,
});
final String archiveName = path.join(tmpDir.absolute.path,
final String archiveName = path.join(tempDir.absolute.path,
'flutter_${platformName}_v1.2.3-dev${platform.isLinux ? '.tar.xz' : '.zip'}');
if (platform.isWindows) {
calls['7za a -tzip -mx=9 $archiveName flutter'] = null;
@@ -182,8 +177,8 @@ void main() {
}
processManager.fakeResults = calls;
creator = new ArchiveCreator(
tmpDir,
tmpDir,
tempDir,
tempDir,
testRef,
Branch.dev,
processManager: processManager,
@@ -214,17 +209,11 @@ void main() {
setUp(() async {
processManager = new FakeProcessManager();
tempDir = await Directory.systemTemp.createTemp('flutter_');
tempDir.createSync();
tempDir = Directory.systemTemp.createTempSync('flutter_prepage_package_test.');
});
tearDown(() async {
// On Windows, the directory is locked and not able to be deleted yet. So
// we just leave some (very small, because we're not actually building
// archives here) trash around to be deleted at the next reboot.
if (!platform.isWindows) {
await tempDir.delete(recursive: true);
}
tryToDelete(tempDir);
});
test('calls the right processes', () async {