Skip to content

Commit

Permalink
Update Gold to fallback on skipping comparator when offline (#44619)
Browse files Browse the repository at this point in the history
  • Loading branch information
Piinks committed Nov 15, 2019
1 parent e3ae7fa commit b1ca7f4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
50 changes: 36 additions & 14 deletions packages/flutter_goldens/lib/flutter_goldens.dart
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'dart:async';
import 'dart:io' as io;
import 'dart:typed_data';

import 'package:file/file.dart';
Expand Down Expand Up @@ -30,11 +31,14 @@ Future<void> main(FutureOr<void> testMain()) async {
goldenFileComparator = await FlutterSkiaGoldFileComparator.fromDefaultComparator(platform);
} else if (FlutterPreSubmitFileComparator.isAvailableForEnvironment(platform)) {
goldenFileComparator = await FlutterPreSubmitFileComparator.fromDefaultComparator(platform);
} else if (FlutterSkippingGoldenFileComparator.isAvailableForEnvironment(platform)){
goldenFileComparator = FlutterSkippingGoldenFileComparator.fromDefaultComparator();
} else if (FlutterSkippingGoldenFileComparator.isAvailableForEnvironment(platform)) {
goldenFileComparator = FlutterSkippingGoldenFileComparator.fromDefaultComparator(
'Golden file testing is unavailable on LUCI and some Cirrus shards.'
);
} else {
goldenFileComparator = await FlutterLocalFileComparator.fromDefaultComparator(platform);
}

await testMain();
}

Expand Down Expand Up @@ -74,7 +78,8 @@ Future<void> main(FutureOr<void> testMain()) async {
/// The [FlutterSkippingGoldenFileComparator] is utilized to skip tests outside
/// of the appropriate environments. Currently, tests executing in post-submit
/// on the LUCI build environment are skipped, as post-submit checks are done
/// on Cirrus.
/// on Cirrus. This comparator is also used when an internet connection is
/// unavailable.
abstract class FlutterGoldenFileComparator extends GoldenFileComparator {
/// Creates a [FlutterGoldenFileComparator] that will resolve golden file
/// URIs relative to the specified [basedir], and retrieve golden baselines
Expand Down Expand Up @@ -349,7 +354,8 @@ class FlutterPreSubmitFileComparator extends FlutterGoldenFileComparator {
/// conditions that do not execute golden file tests.
///
/// Currently, this comparator is used in post-submit checks on LUCI and with
/// some Cirrus shards that do not run framework tests.
/// some Cirrus shards that do not run framework tests. This comparator is also
/// used when an internet connection is not available for contacting Gold.
///
/// See also:
///
Expand All @@ -370,25 +376,32 @@ class FlutterSkippingGoldenFileComparator extends FlutterGoldenFileComparator {
FlutterSkippingGoldenFileComparator(
final Uri basedir,
final SkiaGoldClient skiaClient,
) : super(basedir, skiaClient);
this.reason,
) : assert(reason != null),
super(basedir, skiaClient);

/// Describes the reason for using the [FlutterSkippingGoldenFileComparator].
///
/// Cannot be null.
final String reason;

/// Creates a new [FlutterSkippingGoldenFileComparator] that mirrors the
/// relative path resolution of the default [goldenFileComparator].
static FlutterSkippingGoldenFileComparator fromDefaultComparator({
static FlutterSkippingGoldenFileComparator fromDefaultComparator(
String reason, {
LocalFileComparator defaultComparator,
}) {
defaultComparator ??= goldenFileComparator;
const FileSystem fs = LocalFileSystem();
final Uri basedir = defaultComparator.basedir;
final SkiaGoldClient skiaClient = SkiaGoldClient(fs.directory(basedir));
return FlutterSkippingGoldenFileComparator(basedir, skiaClient);
return FlutterSkippingGoldenFileComparator(basedir, skiaClient, reason);
}

@override
Future<bool> compare(Uint8List imageBytes, Uri golden) async {
print(
'Skipping "$golden" test : Golden file testing is unavailable on LUCI and '
'some Cirrus shards.'
'Skipping "$golden" test : $reason'
);
return true;
}
Expand Down Expand Up @@ -451,16 +464,16 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC
/// Creates a new [FlutterLocalFileComparator] that mirrors the
/// relative path resolution of the default [goldenFileComparator].
///
/// The [goldens] and [defaultComparator] parameters are visible for testing
/// purposes only.
/// The [goldens], [defaultComparator], and [baseDirectory] parameters are
/// visible for testing purposes only.
static Future<FlutterGoldenFileComparator> fromDefaultComparator(
final Platform platform, {
SkiaGoldClient goldens,
LocalFileComparator defaultComparator,
Directory baseDirectory,
}) async {

defaultComparator ??= goldenFileComparator;
final Directory baseDirectory = FlutterGoldenFileComparator.getBaseDirectory(
baseDirectory ??= FlutterGoldenFileComparator.getBaseDirectory(
defaultComparator,
platform,
);
Expand All @@ -470,7 +483,16 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC
}

goldens ??= SkiaGoldClient(baseDirectory);
await goldens.getExpectations();

try {
await goldens.getExpectations();
} on io.OSError catch (_) {
return FlutterSkippingGoldenFileComparator(
baseDirectory.uri,
goldens,
'No network connection available for contacting Gold.',
);
}

return FlutterLocalFileComparator(baseDirectory.uri, goldens);
}
Expand Down
16 changes: 16 additions & 0 deletions packages/flutter_goldens/test/flutter_goldens_test.dart
Expand Up @@ -762,6 +762,20 @@ void main() {
shouldThrow = false;
completer.complete(Future<bool>.value(false));
});

test('returns FlutterSkippingGoldenFileComparator when network connection is unavailable', () async {
final MockDirectory mockDirectory = MockDirectory();
when(mockDirectory.existsSync()).thenReturn(true);
when(mockDirectory.uri).thenReturn(Uri.parse('/flutter'));
when(mockSkiaClient.getExpectations())
.thenAnswer((_) => throw const OSError());
final FlutterGoldenFileComparator comparator = await FlutterLocalFileComparator.fromDefaultComparator(
platform,
goldens: mockSkiaClient,
baseDirectory: mockDirectory,
);
expect(comparator.runtimeType, FlutterSkippingGoldenFileComparator);
});
});
});
}
Expand All @@ -772,6 +786,8 @@ class MockSkiaGoldClient extends Mock implements SkiaGoldClient {}

class MockLocalFileComparator extends Mock implements LocalFileComparator {}

class MockDirectory extends Mock implements Directory {}

class MockHttpClient extends Mock implements HttpClient {}

class MockHttpClientRequest extends Mock implements HttpClientRequest {}
Expand Down

0 comments on commit b1ca7f4

Please sign in to comment.