From a74f591d0c05f2173448b2b421785b8dc51e8370 Mon Sep 17 00:00:00 2001 From: Danny Tuppeny Date: Tue, 4 Sep 2018 17:12:24 +0100 Subject: [PATCH] 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 --- .../test/android/gradle_test.dart | 15 +---- .../test/asset_bundle_package_fonts_test.dart | 63 +++++++++++++------ .../test/asset_bundle_package_test.dart | 14 +---- .../test/flutter_manifest_test.dart | 15 +---- .../test/ios/xcodeproj_test.dart | 15 +---- .../test/src/pubspec_schema.dart | 20 ++++++ 6 files changed, 74 insertions(+), 68 deletions(-) create mode 100644 packages/flutter_tools/test/src/pubspec_schema.dart diff --git a/packages/flutter_tools/test/android/gradle_test.dart b/packages/flutter_tools/test/android/gradle_test.dart index e8a2419478..634ddc750d 100644 --- a/packages/flutter_tools/test/android/gradle_test.dart +++ b/packages/flutter_tools/test/android/gradle_test.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/build_info.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/project.dart'; import 'package:mockito/mockito.dart'; @@ -19,6 +18,7 @@ import 'package:process/process.dart'; import '../src/common.dart'; import '../src/context.dart'; +import '../src/pubspec_schema.dart'; void main() { Cache.flutterRoot = getFlutterRoot(); @@ -186,8 +186,7 @@ someOtherProperty: someOtherValue manifestFile.writeAsStringSync(manifest); // write schemaData otherwise pubspec.yaml file can't be loaded - const String schemaData = '{}'; - writeSchemaFile(fs, schemaData); + writeEmptySchemaFile(fs); try { updateLocalProperties( @@ -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) { return new FakePlatform.fromPlatform(const LocalPlatform())..operatingSystem = name; } diff --git a/packages/flutter_tools/test/asset_bundle_package_fonts_test.dart b/packages/flutter_tools/test/asset_bundle_package_fonts_test.dart index 4804dba56e..393b89d73e 100644 --- a/packages/flutter_tools/test/asset_bundle_package_fonts_test.dart +++ b/packages/flutter_tools/test/asset_bundle_package_fonts_test.dart @@ -6,15 +6,26 @@ import 'dart:async'; import 'dart:convert'; import 'package:file/file.dart'; +import 'package:file/memory.dart'; import 'package:flutter_tools/src/asset.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 'src/common.dart'; import 'src/context.dart'; +import 'src/pubspec_schema.dart'; 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}) { if (fontsSection == null) { fontsSection = ''; @@ -26,7 +37,7 @@ $fontsSection '''; } - fs.file(path) + fs.file(fixPath(path)) ..createSync(recursive: true) ..writeAsStringSync(''' name: $name @@ -84,30 +95,26 @@ $fontsSection } void writeFontAsset(String path, String font) { - fs.file('$path$font') + fs.file(fixPath('$path$font')) ..createSync(recursive: true) ..writeAsStringSync(font); } - // 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_test.'); - oldCurrentDir = fs.currentDirectory; - fs.currentDirectory = tempDir; - }); - - tearDown(() { - fs.currentDirectory = oldCurrentDir; - tryToDelete(tempDir); - }); - group('AssetBundle fonts from packages', () { + FileSystem testFileSystem; + + setUp(() async { + testFileSystem = new MemoryFileSystem( + style: platform.isWindows + ? FileSystemStyle.windows + : FileSystemStyle.posix, + ); + testFileSystem.currentDirectory = testFileSystem.systemTempDirectory.createTempSync('flutter_asset_bundle_test.'); + }); + testUsingContext('App includes neither font manifest nor fonts when no defines fonts', () async { establishFlutterRoot(); + writeEmptySchemaFile(fs); writePubspecFile('pubspec.yaml', 'test'); writePackagesFile('test_package:p/p/lib/'); @@ -117,10 +124,13 @@ $fontsSection await bundle.build(manifestPath: 'pubspec.yaml'); expect(bundle.entries.length, 3); // LICENSE, AssetManifest, FontManifest expect(bundle.entries.containsKey('FontManifest.json'), isTrue); + }, overrides: { + FileSystem: () => testFileSystem, }); testUsingContext('App font uses font file from package', () async { establishFlutterRoot(); + writeEmptySchemaFile(fs); const String fontsSection = ''' - family: foo @@ -142,10 +152,13 @@ $fontsSection ['test_package'], expectedFontManifest, ); + }, overrides: { + FileSystem: () => testFileSystem, }); testUsingContext('App font uses local font file and package font file', () async { establishFlutterRoot(); + writeEmptySchemaFile(fs); const String fontsSection = ''' - family: foo @@ -171,10 +184,13 @@ $fontsSection ['test_package'], expectedFontManifest, ); + }, overrides: { + FileSystem: () => testFileSystem, }); testUsingContext('App uses package font with own font file', () async { establishFlutterRoot(); + writeEmptySchemaFile(fs); writePubspecFile('pubspec.yaml', 'test'); writePackagesFile('test_package:p/p/lib/'); @@ -201,10 +217,13 @@ $fontsSection ['test_package'], expectedFontManifest, ); + }, overrides: { + FileSystem: () => testFileSystem, }); testUsingContext('App uses package font with font file from another package', () async { establishFlutterRoot(); + writeEmptySchemaFile(fs); writePubspecFile('pubspec.yaml', 'test'); writePackagesFile('test_package:p/p/lib/\ntest_package2:p2/p/lib/'); @@ -232,10 +251,13 @@ $fontsSection ['test_package2'], expectedFontManifest, ); + }, overrides: { + FileSystem: () => testFileSystem, }); testUsingContext('App uses package font with properties and own font file', () async { establishFlutterRoot(); + writeEmptySchemaFile(fs); writePubspecFile('pubspec.yaml', 'test'); writePackagesFile('test_package:p/p/lib/'); @@ -264,10 +286,13 @@ $fontsSection ['test_package'], expectedFontManifest, ); + }, overrides: { + FileSystem: () => testFileSystem, }); testUsingContext('App uses local font and package font with own font file.', () async { establishFlutterRoot(); + writeEmptySchemaFile(fs); const String fontsSection = ''' - family: foo @@ -300,6 +325,8 @@ $fontsSection ['test_package'], expectedFontManifest, ); + }, overrides: { + FileSystem: () => testFileSystem, }); }); } diff --git a/packages/flutter_tools/test/asset_bundle_package_test.dart b/packages/flutter_tools/test/asset_bundle_package_test.dart index 4361bba3ba..d0a7d0cf69 100644 --- a/packages/flutter_tools/test/asset_bundle_package_test.dart +++ b/packages/flutter_tools/test/asset_bundle_package_test.dart @@ -16,6 +16,7 @@ import 'package:flutter_tools/src/flutter_manifest.dart'; import 'src/common.dart'; import 'src/context.dart'; +import 'src/pubspec_schema.dart'; void main() { void writePubspecFile(String path, String name, {List assets}) { @@ -539,16 +540,7 @@ $assetsSection 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(),) { final FileSystem windowsFs = new MemoryFileSystem( @@ -564,7 +556,7 @@ $assetsSection testUsingContext('$description - on windows FS', () async { establishFlutterRoot(); - writeSchema(schema, windowsFs); + writeSchemaFile(windowsFs, schema); await testMethod(); }, overrides: { FileSystem: () => windowsFs, @@ -576,7 +568,7 @@ $assetsSection testUsingContext('$description - on posix FS', () async { establishFlutterRoot(); - writeSchema(schema, posixFs); + writeSchemaFile(posixFs, schema); await testMethod(); }, overrides: { FileSystem: () => posixFs, diff --git a/packages/flutter_tools/test/flutter_manifest_test.dart b/packages/flutter_tools/test/flutter_manifest_test.dart index 68348f79ca..0733412248 100644 --- a/packages/flutter_tools/test/flutter_manifest_test.dart +++ b/packages/flutter_tools/test/flutter_manifest_test.dart @@ -12,6 +12,7 @@ import 'package:flutter_tools/src/flutter_manifest.dart'; import 'src/common.dart'; import 'src/context.dart'; +import 'src/pubspec_schema.dart'; void main() { setUpAll(() { @@ -515,23 +516,11 @@ flutter: 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, dynamic testMethod()) { - const String schemaData = '{}'; - testUsingContext(description, () async { - writeSchemaFile( filesystem, schemaData); + writeEmptySchemaFile(filesystem); testMethod(); }, overrides: { diff --git a/packages/flutter_tools/test/ios/xcodeproj_test.dart b/packages/flutter_tools/test/ios/xcodeproj_test.dart index 441bfd2d11..21e7f143bf 100644 --- a/packages/flutter_tools/test/ios/xcodeproj_test.dart +++ b/packages/flutter_tools/test/ios/xcodeproj_test.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/io.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/project.dart'; import 'package:mockito/mockito.dart'; @@ -18,6 +17,7 @@ import 'package:process/process.dart'; import '../src/common.dart'; import '../src/context.dart'; +import '../src/pubspec_schema.dart'; const String xcodebuild = '/usr/bin/xcodebuild'; @@ -377,16 +377,6 @@ Information about project "Runner": 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 checkBuildVersion({ String manifestString, BuildInfo buildInfo, @@ -401,8 +391,7 @@ Information about project "Runner": manifestFile.writeAsStringSync(manifestString); // write schemaData otherwise pubspec.yaml file can't be loaded - const String schemaData = '{}'; - writeSchemaFile(fs, schemaData); + writeEmptySchemaFile(fs); await updateGeneratedXcodeProperties( project: await FlutterProject.fromPath('path/to/project'), diff --git a/packages/flutter_tools/test/src/pubspec_schema.dart b/packages/flutter_tools/test/src/pubspec_schema.dart new file mode 100644 index 0000000000..e0918bd43b --- /dev/null +++ b/packages/flutter_tools/test/src/pubspec_schema.dart @@ -0,0 +1,20 @@ +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, '{}'); +}