Skip to content

Commit a8879f9

Browse files
srawlinsCommit Queue
authored andcommitted
linter: move test_utils out of lib
Change-Id: Ib0ca7fbcf8274c5fed36e73707c9ce206d9f2ff0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/444220 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 106c70f commit a8879f9

File tree

5 files changed

+38
-60
lines changed

5 files changed

+38
-60
lines changed

pkg/linter/tool/benchmark.dart

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import 'package:args/args.dart';
1616
import 'package:linter/src/extensions.dart';
1717
import 'package:linter/src/rules.dart';
1818
import 'package:linter/src/test_utilities/analysis_error_info.dart';
19-
import 'package:linter/src/test_utilities/linter_options.dart';
20-
import 'package:linter/src/test_utilities/test_linter.dart';
2119
import 'package:path/path.dart' as path;
2220
import 'package:yaml/yaml.dart';
2321

2422
import 'lint_sets.dart';
23+
import 'linter_options.dart';
24+
import 'test_linter.dart';
2525

2626
/// Benchmarks lint rules.
2727
Future<void> main(List<String> args) async {
@@ -134,6 +134,7 @@ Future<void> runLinter(List<String> args) async {
134134

135135
var configFile = options['config'];
136136
var ruleNames = options['rules'];
137+
var customSdk = options.option('dart-sdk');
137138

138139
LinterOptions linterOptions;
139140
if (configFile is String) {
@@ -144,7 +145,10 @@ Future<void> runLinter(List<String> args) async {
144145
(rule) => !ruleConfigs.any((rc) => rc.disables(rule.name)),
145146
);
146147

147-
linterOptions = LinterOptions(enabledRules: enabledRules);
148+
linterOptions = LinterOptions(
149+
enabledRules: enabledRules,
150+
dartSdkPath: customSdk,
151+
);
148152
} else if (ruleNames is Iterable<String> && ruleNames.isNotEmpty) {
149153
var rules = <AbstractAnalysisRule>[];
150154
for (var ruleName in ruleNames) {
@@ -155,18 +159,11 @@ Future<void> runLinter(List<String> args) async {
155159
}
156160
rules.add(rule);
157161
}
158-
linterOptions = LinterOptions(enabledRules: rules);
162+
linterOptions = LinterOptions(enabledRules: rules, dartSdkPath: customSdk);
159163
} else {
160-
linterOptions = LinterOptions();
161-
}
162-
163-
var customSdk = options['dart-sdk'];
164-
if (customSdk is String) {
165-
linterOptions.dartSdkPath = customSdk;
164+
linterOptions = LinterOptions(dartSdkPath: customSdk);
166165
}
167166

168-
linterOptions.enableTiming = true;
169-
170167
var filesToLint = [
171168
for (var path in paths)
172169
...collectFiles(

pkg/linter/tool/checks/driver.dart

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,7 @@ class Driver {
9595
try {
9696
var result = await context.currentSession.getErrors(filePath);
9797
if (result is ErrorsResult) {
98-
var filtered =
99-
result.diagnostics
100-
.where((e) => e.diagnosticCode.name != 'TODO')
101-
.toList();
102-
if (filtered.isNotEmpty) {
103-
errors.add(DiagnosticInfo(filtered, result.lineInfo));
104-
}
98+
errors.add(DiagnosticInfo(result.diagnostics, result.lineInfo));
10599
}
106100
} on Exception catch (e) {
107101
_print('Exception caught analyzing: $filePath');

pkg/linter/lib/src/test_utilities/lint_driver.dart renamed to pkg/linter/tool/lint_driver.dart

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ import 'dart:io' as io;
66

77
import 'package:analyzer/dart/analysis/results.dart';
88
import 'package:analyzer/file_system/file_system.dart';
9+
import 'package:analyzer/file_system/physical_file_system.dart' as file_system;
910
import 'package:analyzer/instrumentation/instrumentation.dart';
10-
// ignore: implementation_imports
1111
import 'package:analyzer/src/dart/analysis/analysis_context_collection.dart';
12-
// ignore: implementation_imports
1312
import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine;
14-
// ignore: implementation_imports
1513
import 'package:analyzer/src/lint/io.dart';
14+
import 'package:linter/src/test_utilities/analysis_error_info.dart';
1615

17-
import 'analysis_error_info.dart';
1816
import 'linter_options.dart';
1917

2018
class LintDriver {
@@ -24,9 +22,10 @@ class LintDriver {
2422

2523
final LinterOptions _options;
2624

27-
final ResourceProvider _resourceProvider;
25+
LintDriver(this._options);
2826

29-
LintDriver(this._options, this._resourceProvider);
27+
ResourceProvider get _resourceProvider =>
28+
file_system.PhysicalResourceProvider.INSTANCE;
3029

3130
Future<List<DiagnosticInfo>> analyze(Iterable<io.File> files) async {
3231
AnalysisEngine.instance.instrumentationService = _StdInstrumentation();
@@ -45,7 +44,7 @@ class LintDriver {
4544
growable: false,
4645
);
4746
},
48-
enableLintRuleTiming: _options.enableTiming,
47+
enableLintRuleTiming: true,
4948
);
5049

5150
_filesAnalyzed.addAll(filesPaths);
@@ -63,10 +62,8 @@ class LintDriver {
6362
return result;
6463
}
6564

66-
String _absoluteNormalizedPath(String path) {
67-
var pathContext = _resourceProvider.pathContext;
68-
return pathContext.normalize(pathContext.absolute(path));
69-
}
65+
String _absoluteNormalizedPath(String path) => _resourceProvider.pathContext
66+
.normalize(_resourceProvider.pathContext.absolute(path));
7067
}
7168

7269
/// Prints logging information comments to the [outSink] and error messages to

pkg/linter/lib/src/test_utilities/linter_options.dart renamed to pkg/linter/tool/linter_options.dart

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// ignore: implementation_imports
65
import 'package:analyzer/src/lint/linter.dart';
7-
// ignore: implementation_imports
86
import 'package:analyzer/src/lint/registry.dart';
97

108
class LinterOptions {
119
final Iterable<AbstractAnalysisRule> enabledRules;
1210

1311
/// The path to the Dart SDK.
14-
String? dartSdkPath;
12+
final String? dartSdkPath;
1513

16-
/// Whether to gather timing data during analysis.
17-
bool enableTiming = false;
18-
19-
LinterOptions({Iterable<AbstractAnalysisRule>? enabledRules})
20-
: enabledRules = enabledRules ?? Registry.ruleRegistry;
14+
LinterOptions({
15+
Iterable<AbstractAnalysisRule>? enabledRules,
16+
this.dartSdkPath,
17+
}) : enabledRules = enabledRules ?? Registry.ruleRegistry;
2118
}

pkg/linter/lib/src/test_utilities/test_linter.dart renamed to pkg/linter/tool/test_linter.dart

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@ import 'dart:io';
66

77
import 'package:analyzer/diagnostic/diagnostic.dart';
88
import 'package:analyzer/error/listener.dart';
9-
import 'package:analyzer/file_system/file_system.dart' as file_system;
109
import 'package:analyzer/file_system/physical_file_system.dart' as file_system;
1110
import 'package:analyzer/source/file_source.dart';
1211
import 'package:analyzer/source/source.dart';
13-
// ignore: implementation_imports
1412
import 'package:analyzer/src/lint/pub.dart';
15-
// ignore: implementation_imports
1613
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
17-
import 'package:meta/meta.dart';
14+
import 'package:linter/src/test_utilities/analysis_error_info.dart';
1815
import 'package:path/path.dart' as path;
1916

20-
import 'analysis_error_info.dart';
2117
import 'lint_driver.dart';
2218
import 'linter_options.dart';
2319

@@ -27,33 +23,37 @@ Source _createSource(Uri uri) {
2723
return FileSource(file, uri);
2824
}
2925

30-
/// Dart source linter, only for package:linter's tools and tests.
3126
class TestLinter implements DiagnosticListener {
3227
final errors = <Diagnostic>[];
3328

3429
final LinterOptions options;
35-
final file_system.ResourceProvider _resourceProvider;
30+
TestLinter(this.options);
3631

37-
TestLinter(this.options, {file_system.ResourceProvider? resourceProvider})
38-
: _resourceProvider =
39-
resourceProvider ?? file_system.PhysicalResourceProvider.INSTANCE;
32+
path.Context get _pathContext =>
33+
file_system.PhysicalResourceProvider.INSTANCE.pathContext;
4034

4135
Future<List<DiagnosticInfo>> lintFiles(List<File> files) async {
42-
var lintDriver = LintDriver(options, _resourceProvider);
36+
var lintDriver = LintDriver(options);
4337
var errors = await lintDriver.analyze(
4438
files.where((f) => f.path.endsWith('.dart')),
4539
);
4640
for (var file in files.where(_isPubspecFile)) {
47-
lintPubspecSource(
41+
_lintPubspecSource(
4842
contents: file.readAsStringSync(),
49-
sourcePath: _resourceProvider.pathContext.normalize(file.absolute.path),
43+
sourcePath: _pathContext.normalize(file.absolute.path),
5044
);
5145
}
5246
return errors;
5347
}
5448

55-
@visibleForTesting
56-
void lintPubspecSource({required String contents, String? sourcePath}) {
49+
@override
50+
void onDiagnostic(Diagnostic error) => errors.add(error);
51+
52+
/// Returns whether this [entry] is a pubspec file.
53+
bool _isPubspecFile(FileSystemEntity entry) =>
54+
path.basename(entry.path) == file_paths.pubspecYaml;
55+
56+
void _lintPubspecSource({required String contents, String? sourcePath}) {
5757
var sourceUrl = sourcePath == null ? null : path.toUri(sourcePath);
5858
var spec = Pubspec.parse(contents, sourceUrl: sourceUrl);
5959

@@ -75,11 +75,4 @@ class TestLinter implements DiagnosticListener {
7575
}
7676
}
7777
}
78-
79-
@override
80-
void onDiagnostic(Diagnostic error) => errors.add(error);
81-
82-
/// Returns whether this [entry] is a pubspec file.
83-
bool _isPubspecFile(FileSystemEntity entry) =>
84-
path.basename(entry.path) == file_paths.pubspecYaml;
8578
}

0 commit comments

Comments
 (0)