flutter/packages/flutter_tools/test/asset_bundle_variant_test.dart
Ian Hickson 3dec6a6930
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.
2018-08-17 13:17:23 -07:00

90 lines
2.6 KiB
Dart

// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:convert';
import 'package:file/file.dart';
import 'package:flutter_tools/src/asset.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/cache.dart';
import 'src/common.dart';
import 'src/context.dart';
void main() {
// These tests do not use a memory file system because we want to ensure that
// asset bundles work correctly on Windows and Posix systems.
Directory tempDir;
Directory oldCurrentDir;
setUp(() async {
tempDir = fs.systemTempDirectory.createTempSync('flutter_asset_bundle_variant_test.');
oldCurrentDir = fs.currentDirectory;
fs.currentDirectory = tempDir;
});
tearDown(() {
fs.currentDirectory = oldCurrentDir;
tryToDelete(tempDir);
});
group('AssetBundle asset variants', () {
testUsingContext('main asset and variants', () async {
// Setting flutterRoot here so that it picks up the MemoryFileSystem's
// path separator.
Cache.flutterRoot = getFlutterRoot();
fs.file('pubspec.yaml')
..createSync()
..writeAsStringSync(
'''
name: test
dependencies:
flutter:
sdk: flutter
flutter:
assets:
- a/b/c/foo
'''
);
fs.file('.packages')..createSync();
final List<String> assets = <String>[
'a/b/c/foo',
'a/b/c/var1/foo',
'a/b/c/var2/foo',
'a/b/c/var3/foo',
];
for (String asset in assets) {
fs.file(asset)
..createSync(recursive: true)
..writeAsStringSync(asset);
}
AssetBundle bundle = AssetBundleFactory.instance.createBundle();
await bundle.build(manifestPath: 'pubspec.yaml');
// The main asset file, /a/b/c/foo, and its variants exist.
for (String asset in assets) {
expect(bundle.entries.containsKey(asset), true);
expect(utf8.decode(await bundle.entries[asset].contentsAsBytes()), asset);
}
fs.file('a/b/c/foo').deleteSync();
bundle = AssetBundleFactory.instance.createBundle();
await bundle.build(manifestPath: 'pubspec.yaml');
// Now the main asset file, /a/b/c/foo, does not exist. This is OK because
// the /a/b/c/*/foo variants do exist.
expect(bundle.entries.containsKey('a/b/c/foo'), false);
for (String asset in assets.skip(1)) {
expect(bundle.entries.containsKey(asset), true);
expect(utf8.decode(await bundle.entries[asset].contentsAsBytes()), asset);
}
});
});
}