Skip to content

Commit

Permalink
migrate canonicalization_test from integration (#4615)
Browse files Browse the repository at this point in the history
* migrate `canonicalization_test` from integration

* fmt
  • Loading branch information
pq authored Jul 25, 2023
1 parent e78e0ce commit def2900
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 129 deletions.
2 changes: 2 additions & 0 deletions test/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:analyzer/src/lint/io.dart';

import 'annotation_test.dart' as annotation_test;
import 'ascii_utils_test.dart' as ascii_utils_test;
import 'canonicalization_test.dart' as canonicalization_test;
import 'doc_test.dart' as doc_test;
import 'engine_test.dart' as engine_test;
import 'formatter_test.dart' as formatter_test;
Expand Down Expand Up @@ -34,6 +35,7 @@ void main() {

annotation_test.main();
ascii_utils_test.main();
canonicalization_test.main();
doc_test.main();
engine_test.main();
formatter_test.main();
Expand Down
41 changes: 41 additions & 0 deletions test/canonicalization_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2023, 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 'package:test_reflective_loader/test_reflective_loader.dart';

import 'rule_test_support.dart';

main() {
defineReflectiveSuite(() {
defineReflectiveTests(CanonicalizationTest);
});
}

@reflectiveTest
class CanonicalizationTest extends PubPackageResolutionTest {
// ignore: non_constant_identifier_names
test_canonicalizedPathResolution() async {
/// https://github.com/dart-lang/linter/commit/fcb8c9093c9c7a7cfabe27e17d40bd09d4c408f7
newFile('$testPackageLibPath/lib3.dart', r'''
C g() => C();
class C {}
''');
newFile('$testPackageLibPath/lib2.dart', r'''
import 'lib3.dart';
void f(C c) {}
''');

await assertNoDiagnostics(r'''
import 'package:test/lib2.dart';
import 'lib3.dart';
void test() {
f(g());
}
''');
}
}
15 changes: 0 additions & 15 deletions test/integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,6 @@ void coreTests() {
});
});

group('canonicalization', () {
var currentOut = outSink;
var collectingOut = CollectingSink();
setUp(() => outSink = collectingOut);
tearDown(() {
collectingOut.buffer.clear();
outSink = currentOut;
});
test('no warnings due to bad canonicalization', () async {
await cli.runLinter(['$integrationTestDir/p4'], LinterOptions([]));
expect(collectingOut.trim(),
startsWith('3 files analyzed, 0 issues found, in'));
});
});

group('examples', () {
test('all.yaml', () {
var src = readFile('example/all.yaml');
Expand Down
138 changes: 69 additions & 69 deletions test/rule_test_support.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,6 @@ mixin LanguageVersion219Mixin on PubPackageResolutionTest {
}

abstract class LintRuleTest extends PubPackageResolutionTest {
bool get dumpAstOnFailures => true;

String get lintRule;

@override
Expand All @@ -154,6 +152,44 @@ abstract class LintRuleTest extends PubPackageResolutionTest {
return [ruleName];
}

ExpectedLint lint(int offset, int length, {Pattern? messageContains}) =>
ExpectedLint(lintRule, offset, length, messageContains: messageContains);
}

class PubPackageResolutionTest extends _ContextResolutionTest {
final List<String> _lintRules = const [];

bool get addFlutterPackageDep => false;

bool get addJsPackageDep => false;

bool get addMetaPackageDep => false;

@override
List<String> get collectionIncludedPaths => [workspaceRootPath];

bool get dumpAstOnFailures => true;

List<String> get experiments => [];

/// The path that is not in [workspaceRootPath], contains external packages.
String get packagesRootPath => '/packages';

String get testFileName => 'test.dart';

@override
String get testFilePath => '$testPackageLibPath/$testFileName';

String? get testPackageLanguageVersion => null;

String get testPackageLibPath => '$testPackageRootPath/lib';

String get testPackagePubspecPath => '$testPackageRootPath/pubspec.yaml';

String get testPackageRootPath => '$workspaceRootPath/test';

String get workspaceRootPath => '/home';

/// Assert that the number of diagnostics that have been gathered matches the
/// number of [expectedDiagnostics] and that they have the expected error
/// descriptions and locations. The order in which the diagnostics were
Expand Down Expand Up @@ -298,73 +334,6 @@ abstract class LintRuleTest extends PubPackageResolutionTest {
await assertDiagnosticsIn(errors, expectedDiagnostics);
}

ExpectedLint lint(int offset, int length, {Pattern? messageContains}) =>
ExpectedLint(lintRule, offset, length, messageContains: messageContains);

Future<List<AnalysisError>> _resolvePubspecFile(String content) async {
var path = convertPath(testPackagePubspecPath);
var pubspecRules = <LintRule, PubspecVisitor<Object?>>{};
for (var rule in Registry.ruleRegistry
.where((rule) => _lintRules.contains(rule.name))) {
var visitor = rule.getPubspecVisitor();
if (visitor != null) {
pubspecRules[rule] = visitor;
}
}

if (pubspecRules.isEmpty) {
throw UnsupportedError(
'Resolving pubspec files only supported with rules with '
'PubspecVisitors.');
}

var sourceUri = resourceProvider.pathContext.toUri(path);
var pubspecAst = Pubspec.parse(content,
sourceUrl: sourceUri, resourceProvider: resourceProvider);
var listener = RecordingErrorListener();
var reporter = ErrorReporter(
listener, resourceProvider.getFile(path).createSource(sourceUri),
isNonNullableByDefault: false);
for (var entry in pubspecRules.entries) {
entry.key.reporter = reporter;
pubspecAst.accept(entry.value);
}
return [...listener.errors];
}
}

class PubPackageResolutionTest extends _ContextResolutionTest {
final List<String> _lintRules = const [];

bool get addFlutterPackageDep => false;

bool get addJsPackageDep => false;

bool get addMetaPackageDep => false;

@override
List<String> get collectionIncludedPaths => [workspaceRootPath];

List<String> get experiments => [];

/// The path that is not in [workspaceRootPath], contains external packages.
String get packagesRootPath => '/packages';

String get testFileName => 'test.dart';

@override
String get testFilePath => '$testPackageLibPath/$testFileName';

String? get testPackageLanguageVersion => null;

String get testPackageLibPath => '$testPackageRootPath/lib';

String get testPackagePubspecPath => '$testPackageRootPath/pubspec.yaml';

String get testPackageRootPath => '$workspaceRootPath/test';

String get workspaceRootPath => '/home';

@override
@mustCallSuper
void setUp() {
Expand Down Expand Up @@ -447,6 +416,37 @@ class PubPackageResolutionTest extends _ContextResolutionTest {
newPubspecYamlFile(testPackageRootPath, config.toContent());
}

Future<List<AnalysisError>> _resolvePubspecFile(String content) async {
var path = convertPath(testPackagePubspecPath);
var pubspecRules = <LintRule, PubspecVisitor<Object?>>{};
for (var rule in Registry.ruleRegistry
.where((rule) => _lintRules.contains(rule.name))) {
var visitor = rule.getPubspecVisitor();
if (visitor != null) {
pubspecRules[rule] = visitor;
}
}

if (pubspecRules.isEmpty) {
throw UnsupportedError(
'Resolving pubspec files only supported with rules with '
'PubspecVisitors.');
}

var sourceUri = resourceProvider.pathContext.toUri(path);
var pubspecAst = Pubspec.parse(content,
sourceUrl: sourceUri, resourceProvider: resourceProvider);
var listener = RecordingErrorListener();
var reporter = ErrorReporter(
listener, resourceProvider.getFile(path).createSource(sourceUri),
isNonNullableByDefault: false);
for (var entry in pubspecRules.entries) {
entry.key.reporter = reporter;
pubspecAst.accept(entry.value);
}
return [...listener.errors];
}

/// Create a fake 'flutter' package that can be used by tests.
static void addFlutterPackageFiles(Folder rootFolder) {
var libFolder = rootFolder.getChildAssumingFolder('lib');
Expand Down
14 changes: 0 additions & 14 deletions test_data/integration/p4/.dart_tool/package_config.json

This file was deleted.

13 changes: 0 additions & 13 deletions test_data/integration/p4/lib/lib1.dart

This file was deleted.

9 changes: 0 additions & 9 deletions test_data/integration/p4/lib/lib2.dart

This file was deleted.

9 changes: 0 additions & 9 deletions test_data/integration/p4/lib/lib3.dart

This file was deleted.

0 comments on commit def2900

Please sign in to comment.