Skip to content

Commit

Permalink
[native_assets_builder] Fix flaky test
Browse files Browse the repository at this point in the history
  • Loading branch information
dcharkes committed Dec 30, 2024
1 parent f7af11e commit 98c535c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
21 changes: 18 additions & 3 deletions pkgs/native_assets_builder/test_data/transformer/hook/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// for details. 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 'dart:io';

import 'package:crypto/crypto.dart';
import 'package:native_assets_cli/data_assets.dart';
import 'package:transformer/src/transform.dart';

Expand All @@ -17,21 +19,32 @@ void main(List<String> arguments) async {
var transformedFiles = 0;
var cachedFiles = 0;

final hashesFile =
File.fromUri(config.outputDirectoryShared.resolve('hashes.json'));
final hashes = await hashesFile.exists()
? (json.decoder.convert(await hashesFile.readAsString()) as Map)
.cast<String, String>()
: <String, String>{};
final newHashes = <String, String>{};

await for (final sourceFile in dataDirectory.list()) {
if (sourceFile is! File) {
continue;
}

final sourceName =
sourceFile.uri.pathSegments.lastWhere((e) => e.isNotEmpty);
final sourceContents = await sourceFile.readAsString();
final sourceHash = md5.convert(sourceContents.codeUnits).toString();
newHashes[sourceName] = sourceHash;
final prevHash = hashes[sourceName];

final name = sourceName.replaceFirst('data', 'data_transformed');
final targetFile = File.fromUri(config.outputDirectoryShared
.resolve(sourceName.replaceFirst('data', 'data_transformed')));

// TODO(dacoharkes): Timestamps are not enough for correct caching.
if (!await targetFile.exists() ||
!(await sourceFile.lastModified())
.isBefore(await targetFile.lastModified())) {
if (!await targetFile.exists() || sourceHash != prevHash) {
await transformFile(sourceFile, targetFile);
transformedFiles++;
} else {
Expand All @@ -50,6 +63,8 @@ void main(List<String> arguments) async {
);
}

await hashesFile.writeAsString(json.encoder.convert(newHashes));

print('Transformed $transformedFiles files.');
print('Reused $cachedFiles cached files.');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ environment:
sdk: '>=3.3.0 <4.0.0'

dependencies:
crypto: ^3.0.6
native_assets_cli: ^0.10.0
# native_assets_cli:
# path: ../../../native_assets_cli/
Expand Down

0 comments on commit 98c535c

Please sign in to comment.