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

@@ -22,6 +22,17 @@ export 'package:test/test.dart' hide TypeMatcher, isInstanceOf; // Defines a 'pa
// TODO(ianh): Remove this once https://github.com/dart-lang/matcher/issues/98 is fixed
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');
}
}
/// Gets the path to the root of the Flutter repository.
///
/// This will first look for a `FLUTTER_ROOT` environment variable. If the

View File

@@ -48,13 +48,16 @@ void testUsingContext(String description, dynamic testMethod(), {
// leak a sticky $HOME/.flutter_settings behind!
Directory configDir;
tearDown(() {
configDir?.deleteSync(recursive: true);
configDir = null;
if (configDir != null) {
tryToDelete(configDir);
configDir = null;
}
});
Config buildConfig(FileSystem fs) {
configDir = fs.systemTempDirectory.createTempSync('config-dir');
configDir = fs.systemTempDirectory.createTempSync('flutter_config_dir_test.');
final File settingsFile = fs.file(
fs.path.join(configDir.path, '.flutter_settings'));
fs.path.join(configDir.path, '.flutter_settings')
);
return new Config(settingsFile);
}

View File

@@ -44,7 +44,7 @@ class MockAndroidSdk extends Mock implements AndroidSdk {
bool withNdkSysroot = false,
bool withSdkManager = true,
}) {
final Directory dir = fs.systemTempDirectory.createTempSync('android-sdk');
final Directory dir = fs.systemTempDirectory.createTempSync('flutter_mock_android_sdk.');
_createSdkFile(dir, 'platform-tools/adb');
@@ -66,18 +66,23 @@ class MockAndroidSdk extends Mock implements AndroidSdk {
if (withNdkDir != null) {
final String ndkCompiler = fs.path.join(
'ndk-bundle',
'toolchains',
'arm-linux-androideabi-4.9',
'prebuilt',
withNdkDir,
'bin',
'arm-linux-androideabi-gcc');
'ndk-bundle',
'toolchains',
'arm-linux-androideabi-4.9',
'prebuilt',
withNdkDir,
'bin',
'arm-linux-androideabi-gcc',
);
_createSdkFile(dir, ndkCompiler);
}
if (withNdkSysroot) {
final String armPlatform =
fs.path.join('ndk-bundle', 'platforms', 'android-9', 'arch-arm');
final String armPlatform = fs.path.join(
'ndk-bundle',
'platforms',
'android-9',
'arch-arm',
);
_createDir(dir, armPlatform);
}