From fadaa36489b82b283eb09a1549716d2a0bd43ff7 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Thu, 14 Nov 2024 00:54:33 +0000 Subject: [PATCH] linter: Remove support for unmocked lint rule legacy tests 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 Auto-Submit: Samuel Rawlins Reviewed-by: Phil Quitslund --- pkg/linter/test/rule_test.dart | 52 ++++++---------------------------- 1 file changed, 9 insertions(+), 43 deletions(-) diff --git a/pkg/linter/test/rule_test.dart b/pkg/linter/test/rule_test.dart index 0c9a38df110a..ca1ab1b13ea7 100644 --- a/pkg/linter/test/rule_test.dart +++ b/pkg/linter/test/rule_test.dart @@ -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'; @@ -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> _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