Skip to content

Commit

Permalink
linter: Remove support for unmocked lint rule legacy tests
Browse files Browse the repository at this point in the history
The `unsafe_html` lint rule was the last (or only?) rule to use an
unmocked SDK in legacy tests, and it is removed.

Change-Id: I86cdb77fd0e98fab5ec9351962d38af74ba87bd0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394568
Commit-Queue: Phil Quitslund <[email protected]>
Auto-Submit: Samuel Rawlins <[email protected]>
Reviewed-by: Phil Quitslund <[email protected]>
  • Loading branch information
srawlins authored and Commit Queue committed Nov 14, 2024
1 parent 901b2eb commit fadaa36
Showing 1 changed file with 9 additions and 43 deletions.
52 changes: 9 additions & 43 deletions pkg/linter/test/rule_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

import 'dart:io';

import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/file_system/physical_file_system.dart';
import 'package:analyzer/src/dart/analysis/analysis_options.dart';
import 'package:analyzer/src/lint/io.dart';
import 'package:analyzer/src/lint/registry.dart';
Expand Down Expand Up @@ -75,53 +72,22 @@ void defineRuleUnitTests() {
});
}

void testRule(String ruleName, File file, {bool useMockSdk = true}) {
void testRule(String ruleName, File file) {
test(ruleName, () async {
if (!file.existsSync()) {
throw Exception('No rule found defined at: ${file.path}');
}

var errorInfos =
await _getErrorInfos(ruleName, file, useMockSdk: useMockSdk);
_validateExpectedLints(file, errorInfos);
});
}

Future<Iterable<AnalysisErrorInfo>> _getErrorInfos(String ruleName, File file,
{required bool useMockSdk}) async {
registerLintRules();
var rule = Registry.ruleRegistry[ruleName];
if (rule == null) {
fail('rule `$ruleName` is not registered; unable to test.');
}
registerLintRules();
var rule = Registry.ruleRegistry[ruleName];
if (rule == null) {
fail('rule `$ruleName` is not registered; unable to test.');
}

if (useMockSdk) {
var driver = buildDriver(rule, file);
return await driver.lintFiles([file]);
}

var path = p.normalize(file.absolute.path);
var collection = AnalysisContextCollection(
includedPaths: [path],
resourceProvider: PhysicalResourceProvider.INSTANCE,
);

var context = collection.contexts.first;
var contextFile = (context.currentSession.getFile(path) as FileResult).file;
var options =
context.getAnalysisOptionsForFile(contextFile) as AnalysisOptionsImpl;
options.lintRules = options.lintRules.toList();

// TODO(pq): consider a different way to configure lints
// https://github.com/dart-lang/sdk/issues/54045
options.lintRules.add(rule);
options.lint = true;

var result =
await context.currentSession.getResolvedUnit(path) as ResolvedUnitResult;
return [
AnalysisErrorInfo(result.errors, result.lineInfo),
];
var errorInfos = await driver.lintFiles([file]);
_validateExpectedLints(file, errorInfos);
});
}

/// Parse lint annotations in the given [file] and validate that they correspond
Expand Down

0 comments on commit fadaa36

Please sign in to comment.