Unverified Commit 807ca68d authored by Darren Austin's avatar Darren Austin Committed by GitHub

Enhance the skip test parsing the analyzer script. (#88894)

parent cff3db1d
...@@ -8,6 +8,11 @@ import 'dart:core' hide print; ...@@ -8,6 +8,11 @@ import 'dart:core' hide print;
import 'dart:io' hide exit; import 'dart:io' hide exit;
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/analysis/utilities.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:crypto/crypto.dart'; import 'package:crypto/crypto.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'package:path/path.dart' as path; import 'package:path/path.dart' as path;
...@@ -283,25 +288,66 @@ Future<void> _verifyNoMissingLicenseForExtension(String workingDirectory, String ...@@ -283,25 +288,66 @@ Future<void> _verifyNoMissingLicenseForExtension(String workingDirectory, String
} }
} }
final RegExp _skipTestCommentPattern = RegExp(r'\bskip:.*?//(.*)'); class _TestSkip {
_TestSkip(this.line, this.content);
final int line;
final String content;
}
Iterable<_TestSkip> _getTestSkips(File file) {
final ParseStringResult parseResult = parseFile(
featureSet: FeatureSet.latestLanguageVersion(),
path: file.absolute.path,
);
final _TestSkipLinesVisitor<CompilationUnit> visitor = _TestSkipLinesVisitor<CompilationUnit>(parseResult);
visitor.visitCompilationUnit(parseResult.unit);
return visitor.skips;
}
class _TestSkipLinesVisitor<T> extends RecursiveAstVisitor<T> {
_TestSkipLinesVisitor(this.parseResult) : skips = <_TestSkip>{};
final ParseStringResult parseResult;
final Set<_TestSkip> skips;
static bool isTestMethod(String name) {
return name.startsWith('test') || name == 'group' || name == 'expect';
}
@override
T? visitMethodInvocation(MethodInvocation node) {
if (isTestMethod(node.methodName.toString())) {
for (final Expression argument in node.argumentList.arguments) {
if (argument is NamedExpression && argument.name.label.name == 'skip') {
final int lineNumber = parseResult.lineInfo.getLocation(argument.beginToken.charOffset).lineNumber;
final String content = parseResult.content.substring(parseResult.lineInfo.getOffsetOfLine(lineNumber - 1),
parseResult.lineInfo.getOffsetOfLine(lineNumber) - 1);
skips.add(_TestSkip(lineNumber, content));
}
}
}
return super.visitMethodInvocation(node);
}
}
final RegExp _skipTestCommentPattern = RegExp(r'//(.*)$');
const Pattern _skipTestIntentionalPattern = '[intended]'; const Pattern _skipTestIntentionalPattern = '[intended]';
final Pattern _skipTestTrackingBugPattern = RegExp(r'https+?://github.com/.*/issues/[0-9]+'); final Pattern _skipTestTrackingBugPattern = RegExp(r'https+?://github.com/.*/issues/[0-9]+');
Future<void> verifySkipTestComments(String workingDirectory) async { Future<void> verifySkipTestComments(String workingDirectory) async {
final List<String> errors = <String>[]; final List<String> errors = <String>[];
final Stream<File> testFiles = _allFiles(workingDirectory, 'dart', minimumMatches: 1500) final Stream<File> testFiles =_allFiles(workingDirectory, 'dart', minimumMatches: 1500)
.where((File f) => f.path.endsWith('_test.dart')); .where((File f) => f.path.endsWith('_test.dart'));
await for (final File file in testFiles) { await for (final File file in testFiles) {
final List<String> lines = file.readAsLinesSync(); for (final _TestSkip skip in _getTestSkips(file)) {
for (int index = 0; index < lines.length; index++) { final Match? match = _skipTestCommentPattern.firstMatch(skip.content);
final Match? match = _skipTestCommentPattern.firstMatch(lines[index]);
final String? skipComment = match?.group(1); final String? skipComment = match?.group(1);
if (skipComment != null if (skipComment == null ||
&& !skipComment.contains(_skipTestIntentionalPattern) !(skipComment.contains(_skipTestIntentionalPattern) ||
&& !skipComment.contains(_skipTestTrackingBugPattern)) { skipComment.contains(_skipTestTrackingBugPattern))) {
final int sourceLine = index + 1; errors.add('${file.path}:${skip.line}: skip test without a justification comment.');
errors.add('${file.path}:$sourceLine}: skip test without a justification comment.');
} }
} }
} }
......
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