Add tag support for executing reduced test sets (#86821)

This commit is contained in:
Kate Lovett
2021-08-30 17:41:02 -05:00
committed by GitHub
parent 4e1711135d
commit 616f9bcffa
70 changed files with 495 additions and 42 deletions

View File

@@ -23,6 +23,7 @@ import 'utils.dart';
final String flutterRoot = path.dirname(path.dirname(path.dirname(path.fromUri(Platform.script))));
final String flutter = path.join(flutterRoot, 'bin', Platform.isWindows ? 'flutter.bat' : 'flutter');
final String flutterPackages = path.join(flutterRoot, 'packages');
final String dart = path.join(flutterRoot, 'bin', 'cache', 'dart-sdk', 'bin', Platform.isWindows ? 'dart.exe' : 'dart');
final String pub = path.join(flutterRoot, 'bin', 'cache', 'dart-sdk', 'bin', Platform.isWindows ? 'pub.bat' : 'pub');
final String pubCache = path.join(flutterRoot, '.pub-cache');
@@ -65,6 +66,9 @@ Future<void> run(List<String> arguments) async {
print('$clock Deprecations...');
await verifyDeprecations(flutterRoot);
print('$clock Goldens...');
await verifyGoldenTags(flutterPackages);
print('$clock Skip test comments...');
await verifySkipTestComments(flutterRoot);
@@ -146,6 +150,72 @@ Future<void> run(List<String> arguments) async {
// TESTS
final RegExp _findGoldenTestPattern = RegExp(r'matchesGoldenFile\(');
final RegExp _findGoldenDefinitionPattern = RegExp(r'matchesGoldenFile\(Object');
final RegExp _leadingComment = RegExp(r'\/\/');
final RegExp _goldenTagPattern1 = RegExp(r'@Tags\(');
final RegExp _goldenTagPattern2 = RegExp(r"'reduced-test-set'");
/// Only golden file tests in the flutter package are subject to reduced testing,
/// for example, invocations in flutter_test to validate comparator
/// functionality do not require tagging.
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 {
final List<String> errors = <String>[];
await for (final File file in _allFiles(workingDirectory, 'dart', minimumMatches: minimumMatches)) {
bool needsTag = false;
bool hasTagNotation = false;
bool hasReducedTag = false;
bool ignoreForFile = false;
final List<String> lines = file.readAsLinesSync();
for (final String line in lines) {
if (line.contains(_goldenTagPattern1)) {
hasTagNotation = true;
}
if (line.contains(_goldenTagPattern2)) {
hasReducedTag = true;
}
if (line.contains(_findGoldenTestPattern)
&& !line.contains(_findGoldenDefinitionPattern)
&& !line.contains(_leadingComment)
&& !line.contains(_ignoreGoldenTag)) {
needsTag = true;
}
if (line.contains(_ignoreGoldenTagForFile)) {
ignoreForFile = true;
}
// If the file is being ignored or a reduced test tag is already accounted
// for, skip parsing the rest of the lines for golden file tests.
if (ignoreForFile || (hasTagNotation && hasReducedTag)) {
break;
}
}
// If a reduced test tag is already accounted for, move on to the next file.
if (ignoreForFile || (hasTagNotation && hasReducedTag)) {
continue;
}
// If there are golden file tests, ensure they are tagged for all reduced
// test environments.
if (needsTag) {
if (!hasTagNotation) {
errors.add('${file.path}: Files containing golden tests must be tagged using '
'`@Tags(...)` at the top of the file before import statements.');
} else if (!hasReducedTag) {
errors.add('${file.path}: Files containing golden tests must be tagged with '
"'reduced-test-set'.");
}
}
}
if (errors.isNotEmpty) {
exitWithError(<String>[
...errors,
'${bold}See: https://github.com/flutter/flutter/wiki/Writing-a-golden-file-test-for-package:flutter$reset',
]);
}
}
final RegExp _findDeprecationPattern = RegExp(r'@[Dd]eprecated');
final RegExp _deprecationPattern1 = RegExp(r'^( *)@Deprecated\($'); // flutter_ignore: deprecation_syntax (see analyze.dart)
final RegExp _deprecationPattern2 = RegExp(r"^ *'(.+) '$");

View File

@@ -1565,10 +1565,18 @@ Future<void> _runFlutterTest(String workingDirectory, {
}) async {
assert(!printOutput || outputChecker == null, 'Output either can be printed or checked but not both');
final List<String> tags = <String>[];
// Recipe configured reduced test shards will only execute tests with the
// appropriate tag.
if ((Platform.environment['REDUCED_TEST_SET'] ?? 'False') == 'True') {
tags.addAll(<String>['-t', 'reduced-test-set']);
}
final List<String> args = <String>[
'test',
if (shuffleTests) '--test-randomize-ordering-seed=$shuffleSeed',
...options,
...tags,
...flutterTestArgs,
];

View File

@@ -0,0 +1,9 @@
// 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.
/// Mack class definition for testing.
/// Should not be tag-enforced, no analysis failures should be found.
void matchesGoldenFile(Object key) {
return;
}

View File

@@ -0,0 +1,42 @@
// 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.
/// Sample Code
///
/// Should not be tag-enforced, no analysis failures should be found.
///
/// {@tool snippet}
/// Sample invocations of [matchesGoldenFile].
///
/// ```dart
/// await expectLater(
/// find.text('Save'),
/// matchesGoldenFile('save.png'),
/// );
///
/// await expectLater(
/// image,
/// matchesGoldenFile('save.png'),
/// );
///
/// await expectLater(
/// imageFuture,
/// matchesGoldenFile(
/// 'save.png',
/// version: 2,
/// ),
/// );
///
/// await expectLater(
/// find.byType(MyWidget),
/// matchesGoldenFile('goldens/myWidget.png'),
/// );
/// ```
/// {@end-tool}
///
// Other comments
// matchesGoldenFile('comment.png');
String literal = 'matchesGoldenFile()'; // flutter_ignore: golden_tag (see analyze.dart)

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() {
matchesGoldenFile('missing_tag.png');
}

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() {
matchesGoldenFile('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() {
matchesGoldenFile('missing_tag.png');
}

View File

@@ -73,6 +73,51 @@ void main() {
);
});
test('analyze.dart - verifyGoldenTags', () async {
final String result = await capture(() => verifyGoldenTags(testRootPath, minimumMatches: 6), exitCode: 1);
const String noTag = 'Files containing golden tests must be '
'tagged using `@Tags(...)` at the top of the file before import statements.';
const String missingTag = 'Files containing golden tests must be '
"tagged with 'reduced-test-set'.";
String lines = <String>[
'test/analyze-test-input/root/packages/foo/golden_missing_tag.dart: $missingTag',
'test/analyze-test-input/root/packages/foo/golden_no_tag.dart: $noTag',
]
.map((String line) {
return line
.replaceAll('/', Platform.isWindows ? r'\' : '/');
})
.join('\n');
try {
expect(
result,
'━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n'
'$lines\n'
'See: https://github.com/flutter/flutter/wiki/Writing-a-golden-file-test-for-package:flutter\n'
'━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n'
);
} catch (_) {
// This list of files may come up in one order or the other.
lines = <String>[
'test/analyze-test-input/root/packages/foo/golden_no_tag.dart: $noTag',
'test/analyze-test-input/root/packages/foo/golden_missing_tag.dart: $missingTag',
]
.map((String line) {
return line
.replaceAll('/', Platform.isWindows ? r'\' : '/');
})
.join('\n');
expect(
result,
'━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n'
'$lines\n'
'See: https://github.com/flutter/flutter/wiki/Writing-a-golden-file-test-for-package:flutter\n'
'━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n'
);
}
});
test('analyze.dart - verifyNoMissingLicense', () async {
final String result = await capture(() => verifyNoMissingLicense(testRootPath, checkMinimums: false), exitCode: 1);
final String lines = 'test/analyze-test-input/root/packages/foo/foo.dart'