From 0ba210f3d2e4c28223c91814b814f148d3766e1c Mon Sep 17 00:00:00 2001 From: yamarkz Date: Wed, 22 Mar 2023 22:53:57 +0900 Subject: [PATCH 01/19] Support error severity screening option --- packages/custom_lint/bin/custom_lint.dart | 19 +++++- packages/custom_lint/lib/custom_lint.dart | 79 ++++++++++++++++++----- 2 files changed, 81 insertions(+), 17 deletions(-) diff --git a/packages/custom_lint/bin/custom_lint.dart b/packages/custom_lint/bin/custom_lint.dart index 254b2653..3bd040a8 100644 --- a/packages/custom_lint/bin/custom_lint.dart +++ b/packages/custom_lint/bin/custom_lint.dart @@ -6,6 +6,16 @@ import 'package:custom_lint/custom_lint.dart'; Future entrypoint([List args = const []]) async { final parser = ArgParser() + ..addFlag( + 'fatal-infos', + help: 'Treat info level issues as fatal', + negatable: false, + ) + ..addFlag( + 'fatal-warnings', + help: 'Treat warning level issues as fatal', + defaultsTo: true, + ) ..addFlag( 'watch', help: "Watches plugins' sources and perform a hot-reload on change", @@ -27,8 +37,15 @@ Future entrypoint([List args = const []]) async { } final watchMode = result['watch'] as bool; + final fatalInfos = result['fatal-infos'] as bool; + final fatalWarnings = result['fatal-warnings'] as bool; - await customLint(workingDirectory: Directory.current, watchMode: watchMode); + await customLint( + workingDirectory: Directory.current, + watchMode: watchMode, + fatalInfos: fatalInfos, + fatalWarnings: fatalWarnings, + ); } void main([List args = const []]) async { diff --git a/packages/custom_lint/lib/custom_lint.dart b/packages/custom_lint/lib/custom_lint.dart index 56a06595..6ad3355d 100644 --- a/packages/custom_lint/lib/custom_lint.dart +++ b/packages/custom_lint/lib/custom_lint.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:convert'; import 'dart:io'; +import 'package:analyzer_plugin/protocol/protocol_common.dart'; import 'package:analyzer_plugin/protocol/protocol_generated.dart'; import 'package:collection/collection.dart'; import 'package:path/path.dart' as p; @@ -34,6 +35,8 @@ q: Quit /// Watch mode cannot be enabled if in release mode. Future customLint({ bool watchMode = true, + bool fatalInfos = false, + bool fatalWarnings = true, required Directory workingDirectory, }) async { // Reset the code @@ -50,13 +53,21 @@ Future customLint({ (customLintServer) async { final runner = CustomLintRunner(customLintServer, workingDirectory, channel); - try { await runner.initialize; - await _runPlugins(runner, reload: false); + await _runPlugins( + runner, + reload: false, + fatalInfos: fatalInfos, + fatalWarnings: fatalWarnings, + ); if (watchMode) { - await _startWatchMode(runner); + await _startWatchMode( + runner, + fatalInfos: fatalInfos, + fatalWarnings: fatalWarnings, + ); } } catch (err) { exitCode = 1; @@ -70,15 +81,17 @@ Future customLint({ Future _runPlugins( CustomLintRunner runner, { required bool reload, + required bool fatalInfos, + required bool fatalWarnings, }) async { try { final lints = await runner.getLints(reload: reload); - - if (lints.any((lintsForFile) => lintsForFile.errors.isNotEmpty)) { - exitCode = 1; - } - - _renderLints(lints, workingDirectory: runner.workingDirectory); + _renderLints( + lints, + workingDirectory: runner.workingDirectory, + fatalInfos: fatalInfos, + fatalWarnings: fatalWarnings, + ); } catch (err, stack) { exitCode = 1; stderr.writeln('$err\n$stack'); @@ -88,6 +101,8 @@ Future _runPlugins( void _renderLints( List lints, { required Directory workingDirectory, + required bool fatalInfos, + required bool fatalWarnings, }) { var errors = lints.expand((lint) => lint.errors); @@ -113,18 +128,45 @@ void _renderLints( if (errors.isEmpty) { stdout.writeln('No issues found!'); return; + } else { + for (final error in errors) { + stdout.writeln( + ' ${_relativeFilePath(error.location.file, workingDirectory)}:${error.location.startLine}:${error.location.startColumn}' + ' • ${error.message} • ${error.code}', + ); + } } - exitCode = 1; + var hasErrors = false; + var hasWarnings = false; + var hasInfos = false; for (final error in errors) { - stdout.writeln( - ' ${_relativeFilePath(error.location.file, workingDirectory)}:${error.location.startLine}:${error.location.startColumn}' - ' • ${error.message} • ${error.code}', - ); + hasErrors |= error.severity == AnalysisErrorSeverity.ERROR; + hasWarnings |= error.severity == AnalysisErrorSeverity.WARNING; + hasInfos |= error.severity == AnalysisErrorSeverity.INFO; + } + + if (hasErrors) { + exitCode = 1; + return; + } + + if (fatalWarnings && hasWarnings) { + exitCode = 1; + return; + } + + if (fatalInfos && hasInfos) { + exitCode = 1; + return; } } -Future _startWatchMode(CustomLintRunner runner) async { +Future _startWatchMode( + CustomLintRunner runner, { + required bool fatalInfos, + required bool fatalWarnings, +}) async { if (stdin.hasTerminal) { stdin // Let's not pollute the output with whatever the user types @@ -141,7 +183,12 @@ Future _startWatchMode(CustomLintRunner runner) async { case 'r': // Rerunning lints stdout.writeln('Manual Reload...'); - await _runPlugins(runner, reload: true); + await _runPlugins( + runner, + reload: true, + fatalInfos: fatalInfos, + fatalWarnings: fatalWarnings, + ); break; case 'q': // Let's quit the command line From 3a8107d39f6cab1cf4b16534e01da01baaa0d80d Mon Sep 17 00:00:00 2001 From: yamarkz Date: Wed, 22 Mar 2023 22:59:09 +0900 Subject: [PATCH 02/19] Add error severity screening option test --- packages/custom_lint/test/cli_test.dart | 88 +++++++++++++++++-- packages/custom_lint/test/create_project.dart | 4 +- packages/custom_lint/test/run_plugin.dart | 2 + 3 files changed, 87 insertions(+), 7 deletions(-) diff --git a/packages/custom_lint/test/cli_test.dart b/packages/custom_lint/test/cli_test.dart index 1bd9527e..53d238cd 100644 --- a/packages/custom_lint/test/cli_test.dart +++ b/packages/custom_lint/test/cli_test.dart @@ -11,6 +11,7 @@ final oyPluginSource = createPluginSource([ TestLintRule( code: 'oy', message: 'Oy', + errorSeverity: 'ErrorSeverity.WARNING', ) ]); @@ -18,6 +19,14 @@ final helloWordPluginSource = createPluginSource([ TestLintRule( code: 'hello_world', message: 'Hello world', + errorSeverity: 'ErrorSeverity.WARNING', + ) +]); + +final helloDartPluginSource = createPluginSource([ + TestLintRule( + code: 'hello_dart', + message: 'Hello Dart', ) ]); @@ -86,6 +95,72 @@ lib/custom_lint_client.dart:13:29: Error: Undefined name 'createPlugin'. ); }); + test('exits with 0 when pass argument `--no-fatal-warnings`', () async { + final plugin = createPlugin(name: 'test_lint', main: helloWordPluginSource); + + final app = createLintUsage( + source: { + 'lib/main.dart': 'void fn() {}', + }, + plugins: {'test_lint': plugin.uri}, + name: 'test_app', + ); + + await runWithIOOverride( + (out, err) async { + await cli.entrypoint(['--no-fatal-warnings']); + + expect(err, emitsDone); + expect(exitCode, 0); + }, + currentDirectory: app, + ); + }); + + test('exits with 1 when pass argument `--fatal-warnings`', () async { + final plugin = createPlugin(name: 'test_lint', main: helloWordPluginSource); + + final app = createLintUsage( + source: { + 'lib/main.dart': 'void fn() {}', + }, + plugins: {'test_lint': plugin.uri}, + name: 'test_app', + ); + + await runWithIOOverride( + (out, err) async { + await cli.entrypoint(['--fatal-warnings']); + + expect(err, emitsDone); + expect(exitCode, 1); + }, + currentDirectory: app, + ); + }); + + test('exits with 1 when pass argument `--fatal-infos`', () async { + final plugin = createPlugin(name: 'test_lint', main: helloDartPluginSource); + + final app = createLintUsage( + source: { + 'lib/main.dart': 'void fn() {}', + }, + plugins: {'test_lint': plugin.uri}, + name: 'test_app', + ); + + await runWithIOOverride( + (out, err) async { + await cli.entrypoint(['--fatal-infos']); + + expect(err, emitsDone); + expect(exitCode, 1); + }, + currentDirectory: app, + ); + }); + test('CLI lists warnings from all plugins and set exit code', () async { final plugin = createPlugin(name: 'test_lint', main: helloWordPluginSource); final plugin2 = createPlugin(name: 'test_lint2', main: oyPluginSource); @@ -237,6 +312,7 @@ Bad state: fail final plugin = createPlugin( name: 'test_lint', main: ''' +import 'package:analyzer/error/error.dart'; import 'package:analyzer/error/listener.dart'; import 'package:custom_lint_builder/custom_lint_builder.dart'; @@ -248,7 +324,7 @@ class _HelloWorldLint extends PluginBase { } class _Lint extends DartLintRule { - const _Lint() : super(code: const LintCode(name: 'a', problemMessage: 'a')); + const _Lint() : super(code: const LintCode(name: 'a', problemMessage: 'a', errorSeverity: ErrorSeverity.WARNING)); @override void run( @@ -258,27 +334,27 @@ class _Lint extends DartLintRule { ) { final line2 = resolver.lineInfo.getOffsetOfLine(1); reporter.reportErrorForOffset( - const LintCode(name: 'x2', problemMessage: 'x2'), + const LintCode(name: 'x2', problemMessage: 'x2', errorSeverity: ErrorSeverity.WARNING), line2 + 1, 1, ); reporter.reportErrorForOffset( - const LintCode(name: 'a', problemMessage: 'a'), + const LintCode(name: 'a', problemMessage: 'a', errorSeverity: ErrorSeverity.WARNING), line2 + 1, 1, ); reporter.reportErrorForOffset( - const LintCode(name: 'x', problemMessage: 'x'), + const LintCode(name: 'x', problemMessage: 'x', errorSeverity: ErrorSeverity.WARNING), line2 + 1, 1, ); reporter.reportErrorForOffset( - const LintCode(name: 'y', problemMessage: 'y'), + const LintCode(name: 'y', problemMessage: 'y', errorSeverity: ErrorSeverity.WARNING), line2, 1, ); reporter.reportErrorForOffset( - const LintCode(name: 'z', problemMessage: 'z'), + const LintCode(name: 'z', problemMessage: 'z', errorSeverity: ErrorSeverity.WARNING), 0, 1, ); diff --git a/packages/custom_lint/test/create_project.dart b/packages/custom_lint/test/create_project.dart index ab829a5c..32626555 100644 --- a/packages/custom_lint/test/create_project.dart +++ b/packages/custom_lint/test/create_project.dart @@ -29,6 +29,7 @@ class TestLintRule { this.onVariable = '', this.ruleMembers = '', this.fixes = const [], + this.errorSeverity = 'ErrorSeverity.INFO', }); final String code; @@ -37,6 +38,7 @@ class TestLintRule { final String onVariable; final String ruleMembers; final List fixes; + final String errorSeverity; } class TestLintFix { @@ -102,7 +104,7 @@ class ${fix.name} extends DartFix { class ${rule.code} extends DartLintRule { ${rule.code}() : super( - code: LintCode(name: '${rule.code}', problemMessage: '${rule.message}'), + code: LintCode(name: '${rule.code}', problemMessage: '${rule.message}', errorSeverity: ${rule.errorSeverity}), ); $fixes diff --git a/packages/custom_lint/test/run_plugin.dart b/packages/custom_lint/test/run_plugin.dart index 74690b79..30b8b1ba 100644 --- a/packages/custom_lint/test/run_plugin.dart +++ b/packages/custom_lint/test/run_plugin.dart @@ -23,6 +23,8 @@ CustomLintRunner startRunnerForApp( bool ignoreErrors = false, bool includeBuiltInLints = true, bool watchMode = false, + bool fatalInfos = false, + bool fatalWarnings = false, }) { final zone = Zone.current; final channel = ServerIsolateChannel(); From b759a52e1c001f86d2586ddc53cdcad7a4bbe848 Mon Sep 17 00:00:00 2001 From: yamarkz Date: Sat, 8 Apr 2023 17:56:07 +0900 Subject: [PATCH 03/19] fix test case --- packages/custom_lint/test/cli_process_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/custom_lint/test/cli_process_test.dart b/packages/custom_lint/test/cli_process_test.dart index 74d776d3..fa6d1ed7 100644 --- a/packages/custom_lint/test/cli_process_test.dart +++ b/packages/custom_lint/test/cli_process_test.dart @@ -72,7 +72,7 @@ void main() { test_app2/lib/main2.dart:1:6 • Hello world • hello_world ''', ); - expect(process.exitCode, 1); + expect(process.exitCode, 0); }); group('Correctly exits when', () { From 0e4cbbe486b9d4fcec5898336642c46e7da13a1b Mon Sep 17 00:00:00 2001 From: yamarkz Date: Sat, 8 Apr 2023 17:59:48 +0900 Subject: [PATCH 04/19] sort arguments and remove default value --- packages/custom_lint/lib/custom_lint.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/custom_lint/lib/custom_lint.dart b/packages/custom_lint/lib/custom_lint.dart index 54d78645..1346ab55 100644 --- a/packages/custom_lint/lib/custom_lint.dart +++ b/packages/custom_lint/lib/custom_lint.dart @@ -36,9 +36,9 @@ q: Quit /// Watch mode cannot be enabled if in release mode. Future customLint({ bool watchMode = true, + required Directory workingDirectory, bool fatalInfos = false, bool fatalWarnings = true, - required Directory workingDirectory, }) async { // Reset the code exitCode = 0; @@ -63,8 +63,8 @@ Future _runServer( ServerIsolateChannel channel, { required bool watchMode, required Directory workingDirectory, - bool fatalInfos = false, - bool fatalWarnings = true, + required bool fatalInfos, + required bool fatalWarnings, }) async { final customLintServer = await CustomLintServer.start( sendPort: channel.receivePort.sendPort, From 9aeebb75c63298f7a79c38730e804fe9a7717d1e Mon Sep 17 00:00:00 2001 From: Kazuki YAMAGUCHI Date: Sun, 18 Jun 2023 14:52:03 +0900 Subject: [PATCH 05/19] Update packages/custom_lint/lib/custom_lint.dart Co-authored-by: Remi Rousselet --- packages/custom_lint/lib/custom_lint.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/custom_lint/lib/custom_lint.dart b/packages/custom_lint/lib/custom_lint.dart index 3a046051..1481b4e6 100644 --- a/packages/custom_lint/lib/custom_lint.dart +++ b/packages/custom_lint/lib/custom_lint.dart @@ -37,7 +37,7 @@ q: Quit Future customLint({ bool watchMode = true, required Directory workingDirectory, - bool fatalInfos = false, + bool fatalInfos = true, bool fatalWarnings = true, }) async { // Reset the code From 36140d74bcb9c042629c4358d51162d5c861a0bd Mon Sep 17 00:00:00 2001 From: yamarkz Date: Sun, 18 Jun 2023 15:02:48 +0900 Subject: [PATCH 06/19] Rewrite error handling --- packages/custom_lint/lib/custom_lint.dart | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/custom_lint/lib/custom_lint.dart b/packages/custom_lint/lib/custom_lint.dart index 1481b4e6..dee4bf2a 100644 --- a/packages/custom_lint/lib/custom_lint.dart +++ b/packages/custom_lint/lib/custom_lint.dart @@ -166,22 +166,20 @@ void _renderLints( if (errors.isEmpty) { stdout.writeln('No issues found!'); return; - } else { - for (final error in errors) { - stdout.writeln( - ' ${_relativeFilePath(error.location.file, workingDirectory)}:${error.location.startLine}:${error.location.startColumn}' - ' • ${error.message} • ${error.code}', - ); - } } var hasErrors = false; var hasWarnings = false; var hasInfos = false; for (final error in errors) { - hasErrors |= error.severity == AnalysisErrorSeverity.ERROR; - hasWarnings |= error.severity == AnalysisErrorSeverity.WARNING; - hasInfos |= error.severity == AnalysisErrorSeverity.INFO; + stdout.writeln( + ' ${_relativeFilePath(error.location.file, workingDirectory)}:${error.location.startLine}:${error.location.startColumn}' + ' • ${error.message} • ${error.code}', + ); + hasErrors = hasErrors || error.severity == AnalysisErrorSeverity.ERROR; + hasWarnings = + hasWarnings || error.severity == AnalysisErrorSeverity.WARNING; + hasInfos = hasInfos || error.severity == AnalysisErrorSeverity.INFO; } if (hasErrors) { From 8fffd8d21664d4607fca402c88c9dd737d2fa476 Mon Sep 17 00:00:00 2001 From: yamarkz Date: Sun, 18 Jun 2023 15:33:09 +0900 Subject: [PATCH 07/19] Rewrite the definition of ErrorSeverity in TestLintRule --- packages/custom_lint/test/cli_test.dart | 5 +++-- packages/custom_lint/test/create_project.dart | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/custom_lint/test/cli_test.dart b/packages/custom_lint/test/cli_test.dart index e64617b5..1654f649 100644 --- a/packages/custom_lint/test/cli_test.dart +++ b/packages/custom_lint/test/cli_test.dart @@ -1,5 +1,6 @@ import 'dart:io'; +import 'package:analyzer/error/error.dart'; import 'package:test/test.dart'; import '../bin/custom_lint.dart' as cli; @@ -11,7 +12,7 @@ final oyPluginSource = createPluginSource([ TestLintRule( code: 'oy', message: 'Oy', - errorSeverity: 'ErrorSeverity.WARNING', + errorSeverity: ErrorSeverity.WARNING, ) ]); @@ -19,7 +20,7 @@ final helloWordPluginSource = createPluginSource([ TestLintRule( code: 'hello_world', message: 'Hello world', - errorSeverity: 'ErrorSeverity.WARNING', + errorSeverity: ErrorSeverity.WARNING, ) ]); diff --git a/packages/custom_lint/test/create_project.dart b/packages/custom_lint/test/create_project.dart index c1ea17f7..a920ab81 100644 --- a/packages/custom_lint/test/create_project.dart +++ b/packages/custom_lint/test/create_project.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'dart:io'; +import 'package:analyzer/error/error.dart'; import 'package:path/path.dart'; import 'package:test/scaffolding.dart'; @@ -30,7 +31,7 @@ class TestLintRule { this.onVariable = '', this.ruleMembers = '', this.fixes = const [], - this.errorSeverity = 'ErrorSeverity.INFO', + this.errorSeverity = ErrorSeverity.INFO, }); final String code; @@ -40,7 +41,7 @@ class TestLintRule { final String onVariable; final String ruleMembers; final List fixes; - final String errorSeverity; + final ErrorSeverity errorSeverity; } class TestLintFix { @@ -105,7 +106,7 @@ class ${fix.name} extends DartFix { class ${rule.code} extends DartLintRule { ${rule.code}() : super( - code: LintCode(name: '${rule.code}', problemMessage: '${rule.message}', errorSeverity: ${rule.errorSeverity}), + code: LintCode(name: '${rule.code}', problemMessage: '${rule.message}', errorSeverity: ErrorSeverity.${rule.errorSeverity.displayName.toUpperCase()}), ); $fixes From c309f2a8b50c222b0c087e78d32fd3a82e9c0c58 Mon Sep 17 00:00:00 2001 From: yamarkz Date: Sat, 12 Aug 2023 11:13:43 +0900 Subject: [PATCH 08/19] Rewrite conditional branch --- packages/custom_lint/lib/custom_lint.dart | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/packages/custom_lint/lib/custom_lint.dart b/packages/custom_lint/lib/custom_lint.dart index f33ce595..e64dbbe8 100644 --- a/packages/custom_lint/lib/custom_lint.dart +++ b/packages/custom_lint/lib/custom_lint.dart @@ -182,17 +182,7 @@ void _renderLints( hasInfos = hasInfos || error.severity == AnalysisErrorSeverity.INFO; } - if (hasErrors) { - exitCode = 1; - return; - } - - if (fatalWarnings && hasWarnings) { - exitCode = 1; - return; - } - - if (fatalInfos && hasInfos) { + if (hasErrors || (fatalWarnings && hasWarnings) || (fatalInfos && hasInfos)) { exitCode = 1; return; } From 8549d02d3d1041480c0db76ef0c90799b07c4c89 Mon Sep 17 00:00:00 2001 From: yamarkz Date: Sat, 12 Aug 2023 11:15:24 +0900 Subject: [PATCH 09/19] Set default option true fatal-infos --- packages/custom_lint/bin/custom_lint.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/custom_lint/bin/custom_lint.dart b/packages/custom_lint/bin/custom_lint.dart index ec6bd422..6dd2998c 100644 --- a/packages/custom_lint/bin/custom_lint.dart +++ b/packages/custom_lint/bin/custom_lint.dart @@ -9,7 +9,7 @@ Future entrypoint([List args = const []]) async { ..addFlag( 'fatal-infos', help: 'Treat info level issues as fatal', - negatable: false, + defaultsTo: true, ) ..addFlag( 'fatal-warnings', From 16e1a6a72f5dd6c0cc547e9702ceb05e03e92d25 Mon Sep 17 00:00:00 2001 From: yamarkz Date: Sat, 12 Aug 2023 11:36:58 +0900 Subject: [PATCH 10/19] Fix written test --- .../custom_lint/test/cli_process_test.dart | 2 +- packages/custom_lint/test/cli_test.dart | 88 ++----------------- 2 files changed, 7 insertions(+), 83 deletions(-) diff --git a/packages/custom_lint/test/cli_process_test.dart b/packages/custom_lint/test/cli_process_test.dart index e52053f0..6f3808e7 100644 --- a/packages/custom_lint/test/cli_process_test.dart +++ b/packages/custom_lint/test/cli_process_test.dart @@ -65,7 +65,7 @@ void main() { test_app2/lib/main2.dart:1:6 • Hello world • hello_world • INFO ''', ); - expect(process.exitCode, 0); + expect(process.exitCode, 1); }); group('Correctly exits when', () { diff --git a/packages/custom_lint/test/cli_test.dart b/packages/custom_lint/test/cli_test.dart index 6db7a8db..68571752 100644 --- a/packages/custom_lint/test/cli_test.dart +++ b/packages/custom_lint/test/cli_test.dart @@ -1,6 +1,5 @@ import 'dart:io'; -import 'package:analyzer/error/error.dart'; import 'package:test/test.dart'; import '../bin/custom_lint.dart' as cli; @@ -12,7 +11,6 @@ final oyPluginSource = createPluginSource([ TestLintRule( code: 'oy', message: 'Oy', - errorSeverity: ErrorSeverity.WARNING, ), ]); @@ -20,17 +18,9 @@ final helloWordPluginSource = createPluginSource([ TestLintRule( code: 'hello_world', message: 'Hello world', - errorSeverity: ErrorSeverity.WARNING, ) ]); -final helloDartPluginSource = createPluginSource([ - TestLintRule( - code: 'hello_dart', - message: 'Hello Dart', - ), -]); - void main() { test('exits with 0 when no lint and no error are found', () async { final plugin = createPlugin(name: 'test_lint', main: emptyPluginSource); @@ -96,72 +86,6 @@ lib/custom_lint_client.dart:13:29: Error: Undefined name 'createPlugin'. ); }); - test('exits with 0 when pass argument `--no-fatal-warnings`', () async { - final plugin = createPlugin(name: 'test_lint', main: helloWordPluginSource); - - final app = createLintUsage( - source: { - 'lib/main.dart': 'void fn() {}', - }, - plugins: {'test_lint': plugin.uri}, - name: 'test_app', - ); - - await runWithIOOverride( - (out, err) async { - await cli.entrypoint(['--no-fatal-warnings']); - - expect(err, emitsDone); - expect(exitCode, 0); - }, - currentDirectory: app, - ); - }); - - test('exits with 1 when pass argument `--fatal-warnings`', () async { - final plugin = createPlugin(name: 'test_lint', main: helloWordPluginSource); - - final app = createLintUsage( - source: { - 'lib/main.dart': 'void fn() {}', - }, - plugins: {'test_lint': plugin.uri}, - name: 'test_app', - ); - - await runWithIOOverride( - (out, err) async { - await cli.entrypoint(['--fatal-warnings']); - - expect(err, emitsDone); - expect(exitCode, 1); - }, - currentDirectory: app, - ); - }); - - test('exits with 1 when pass argument `--fatal-infos`', () async { - final plugin = createPlugin(name: 'test_lint', main: helloDartPluginSource); - - final app = createLintUsage( - source: { - 'lib/main.dart': 'void fn() {}', - }, - plugins: {'test_lint': plugin.uri}, - name: 'test_app', - ); - - await runWithIOOverride( - (out, err) async { - await cli.entrypoint(['--fatal-infos']); - - expect(err, emitsDone); - expect(exitCode, 1); - }, - currentDirectory: app, - ); - }); - test('CLI lists warnings from all plugins and set exit code', () async { final plugin = createPlugin(name: 'test_lint', main: helloWordPluginSource); final plugin2 = createPlugin(name: 'test_lint2', main: oyPluginSource); @@ -324,7 +248,7 @@ class _HelloWorldLint extends PluginBase { } class _Lint extends DartLintRule { - const _Lint() : super(code: const LintCode(name: 'a', problemMessage: 'a', errorSeverity: ErrorSeverity.WARNING)); + const _Lint() : super(code: const LintCode(name: 'a', problemMessage: 'a')); @override void run( @@ -334,27 +258,27 @@ class _Lint extends DartLintRule { ) { final line2 = resolver.lineInfo.getOffsetOfLine(1); reporter.reportErrorForOffset( - const LintCode(name: 'x2', problemMessage: 'x2', errorSeverity: ErrorSeverity.WARNING), + const LintCode(name: 'x2', problemMessage: 'x2'), line2 + 1, 1, ); reporter.reportErrorForOffset( - const LintCode(name: 'a', problemMessage: 'a', errorSeverity: ErrorSeverity.WARNING), + const LintCode(name: 'a', problemMessage: 'a'), line2 + 1, 1, ); reporter.reportErrorForOffset( - const LintCode(name: 'x', problemMessage: 'x', errorSeverity: ErrorSeverity.WARNING), + const LintCode(name: 'x', problemMessage: 'x'), line2 + 1, 1, ); reporter.reportErrorForOffset( - const LintCode(name: 'y', problemMessage: 'y', errorSeverity: ErrorSeverity.WARNING), + const LintCode(name: 'y', problemMessage: 'y'), line2, 1, ); reporter.reportErrorForOffset( - const LintCode(name: 'z', problemMessage: 'z', errorSeverity: ErrorSeverity.WARNING), + const LintCode(name: 'z', problemMessage: 'z'), 0, 1, ); From bf8282b40446372ecd6be28f5c5076f7849aa7f5 Mon Sep 17 00:00:00 2001 From: yamarkz Date: Sat, 12 Aug 2023 11:39:42 +0900 Subject: [PATCH 11/19] Remove unnecessary test code --- packages/custom_lint/test/cli_test.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/custom_lint/test/cli_test.dart b/packages/custom_lint/test/cli_test.dart index 68571752..91b49186 100644 --- a/packages/custom_lint/test/cli_test.dart +++ b/packages/custom_lint/test/cli_test.dart @@ -18,7 +18,7 @@ final helloWordPluginSource = createPluginSource([ TestLintRule( code: 'hello_world', message: 'Hello world', - ) + ), ]); void main() { @@ -236,7 +236,6 @@ Bad state: fail final plugin = createPlugin( name: 'test_lint', main: ''' -import 'package:analyzer/error/error.dart'; import 'package:analyzer/error/listener.dart'; import 'package:custom_lint_builder/custom_lint_builder.dart'; From 85224170c0d5da40a11c802d03e142cffa80e3a3 Mon Sep 17 00:00:00 2001 From: yamarkz Date: Sat, 12 Aug 2023 12:08:54 +0900 Subject: [PATCH 12/19] Add no option test code --- packages/custom_lint/test/cli_test.dart | 56 +++++++++++++++++++ packages/custom_lint/test/create_project.dart | 4 +- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/packages/custom_lint/test/cli_test.dart b/packages/custom_lint/test/cli_test.dart index 91b49186..4bfe8926 100644 --- a/packages/custom_lint/test/cli_test.dart +++ b/packages/custom_lint/test/cli_test.dart @@ -1,5 +1,6 @@ import 'dart:io'; +import 'package:analyzer/error/error.dart'; import 'package:test/test.dart'; import '../bin/custom_lint.dart' as cli; @@ -86,6 +87,61 @@ lib/custom_lint_client.dart:13:29: Error: Undefined name 'createPlugin'. ); }); + test('exits with 1 when pass argument `--no-fatal-infos`', () async { + final plugin = createPlugin(name: 'test_lint', main: helloWordPluginSource); + + final app = createLintUsage( + source: { + 'lib/main.dart': 'void fn() {}', + }, + plugins: {'test_lint': plugin.uri}, + name: 'test_app', + ); + + await runWithIOOverride( + (out, err) async { + await cli.entrypoint(['--no-fatal-infos']); + + expect(err, emitsDone); + expect(exitCode, 0); + }, + currentDirectory: app, + ); + }); + + test( + 'exits with 0 when found warning and pass argument `--no-fatal-warnings`', + () async { + final plugin = createPlugin( + name: 'test_lint', + main: createPluginSource([ + TestLintRule( + code: 'hello_world', + message: 'Hello world', + errorSeverity: ErrorSeverity.WARNING, + ), + ]), + ); + + final app = createLintUsage( + source: { + 'lib/main.dart': 'void fn() {}', + }, + plugins: {'test_lint': plugin.uri}, + name: 'test_app', + ); + + await runWithIOOverride( + (out, err) async { + await cli.entrypoint(['--no-fatal-warnings']); + + expect(err, emitsDone); + expect(exitCode, 0); + }, + currentDirectory: app, + ); + }); + test('CLI lists warnings from all plugins and set exit code', () async { final plugin = createPlugin(name: 'test_lint', main: helloWordPluginSource); final plugin2 = createPlugin(name: 'test_lint2', main: oyPluginSource); diff --git a/packages/custom_lint/test/create_project.dart b/packages/custom_lint/test/create_project.dart index 7740848e..dff3c059 100644 --- a/packages/custom_lint/test/create_project.dart +++ b/packages/custom_lint/test/create_project.dart @@ -106,7 +106,9 @@ class ${fix.name} extends DartFix { class ${rule.code} extends DartLintRule { ${rule.code}() : super( - code: LintCode(name: '${rule.code}', problemMessage: '${rule.message}', errorSeverity: ErrorSeverity.${rule.errorSeverity.displayName.toUpperCase()}), + code: LintCode(name: '${rule.code}', + problemMessage: '${rule.message}', + errorSeverity: ErrorSeverity.${rule.errorSeverity.displayName.toUpperCase()}), ); $fixes From 0267564d84a269b0f82c5586ec6cc7a51375ddcf Mon Sep 17 00:00:00 2001 From: yamarkz Date: Sat, 12 Aug 2023 12:10:42 +0900 Subject: [PATCH 13/19] Fix test case name --- packages/custom_lint/test/cli_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/custom_lint/test/cli_test.dart b/packages/custom_lint/test/cli_test.dart index 4bfe8926..2dacb3fd 100644 --- a/packages/custom_lint/test/cli_test.dart +++ b/packages/custom_lint/test/cli_test.dart @@ -87,7 +87,7 @@ lib/custom_lint_client.dart:13:29: Error: Undefined name 'createPlugin'. ); }); - test('exits with 1 when pass argument `--no-fatal-infos`', () async { + test('exits with 0 when pass argument `--no-fatal-infos`', () async { final plugin = createPlugin(name: 'test_lint', main: helloWordPluginSource); final app = createLintUsage( From 882b9bb00bf7a6b811149ccff1366ed44ae4374a Mon Sep 17 00:00:00 2001 From: yamarkz Date: Mon, 14 Aug 2023 18:50:41 +0900 Subject: [PATCH 14/19] Rewrite test case log output text --- packages/custom_lint/test/cli_test.dart | 28 ++++++++++++++++++------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/custom_lint/test/cli_test.dart b/packages/custom_lint/test/cli_test.dart index 2dacb3fd..586e8f3c 100644 --- a/packages/custom_lint/test/cli_test.dart +++ b/packages/custom_lint/test/cli_test.dart @@ -91,9 +91,7 @@ lib/custom_lint_client.dart:13:29: Error: Undefined name 'createPlugin'. final plugin = createPlugin(name: 'test_lint', main: helloWordPluginSource); final app = createLintUsage( - source: { - 'lib/main.dart': 'void fn() {}', - }, + source: {'lib/main.dart': 'void fn() {}'}, plugins: {'test_lint': plugin.uri}, name: 'test_app', ); @@ -102,8 +100,16 @@ lib/custom_lint_client.dart:13:29: Error: Undefined name 'createPlugin'. (out, err) async { await cli.entrypoint(['--no-fatal-infos']); - expect(err, emitsDone); expect(exitCode, 0); + expect( + out.join(), + completion( + matchIgnoringAnsi(contains, ''' +lib/main.dart:1:6 • Hello world • hello_world • INFO +'''), + ), + ); + expect(err, emitsDone); }, currentDirectory: app, ); @@ -124,9 +130,7 @@ lib/custom_lint_client.dart:13:29: Error: Undefined name 'createPlugin'. ); final app = createLintUsage( - source: { - 'lib/main.dart': 'void fn() {}', - }, + source: {'lib/main.dart': 'void fn() {}'}, plugins: {'test_lint': plugin.uri}, name: 'test_app', ); @@ -135,8 +139,16 @@ lib/custom_lint_client.dart:13:29: Error: Undefined name 'createPlugin'. (out, err) async { await cli.entrypoint(['--no-fatal-warnings']); - expect(err, emitsDone); expect(exitCode, 0); + expect( + out.join(), + completion( + matchIgnoringAnsi(contains, ''' +lib/main.dart:1:6 • Hello world • hello_world • WARNING +'''), + ), + ); + expect(err, emitsDone); }, currentDirectory: app, ); From 1717a10c70ff42701a052a4714e17b988b0cdb7c Mon Sep 17 00:00:00 2001 From: Remi Rousselet Date: Tue, 29 Aug 2023 08:23:34 +0200 Subject: [PATCH 15/19] Update redirect_logs.golden --- .../custom_lint/test/goldens/server_test/redirect_logs.golden | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/custom_lint/test/goldens/server_test/redirect_logs.golden b/packages/custom_lint/test/goldens/server_test/redirect_logs.golden index 6183a0a2..5633a113 100644 --- a/packages/custom_lint/test/goldens/server_test/redirect_logs.golden +++ b/packages/custom_lint/test/goldens/server_test/redirect_logs.golden @@ -1,4 +1,4 @@ [hello_world] 1990-01-01T00:00:00.000 Hello world [hello_world] 1990-01-01T00:00:00.000 Plugin hello_world threw while analyzing app/lib/another.dart: [hello_world] 1990-01-01T00:00:00.000 Bad state: fail -[hello_world] 1990-01-01T00:00:00.000 #0 hello_world.run. (package:test_lint/test_lint.dart:26:3) \ No newline at end of file +[hello_world] 1990-01-01T00:00:00.000 #0 hello_world.run. (package:test_lint/test_lint.dart:28:3) From 440f858458a0ad43166333209543d2774cd47fd1 Mon Sep 17 00:00:00 2001 From: Remi Rousselet Date: Tue, 29 Aug 2023 08:43:29 +0200 Subject: [PATCH 16/19] Update redirect_logs.golden From 63badebce55aca3806d6f577ec5b2ae13dba826e Mon Sep 17 00:00:00 2001 From: Remi Rousselet Date: Tue, 29 Aug 2023 08:44:40 +0200 Subject: [PATCH 17/19] Apply suggestions from code review --- .../custom_lint/test/goldens/server_test/redirect_logs.golden | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/custom_lint/test/goldens/server_test/redirect_logs.golden b/packages/custom_lint/test/goldens/server_test/redirect_logs.golden index 5633a113..812699ae 100644 --- a/packages/custom_lint/test/goldens/server_test/redirect_logs.golden +++ b/packages/custom_lint/test/goldens/server_test/redirect_logs.golden @@ -1,4 +1,4 @@ [hello_world] 1990-01-01T00:00:00.000 Hello world [hello_world] 1990-01-01T00:00:00.000 Plugin hello_world threw while analyzing app/lib/another.dart: [hello_world] 1990-01-01T00:00:00.000 Bad state: fail -[hello_world] 1990-01-01T00:00:00.000 #0 hello_world.run. (package:test_lint/test_lint.dart:28:3) +[hello_world] 1990-01-01T00:00:00.000 #0 hello_world.run. (package:test_lint/test_lint.dart:28:3)``` From 139fa680f11aab6ec0efb2f0f4d978f4852c5e66 Mon Sep 17 00:00:00 2001 From: Remi Rousselet Date: Tue, 29 Aug 2023 08:45:43 +0200 Subject: [PATCH 18/19] Fix golden --- .../custom_lint/test/goldens/server_test/redirect_logs.golden | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/custom_lint/test/goldens/server_test/redirect_logs.golden b/packages/custom_lint/test/goldens/server_test/redirect_logs.golden index 812699ae..29dfd2d6 100644 --- a/packages/custom_lint/test/goldens/server_test/redirect_logs.golden +++ b/packages/custom_lint/test/goldens/server_test/redirect_logs.golden @@ -1,4 +1,4 @@ [hello_world] 1990-01-01T00:00:00.000 Hello world [hello_world] 1990-01-01T00:00:00.000 Plugin hello_world threw while analyzing app/lib/another.dart: [hello_world] 1990-01-01T00:00:00.000 Bad state: fail -[hello_world] 1990-01-01T00:00:00.000 #0 hello_world.run. (package:test_lint/test_lint.dart:28:3)``` +[hello_world] 1990-01-01T00:00:00.000 #0 hello_world.run. (package:test_lint/test_lint.dart:28:3) \ No newline at end of file From c03ffd41520f5dcd8fb5829d383afb97e3aaf568 Mon Sep 17 00:00:00 2001 From: Remi Rousselet Date: Tue, 29 Aug 2023 09:33:46 +0200 Subject: [PATCH 19/19] Increase timeout --- packages/custom_lint/test/server_test.dart | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/custom_lint/test/server_test.dart b/packages/custom_lint/test/server_test.dart index e73e23c8..a9043041 100644 --- a/packages/custom_lint/test/server_test.dart +++ b/packages/custom_lint/test/server_test.dart @@ -385,7 +385,9 @@ if (node.name.lexeme == "fail") { }); group('hot-reload', () { - test('Supports starting custom_lint twice in watch mode at once', () async { + test( + timeout: const Timeout.factor(2), + 'Supports starting custom_lint twice in watch mode at once', () async { final plugin = createPlugin( name: 'test_lint', main: helloWordPluginSource,