move test utilities (#2109)
* move test utilities
* restore namespace
diff --git a/lib/src/test_utilities/annotation.dart b/lib/src/test_utilities/annotation.dart
new file mode 100644
index 0000000..a4c29f5
--- /dev/null
+++ b/lib/src/test_utilities/annotation.dart
@@ -0,0 +1,65 @@
+// Copyright (c) 2019, 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:analyzer/error/error.dart';
+import 'package:analyzer/source/line_info.dart';
+
+Annotation extractAnnotation(int lineNumber, String line) {
+ final regexp =
+ RegExp(r'(//|#) ?LINT( \[([\-+]\d+)?(,?(\d+):(\d+))?\])?( (.*))?$');
+ final match = regexp.firstMatch(line);
+ if (match == null) return null;
+
+ // ignore lints on commented out lines
+ final index = match.start;
+ final comment = match[1];
+ if (line.indexOf(comment) != index) return null;
+
+ final relativeLine = match[3].toInt() ?? 0;
+ final column = match[5].toInt();
+ final length = match[6].toInt();
+ final message = match[8].toNullIfBlank();
+ return Annotation.forLint(message, column, length)
+ ..lineNumber = lineNumber + relativeLine;
+}
+
+/// Information about a 'LINT' annotation/comment.
+class Annotation implements Comparable<Annotation> {
+ final int column;
+ final int length;
+ final String message;
+ final ErrorType type;
+ int lineNumber;
+
+ Annotation(this.message, this.type, this.lineNumber,
+ {this.column, this.length});
+
+ Annotation.forError(AnalysisError error, LineInfo lineInfo)
+ : this(error.message, error.errorCode.type,
+ lineInfo.getLocation(error.offset).lineNumber,
+ column: lineInfo.getLocation(error.offset).columnNumber,
+ length: error.length);
+
+ Annotation.forLint([String message, int column, int length])
+ : this(message, ErrorType.LINT, null, column: column, length: length);
+
+ @override
+ int compareTo(Annotation other) {
+ if (lineNumber != other.lineNumber) {
+ return lineNumber - other.lineNumber;
+ } else if (column != other.column) {
+ return column - other.column;
+ }
+ return message.compareTo(other.message);
+ }
+
+ @override
+ String toString() =>
+ '[$type]: "$message" (line: $lineNumber) - [$column:$length]';
+}
+
+extension on String {
+ int toInt() => this == null ? null : int.parse(this);
+ String toNullIfBlank() => this == null || trim().isEmpty ? null : this;
+}
diff --git a/lib/src/test_utilities/test_resource_provider.dart b/lib/src/test_utilities/test_resource_provider.dart
new file mode 100644
index 0000000..82da039
--- /dev/null
+++ b/lib/src/test_utilities/test_resource_provider.dart
@@ -0,0 +1,61 @@
+// Copyright (c) 2019, 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:io';
+
+import 'package:analyzer/file_system/file_system.dart' as file_system;
+import 'package:analyzer/file_system/memory_file_system.dart';
+import 'package:analyzer/file_system/physical_file_system.dart';
+// ignore: implementation_imports
+import 'package:analyzer/src/test_utilities/mock_sdk.dart';
+
+import '../analyzer.dart';
+
+/// Builds the [DartLinter] with appropriate mock SDK, resource providers, and
+/// package config path.
+DartLinter buildDriver(LintRule rule, File file, {String analysisOptions}) {
+ final memoryResourceProvider = MemoryResourceProvider(
+ context: PhysicalResourceProvider.INSTANCE.pathContext);
+ final resourceProvider = TestResourceProvider(memoryResourceProvider);
+
+ final pathContext = memoryResourceProvider.pathContext;
+ var packageConfigPath = memoryResourceProvider.convertPath(pathContext.join(
+ pathContext.dirname(file.absolute.path), '.mock_packages'));
+ if (!resourceProvider.getFile(packageConfigPath).exists) {
+ packageConfigPath = null;
+ }
+
+ final options = LinterOptions([rule], analysisOptions)
+ ..mockSdk = MockSdk(resourceProvider: memoryResourceProvider)
+ ..resourceProvider = resourceProvider
+ ..packageConfigPath = packageConfigPath;
+
+ return DartLinter(options);
+}
+
+/// A resource provider that accesses entities in a MemoryResourceProvider,
+/// falling back to the PhysicalResourceProvider when they don't exist.
+class TestResourceProvider extends PhysicalResourceProvider {
+ MemoryResourceProvider memoryResourceProvider;
+
+ TestResourceProvider(this.memoryResourceProvider) : super(null);
+
+ @override
+ file_system.File getFile(String path) {
+ final file = memoryResourceProvider.getFile(path);
+ return file.exists ? file : super.getFile(path);
+ }
+
+ @override
+ file_system.Folder getFolder(String path) {
+ final folder = memoryResourceProvider.getFolder(path);
+ return folder.exists ? folder : super.getFolder(path);
+ }
+
+ @override
+ file_system.Resource getResource(String path) {
+ final resource = memoryResourceProvider.getResource(path);
+ return resource.exists ? resource : super.getResource(path);
+ }
+}
diff --git a/test/rule_test.dart b/test/rule_test.dart
index ff7bcb8..7028ef8 100644
--- a/test/rule_test.dart
+++ b/test/rule_test.dart
@@ -18,14 +18,14 @@
import 'package:linter/src/rules.dart';
import 'package:linter/src/rules/implementation_imports.dart';
import 'package:linter/src/rules/package_prefixed_library_names.dart';
+import 'package:linter/src/test_utilities/annotation.dart';
+import 'package:linter/src/test_utilities/test_resource_provider.dart';
import 'package:linter/src/version.dart';
import 'package:path/path.dart' as p;
import 'package:test/test.dart';
import 'rules/experiments/experiments.dart';
-import 'util/annotation.dart';
import 'util/annotation_matcher.dart';
-import 'util/test_resource_provider.dart';
import 'util/test_utils.dart';
void main() {
diff --git a/test/util/annotation.dart b/test/util/annotation.dart
index dd2e521..f292018 100644
--- a/test/util/annotation.dart
+++ b/test/util/annotation.dart
@@ -2,65 +2,6 @@
// 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:analyzer/error/error.dart';
-import 'package:analyzer/src/generated/source.dart';
-import 'package:linter/src/analyzer.dart';
-
-Annotation extractAnnotation(int lineNumber, String line) {
- final regexp =
- RegExp(r'(//|#) ?LINT( \[([\-+]\d+)?(,?(\d+):(\d+))?\])?( (.*))?$');
- final match = regexp.firstMatch(line);
- if (match == null) return null;
-
- // ignore lints on commented out lines
- final index = match.start;
- final comment = match[1];
- if (line.indexOf(comment) != index) return null;
-
- final relativeLine = match[3].toInt() ?? 0;
- final column = match[5].toInt();
- final length = match[6].toInt();
- final message = match[8].toNullIfBlank();
- return Annotation.forLint(message, column, length)
- ..lineNumber = lineNumber + relativeLine;
-}
-
-extension on String {
- int toInt() => this == null ? null : int.parse(this);
- String toNullIfBlank() => this == null || trim().isEmpty ? null : this;
-}
-
-/// Information about a 'LINT' annotation/comment.
-class Annotation implements Comparable<Annotation> {
- final int column;
- final int length;
- final String message;
- final ErrorType type;
- int lineNumber;
-
- Annotation(this.message, this.type, this.lineNumber,
- {this.column, this.length});
-
- Annotation.forError(AnalysisError error, LineInfo lineInfo)
- : this(error.message, error.errorCode.type,
- lineInfo.getLocation(error.offset).lineNumber,
- column: lineInfo.getLocation(error.offset).columnNumber,
- length: error.length);
-
- Annotation.forLint([String message, int column, int length])
- : this(message, ErrorType.LINT, null, column: column, length: length);
-
- @override
- int compareTo(Annotation other) {
- if (lineNumber != other.lineNumber) {
- return lineNumber - other.lineNumber;
- } else if (column != other.column) {
- return column - other.column;
- }
- return message.compareTo(other.message);
- }
-
- @override
- String toString() =>
- '[$type]: "$message" (line: $lineNumber) - [$column:$length]';
-}
+@Deprecated(
+ 'Prefer importing `package:linter/src/test_utilities/annotation.dart` directly')
+export 'package:linter/src/test_utilities/annotation.dart';
diff --git a/test/util/annotation_matcher.dart b/test/util/annotation_matcher.dart
index babfe4e..50d6154 100644
--- a/test/util/annotation_matcher.dart
+++ b/test/util/annotation_matcher.dart
@@ -3,9 +3,12 @@
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/error/error.dart';
+import 'package:linter/src/test_utilities/annotation.dart';
import 'package:test/test.dart';
-import 'annotation.dart';
+AnnotationMatcher matchesAnnotation(
+ String message, ErrorType type, int lineNumber) =>
+ AnnotationMatcher(Annotation(message, type, lineNumber));
class AnnotationMatcher extends Matcher {
final Annotation _expected;
@@ -37,7 +40,3 @@
_expected.lineNumber == other.lineNumber;
}
}
-
-AnnotationMatcher matchesAnnotation(
- String message, ErrorType type, int lineNumber) =>
- AnnotationMatcher(Annotation(message, type, lineNumber));
diff --git a/test/util/annotation_test.dart b/test/util/annotation_test.dart
index ae1bd9a..ef56302 100644
--- a/test/util/annotation_test.dart
+++ b/test/util/annotation_test.dart
@@ -3,9 +3,9 @@
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/error/error.dart';
+import 'package:linter/src/test_utilities/annotation.dart';
import 'package:test/test.dart';
-import 'annotation.dart';
import 'annotation_matcher.dart';
void main() {
diff --git a/test/util/test_resource_provider.dart b/test/util/test_resource_provider.dart
index 76ab6ce..7c3fb4f 100644
--- a/test/util/test_resource_provider.dart
+++ b/test/util/test_resource_provider.dart
@@ -2,59 +2,6 @@
// 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:io';
-
-import 'package:analyzer/file_system/file_system.dart' as file_system;
-import 'package:analyzer/file_system/memory_file_system.dart';
-import 'package:analyzer/file_system/physical_file_system.dart';
-import 'package:analyzer/src/lint/linter.dart';
-import 'package:analyzer/src/test_utilities/mock_sdk.dart';
-import 'package:linter/src/analyzer.dart';
-
-/// Builds the [DartLinter] with appropriate mock SDK, resource providers, and
-/// package config path.
-DartLinter buildDriver(LintRule rule, File file, {String analysisOptions}) {
- final memoryResourceProvider = MemoryResourceProvider(
- context: PhysicalResourceProvider.INSTANCE.pathContext);
- final resourceProvider = TestResourceProvider(memoryResourceProvider);
-
- final pathContext = memoryResourceProvider.pathContext;
- var packageConfigPath = memoryResourceProvider.convertPath(pathContext.join(
- pathContext.dirname(file.absolute.path), '.mock_packages'));
- if (!resourceProvider.getFile(packageConfigPath).exists) {
- packageConfigPath = null;
- }
-
- final options = LinterOptions([rule], analysisOptions)
- ..mockSdk = MockSdk(resourceProvider: memoryResourceProvider)
- ..resourceProvider = resourceProvider
- ..packageConfigPath = packageConfigPath;
-
- return DartLinter(options);
-}
-
-/// A resource provider that accesses entities in a MemoryResourceProvider,
-/// falling back to the PhysicalResourceProvider when they don't exist.
-class TestResourceProvider extends PhysicalResourceProvider {
- MemoryResourceProvider memoryResourceProvider;
-
- TestResourceProvider(this.memoryResourceProvider) : super(null);
-
- @override
- file_system.File getFile(String path) {
- final file = memoryResourceProvider.getFile(path);
- return file.exists ? file : super.getFile(path);
- }
-
- @override
- file_system.Folder getFolder(String path) {
- final folder = memoryResourceProvider.getFolder(path);
- return folder.exists ? folder : super.getFolder(path);
- }
-
- @override
- file_system.Resource getResource(String path) {
- final resource = memoryResourceProvider.getResource(path);
- return resource.exists ? resource : super.getResource(path);
- }
-}
+@Deprecated(
+ 'Prefer importing `package:linter/src/test_utilities/test_resource_provider.dart` directly')
+export 'package:linter/src/test_utilities/test_resource_provider.dart';