Commit 14016523 authored by Mikkel Nygaard Ravn's avatar Mikkel Nygaard Ravn Committed by GitHub

Ensure snapshot rebuild logic takes main path into account (#11924)

parent 39680ebf
...@@ -7,7 +7,7 @@ import 'dart:convert' show JSON; ...@@ -7,7 +7,7 @@ import 'dart:convert' show JSON;
import 'package:crypto/crypto.dart' show md5; import 'package:crypto/crypto.dart' show md5;
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'package:quiver/core.dart' show hash4; import 'package:quiver/core.dart' show hash2;
import '../artifacts.dart'; import '../artifacts.dart';
import '../build_info.dart'; import '../build_info.dart';
...@@ -21,7 +21,9 @@ GenSnapshot get genSnapshot => context.putIfAbsent(GenSnapshot, () => const GenS ...@@ -21,7 +21,9 @@ GenSnapshot get genSnapshot => context.putIfAbsent(GenSnapshot, () => const GenS
/// A snapshot build configuration. /// A snapshot build configuration.
class SnapshotType { class SnapshotType {
const SnapshotType(this.platform, this.mode); SnapshotType(this.platform, this.mode) {
assert(mode != null);
}
final TargetPlatform platform; final TargetPlatform platform;
final BuildMode mode; final BuildMode mode;
...@@ -55,84 +57,70 @@ class GenSnapshot { ...@@ -55,84 +57,70 @@ class GenSnapshot {
} }
} }
/// A collection of checksums for a set of input files. /// A fingerprint for a set of build input files and properties.
/// ///
/// This class can be used during build actions to compute a checksum of the /// This class can be used during build actions to compute a fingerprint of the
/// build action inputs, and if unchanged from the previous build, skip the /// build action inputs, and if unchanged from the previous build, skip the
/// build step. This assumes that build outputs are strictly a product of the /// build step. This assumes that build outputs are strictly a product of the
/// input files. /// fingerprint inputs.
class Checksum { class Fingerprint {
Checksum.fromFiles(SnapshotType type, this._mainPath, Set<String> inputPaths) { Fingerprint.fromBuildInputs(Map<String, String> properties, Iterable<String> inputPaths) {
final Iterable<File> files = inputPaths.map(fs.file); final Iterable<File> files = inputPaths.map(fs.file);
final Iterable<File> missingInputs = files.where((File file) => !file.existsSync()); final Iterable<File> missingInputs = files.where((File file) => !file.existsSync());
if (missingInputs.isNotEmpty) if (missingInputs.isNotEmpty)
throw new ArgumentError('Missing input files:\n' + missingInputs.join('\n')); throw new ArgumentError('Missing input files:\n' + missingInputs.join('\n'));
_buildMode = type.mode.toString();
_targetPlatform = type.platform?.toString() ?? '';
_checksums = <String, String>{}; _checksums = <String, String>{};
for (File file in files) { for (File file in files) {
final List<int> bytes = file.readAsBytesSync(); final List<int> bytes = file.readAsBytesSync();
_checksums[file.path] = md5.convert(bytes).toString(); _checksums[file.path] = md5.convert(bytes).toString();
} }
_properties = <String, String>{}..addAll(properties);
} }
/// Creates a checksum from serialized JSON. /// Creates a Fingerprint from serialized JSON.
/// ///
/// Throws [ArgumentError] in the following cases: /// Throws [ArgumentError], if there is a version mismatch between the
/// * Version mismatch between the serializing framework and this framework. /// serializing framework and this framework.
/// * buildMode is unspecified. Fingerprint.fromJson(String json) {
/// * targetPlatform is unspecified.
/// * File checksum map is unspecified.
Checksum.fromJson(String json) {
final Map<String, dynamic> content = JSON.decode(json); final Map<String, dynamic> content = JSON.decode(json);
final String version = content['version']; final String version = content['version'];
if (version != FlutterVersion.instance.frameworkRevision) if (version != FlutterVersion.instance.frameworkRevision)
throw new ArgumentError('Incompatible checksum version: $version'); throw new ArgumentError('Incompatible fingerprint version: $version');
_checksums = content['files'] ?? <String, String>{};
_buildMode = content['buildMode']; _properties = content['properties'] ?? <String, String>{};
if (_buildMode == null || _buildMode.isEmpty)
throw new ArgumentError('Build mode unspecified in checksum JSON');
_targetPlatform = content['targetPlatform'];
if (_targetPlatform == null)
throw new ArgumentError('Target platform unspecified in checksum JSON');
_mainPath = content['entrypoint'];
if (_mainPath == null)
throw new ArgumentError('Entrypoint unspecified in checksum JSON');
_checksums = content['files'];
if (_checksums == null)
throw new ArgumentError('File checksums unspecified in checksum JSON');
} }
String _mainPath;
String _buildMode;
String _targetPlatform;
Map<String, String> _checksums; Map<String, String> _checksums;
Map<String, String> _properties;
String toJson() => JSON.encode(<String, dynamic>{ String toJson() => JSON.encode(<String, dynamic>{
'version': FlutterVersion.instance.frameworkRevision, 'version': FlutterVersion.instance.frameworkRevision,
'buildMode': _buildMode, 'properties': _properties,
'entrypoint': _mainPath,
'targetPlatform': _targetPlatform,
'files': _checksums, 'files': _checksums,
}); });
@override @override
bool operator==(dynamic other) { bool operator==(dynamic other) {
return other is Checksum && if (identical(other, this))
_buildMode == other._buildMode && return true;
_targetPlatform == other._targetPlatform && if (other.runtimeType != runtimeType)
_mainPath == other._mainPath && return false;
_checksums.length == other._checksums.length && final Fingerprint typedOther = other;
_checksums.keys.every((String key) => _checksums[key] == other._checksums[key]); return _equalMaps(typedOther._checksums, _checksums)
&& _equalMaps(typedOther._properties, _properties);
}
bool _equalMaps(Map<String, String> a, Map<String, String> b) {
return a.length == b.length
&& a.keys.every((String key) => a[key] == b[key]);
} }
@override @override
int get hashCode => hash4(_buildMode, _targetPlatform, _mainPath, _checksums); // Ignore map entries here to avoid becoming inconsistent with equals
// due to differences in map entry order.
int get hashCode => hash2(_properties.length, _checksums.length);
} }
final RegExp _separatorExpr = new RegExp(r'([^\\]) '); final RegExp _separatorExpr = new RegExp(r'([^\\]) ');
...@@ -177,14 +165,14 @@ class Snapshotter { ...@@ -177,14 +165,14 @@ class Snapshotter {
@required String depfilePath, @required String depfilePath,
@required String packagesPath @required String packagesPath
}) async { }) async {
const SnapshotType type = const SnapshotType(null, BuildMode.debug); final SnapshotType type = new SnapshotType(null, BuildMode.debug);
final List<String> args = <String>[ final List<String> args = <String>[
'--snapshot_kind=script', '--snapshot_kind=script',
'--script_snapshot=$snapshotPath', '--script_snapshot=$snapshotPath',
mainPath, mainPath,
]; ];
final String checksumsPath = '$depfilePath.checksums'; final String fingerprintPath = '$depfilePath.fingerprint';
final int exitCode = await _build( final int exitCode = await _build(
snapshotType: type, snapshotType: type,
outputSnapshotPath: snapshotPath, outputSnapshotPath: snapshotPath,
...@@ -192,11 +180,11 @@ class Snapshotter { ...@@ -192,11 +180,11 @@ class Snapshotter {
snapshotArgs: args, snapshotArgs: args,
depfilePath: depfilePath, depfilePath: depfilePath,
mainPath: mainPath, mainPath: mainPath,
checksumsPath: checksumsPath, fingerprintPath: fingerprintPath,
); );
if (exitCode != 0) if (exitCode != 0)
return exitCode; return exitCode;
await _writeChecksum(type, snapshotPath, depfilePath, mainPath, checksumsPath); await _writeFingerprint(type, snapshotPath, depfilePath, mainPath, fingerprintPath);
return exitCode; return exitCode;
} }
...@@ -212,10 +200,10 @@ class Snapshotter { ...@@ -212,10 +200,10 @@ class Snapshotter {
@required String packagesPath, @required String packagesPath,
@required String depfilePath, @required String depfilePath,
@required String mainPath, @required String mainPath,
@required String checksumsPath, @required String fingerprintPath,
}) async { }) async {
if (!await _isBuildRequired(snapshotType, outputSnapshotPath, depfilePath, mainPath, checksumsPath)) { if (!await _isBuildRequired(snapshotType, outputSnapshotPath, depfilePath, mainPath, fingerprintPath)) {
printTrace('Skipping snapshot build. Checksums match.'); printTrace('Skipping snapshot build. Fingerprints match.');
return 0; return 0;
} }
...@@ -229,41 +217,49 @@ class Snapshotter { ...@@ -229,41 +217,49 @@ class Snapshotter {
if (exitCode != 0) if (exitCode != 0)
return exitCode; return exitCode;
_writeChecksum(snapshotType, outputSnapshotPath, depfilePath, mainPath, checksumsPath); _writeFingerprint(snapshotType, outputSnapshotPath, depfilePath, mainPath, fingerprintPath);
return 0; return 0;
} }
Future<bool> _isBuildRequired(SnapshotType type, String outputSnapshotPath, String depfilePath, String mainPath, String checksumsPath) async { Future<bool> _isBuildRequired(SnapshotType type, String outputSnapshotPath, String depfilePath, String mainPath, String fingerprintPath) async {
final File checksumFile = fs.file(checksumsPath); final File fingerprintFile = fs.file(fingerprintPath);
final File outputSnapshotFile = fs.file(outputSnapshotPath); final File outputSnapshotFile = fs.file(outputSnapshotPath);
final File depfile = fs.file(depfilePath); final File depfile = fs.file(depfilePath);
if (!outputSnapshotFile.existsSync() || !depfile.existsSync() || !checksumFile.existsSync()) if (!outputSnapshotFile.existsSync() || !depfile.existsSync() || !fingerprintFile.existsSync())
return true; return true;
try { try {
if (checksumFile.existsSync()) { if (fingerprintFile.existsSync()) {
final Checksum oldChecksum = new Checksum.fromJson(await checksumFile.readAsString()); final Fingerprint oldFingerprint = new Fingerprint.fromJson(await fingerprintFile.readAsString());
final Set<String> checksumPaths = await readDepfile(depfilePath) final Set<String> inputFilePaths = await readDepfile(depfilePath)..addAll(<String>[outputSnapshotPath, mainPath]);
..addAll(<String>[outputSnapshotPath, mainPath]); final Fingerprint newFingerprint = createFingerprint(type, mainPath, inputFilePaths);
final Checksum newChecksum = new Checksum.fromFiles(type, mainPath, checksumPaths); return oldFingerprint != newFingerprint;
return oldChecksum != newChecksum;
} }
} catch (e) { } catch (e) {
// Log exception and continue, this step is a performance improvement only. // Log exception and continue, this step is a performance improvement only.
printTrace('Rebuilding snapshot due to checksum validation error: $e'); printTrace('Rebuilding snapshot due to fingerprint check error: $e');
} }
return true; return true;
} }
Future<Null> _writeChecksum(SnapshotType type, String outputSnapshotPath, String depfilePath, String mainPath, String checksumsPath) async { Future<Null> _writeFingerprint(SnapshotType type, String outputSnapshotPath, String depfilePath, String mainPath, String fingerprintPath) async {
try { try {
final Set<String> checksumPaths = await readDepfile(depfilePath) final Set<String> inputFilePaths = await readDepfile(depfilePath)
..addAll(<String>[outputSnapshotPath, mainPath]); ..addAll(<String>[outputSnapshotPath, mainPath]);
final Checksum checksum = new Checksum.fromFiles(type, mainPath, checksumPaths); final Fingerprint fingerprint = createFingerprint(type, mainPath, inputFilePaths);
await fs.file(checksumsPath).writeAsString(checksum.toJson()); await fs.file(fingerprintPath).writeAsString(fingerprint.toJson());
} catch (e, s) { } catch (e, s) {
// Log exception and continue, this step is a performance improvement only. // Log exception and continue, this step is a performance improvement only.
printTrace('Error during snapshot checksum output: $e\n$s'); print('Error during snapshot fingerprinting: $e\n$s');
} }
} }
static Fingerprint createFingerprint(SnapshotType type, String mainPath, Iterable<String> inputFilePaths) {
final Map<String, String> properties = <String, String>{
'buildMode': type.mode.toString(),
'targetPlatform': type.platform?.toString() ?? '',
'entryPoint': mainPath,
};
return new Fingerprint.fromBuildInputs(properties, inputFilePaths);
}
} }
...@@ -286,25 +286,25 @@ Future<String> _buildAotSnapshot( ...@@ -286,25 +286,25 @@ Future<String> _buildAotSnapshot(
genSnapshotCmd.add(mainPath); genSnapshotCmd.add(mainPath);
final SnapshotType snapshotType = new SnapshotType(platform, buildMode); final SnapshotType snapshotType = new SnapshotType(platform, buildMode);
final File checksumFile = fs.file('$dependencies.checksum'); final File fingerprintFile = fs.file('$dependencies.fingerprint');
final List<File> checksumFiles = <File>[checksumFile, fs.file(dependencies)] final List<File> fingerprintFiles = <File>[fingerprintFile, fs.file(dependencies)]
..addAll(inputPaths.map(fs.file)) ..addAll(inputPaths.map(fs.file))
..addAll(outputPaths.map(fs.file)); ..addAll(outputPaths.map(fs.file));
if (checksumFiles.every((File file) => file.existsSync())) { if (fingerprintFiles.every((File file) => file.existsSync())) {
try { try {
final String json = await checksumFile.readAsString(); final String json = await fingerprintFile.readAsString();
final Checksum oldChecksum = new Checksum.fromJson(json); final Fingerprint oldFingerprint = new Fingerprint.fromJson(json);
final Set<String> snapshotInputPaths = await readDepfile(dependencies) final Set<String> snapshotInputPaths = await readDepfile(dependencies)
..add(mainPath) ..add(mainPath)
..addAll(outputPaths); ..addAll(outputPaths);
final Checksum newChecksum = new Checksum.fromFiles(snapshotType, mainPath, snapshotInputPaths); final Fingerprint newFingerprint = Snapshotter.createFingerprint(snapshotType, mainPath, snapshotInputPaths);
if (oldChecksum == newChecksum) { if (oldFingerprint == newFingerprint) {
printTrace('Skipping AOT snapshot build. Checksums match.'); printStatus('Skipping AOT snapshot build. Fingerprint match.');
return outputPath; return outputPath;
} }
} catch (e) { } catch (e) {
// Log exception and continue, this step is a performance improvement only. // Log exception and continue, this step is a performance improvement only.
printTrace('Rebuilding snapshot due to checksum validation error: $e'); printTrace('Rebuilding snapshot due to fingerprint check error: $e');
} }
} }
...@@ -370,16 +370,16 @@ Future<String> _buildAotSnapshot( ...@@ -370,16 +370,16 @@ Future<String> _buildAotSnapshot(
await runCheckedAsync(linkCommand); await runCheckedAsync(linkCommand);
} }
// Compute and record checksums. // Compute and record build fingerprint.
try { try {
final Set<String> snapshotInputPaths = await readDepfile(dependencies) final Set<String> snapshotInputPaths = await readDepfile(dependencies)
..add(mainPath) ..add(mainPath)
..addAll(outputPaths); ..addAll(outputPaths);
final Checksum checksum = new Checksum.fromFiles(snapshotType, mainPath, snapshotInputPaths); final Fingerprint fingerprint = Snapshotter.createFingerprint(snapshotType, mainPath, snapshotInputPaths);
await checksumFile.writeAsString(checksum.toJson()); await fingerprintFile.writeAsString(fingerprint.toJson());
} catch (e, s) { } catch (e, s) {
// Log exception and continue, this step is a performance improvement only. // Log exception and continue, this step is a performance improvement only.
printTrace('Error during AOT snapshot checksum output: $e\n$s'); printStatus('Error during AOT snapshot fingerprinting: $e\n$s');
} }
return outputPath; return outputPath;
......
...@@ -24,14 +24,12 @@ class _FakeGenSnapshot implements GenSnapshot { ...@@ -24,14 +24,12 @@ class _FakeGenSnapshot implements GenSnapshot {
this.succeed: true, this.succeed: true,
this.snapshotPath: 'output.snapshot', this.snapshotPath: 'output.snapshot',
this.snapshotContent: '', this.snapshotContent: '',
this.depfilePath: 'output.snapshot.d',
this.depfileContent: 'output.snapshot.d : main.dart', this.depfileContent: 'output.snapshot.d : main.dart',
}); });
final bool succeed; final bool succeed;
final String snapshotPath; final String snapshotPath;
final String snapshotContent; final String snapshotContent;
final String depfilePath;
final String depfileContent; final String depfileContent;
int _callCount = 0; int _callCount = 0;
...@@ -55,7 +53,18 @@ class _FakeGenSnapshot implements GenSnapshot { ...@@ -55,7 +53,18 @@ class _FakeGenSnapshot implements GenSnapshot {
} }
void main() { void main() {
group('Checksum', () { group('SnapshotType', () {
test('throws, if build mode is null', () {
expect(
() => new SnapshotType(TargetPlatform.android_x64, null),
throwsA(anything),
);
});
test('does not throw, if target platform is null', () {
expect(new SnapshotType(null, BuildMode.release), isNotNull);
});
});
group('Fingerprint', () {
MockFlutterVersion mockVersion; MockFlutterVersion mockVersion;
const String kVersion = '123456abcdef'; const String kVersion = '123456abcdef';
...@@ -64,7 +73,7 @@ void main() { ...@@ -64,7 +73,7 @@ void main() {
when(mockVersion.frameworkRevision).thenReturn(kVersion); when(mockVersion.frameworkRevision).thenReturn(kVersion);
}); });
group('fromFiles', () { group('fromBuildInputs', () {
MemoryFileSystem fs; MemoryFileSystem fs;
setUp(() { setUp(() {
...@@ -73,78 +82,69 @@ void main() { ...@@ -73,78 +82,69 @@ void main() {
testUsingContext('throws if any input file does not exist', () async { testUsingContext('throws if any input file does not exist', () async {
await fs.file('a.dart').create(); await fs.file('a.dart').create();
const SnapshotType snapshotType = const SnapshotType(TargetPlatform.ios, BuildMode.debug);
expect( expect(
() => new Checksum.fromFiles(snapshotType, 'a.dart', <String>['a.dart', 'b.dart'].toSet()), () => new Fingerprint.fromBuildInputs(<String, String>{}, <String>['a.dart', 'b.dart']),
throwsA(anything), throwsArgumentError,
);
}, 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(snapshotType, 'a.dart', <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(snapshotType, 'a.dart', <String>['a.dart'].toSet()),
isNotNull,
); );
}, overrides: <Type, Generator>{ FileSystem: () => fs }); }, overrides: <Type, Generator>{ FileSystem: () => fs });
testUsingContext('populates checksums for valid files', () async { testUsingContext('populates checksums for valid files', () async {
await fs.file('a.dart').writeAsString('This is a'); await fs.file('a.dart').writeAsString('This is a');
await fs.file('b.dart').writeAsString('This is b'); await fs.file('b.dart').writeAsString('This is b');
const SnapshotType snapshotType = const SnapshotType(TargetPlatform.ios, BuildMode.debug); final Fingerprint fingerprint = new Fingerprint.fromBuildInputs(<String, String>{}, <String>['a.dart', 'b.dart']);
final Checksum checksum = new Checksum.fromFiles(snapshotType, 'a.dart', <String>['a.dart', 'b.dart'].toSet());
final Map<String, dynamic> json = JSON.decode(checksum.toJson()); final Map<String, dynamic> json = JSON.decode(fingerprint.toJson());
expect(json, hasLength(5));
expect(json['version'], mockVersion.frameworkRevision);
expect(json['buildMode'], BuildMode.debug.toString());
expect(json['targetPlatform'], TargetPlatform.ios.toString());
expect(json['entrypoint'], 'a.dart');
expect(json['files'], hasLength(2)); expect(json['files'], hasLength(2));
expect(json['files']['a.dart'], '8a21a15fad560b799f6731d436c1b698'); expect(json['files']['a.dart'], '8a21a15fad560b799f6731d436c1b698');
expect(json['files']['b.dart'], '6f144e08b58cd0925328610fad7ac07c'); expect(json['files']['b.dart'], '6f144e08b58cd0925328610fad7ac07c');
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{ FileSystem: () => fs });
FileSystem: () => fs,
FlutterVersion: () => mockVersion, testUsingContext('includes framework version', () {
}); final Fingerprint fingerprint = new Fingerprint.fromBuildInputs(<String, String>{}, <String>[]);
final Map<String, dynamic> json = JSON.decode(fingerprint.toJson());
expect(json['version'], mockVersion.frameworkRevision);
}, overrides: <Type, Generator>{ FlutterVersion: () => mockVersion });
testUsingContext('includes provided properties', () {
final Fingerprint fingerprint = new Fingerprint.fromBuildInputs(<String, String>{'a': 'A', 'b': 'B'}, <String>[]);
final Map<String, dynamic> json = JSON.decode(fingerprint.toJson());
expect(json['properties'], hasLength(2));
expect(json['properties']['a'], 'A');
expect(json['properties']['b'], 'B');
}, overrides: <Type, Generator>{ FlutterVersion: () => mockVersion });
}); });
group('fromJson', () { group('fromJson', () {
testUsingContext('throws if JSON is invalid', () async { testUsingContext('throws if JSON is invalid', () async {
expect(() => new Checksum.fromJson('<xml></xml>'), throwsA(anything)); expect(() => new Fingerprint.fromJson('<xml></xml>'), throwsA(anything));
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FlutterVersion: () => mockVersion, FlutterVersion: () => mockVersion,
}); });
testUsingContext('populates checksums for valid JSON', () async { testUsingContext('creates fingerprint from valid JSON', () async {
final String json = JSON.encode(<String, dynamic>{ final String json = JSON.encode(<String, dynamic>{
'version': kVersion, 'version': kVersion,
'properties': <String, String>{
'buildMode': BuildMode.release.toString(), 'buildMode': BuildMode.release.toString(),
'targetPlatform': TargetPlatform.ios.toString(), 'targetPlatform': TargetPlatform.ios.toString(),
'entrypoint': 'a.dart', 'entryPoint': 'a.dart',
},
'files': <String, dynamic>{ 'files': <String, dynamic>{
'a.dart': '8a21a15fad560b799f6731d436c1b698', 'a.dart': '8a21a15fad560b799f6731d436c1b698',
'b.dart': '6f144e08b58cd0925328610fad7ac07c', 'b.dart': '6f144e08b58cd0925328610fad7ac07c',
}, },
}); });
final Checksum checksum = new Checksum.fromJson(json); final Fingerprint fingerprint = new Fingerprint.fromJson(json);
final Map<String, dynamic> content = JSON.decode(fingerprint.toJson());
final Map<String, dynamic> content = JSON.decode(checksum.toJson()); expect(content, hasLength(3));
expect(content, hasLength(5));
expect(content['version'], mockVersion.frameworkRevision); expect(content['version'], mockVersion.frameworkRevision);
expect(content['buildMode'], BuildMode.release.toString()); expect(content['properties'], hasLength(3));
expect(content['targetPlatform'], TargetPlatform.ios.toString()); expect(content['properties']['buildMode'], BuildMode.release.toString());
expect(content['entrypoint'], 'a.dart'); expect(content['properties']['targetPlatform'], TargetPlatform.ios.toString());
expect(content['properties']['entryPoint'], 'a.dart');
expect(content['files'], hasLength(2));
expect(content['files']['a.dart'], '8a21a15fad560b799f6731d436c1b698'); expect(content['files']['a.dart'], '8a21a15fad560b799f6731d436c1b698');
expect(content['files']['b.dart'], '6f144e08b58cd0925328610fad7ac07c'); expect(content['files']['b.dart'], '6f144e08b58cd0925328610fad7ac07c');
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
...@@ -154,81 +154,56 @@ void main() { ...@@ -154,81 +154,56 @@ void main() {
testUsingContext('throws ArgumentError for unknown versions', () async { testUsingContext('throws ArgumentError for unknown versions', () async {
final String json = JSON.encode(<String, dynamic>{ final String json = JSON.encode(<String, dynamic>{
'version': 'bad', 'version': 'bad',
'buildMode': BuildMode.release.toString(), 'properties':<String, String>{},
'targetPlatform': TargetPlatform.ios.toString(), 'files':<String, String>{},
'entrypoint': 'a.dart',
'files': <String, dynamic>{
'a.dart': '8a21a15fad560b799f6731d436c1b698',
'b.dart': '6f144e08b58cd0925328610fad7ac07c',
},
}); });
expect(() => new Checksum.fromJson(json), throwsArgumentError); expect(() => new Fingerprint.fromJson(json), throwsArgumentError);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FlutterVersion: () => mockVersion, FlutterVersion: () => mockVersion,
}); });
});
group('operator ==', () { testUsingContext('throws ArgumentError if version is not present', () async {
testUsingContext('reports not equal if build modes do not match', () async { final String json = JSON.encode(<String, dynamic>{
final Map<String, dynamic> a = <String, dynamic>{ 'properties':<String, String>{},
'version': kVersion, 'files':<String, String>{},
'buildMode': BuildMode.debug.toString(), });
'targetPlatform': TargetPlatform.ios.toString(), expect(() => new Fingerprint.fromJson(json), throwsArgumentError);
'entrypoint': 'a.dart',
'files': <String, dynamic>{
'a.dart': '8a21a15fad560b799f6731d436c1b698',
'b.dart': '6f144e08b58cd0925328610fad7ac07c',
},
};
final Map<String, dynamic> b = new Map<String, dynamic>.from(a);
b['buildMode'] = BuildMode.release.toString();
expect(new Checksum.fromJson(JSON.encode(a)) == new Checksum.fromJson(JSON.encode(b)), isFalse);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FlutterVersion: () => mockVersion, FlutterVersion: () => mockVersion,
}); });
testUsingContext('reports not equal if target platforms do not match', () async { testUsingContext('treats missing properties and files entries as if empty', () async {
final Map<String, dynamic> a = <String, dynamic>{ final String json = JSON.encode(<String, dynamic>{
'version': kVersion, 'version': kVersion,
'buildMode': BuildMode.debug.toString(), });
'targetPlatform': TargetPlatform.ios.toString(), expect(new Fingerprint.fromJson(json), new Fingerprint.fromBuildInputs(<String, String>{}, <String>[]));
'entrypoint': 'a.dart',
'files': <String, dynamic>{
'a.dart': '8a21a15fad560b799f6731d436c1b698',
'b.dart': '6f144e08b58cd0925328610fad7ac07c',
},
};
final Map<String, dynamic> b = new Map<String, dynamic>.from(a);
b['targetPlatform'] = TargetPlatform.android_arm.toString();
expect(new Checksum.fromJson(JSON.encode(a)) == new Checksum.fromJson(JSON.encode(b)), isFalse);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FlutterVersion: () => mockVersion, FlutterVersion: () => mockVersion,
}); });
});
testUsingContext('reports not equal if entrypoints do not match', () async { group('operator ==', () {
testUsingContext('reports not equal if properties do not match', () async {
final Map<String, dynamic> a = <String, dynamic>{ final Map<String, dynamic> a = <String, dynamic>{
'version': kVersion, 'version': kVersion,
'properties': <String, String>{
'buildMode': BuildMode.debug.toString(), 'buildMode': BuildMode.debug.toString(),
'targetPlatform': TargetPlatform.ios.toString(),
'entrypoint': 'a.dart',
'files': <String, dynamic>{
'a.dart': '8a21a15fad560b799f6731d436c1b698',
'b.dart': '6f144e08b58cd0925328610fad7ac07c',
}, },
'files': <String, dynamic>{},
}; };
final Map<String, dynamic> b = new Map<String, dynamic>.from(a); final Map<String, dynamic> b = new Map<String, dynamic>.from(a);
b['entrypoint'] = 'b.dart'; b['properties'] = <String, String>{
expect(new Checksum.fromJson(JSON.encode(a)) == new Checksum.fromJson(JSON.encode(b)), isFalse); 'buildMode': BuildMode.release.toString(),
};
expect(new Fingerprint.fromJson(JSON.encode(a)) == new Fingerprint.fromJson(JSON.encode(b)), isFalse);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FlutterVersion: () => mockVersion, FlutterVersion: () => mockVersion,
}); });
testUsingContext('reports not equal if checksums do not match', () async { testUsingContext('reports not equal if file checksums do not match', () async {
final Map<String, dynamic> a = <String, dynamic>{ final Map<String, dynamic> a = <String, dynamic>{
'version': kVersion, 'version': kVersion,
'buildMode': BuildMode.debug.toString(), 'properties': <String, String>{},
'targetPlatform': TargetPlatform.ios.toString(),
'entrypoint': 'a.dart',
'files': <String, dynamic>{ 'files': <String, dynamic>{
'a.dart': '8a21a15fad560b799f6731d436c1b698', 'a.dart': '8a21a15fad560b799f6731d436c1b698',
'b.dart': '6f144e08b58cd0925328610fad7ac07c', 'b.dart': '6f144e08b58cd0925328610fad7ac07c',
...@@ -239,17 +214,15 @@ void main() { ...@@ -239,17 +214,15 @@ void main() {
'a.dart': '8a21a15fad560b799f6731d436c1b698', 'a.dart': '8a21a15fad560b799f6731d436c1b698',
'b.dart': '6f144e08b58cd0925328610fad7ac07d', 'b.dart': '6f144e08b58cd0925328610fad7ac07d',
}; };
expect(new Checksum.fromJson(JSON.encode(a)) == new Checksum.fromJson(JSON.encode(b)), isFalse); expect(new Fingerprint.fromJson(JSON.encode(a)) == new Fingerprint.fromJson(JSON.encode(b)), isFalse);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FlutterVersion: () => mockVersion, FlutterVersion: () => mockVersion,
}); });
testUsingContext('reports not equal if keys do not match', () async { testUsingContext('reports not equal if file paths do not match', () async {
final Map<String, dynamic> a = <String, dynamic>{ final Map<String, dynamic> a = <String, dynamic>{
'version': kVersion, 'version': kVersion,
'buildMode': BuildMode.debug.toString(), 'properties': <String, String>{},
'targetPlatform': TargetPlatform.ios.toString(),
'entrypoint': 'a.dart',
'files': <String, dynamic>{ 'files': <String, dynamic>{
'a.dart': '8a21a15fad560b799f6731d436c1b698', 'a.dart': '8a21a15fad560b799f6731d436c1b698',
'b.dart': '6f144e08b58cd0925328610fad7ac07c', 'b.dart': '6f144e08b58cd0925328610fad7ac07c',
...@@ -260,27 +233,40 @@ void main() { ...@@ -260,27 +233,40 @@ void main() {
'a.dart': '8a21a15fad560b799f6731d436c1b698', 'a.dart': '8a21a15fad560b799f6731d436c1b698',
'c.dart': '6f144e08b58cd0925328610fad7ac07d', 'c.dart': '6f144e08b58cd0925328610fad7ac07d',
}; };
expect(new Checksum.fromJson(JSON.encode(a)) == new Checksum.fromJson(JSON.encode(b)), isFalse); expect(new Fingerprint.fromJson(JSON.encode(a)) == new Fingerprint.fromJson(JSON.encode(b)), isFalse);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FlutterVersion: () => mockVersion, FlutterVersion: () => mockVersion,
}); });
testUsingContext('reports equal if all checksums match', () async { testUsingContext('reports equal if properties and file checksums match', () async {
final Map<String, dynamic> a = <String, dynamic>{ final Map<String, dynamic> a = <String, dynamic>{
'version': kVersion, 'version': kVersion,
'properties': <String, String>{
'buildMode': BuildMode.debug.toString(), 'buildMode': BuildMode.debug.toString(),
'targetPlatform': TargetPlatform.ios.toString(), 'targetPlatform': TargetPlatform.ios.toString(),
'entrypoint': 'a.dart', 'entryPoint': 'a.dart',
},
'files': <String, dynamic>{ 'files': <String, dynamic>{
'a.dart': '8a21a15fad560b799f6731d436c1b698', 'a.dart': '8a21a15fad560b799f6731d436c1b698',
'b.dart': '6f144e08b58cd0925328610fad7ac07c', 'b.dart': '6f144e08b58cd0925328610fad7ac07c',
}, },
}; };
expect(new Checksum.fromJson(JSON.encode(a)) == new Checksum.fromJson(JSON.encode(a)), isTrue); expect(new Fingerprint.fromJson(JSON.encode(a)) == new Fingerprint.fromJson(JSON.encode(a)), isTrue);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FlutterVersion: () => mockVersion, FlutterVersion: () => mockVersion,
}); });
}); });
group('hashCode', () {
testUsingContext('is consistent with equals, even if map entries are reordered', () async {
final Fingerprint a = new Fingerprint.fromJson('{"version":"$kVersion","properties":{"a":"A","b":"B"},"files":{}}');
final Fingerprint b = new Fingerprint.fromJson('{"version":"$kVersion","properties":{"b":"B","a":"A"},"files":{}}');
expect(a, b);
expect(a.hashCode, b.hashCode);
}, overrides: <Type, Generator>{
FlutterVersion: () => mockVersion,
});
});
}); });
group('readDepfile', () { group('readDepfile', () {
...@@ -351,7 +337,7 @@ void main() { ...@@ -351,7 +337,7 @@ void main() {
expect(genSnapshot.callCount, 1); expect(genSnapshot.callCount, 1);
final Map<String, dynamic> json = JSON.decode(await fs.file('output.snapshot.d.checksums').readAsString()); final Map<String, dynamic> json = JSON.decode(await fs.file('output.snapshot.d.fingerprint').readAsString());
expect(json['files'], hasLength(2)); expect(json['files'], hasLength(2));
expect(json['files']['main.dart'], '27f5ebf0f8c559b2af9419d190299a5e'); expect(json['files']['main.dart'], '27f5ebf0f8c559b2af9419d190299a5e');
expect(json['files']['output.snapshot'], 'd41d8cd98f00b204e9800998ecf8427e'); expect(json['files']['output.snapshot'], 'd41d8cd98f00b204e9800998ecf8427e');
...@@ -365,7 +351,7 @@ void main() { ...@@ -365,7 +351,7 @@ void main() {
await fs.file('main.dart').writeAsString('void main() {}'); await fs.file('main.dart').writeAsString('void main() {}');
await fs.file('output.snapshot').create(); await fs.file('output.snapshot').create();
await fs.file('output.snapshot.d').writeAsString('output.snapshot : main.dart'); await fs.file('output.snapshot.d').writeAsString('output.snapshot : main.dart');
await fs.file('output.snapshot.d.checksums').writeAsString(JSON.encode(<String, dynamic>{ await fs.file('output.snapshot.d.fingerprint').writeAsString(JSON.encode(<String, dynamic>{
'version': '$kVersion', 'version': '$kVersion',
'buildMode': BuildMode.debug.toString(), 'buildMode': BuildMode.debug.toString(),
'files': <String, dynamic>{ 'files': <String, dynamic>{
...@@ -382,7 +368,7 @@ void main() { ...@@ -382,7 +368,7 @@ void main() {
expect(genSnapshot.callCount, 1); expect(genSnapshot.callCount, 1);
final Map<String, dynamic> json = JSON.decode(await fs.file('output.snapshot.d.checksums').readAsString()); final Map<String, dynamic> json = JSON.decode(await fs.file('output.snapshot.d.fingerprint').readAsString());
expect(json['files'], hasLength(2)); expect(json['files'], hasLength(2));
expect(json['files']['main.dart'], '27f5ebf0f8c559b2af9419d190299a5e'); expect(json['files']['main.dart'], '27f5ebf0f8c559b2af9419d190299a5e');
expect(json['files']['output.snapshot'], 'd41d8cd98f00b204e9800998ecf8427e'); expect(json['files']['output.snapshot'], 'd41d8cd98f00b204e9800998ecf8427e');
...@@ -395,9 +381,13 @@ void main() { ...@@ -395,9 +381,13 @@ void main() {
testUsingContext('builds snapshot and checksums when checksums match but previous snapshot not present', () async { 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('main.dart').writeAsString('void main() {}');
await fs.file('output.snapshot.d').writeAsString('output.snapshot : main.dart'); await fs.file('output.snapshot.d').writeAsString('output.snapshot : main.dart');
await fs.file('output.snapshot.d.checksums').writeAsString(JSON.encode(<String, dynamic>{ await fs.file('output.snapshot.d.fingerprint').writeAsString(JSON.encode(<String, dynamic>{
'version': '$kVersion', 'version': '$kVersion',
'properties': <String, String>{
'buildMode': BuildMode.debug.toString(), 'buildMode': BuildMode.debug.toString(),
'targetPlatform': '',
'entryPoint': 'main.dart',
},
'files': <String, dynamic>{ 'files': <String, dynamic>{
'main.dart': '27f5ebf0f8c559b2af9419d190299a5e', 'main.dart': '27f5ebf0f8c559b2af9419d190299a5e',
'output.snapshot': 'd41d8cd98f00b204e9800998ecf8427e', 'output.snapshot': 'd41d8cd98f00b204e9800998ecf8427e',
...@@ -412,7 +402,7 @@ void main() { ...@@ -412,7 +402,7 @@ void main() {
expect(genSnapshot.callCount, 1); expect(genSnapshot.callCount, 1);
final Map<String, dynamic> json = JSON.decode(await fs.file('output.snapshot.d.checksums').readAsString()); final Map<String, dynamic> json = JSON.decode(await fs.file('output.snapshot.d.fingerprint').readAsString());
expect(json['files'], hasLength(2)); expect(json['files'], hasLength(2));
expect(json['files']['main.dart'], '27f5ebf0f8c559b2af9419d190299a5e'); expect(json['files']['main.dart'], '27f5ebf0f8c559b2af9419d190299a5e');
expect(json['files']['output.snapshot'], 'd41d8cd98f00b204e9800998ecf8427e'); expect(json['files']['output.snapshot'], 'd41d8cd98f00b204e9800998ecf8427e');
...@@ -422,10 +412,9 @@ void main() { ...@@ -422,10 +412,9 @@ void main() {
GenSnapshot: () => genSnapshot, GenSnapshot: () => genSnapshot,
}); });
testUsingContext('builds snapshot and checksums when main entry point changes', () async { testUsingContext('builds snapshot and fingerprint when main entry point changes to other dependency', () async {
final _FakeGenSnapshot genSnapshot = new _FakeGenSnapshot( final _FakeGenSnapshot genSnapshot = new _FakeGenSnapshot(
snapshotPath: 'output.snapshot', snapshotPath: 'output.snapshot',
depfilePath: 'output.snapshot.d',
depfileContent: 'output.snapshot : main.dart other.dart', depfileContent: 'output.snapshot : main.dart other.dart',
); );
context.setVariable(GenSnapshot, genSnapshot); context.setVariable(GenSnapshot, genSnapshot);
...@@ -434,10 +423,13 @@ void main() { ...@@ -434,10 +423,13 @@ void main() {
await fs.file('other.dart').writeAsString('import "main.dart";\nvoid main() {}'); await fs.file('other.dart').writeAsString('import "main.dart";\nvoid main() {}');
await fs.file('output.snapshot').create(); await fs.file('output.snapshot').create();
await fs.file('output.snapshot.d').writeAsString('output.snapshot : main.dart'); await fs.file('output.snapshot.d').writeAsString('output.snapshot : main.dart');
await fs.file('output.snapshot.d.checksums').writeAsString(JSON.encode(<String, dynamic>{ await fs.file('output.snapshot.d.fingerprint').writeAsString(JSON.encode(<String, dynamic>{
'version': '$kVersion', 'version': kVersion,
'properties': <String, String>{
'buildMode': BuildMode.debug.toString(), 'buildMode': BuildMode.debug.toString(),
'targetPlatform': '', 'targetPlatform': '',
'entryPoint': 'main.dart',
},
'files': <String, dynamic>{ 'files': <String, dynamic>{
'main.dart': 'bc096b33f14dde5e0ffaf93a1d03395c', 'main.dart': 'bc096b33f14dde5e0ffaf93a1d03395c',
'other.dart': 'e0c35f083f0ad76b2d87100ec678b516', 'other.dart': 'e0c35f083f0ad76b2d87100ec678b516',
...@@ -452,7 +444,8 @@ void main() { ...@@ -452,7 +444,8 @@ void main() {
); );
expect(genSnapshot.callCount, 1); expect(genSnapshot.callCount, 1);
final Map<String, dynamic> json = JSON.decode(await fs.file('output.snapshot.d.checksums').readAsString()); final Map<String, dynamic> json = JSON.decode(await fs.file('output.snapshot.d.fingerprint').readAsString());
expect(json['properties']['entryPoint'], 'other.dart');
expect(json['files'], hasLength(3)); expect(json['files'], hasLength(3));
expect(json['files']['main.dart'], 'bc096b33f14dde5e0ffaf93a1d03395c'); expect(json['files']['main.dart'], 'bc096b33f14dde5e0ffaf93a1d03395c');
expect(json['files']['other.dart'], 'e0c35f083f0ad76b2d87100ec678b516'); expect(json['files']['other.dart'], 'e0c35f083f0ad76b2d87100ec678b516');
...@@ -462,15 +455,17 @@ void main() { ...@@ -462,15 +455,17 @@ void main() {
FlutterVersion: () => mockVersion, FlutterVersion: () => mockVersion,
}); });
testUsingContext('skips snapshot when checksums match and previous snapshot is present', () async { testUsingContext('skips snapshot when fingerprints match and previous snapshot is present', () async {
await fs.file('main.dart').writeAsString('void main() {}'); await fs.file('main.dart').writeAsString('void main() {}');
await fs.file('output.snapshot').create(); await fs.file('output.snapshot').create();
await fs.file('output.snapshot.d').writeAsString('output.snapshot : main.dart'); await fs.file('output.snapshot.d').writeAsString('output.snapshot : main.dart');
await fs.file('output.snapshot.d.checksums').writeAsString(JSON.encode(<String, dynamic>{ await fs.file('output.snapshot.d.fingerprint').writeAsString(JSON.encode(<String, dynamic>{
'version': '$kVersion', 'version': kVersion,
'properties': <String, String>{
'buildMode': BuildMode.debug.toString(), 'buildMode': BuildMode.debug.toString(),
'targetPlatform': '', 'targetPlatform': '',
'entrypoint': 'main.dart', 'entryPoint': 'main.dart',
},
'files': <String, dynamic>{ 'files': <String, dynamic>{
'main.dart': '27f5ebf0f8c559b2af9419d190299a5e', 'main.dart': '27f5ebf0f8c559b2af9419d190299a5e',
'output.snapshot': 'd41d8cd98f00b204e9800998ecf8427e', 'output.snapshot': 'd41d8cd98f00b204e9800998ecf8427e',
...@@ -485,7 +480,7 @@ void main() { ...@@ -485,7 +480,7 @@ void main() {
expect(genSnapshot.callCount, 0); expect(genSnapshot.callCount, 0);
final Map<String, dynamic> json = JSON.decode(await fs.file('output.snapshot.d.checksums').readAsString()); final Map<String, dynamic> json = JSON.decode(await fs.file('output.snapshot.d.fingerprint').readAsString());
expect(json['files'], hasLength(2)); expect(json['files'], hasLength(2));
expect(json['files']['main.dart'], '27f5ebf0f8c559b2af9419d190299a5e'); expect(json['files']['main.dart'], '27f5ebf0f8c559b2af9419d190299a5e');
expect(json['files']['output.snapshot'], 'd41d8cd98f00b204e9800998ecf8427e'); expect(json['files']['output.snapshot'], 'd41d8cd98f00b204e9800998ecf8427e');
...@@ -494,5 +489,46 @@ void main() { ...@@ -494,5 +489,46 @@ void main() {
FlutterVersion: () => mockVersion, FlutterVersion: () => mockVersion,
GenSnapshot: () => genSnapshot, GenSnapshot: () => genSnapshot,
}); });
group('createFingerprint', () {
test('creates fingerprint with target platform', () {
final Fingerprint fingerprint = Snapshotter.createFingerprint(
new SnapshotType(TargetPlatform.android_x64, BuildMode.release),
'a.dart',
<String>[],
);
expect(fingerprint, new Fingerprint.fromBuildInputs(<String, String>{
'buildMode': 'BuildMode.release',
'targetPlatform': 'TargetPlatform.android_x64',
'entryPoint': 'a.dart',
}, <String>[]));
});
test('creates fingerprint without target platform', () {
final Fingerprint fingerprint = Snapshotter.createFingerprint(
new SnapshotType(null, BuildMode.release),
'a.dart',
<String>[],
);
expect(fingerprint, new Fingerprint.fromBuildInputs(<String, String>{
'buildMode': 'BuildMode.release',
'targetPlatform': '',
'entryPoint': 'a.dart',
}, <String>[]));
});
testUsingContext('creates fingerprint with file checksums', () async {
await fs.file('a.dart').create();
await fs.file('b.dart').create();
final Fingerprint fingerprint = Snapshotter.createFingerprint(
new SnapshotType(TargetPlatform.android_x64, BuildMode.release),
'a.dart',
<String>['a.dart', 'b.dart'],
);
expect(fingerprint, new Fingerprint.fromBuildInputs(<String, String>{
'buildMode': 'BuildMode.release',
'targetPlatform': 'TargetPlatform.android_x64',
'entryPoint': 'a.dart',
}, <String>['a.dart', 'b.dart']));
}, overrides: <Type, Generator>{ FileSystem: () => fs });
});
}); });
} }
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