Extract snapshotting logic to Snapshotter class (#11820)

Extract a Snapshotter class that can be shared between FLX snapshotting,
AOT snapshotting, and assembly AOT snapshotting. Allows for better
testability of snapshotting logic.

* Extracts script snapshotting used in FLX build.
* Adds tests for snapshot checksumming, build invalidation/skipping.

Remaining work: disentangle + extract AOT snapshotting and Assembly AOT
snapshotting logic from build_aot.dart.
This commit is contained in:
Chris Bracken
2017-08-29 10:55:14 -07:00
committed by GitHub
parent 8d5fe6d430
commit 27d3e8a41f
4 changed files with 377 additions and 84 deletions

View File

@@ -2,11 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:convert';
import 'dart:convert' show JSON;
import 'package:file/memory.dart';
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/base/build.dart';
import 'package:flutter_tools/src/base/context.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/version.dart';
import 'package:mockito/mockito.dart';
@@ -16,6 +19,41 @@ import '../src/context.dart';
class MockFlutterVersion extends Mock implements FlutterVersion {}
class _FakeGenSnapshot implements GenSnapshot {
_FakeGenSnapshot({
this.succeed: true,
this.snapshotPath: 'output.snapshot',
this.snapshotContent: '',
this.depfilePath: 'output.snapshot.d',
this.depfileContent: 'output.snapshot.d : main.dart',
});
final bool succeed;
final String snapshotPath;
final String snapshotContent;
final String depfilePath;
final String depfileContent;
int _callCount = 0;
int get callCount => _callCount;
@override
Future<int> run({
SnapshotType snapshotType,
String packagesPath,
String depfilePath,
Iterable<String> additionalArgs,
}) async {
_callCount += 1;
if (!succeed)
return 1;
await fs.file(snapshotPath).writeAsString(snapshotContent);
await fs.file(depfilePath).writeAsString(depfileContent);
return 0;
}
}
void main() {
group('Checksum', () {
MockFlutterVersion mockVersion;
@@ -35,24 +73,27 @@ void main() {
testUsingContext('throws if any input file does not exist', () async {
await fs.file('a.dart').create();
const SnapshotType snapshotType = const SnapshotType(TargetPlatform.ios, BuildMode.debug);
expect(
() => new Checksum.fromFiles(BuildMode.debug, TargetPlatform.ios, <String>['a.dart', 'b.dart'].toSet()),
() => new Checksum.fromFiles(snapshotType, <String>['a.dart', 'b.dart'].toSet()),
throwsA(anything),
);
}, overrides: <Type, Generator>{ FileSystem: () => fs });
testUsingContext('throws if any build mode is null', () async {
await fs.file('a.dart').create();
const SnapshotType snapshotType = const SnapshotType(TargetPlatform.ios, null);
expect(
() => new Checksum.fromFiles(null, TargetPlatform.ios, <String>['a.dart', 'b.dart'].toSet()),
() => new Checksum.fromFiles(snapshotType, <String>['a.dart', 'b.dart'].toSet()),
throwsA(anything),
);
}, overrides: <Type, Generator>{ FileSystem: () => fs });
testUsingContext('does not throw if any target platform is null', () async {
await fs.file('a.dart').create();
const SnapshotType snapshotType = const SnapshotType(null, BuildMode.debug);
expect(
new Checksum.fromFiles(BuildMode.debug, null, <String>['a.dart'].toSet()),
new Checksum.fromFiles(snapshotType, <String>['a.dart'].toSet()),
isNotNull,
);
}, overrides: <Type, Generator>{ FileSystem: () => fs });
@@ -60,7 +101,8 @@ void main() {
testUsingContext('populates checksums for valid files', () async {
await fs.file('a.dart').writeAsString('This is a');
await fs.file('b.dart').writeAsString('This is b');
final Checksum checksum = new Checksum.fromFiles(BuildMode.debug, TargetPlatform.ios, <String>['a.dart', 'b.dart'].toSet());
const SnapshotType snapshotType = const SnapshotType(TargetPlatform.ios, BuildMode.debug);
final Checksum checksum = new Checksum.fromFiles(snapshotType, <String>['a.dart', 'b.dart'].toSet());
final Map<String, dynamic> json = JSON.decode(checksum.toJson());
expect(json, hasLength(4));
@@ -187,4 +229,174 @@ void main() {
]));
}, overrides: <Type, Generator>{ FileSystem: () => fs });
});
group('Snapshotter', () {
const String kVersion = '123456abcdef';
_FakeGenSnapshot genSnapshot;
MemoryFileSystem fs;
MockFlutterVersion mockVersion;
Snapshotter snapshotter;
setUp(() {
fs = new MemoryFileSystem();
genSnapshot = new _FakeGenSnapshot();
mockVersion = new MockFlutterVersion();
snapshotter = new Snapshotter();
when(mockVersion.frameworkRevision).thenReturn(kVersion);
});
testUsingContext('builds snapshot and checksums when no checksums are present', () async {
await fs.file('main.dart').writeAsString('void main() {}');
await fs.file('output.snapshot').create();
await fs.file('output.snapshot.d').writeAsString('snapshot : main.dart');
await snapshotter.buildScriptSnapshot(
mainPath: 'main.dart',
snapshotPath: 'output.snapshot',
depfilePath: 'output.snapshot.d',
packagesPath: '.packages',
);
expect(genSnapshot.callCount, 1);
final Map<String, dynamic> json = JSON.decode(await fs.file('output.snapshot.d.checksums').readAsString());
expect(json['files'], hasLength(2));
expect(json['files']['main.dart'], '27f5ebf0f8c559b2af9419d190299a5e');
expect(json['files']['output.snapshot'], 'd41d8cd98f00b204e9800998ecf8427e');
}, overrides: <Type, Generator>{
FileSystem: () => fs,
FlutterVersion: () => mockVersion,
GenSnapshot: () => genSnapshot,
});
testUsingContext('builds snapshot and checksums when checksums differ', () async {
await fs.file('main.dart').writeAsString('void main() {}');
await fs.file('output.snapshot').create();
await fs.file('output.snapshot.d').writeAsString('output.snapshot : main.dart');
await fs.file('output.snapshot.d.checksums').writeAsString(JSON.encode(<String, dynamic>{
'version': '$kVersion',
'buildMode': BuildMode.debug.toString(),
'files': <String, dynamic>{
'main.dart': '27f5ebf0f8c559b2af9419d190299a5e',
'output.snapshot': 'deadbeef01234567890abcdef0123456',
},
}));
await snapshotter.buildScriptSnapshot(
mainPath: 'main.dart',
snapshotPath: 'output.snapshot',
depfilePath: 'output.snapshot.d',
packagesPath: '.packages',
);
expect(genSnapshot.callCount, 1);
final Map<String, dynamic> json = JSON.decode(await fs.file('output.snapshot.d.checksums').readAsString());
expect(json['files'], hasLength(2));
expect(json['files']['main.dart'], '27f5ebf0f8c559b2af9419d190299a5e');
expect(json['files']['output.snapshot'], 'd41d8cd98f00b204e9800998ecf8427e');
}, overrides: <Type, Generator>{
FileSystem: () => fs,
FlutterVersion: () => mockVersion,
GenSnapshot: () => genSnapshot,
});
testUsingContext('builds snapshot and checksums when checksums match but previous snapshot not present', () async {
await fs.file('main.dart').writeAsString('void main() {}');
await fs.file('output.snapshot.d').writeAsString('output.snapshot : main.dart');
await fs.file('output.snapshot.d.checksums').writeAsString(JSON.encode(<String, dynamic>{
'version': '$kVersion',
'buildMode': BuildMode.debug.toString(),
'files': <String, dynamic>{
'main.dart': '27f5ebf0f8c559b2af9419d190299a5e',
'output.snapshot': 'd41d8cd98f00b204e9800998ecf8427e',
},
}));
await snapshotter.buildScriptSnapshot(
mainPath: 'main.dart',
snapshotPath: 'output.snapshot',
depfilePath: 'output.snapshot.d',
packagesPath: '.packages',
);
expect(genSnapshot.callCount, 1);
final Map<String, dynamic> json = JSON.decode(await fs.file('output.snapshot.d.checksums').readAsString());
expect(json['files'], hasLength(2));
expect(json['files']['main.dart'], '27f5ebf0f8c559b2af9419d190299a5e');
expect(json['files']['output.snapshot'], 'd41d8cd98f00b204e9800998ecf8427e');
}, overrides: <Type, Generator>{
FileSystem: () => fs,
FlutterVersion: () => mockVersion,
GenSnapshot: () => genSnapshot,
});
testUsingContext('builds snapshot and checksums when main entry point changes', () async {
final _FakeGenSnapshot genSnapshot = new _FakeGenSnapshot(
snapshotPath: 'output.snapshot',
depfilePath: 'output.snapshot.d',
depfileContent: 'output.snapshot : other.dart',
);
context.setVariable(GenSnapshot, genSnapshot);
await fs.file('main.dart').writeAsString('void main() {}');
await fs.file('other.dart').writeAsString('void main() { print("Kanpai ima kimi wa jinsei no ookina ookina butai ni tachi"); }');
await fs.file('output.snapshot.d').writeAsString('output.snapshot : main.dart');
await fs.file('output.snapshot.d.checksums').writeAsString(JSON.encode(<String, dynamic>{
'version': '$kVersion',
'buildMode': BuildMode.debug.toString(),
'targetPlatform': '',
'files': <String, dynamic>{
'main.dart': '27f5ebf0f8c559b2af9419d190299a5e',
'output.snapshot': 'd41d8cd98f00b204e9800998ecf8427e',
},
}));
await snapshotter.buildScriptSnapshot(
mainPath: 'other.dart',
snapshotPath: 'output.snapshot',
depfilePath: 'output.snapshot.d',
packagesPath: '.packages',
);
expect(genSnapshot.callCount, 1);
final Map<String, dynamic> json = JSON.decode(await fs.file('output.snapshot.d.checksums').readAsString());
expect(json['files'], hasLength(2));
expect(json['files']['other.dart'], '3238d0ae341339b1731d3c2e195ad177');
expect(json['files']['output.snapshot'], 'd41d8cd98f00b204e9800998ecf8427e');
}, overrides: <Type, Generator>{
FileSystem: () => fs,
FlutterVersion: () => mockVersion,
});
testUsingContext('skips snapshot when checksums match and previous snapshot is present', () async {
await fs.file('main.dart').writeAsString('void main() {}');
await fs.file('output.snapshot').create();
await fs.file('output.snapshot.d').writeAsString('output.snapshot : main.dart');
await fs.file('output.snapshot.d.checksums').writeAsString(JSON.encode(<String, dynamic>{
'version': '$kVersion',
'buildMode': BuildMode.debug.toString(),
'targetPlatform': '',
'files': <String, dynamic>{
'main.dart': '27f5ebf0f8c559b2af9419d190299a5e',
'output.snapshot': 'd41d8cd98f00b204e9800998ecf8427e',
},
}));
await snapshotter.buildScriptSnapshot(
mainPath: 'main.dart',
snapshotPath: 'output.snapshot',
depfilePath: 'output.snapshot.d',
packagesPath: '.packages',
);
expect(genSnapshot.callCount, 0);
final Map<String, dynamic> json = JSON.decode(await fs.file('output.snapshot.d.checksums').readAsString());
expect(json['files'], hasLength(2));
expect(json['files']['main.dart'], '27f5ebf0f8c559b2af9419d190299a5e');
expect(json['files']['output.snapshot'], 'd41d8cd98f00b204e9800998ecf8427e');
}, overrides: <Type, Generator>{
FileSystem: () => fs,
FlutterVersion: () => mockVersion,
GenSnapshot: () => genSnapshot,
});
});
}