Unverified Commit a74f591d authored by Danny Tuppeny's avatar Danny Tuppeny Committed by GitHub

Change asset_bundle_package_font_test to memory file system (#21114)

* Change assert_bundle_package_font_test to memory file system

This is to work towards being able to run the tests without `-j1` (#21113). These tests were using the real filesystem and setting/relying on fs.currentDirectory. There was a comment about this being because the memory provider didnt' support POSIX and Windows, however that seems to have changed since (and many other asset tests already do something similar to this).

* Trim trailing whitespace

* Add a workaround for Windows path slash directions

Strictly this is correct, but the real FS can tolerate either path. The in-memory file system is more strict (see https://github.com/google/file.dart/issues/112).

* Extract a helper for writing schema files in tests

* Missed file when saving!

* Remove redundant comment

* Rename writeBasicSchema -> writeEmptySchema

* Use the file we already have to write contents

* Make comments more descriptive

* Remove another dupe of writeSchema to use the shared one

* Rename schema -> pubspec_schema

* Trim whitespace
parent 041ff621
...@@ -10,7 +10,6 @@ import 'package:flutter_tools/src/artifacts.dart'; ...@@ -10,7 +10,6 @@ import 'package:flutter_tools/src/artifacts.dart';
import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/build_info.dart'; import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/cache.dart'; import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/flutter_manifest.dart';
import 'package:flutter_tools/src/ios/xcodeproj.dart'; import 'package:flutter_tools/src/ios/xcodeproj.dart';
import 'package:flutter_tools/src/project.dart'; import 'package:flutter_tools/src/project.dart';
import 'package:mockito/mockito.dart'; import 'package:mockito/mockito.dart';
...@@ -19,6 +18,7 @@ import 'package:process/process.dart'; ...@@ -19,6 +18,7 @@ import 'package:process/process.dart';
import '../src/common.dart'; import '../src/common.dart';
import '../src/context.dart'; import '../src/context.dart';
import '../src/pubspec_schema.dart';
void main() { void main() {
Cache.flutterRoot = getFlutterRoot(); Cache.flutterRoot = getFlutterRoot();
...@@ -186,8 +186,7 @@ someOtherProperty: someOtherValue ...@@ -186,8 +186,7 @@ someOtherProperty: someOtherValue
manifestFile.writeAsStringSync(manifest); manifestFile.writeAsStringSync(manifest);
// write schemaData otherwise pubspec.yaml file can't be loaded // write schemaData otherwise pubspec.yaml file can't be loaded
const String schemaData = '{}'; writeEmptySchemaFile(fs);
writeSchemaFile(fs, schemaData);
try { try {
updateLocalProperties( updateLocalProperties(
...@@ -331,16 +330,6 @@ flutter: ...@@ -331,16 +330,6 @@ flutter:
}); });
} }
void writeSchemaFile(FileSystem filesystem, String schemaData) {
final String schemaPath = buildSchemaPath(filesystem);
final File schemaFile = filesystem.file(schemaPath);
final String schemaDir = buildSchemaDir(filesystem);
filesystem.directory(schemaDir).createSync(recursive: true);
filesystem.file(schemaFile).writeAsStringSync(schemaData);
}
Platform fakePlatform(String name) { Platform fakePlatform(String name) {
return new FakePlatform.fromPlatform(const LocalPlatform())..operatingSystem = name; return new FakePlatform.fromPlatform(const LocalPlatform())..operatingSystem = name;
} }
......
...@@ -6,15 +6,26 @@ import 'dart:async'; ...@@ -6,15 +6,26 @@ import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'package:file/file.dart'; import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/asset.dart'; import 'package:flutter_tools/src/asset.dart';
import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/cache.dart'; import 'package:flutter_tools/src/cache.dart';
import 'src/common.dart'; import 'src/common.dart';
import 'src/context.dart'; import 'src/context.dart';
import 'src/pubspec_schema.dart';
void main() { void main() {
String fixPath(String path) {
// The in-memory file system is strict about slashes on Windows being the
// correct way so until https://github.com/google/file.dart/issues/112 is
// fixed we fix them here.
// TODO(dantup): Remove this function once the above issue is fixed and
// rolls into Flutter.
return path?.replaceAll('/', fs.path.separator);
}
void writePubspecFile(String path, String name, {String fontsSection}) { void writePubspecFile(String path, String name, {String fontsSection}) {
if (fontsSection == null) { if (fontsSection == null) {
fontsSection = ''; fontsSection = '';
...@@ -26,7 +37,7 @@ $fontsSection ...@@ -26,7 +37,7 @@ $fontsSection
'''; ''';
} }
fs.file(path) fs.file(fixPath(path))
..createSync(recursive: true) ..createSync(recursive: true)
..writeAsStringSync(''' ..writeAsStringSync('''
name: $name name: $name
...@@ -84,30 +95,26 @@ $fontsSection ...@@ -84,30 +95,26 @@ $fontsSection
} }
void writeFontAsset(String path, String font) { void writeFontAsset(String path, String font) {
fs.file('$path$font') fs.file(fixPath('$path$font'))
..createSync(recursive: true) ..createSync(recursive: true)
..writeAsStringSync(font); ..writeAsStringSync(font);
} }
// These tests do not use a memory file system because we want to ensure that group('AssetBundle fonts from packages', () {
// asset bundles work correctly on Windows and Posix systems. FileSystem testFileSystem;
Directory tempDir;
Directory oldCurrentDir;
setUp(() async {
tempDir = fs.systemTempDirectory.createTempSync('flutter_asset_bundle_test.');
oldCurrentDir = fs.currentDirectory;
fs.currentDirectory = tempDir;
});
tearDown(() { setUp(() async {
fs.currentDirectory = oldCurrentDir; testFileSystem = new MemoryFileSystem(
tryToDelete(tempDir); style: platform.isWindows
}); ? FileSystemStyle.windows
: FileSystemStyle.posix,
);
testFileSystem.currentDirectory = testFileSystem.systemTempDirectory.createTempSync('flutter_asset_bundle_test.');
});
group('AssetBundle fonts from packages', () {
testUsingContext('App includes neither font manifest nor fonts when no defines fonts', () async { testUsingContext('App includes neither font manifest nor fonts when no defines fonts', () async {
establishFlutterRoot(); establishFlutterRoot();
writeEmptySchemaFile(fs);
writePubspecFile('pubspec.yaml', 'test'); writePubspecFile('pubspec.yaml', 'test');
writePackagesFile('test_package:p/p/lib/'); writePackagesFile('test_package:p/p/lib/');
...@@ -117,10 +124,13 @@ $fontsSection ...@@ -117,10 +124,13 @@ $fontsSection
await bundle.build(manifestPath: 'pubspec.yaml'); await bundle.build(manifestPath: 'pubspec.yaml');
expect(bundle.entries.length, 3); // LICENSE, AssetManifest, FontManifest expect(bundle.entries.length, 3); // LICENSE, AssetManifest, FontManifest
expect(bundle.entries.containsKey('FontManifest.json'), isTrue); expect(bundle.entries.containsKey('FontManifest.json'), isTrue);
}, overrides: <Type, Generator>{
FileSystem: () => testFileSystem,
}); });
testUsingContext('App font uses font file from package', () async { testUsingContext('App font uses font file from package', () async {
establishFlutterRoot(); establishFlutterRoot();
writeEmptySchemaFile(fs);
const String fontsSection = ''' const String fontsSection = '''
- family: foo - family: foo
...@@ -142,10 +152,13 @@ $fontsSection ...@@ -142,10 +152,13 @@ $fontsSection
<String>['test_package'], <String>['test_package'],
expectedFontManifest, expectedFontManifest,
); );
}, overrides: <Type, Generator>{
FileSystem: () => testFileSystem,
}); });
testUsingContext('App font uses local font file and package font file', () async { testUsingContext('App font uses local font file and package font file', () async {
establishFlutterRoot(); establishFlutterRoot();
writeEmptySchemaFile(fs);
const String fontsSection = ''' const String fontsSection = '''
- family: foo - family: foo
...@@ -171,10 +184,13 @@ $fontsSection ...@@ -171,10 +184,13 @@ $fontsSection
<String>['test_package'], <String>['test_package'],
expectedFontManifest, expectedFontManifest,
); );
}, overrides: <Type, Generator>{
FileSystem: () => testFileSystem,
}); });
testUsingContext('App uses package font with own font file', () async { testUsingContext('App uses package font with own font file', () async {
establishFlutterRoot(); establishFlutterRoot();
writeEmptySchemaFile(fs);
writePubspecFile('pubspec.yaml', 'test'); writePubspecFile('pubspec.yaml', 'test');
writePackagesFile('test_package:p/p/lib/'); writePackagesFile('test_package:p/p/lib/');
...@@ -201,10 +217,13 @@ $fontsSection ...@@ -201,10 +217,13 @@ $fontsSection
<String>['test_package'], <String>['test_package'],
expectedFontManifest, expectedFontManifest,
); );
}, overrides: <Type, Generator>{
FileSystem: () => testFileSystem,
}); });
testUsingContext('App uses package font with font file from another package', () async { testUsingContext('App uses package font with font file from another package', () async {
establishFlutterRoot(); establishFlutterRoot();
writeEmptySchemaFile(fs);
writePubspecFile('pubspec.yaml', 'test'); writePubspecFile('pubspec.yaml', 'test');
writePackagesFile('test_package:p/p/lib/\ntest_package2:p2/p/lib/'); writePackagesFile('test_package:p/p/lib/\ntest_package2:p2/p/lib/');
...@@ -232,10 +251,13 @@ $fontsSection ...@@ -232,10 +251,13 @@ $fontsSection
<String>['test_package2'], <String>['test_package2'],
expectedFontManifest, expectedFontManifest,
); );
}, overrides: <Type, Generator>{
FileSystem: () => testFileSystem,
}); });
testUsingContext('App uses package font with properties and own font file', () async { testUsingContext('App uses package font with properties and own font file', () async {
establishFlutterRoot(); establishFlutterRoot();
writeEmptySchemaFile(fs);
writePubspecFile('pubspec.yaml', 'test'); writePubspecFile('pubspec.yaml', 'test');
writePackagesFile('test_package:p/p/lib/'); writePackagesFile('test_package:p/p/lib/');
...@@ -264,10 +286,13 @@ $fontsSection ...@@ -264,10 +286,13 @@ $fontsSection
<String>['test_package'], <String>['test_package'],
expectedFontManifest, expectedFontManifest,
); );
}, overrides: <Type, Generator>{
FileSystem: () => testFileSystem,
}); });
testUsingContext('App uses local font and package font with own font file.', () async { testUsingContext('App uses local font and package font with own font file.', () async {
establishFlutterRoot(); establishFlutterRoot();
writeEmptySchemaFile(fs);
const String fontsSection = ''' const String fontsSection = '''
- family: foo - family: foo
...@@ -300,6 +325,8 @@ $fontsSection ...@@ -300,6 +325,8 @@ $fontsSection
<String>['test_package'], <String>['test_package'],
expectedFontManifest, expectedFontManifest,
); );
}, overrides: <Type, Generator>{
FileSystem: () => testFileSystem,
}); });
}); });
} }
...@@ -16,6 +16,7 @@ import 'package:flutter_tools/src/flutter_manifest.dart'; ...@@ -16,6 +16,7 @@ import 'package:flutter_tools/src/flutter_manifest.dart';
import 'src/common.dart'; import 'src/common.dart';
import 'src/context.dart'; import 'src/context.dart';
import 'src/pubspec_schema.dart';
void main() { void main() {
void writePubspecFile(String path, String name, {List<String> assets}) { void writePubspecFile(String path, String name, {List<String> assets}) {
...@@ -539,16 +540,7 @@ $assetsSection ...@@ -539,16 +540,7 @@ $assetsSection
return schemaFile.readAsStringSync(); return schemaFile.readAsStringSync();
} }
void writeSchema(String schema, FileSystem filesystem) {
final String schemaPath = buildSchemaPath(filesystem);
final File schemaFile = filesystem.file(schemaPath);
final Directory schemaDir = filesystem.directory(
buildSchemaDir(filesystem));
schemaDir.createSync(recursive: true);
schemaFile.writeAsStringSync(schema);
}
void testUsingContextAndFs(String description, dynamic testMethod(),) { void testUsingContextAndFs(String description, dynamic testMethod(),) {
final FileSystem windowsFs = new MemoryFileSystem( final FileSystem windowsFs = new MemoryFileSystem(
...@@ -564,7 +556,7 @@ $assetsSection ...@@ -564,7 +556,7 @@ $assetsSection
testUsingContext('$description - on windows FS', () async { testUsingContext('$description - on windows FS', () async {
establishFlutterRoot(); establishFlutterRoot();
writeSchema(schema, windowsFs); writeSchemaFile(windowsFs, schema);
await testMethod(); await testMethod();
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FileSystem: () => windowsFs, FileSystem: () => windowsFs,
...@@ -576,7 +568,7 @@ $assetsSection ...@@ -576,7 +568,7 @@ $assetsSection
testUsingContext('$description - on posix FS', () async { testUsingContext('$description - on posix FS', () async {
establishFlutterRoot(); establishFlutterRoot();
writeSchema(schema, posixFs); writeSchemaFile(posixFs, schema);
await testMethod(); await testMethod();
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FileSystem: () => posixFs, FileSystem: () => posixFs,
......
...@@ -12,6 +12,7 @@ import 'package:flutter_tools/src/flutter_manifest.dart'; ...@@ -12,6 +12,7 @@ import 'package:flutter_tools/src/flutter_manifest.dart';
import 'src/common.dart'; import 'src/common.dart';
import 'src/context.dart'; import 'src/context.dart';
import 'src/pubspec_schema.dart';
void main() { void main() {
setUpAll(() { setUpAll(() {
...@@ -515,23 +516,11 @@ flutter: ...@@ -515,23 +516,11 @@ flutter:
expect(flutterManifest.isEmpty, false); expect(flutterManifest.isEmpty, false);
} }
void writeSchemaFile(FileSystem filesystem, String schemaData) {
final String schemaPath = buildSchemaPath(filesystem);
final File schemaFile = filesystem.file(schemaPath);
final String schemaDir = buildSchemaDir(filesystem);
filesystem.directory(schemaDir).createSync(recursive: true);
filesystem.file(schemaFile).writeAsStringSync(schemaData);
}
void testUsingContextAndFs(String description, FileSystem filesystem, void testUsingContextAndFs(String description, FileSystem filesystem,
dynamic testMethod()) { dynamic testMethod()) {
const String schemaData = '{}';
testUsingContext(description, testUsingContext(description,
() async { () async {
writeSchemaFile( filesystem, schemaData); writeEmptySchemaFile(filesystem);
testMethod(); testMethod();
}, },
overrides: <Type, Generator>{ overrides: <Type, Generator>{
......
...@@ -9,7 +9,6 @@ import 'package:flutter_tools/src/artifacts.dart'; ...@@ -9,7 +9,6 @@ import 'package:flutter_tools/src/artifacts.dart';
import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart'; import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/build_info.dart'; import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/flutter_manifest.dart';
import 'package:flutter_tools/src/ios/xcodeproj.dart'; import 'package:flutter_tools/src/ios/xcodeproj.dart';
import 'package:flutter_tools/src/project.dart'; import 'package:flutter_tools/src/project.dart';
import 'package:mockito/mockito.dart'; import 'package:mockito/mockito.dart';
...@@ -18,6 +17,7 @@ import 'package:process/process.dart'; ...@@ -18,6 +17,7 @@ import 'package:process/process.dart';
import '../src/common.dart'; import '../src/common.dart';
import '../src/context.dart'; import '../src/context.dart';
import '../src/pubspec_schema.dart';
const String xcodebuild = '/usr/bin/xcodebuild'; const String xcodebuild = '/usr/bin/xcodebuild';
...@@ -377,16 +377,6 @@ Information about project "Runner": ...@@ -377,16 +377,6 @@ Information about project "Runner":
return properties.isEmpty ? null : properties.first; return properties.isEmpty ? null : properties.first;
} }
void writeSchemaFile(FileSystem filesystem, String schemaData) {
final String schemaPath = buildSchemaPath(filesystem);
final File schemaFile = filesystem.file(schemaPath);
final String schemaDir = buildSchemaDir(filesystem);
filesystem.directory(schemaDir).createSync(recursive: true);
filesystem.file(schemaFile).writeAsStringSync(schemaData);
}
Future<void> checkBuildVersion({ Future<void> checkBuildVersion({
String manifestString, String manifestString,
BuildInfo buildInfo, BuildInfo buildInfo,
...@@ -401,8 +391,7 @@ Information about project "Runner": ...@@ -401,8 +391,7 @@ Information about project "Runner":
manifestFile.writeAsStringSync(manifestString); manifestFile.writeAsStringSync(manifestString);
// write schemaData otherwise pubspec.yaml file can't be loaded // write schemaData otherwise pubspec.yaml file can't be loaded
const String schemaData = '{}'; writeEmptySchemaFile(fs);
writeSchemaFile(fs, schemaData);
await updateGeneratedXcodeProperties( await updateGeneratedXcodeProperties(
project: await FlutterProject.fromPath('path/to/project'), project: await FlutterProject.fromPath('path/to/project'),
......
import 'package:file/file.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/flutter_manifest.dart';
/// Writes a schemaData used for validating pubspec.yaml files when parsing
/// asset information.
void writeSchemaFile(FileSystem filesystem, String schemaData) {
final String schemaPath = buildSchemaPath(filesystem);
final File schemaFile = filesystem.file(schemaPath);
final String schemaDir = buildSchemaDir(filesystem);
filesystem.directory(schemaDir).createSync(recursive: true);
schemaFile.writeAsStringSync(schemaData);
}
/// Writes an empty schemaData that will validate any pubspec.yaml file.
void writeEmptySchemaFile(FileSystem filesystem) {
writeSchemaFile(filesystem, '{}');
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment