Files
sdk/pkg/analyzer/test/error/error_test.dart
T
Sigmund Cherem a7175938c3 Remove use of package_root in analyzer/test/error_test
context: the package_root logic is not portable to how we run this test outside
the sdk in other build systems like bazel. To make it work in bazel, we
currently use const.fromEnvironment to override the location of the package
root. Due to upcoming changes to constant evaluation we can't continue using
const.fromEnvironment.

I considered 3 other options:
 * using Platform.environment
 * changing platform_root to be more sophisticated (e.g. look for a folder
 containing `test`, instead of looking for `pkg`)
 * directly defining the root at the beginning of main based on Platform.script

The latter works out of the box in bazel and is also pretty simple, so I felt it
was a good choice.

Note: there are several other tests depending on package_root that we can update
in a similar fashion. Today this is the only test that depends on package_root
that we are running internally in bazel.
Change-Id: Ie7b79a82c78605048b26b9676eac2fbd8b15aaa7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/98879
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Sigmund Cherem <sigmund@google.com>
2019-04-09 16:46:57 +00:00

100 lines
3.5 KiB
Dart

// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:core';
import 'dart:io';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:path/path.dart' as path;
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../generated/parser_test.dart';
List<String> _analyzerRootComponents;
main() {
_analyzerRootComponents =
path.split(path.fromUri(Platform.script.resolve("../../")));
defineReflectiveSuite(() {
defineReflectiveTests(ErrorCodeValuesTest);
});
}
@reflectiveTest
class ErrorCodeValuesTest extends ParserTestCase {
bool bad() {
return false;
}
List<String> getDeclaredCodes(List<String> relativeComponents) {
List<String> declaredCodes = <String>[];
CompilationUnit definingUnit = parseFile(relativeComponents);
for (CompilationUnitMember declaration in definingUnit.declarations) {
if (declaration is ClassDeclaration) {
ExtendsClause extendsClause = declaration.extendsClause;
if (extendsClause != null &&
extendsClause.superclass.name.name == 'ErrorCode') {
String className = declaration.name.name;
for (ClassMember member in declaration.members) {
if (member is FieldDeclaration &&
member.isStatic &&
(member.fields.type == null ? bad() : true) &&
member.fields.type.toSource() == className) {
String fieldName = member.fields.variables[0].name.name;
declaredCodes.add(className + '.' + fieldName);
}
}
}
}
}
return declaredCodes;
}
List<String> getListedCodes() {
List<String> listedCodes = <String>[];
CompilationUnit listingUnit = parseFile(['lib', 'error', 'error.dart']);
TopLevelVariableDeclaration declaration = listingUnit.declarations
.firstWhere(
(member) =>
member is TopLevelVariableDeclaration &&
member.variables.variables[0].name.name == 'errorCodeValues',
orElse: () => null);
ListLiteral listLiteral = declaration.variables.variables[0].initializer;
for (PrefixedIdentifier element in listLiteral.elements) {
listedCodes.add(element.name);
}
return listedCodes;
}
CompilationUnit parseFile(List<String> relativeComponents) {
List<String> pathComponents = _analyzerRootComponents.toList()
..addAll(relativeComponents);
String filePath = path.normalize(path.joinAll(pathComponents));
return parseCompilationUnit(new File(filePath).readAsStringSync());
}
test_errorCodeValues() {
List<String> listedCodes = getListedCodes();
List<String> missingCodes = <String>[];
List<List<String>> declaringPaths = [
['lib', 'src', 'analysis_options', 'error', 'option_codes.dart'],
['lib', 'src', 'dart', 'error', 'hint_codes.dart'],
['lib', 'src', 'dart', 'error', 'lint_codes.dart'],
['lib', 'src', 'dart', 'error', 'todo_codes.dart'],
['lib', 'src', 'dart', 'error', 'syntactic_errors.dart'],
['lib', 'src', 'error', 'codes.dart'],
['..', 'front_end', 'lib', 'src', 'scanner', 'errors.dart']
];
for (List<String> path in declaringPaths) {
for (String declaredCode in getDeclaredCodes(path)) {
if (!listedCodes.contains(declaredCode)) {
missingCodes.add(declaredCode);
}
}
}
expect(missingCodes, isEmpty);
}
}