goldenImageWriter method

Future<void> goldenImageWriter(
  1. TestReport report
)

Writes the golden images from the report to Cloud Storage and also writes the metadata that allows the reading of the golden images. This will throw an exception on failure.

Implementation

Future<void> goldenImageWriter(TestReport report) async {
  await _loadData();
  final actualPath = goldenImagesPath.startsWith('/')
      ? goldenImagesPath.substring(1)
      : goldenImagesPath;

  final goldenId = _createGoldenImageIdFromReport(report);

  final data = <String, String>{};
  for (var image in report.images) {
    if (image.goldenCompatible == true) {
      data[image.id] = image.hash;
    }
  }
  final golden = GoldenTestImages(
    deviceInfo: report.deviceInfo!,
    goldenHashes: data,
    suiteName: report.suiteName,
    testName: report.name!,
    testVersion: report.version,
  );

  const batchSize = 10;
  final batch = <Future>[];

  final hashes = <String>{};

  final images = List.from(report.images);

  images.removeWhere((image) {
    var found = false;

    for (var e in _tree.entries!) {
      if (e.path == '$actualPath/$goldenId/${image.hash}.png') {
        found = true;
        break;
      }
    }

    return !found;
  });

  for (var image in report.images) {
    if (image.goldenCompatible) {
      if (!hashes.contains(image.hash)) {
        hashes.add(image.hash);
        final path = '$actualPath/$goldenId/${image.hash}.png';
        final entry = _tree.entries!.where((e) => e.path == path);

        if (entry.isNotEmpty) {
          final first = entry.first;
          final hash1 = hex.encode(Digest('SHA-1').process(image.image!));

          if (first.sha == hash1) {
            // Contents are the same, don't bother updating it.
            continue;
          }
        }

        batch.add(uploadFile(
          data: image.image!,
          message: 'Golden Image Update',
          path: path,
        ));

        if (batch.length >= batchSize) {
          await Future.wait(batch);
          batch.clear();
        }
      }
    }
  }

  batch.add(uploadFile(
    data: utf8.encode(golden.toString()),
    message: 'Golden Image Update',
    path: '$actualPath/$goldenId.json',
  ));

  await Future.wait(batch);
  batch.clear();

  _dirty = true;
}