| // Copyright (c) 2026, 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/analysis_rule/analysis_rule.dart'; | |
| import 'package:analyzer/analysis_rule/rule_context.dart'; | |
| import 'package:analyzer/analysis_rule/rule_visitor_registry.dart'; | |
| import 'package:analyzer/dart/ast/ast.dart'; | |
| import 'package:analyzer/dart/ast/visitor.dart'; | |
| import 'package:analyzer/error/error.dart'; | |
| import 'package:analyzer/src/lint/analysis_rule_timers.dart'; | |
| import 'package:linter/src/rules/prefer_single_quotes.dart'; | |
| import 'package:test/test.dart'; | |
| import 'package:test_descriptor/test_descriptor.dart' as d; | |
| import '../tool/benchmark.dart'; | |
| import '../tool/util/io.dart'; | |
| import 'mocks.dart'; | |
| void main() { | |
| group('benchmark', () { | |
| late int originalExitCode; | |
| late StringSink originalErrorSink; | |
| setUp(() { | |
| originalExitCode = exitCode; | |
| originalErrorSink = errorSink; | |
| exitCode = 0; | |
| }); | |
| tearDown(() { | |
| exitCode = originalExitCode; | |
| errorSink = originalErrorSink; | |
| }); | |
| test('does not set exitCode when files produce lint diagnostics', () async { | |
| await d.dir('pkg', [ | |
| d.file('sample.dart', 'var s = "hello";\n'), | |
| ]).create(); | |
| var file = File('${d.sandbox}/pkg/sample.dart'); | |
| await lintFiles([file], rules: [PreferSingleQuotes()], dartSdkPath: null); | |
| expect(exitCode, 0); | |
| }); | |
| test('sets exitCode when a rule throws', () async { | |
| await d.dir('pkg', [ | |
| d.file('sample.dart', 'var s = "hello";\n'), | |
| ]).create(); | |
| var file = File('${d.sandbox}/pkg/sample.dart'); | |
| var errors = StringBuffer(); | |
| errorSink = errors; | |
| await lintFiles([file], rules: [_ThrowingRule()], dartSdkPath: null); | |
| expect(exitCode, loggedAnalyzerErrorExitCode); | |
| expect(errors.toString(), contains('Exception while using a')); | |
| }); | |
| test('resets timers between runs and preserves sub-ms precision', () async { | |
| await d.dir('pkg', [ | |
| d.file('sample.dart', 'var s = "hello";\n'), | |
| ]).create(); | |
| var file = File('${d.sandbox}/pkg/sample.dart'); | |
| var slowFirstRunRule = _SlowFirstRunRule(); | |
| var out = StringBuffer(); | |
| // Seed a stale timer to confirm writeBenchmarks clears prior state. | |
| analysisRuleTimers.timers['stale_rule'] = Stopwatch()..start(); | |
| await writeBenchmarks( | |
| out, | |
| [file], | |
| rules: [slowFirstRunRule, PreferSingleQuotes()], | |
| dartSdkPath: null, | |
| coreRuleset: const ['slow_first_run_rule'], | |
| recommendedRuleset: const ['prefer_single_quotes'], | |
| flutterRuleset: const [], | |
| ); | |
| expect(slowFirstRunRule.runCount, benchmarkRuns); | |
| expect(errorSink, same(originalErrorSink)); | |
| var output = out.toString(); | |
| expect(output, isNot(contains('stale_rule'))); | |
| var slowMatch = RegExp( | |
| r'^slow_first_run_rule \[core\]\s+(\d+\.\d{3})$', | |
| multiLine: true, | |
| ).firstMatch(output); | |
| expect(slowMatch, isNotNull, reason: output); | |
| var slowMs = double.parse(slowMatch!.group(1)!); | |
| // First pass spins >= 25 ms; subsequent passes are near 0 ms. | |
| expect(slowMs, lessThan(20.0)); | |
| var quotesMatch = RegExp( | |
| r'^prefer_single_quotes \[recommended\]\s+(\d+\.\d{3})$', | |
| multiLine: true, | |
| ).firstMatch(output); | |
| expect(quotesMatch, isNotNull, reason: output); | |
| var quotesMs = double.parse(quotesMatch!.group(1)!); | |
| var totalMatch = RegExp( | |
| r'^Total\s+(\d+\.\d{3})$', | |
| multiLine: true, | |
| ).firstMatch(output); | |
| expect(totalMatch, isNotNull, reason: output); | |
| var totalMs = double.parse(totalMatch!.group(1)!); | |
| expect(totalMs, closeTo(slowMs + quotesMs, 0.001)); | |
| }); | |
| }); | |
| } | |
| class _SlowFirstRunRule extends AnalysisRule { | |
| int runCount = 0; | |
| new() : super(name: 'slow_first_run_rule', description: 'Test rule.'); | |
| @override | |
| DiagnosticCode get diagnosticCode => | |
| TestDiagnosticCode('slow_first_run_rule', 'Test diagnostic'); | |
| @override | |
| void registerNodeProcessors( | |
| RuleVisitorRegistry registry, | |
| RuleContext context, | |
| ) { | |
| if (runCount == 0) { | |
| var watch = Stopwatch()..start(); | |
| while (watch.elapsedMilliseconds < 25) {} | |
| } | |
| runCount++; | |
| } | |
| } | |
| class _ThrowingRule extends AnalysisRule { | |
| new() : super(name: 'throwing_rule', description: 'Test rule.'); | |
| @override | |
| DiagnosticCode get diagnosticCode => | |
| TestDiagnosticCode('throwing_rule', 'Test diagnostic'); | |
| @override | |
| void registerNodeProcessors( | |
| RuleVisitorRegistry registry, | |
| RuleContext context, | |
| ) { | |
| registry.addCompilationUnit(this, _ThrowingVisitor()); | |
| } | |
| } | |
| class _ThrowingVisitor extends SimpleAstVisitor<void> { | |
| @override | |
| void visitCompilationUnit(CompilationUnit node) { | |
| throw StateError('Thrown by test rule.'); | |
| } | |
| } |