Allow Flutter golden file tests to be flaky (#114450)

* allow marking a golden check as flaky

* add matchesFlutterGolden to analyze.dart; no tags for flutter_goldens/lib

* Pause

* ++

* ++

* Analyzer therapy

* Once more with feeling

* Nits

* Review feedback

* Silly oops

* Test progress

* More tests

* Finish

* Nits

* Analyzer

* Review feedback

Co-authored-by: Yegor Jbanov <yjbanov@google.com>
This commit is contained in:
Kate Lovett
2022-11-07 19:14:03 -06:00
committed by GitHub
parent 139b8f458d
commit 53e6876226
35 changed files with 2093 additions and 1351 deletions

View File

@@ -8,7 +8,7 @@ import 'dart:typed_data';
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_goldens/flutter_goldens.dart';
import 'package:flutter_goldens/src/flutter_goldens_io.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:platform/platform.dart';

View File

@@ -418,8 +418,8 @@ Future<void> verifyNoSyncAsyncStar(String workingDirectory, {int minimumMatches
}
}
final RegExp _findGoldenTestPattern = RegExp(r'matchesGoldenFile\(');
final RegExp _findGoldenDefinitionPattern = RegExp(r'matchesGoldenFile\(Object');
final RegExp _findGoldenTestPattern = RegExp(r'(matchesGoldenFile|expectFlakyGolden)\(');
final RegExp _findGoldenDefinitionPattern = RegExp(r'(matchesGoldenFile|expectFlakyGolden)\(Object');
final RegExp _leadingComment = RegExp(r'//');
final RegExp _goldenTagPattern1 = RegExp(r'@Tags\(');
final RegExp _goldenTagPattern2 = RegExp(r"'reduced-test-set'");
@@ -431,8 +431,17 @@ const String _ignoreGoldenTag = '// flutter_ignore: golden_tag (see analyze.dart
const String _ignoreGoldenTagForFile = '// flutter_ignore_for_file: golden_tag (see analyze.dart)';
Future<void> verifyGoldenTags(String workingDirectory, { int minimumMatches = 2000 }) async {
// Skip flutter_goldens/lib because this library uses `matchesGoldenFile`
// but is not itself a test that needs tags.
final String flutterGoldensPackageLib = path.join(flutterPackages, 'flutter_goldens', 'lib');
bool isWithinFlutterGoldenLib(File file) {
return path.isWithin(flutterGoldensPackageLib, file.path);
}
final List<String> errors = <String>[];
await for (final File file in _allFiles(workingDirectory, 'dart', minimumMatches: minimumMatches)) {
final Stream<File> allTestFiles = _allFiles(workingDirectory, 'dart', minimumMatches: minimumMatches)
.where((File file) => !isWithinFlutterGoldenLib(file));
await for (final File file in allTestFiles) {
bool needsTag = false;
bool hasTagNotation = false;
bool hasReducedTag = false;

View File

@@ -0,0 +1,16 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This would fail analysis, but it is ignored
// flutter_ignore_for_file: golden_tag (see analyze.dart)
@Tags(<String>['some-other-tag'])
import 'package:test/test.dart';
import 'golden_class.dart';
void main() {
expectFlakyGolden('key', 'String');
}

View File

@@ -0,0 +1,14 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// The reduced test set tag is missing. This should fail analysis.
@Tags(<String>['some-other-tag'])
import 'package:test/test.dart';
import 'golden_class.dart';
void main() {
expectFlakyGolden('finder', 'missing_tag.png');
}

View File

@@ -0,0 +1,11 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// The tag is missing. This should fail analysis.
import 'golden_class.dart';
void main() {
expectFlakyGolden('key', 'missing_tag.png');
}

View File

@@ -7,3 +7,7 @@
void matchesGoldenFile(Object key) {
return;
}
void expectFlakyGolden(Object key, String string){
return;
}

View File

@@ -35,8 +35,11 @@
/// ```
/// {@end-tool}
///
/// expectFlakyGolden(a, b)
// Other comments
// matchesGoldenFile('comment.png');
// expectFlakyGolden(a, b);
String literal = 'matchesGoldenFile()'; // flutter_ignore: golden_tag (see analyze.dart)
String flakyLiteral = 'expectFlakyGolden';

View File

@@ -79,7 +79,9 @@ void main() {
'at the top of the file before import statements.';
const String missingTag = "Files containing golden tests must be tagged with 'reduced-test-set'.";
final List<String> lines = <String>[
'║ test/analyze-test-input/root/packages/foo/flaky_golden_no_tag.dart: $noTag',
'║ test/analyze-test-input/root/packages/foo/golden_missing_tag.dart: $missingTag',
'║ test/analyze-test-input/root/packages/foo/flaky_golden_missing_tag.dart: $missingTag',
'║ test/analyze-test-input/root/packages/foo/golden_no_tag.dart: $noTag',
]
.map((String line) => line.replaceAll('/', Platform.isWindows ? r'\' : '/'))

View File

@@ -17,6 +17,7 @@ const double todoCost = 1009.0; // about two average SWE days, in dollars
const double ignoreCost = 2003.0; // four average SWE days, in dollars
const double pythonCost = 3001.0; // six average SWE days, in dollars
const double skipCost = 2473.0; // 20 hours: 5 to fix the issue we're ignoring, 15 to fix the bugs we missed because the test was off
const double flakyGoldenCost = 2467.0; // Similar to skip cost
const double ignoreForFileCost = 2477.0; // similar thinking as skipCost
const double asDynamicCost = 2011.0; // a few days to refactor the code.
const double deprecationCost = 233.0; // a few hours to remove the old code.
@@ -69,6 +70,9 @@ Future<double> findCostsForFile(File file) async {
if (isTest && line.contains('skip:') && !line.contains('[intended]')) {
total += skipCost;
}
if (isTest && line.contains('expectFlakyGolden(')) {
total += flakyGoldenCost;
}
if (isDart && isOptingOutOfNullSafety(line)) {
total += fileNullSafetyMigrationCost;
}