Unverified Commit b1ca7f41 authored by Kate Lovett's avatar Kate Lovett Committed by GitHub

Update Gold to fallback on skipping comparator when offline (#44619)

parent e3ae7fab
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:async'; import 'dart:async';
import 'dart:io' as io;
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:file/file.dart'; import 'package:file/file.dart';
...@@ -30,11 +31,14 @@ Future<void> main(FutureOr<void> testMain()) async { ...@@ -30,11 +31,14 @@ Future<void> main(FutureOr<void> testMain()) async {
goldenFileComparator = await FlutterSkiaGoldFileComparator.fromDefaultComparator(platform); goldenFileComparator = await FlutterSkiaGoldFileComparator.fromDefaultComparator(platform);
} else if (FlutterPreSubmitFileComparator.isAvailableForEnvironment(platform)) { } else if (FlutterPreSubmitFileComparator.isAvailableForEnvironment(platform)) {
goldenFileComparator = await FlutterPreSubmitFileComparator.fromDefaultComparator(platform); goldenFileComparator = await FlutterPreSubmitFileComparator.fromDefaultComparator(platform);
} else if (FlutterSkippingGoldenFileComparator.isAvailableForEnvironment(platform)){ } else if (FlutterSkippingGoldenFileComparator.isAvailableForEnvironment(platform)) {
goldenFileComparator = FlutterSkippingGoldenFileComparator.fromDefaultComparator(); goldenFileComparator = FlutterSkippingGoldenFileComparator.fromDefaultComparator(
'Golden file testing is unavailable on LUCI and some Cirrus shards.'
);
} else { } else {
goldenFileComparator = await FlutterLocalFileComparator.fromDefaultComparator(platform); goldenFileComparator = await FlutterLocalFileComparator.fromDefaultComparator(platform);
} }
await testMain(); await testMain();
} }
...@@ -74,7 +78,8 @@ Future<void> main(FutureOr<void> testMain()) async { ...@@ -74,7 +78,8 @@ Future<void> main(FutureOr<void> testMain()) async {
/// The [FlutterSkippingGoldenFileComparator] is utilized to skip tests outside /// The [FlutterSkippingGoldenFileComparator] is utilized to skip tests outside
/// of the appropriate environments. Currently, tests executing in post-submit /// of the appropriate environments. Currently, tests executing in post-submit
/// on the LUCI build environment are skipped, as post-submit checks are done /// 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 { abstract class FlutterGoldenFileComparator extends GoldenFileComparator {
/// Creates a [FlutterGoldenFileComparator] that will resolve golden file /// Creates a [FlutterGoldenFileComparator] that will resolve golden file
/// URIs relative to the specified [basedir], and retrieve golden baselines /// URIs relative to the specified [basedir], and retrieve golden baselines
...@@ -349,7 +354,8 @@ class FlutterPreSubmitFileComparator extends FlutterGoldenFileComparator { ...@@ -349,7 +354,8 @@ class FlutterPreSubmitFileComparator extends FlutterGoldenFileComparator {
/// conditions that do not execute golden file tests. /// conditions that do not execute golden file tests.
/// ///
/// Currently, this comparator is used in post-submit checks on LUCI and with /// 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: /// See also:
/// ///
...@@ -370,25 +376,32 @@ class FlutterSkippingGoldenFileComparator extends FlutterGoldenFileComparator { ...@@ -370,25 +376,32 @@ class FlutterSkippingGoldenFileComparator extends FlutterGoldenFileComparator {
FlutterSkippingGoldenFileComparator( FlutterSkippingGoldenFileComparator(
final Uri basedir, final Uri basedir,
final SkiaGoldClient skiaClient, 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 /// Creates a new [FlutterSkippingGoldenFileComparator] that mirrors the
/// relative path resolution of the default [goldenFileComparator]. /// relative path resolution of the default [goldenFileComparator].
static FlutterSkippingGoldenFileComparator fromDefaultComparator({ static FlutterSkippingGoldenFileComparator fromDefaultComparator(
String reason, {
LocalFileComparator defaultComparator, LocalFileComparator defaultComparator,
}) { }) {
defaultComparator ??= goldenFileComparator; defaultComparator ??= goldenFileComparator;
const FileSystem fs = LocalFileSystem(); const FileSystem fs = LocalFileSystem();
final Uri basedir = defaultComparator.basedir; final Uri basedir = defaultComparator.basedir;
final SkiaGoldClient skiaClient = SkiaGoldClient(fs.directory(basedir)); final SkiaGoldClient skiaClient = SkiaGoldClient(fs.directory(basedir));
return FlutterSkippingGoldenFileComparator(basedir, skiaClient); return FlutterSkippingGoldenFileComparator(basedir, skiaClient, reason);
} }
@override @override
Future<bool> compare(Uint8List imageBytes, Uri golden) async { Future<bool> compare(Uint8List imageBytes, Uri golden) async {
print( print(
'Skipping "$golden" test : Golden file testing is unavailable on LUCI and ' 'Skipping "$golden" test : $reason'
'some Cirrus shards.'
); );
return true; return true;
} }
...@@ -451,16 +464,16 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC ...@@ -451,16 +464,16 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC
/// Creates a new [FlutterLocalFileComparator] that mirrors the /// Creates a new [FlutterLocalFileComparator] that mirrors the
/// relative path resolution of the default [goldenFileComparator]. /// relative path resolution of the default [goldenFileComparator].
/// ///
/// The [goldens] and [defaultComparator] parameters are visible for testing /// The [goldens], [defaultComparator], and [baseDirectory] parameters are
/// purposes only. /// visible for testing purposes only.
static Future<FlutterGoldenFileComparator> fromDefaultComparator( static Future<FlutterGoldenFileComparator> fromDefaultComparator(
final Platform platform, { final Platform platform, {
SkiaGoldClient goldens, SkiaGoldClient goldens,
LocalFileComparator defaultComparator, LocalFileComparator defaultComparator,
Directory baseDirectory,
}) async { }) async {
defaultComparator ??= goldenFileComparator; defaultComparator ??= goldenFileComparator;
final Directory baseDirectory = FlutterGoldenFileComparator.getBaseDirectory( baseDirectory ??= FlutterGoldenFileComparator.getBaseDirectory(
defaultComparator, defaultComparator,
platform, platform,
); );
...@@ -470,7 +483,16 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC ...@@ -470,7 +483,16 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC
} }
goldens ??= SkiaGoldClient(baseDirectory); goldens ??= SkiaGoldClient(baseDirectory);
try {
await goldens.getExpectations(); await goldens.getExpectations();
} on io.OSError catch (_) {
return FlutterSkippingGoldenFileComparator(
baseDirectory.uri,
goldens,
'No network connection available for contacting Gold.',
);
}
return FlutterLocalFileComparator(baseDirectory.uri, goldens); return FlutterLocalFileComparator(baseDirectory.uri, goldens);
} }
......
...@@ -762,6 +762,20 @@ void main() { ...@@ -762,6 +762,20 @@ void main() {
shouldThrow = false; shouldThrow = false;
completer.complete(Future<bool>.value(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);
});
}); });
}); });
} }
...@@ -772,6 +786,8 @@ class MockSkiaGoldClient extends Mock implements SkiaGoldClient {} ...@@ -772,6 +786,8 @@ class MockSkiaGoldClient extends Mock implements SkiaGoldClient {}
class MockLocalFileComparator extends Mock implements LocalFileComparator {} class MockLocalFileComparator extends Mock implements LocalFileComparator {}
class MockDirectory extends Mock implements Directory {}
class MockHttpClient extends Mock implements HttpClient {} class MockHttpClient extends Mock implements HttpClient {}
class MockHttpClientRequest extends Mock implements HttpClientRequest {} class MockHttpClientRequest extends Mock implements HttpClientRequest {}
......
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