From 4631717f1b67dac8c1695c881de2a59867e36088 Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Tue, 18 Apr 2017 14:02:06 -0700 Subject: [PATCH] De-duplicate the dartanalyzer command output (#9441) --- dev/bots/test.dart | 2 +- .../lib/src/commands/analyze_once.dart | 16 +++++++ .../flutter_tools/test/analyze_once_test.dart | 46 ++++++++++++++++--- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/dev/bots/test.dart b/dev/bots/test.dart index 07522c3966..8b35da36a3 100644 --- a/dev/bots/test.dart +++ b/dev/bots/test.dart @@ -118,7 +118,7 @@ Future _pubRunTest( String workingDirectory, { String testPath, }) { - final List args = ['run', 'test']; + final List args = ['run', 'test', '-rexpanded']; if (testPath != null) args.add(testPath); return _runCommand(pub, args, workingDirectory: workingDirectory); diff --git a/packages/flutter_tools/lib/src/commands/analyze_once.dart b/packages/flutter_tools/lib/src/commands/analyze_once.dart index ec28913fa4..c413a70c2d 100644 --- a/packages/flutter_tools/lib/src/commands/analyze_once.dart +++ b/packages/flutter_tools/lib/src/commands/analyze_once.dart @@ -11,6 +11,7 @@ import 'package:yaml/yaml.dart' as yaml; import '../base/common.dart'; import '../base/file_system.dart'; import '../base/process.dart'; +import '../base/utils.dart'; import '../cache.dart'; import '../dart/analysis.dart'; import '../globals.dart'; @@ -83,19 +84,34 @@ class AnalyzeOnce extends AnalyzeBase { final String dartanalyzer = fs.path.join(Cache.flutterRoot, 'bin', 'cache', 'dart-sdk', 'bin', 'dartanalyzer'); arguments.insert(0, dartanalyzer); bool noErrors = false; + final Set issues = new Set(); int exitCode = await runCommandAndStreamOutput( arguments, workingDirectory: workingDirectory?.path, mapFunction: (String line) { + // De-duplicate the dartanalyzer command output (https://github.com/dart-lang/sdk/issues/25697). + if (line.startsWith(' ')) { + if (!issues.add(line.trim())) + return null; + } + // Workaround for the fact that dartanalyzer does not exit with a non-zero exit code // when errors are found. // TODO(danrubel): Fix dartanalyzer to return non-zero exit code if (line == 'No issues found!') noErrors = true; + + // Remove text about the issue count ('2 hints found.'); with the duplicates + // above, the printed count would be incorrect. + if (line.endsWith(' found.')) + return null; + return line; }, ); stopwatch.stop(); + if (issues.isNotEmpty) + printStatus('${issues.length} ${pluralize('issue', issues.length)} found.'); final String elapsed = (stopwatch.elapsedMilliseconds / 1000.0).toStringAsFixed(1); // Workaround for the fact that dartanalyzer does not exit with a non-zero exit code // when errors are found. diff --git a/packages/flutter_tools/test/analyze_once_test.dart b/packages/flutter_tools/test/analyze_once_test.dart index b9f0bd1078..c4dc9861fd 100644 --- a/packages/flutter_tools/test/analyze_once_test.dart +++ b/packages/flutter_tools/test/analyze_once_test.dart @@ -84,14 +84,14 @@ void main() { ); await libMain.writeAsString(source); - /// Analyze in the current directory - no arguments + // Analyze in the current directory - no arguments await runCommand( command: new AnalyzeCommand(workingDirectory: tempDir), arguments: ['analyze'], statusTextContains: [ 'Analyzing', 'warning $analyzerSeparator The parameter \'child\' is required', - '1 warning found.', + '1 issue found.', ], toolExit: true, ); @@ -105,7 +105,7 @@ void main() { statusTextContains: [ 'Analyzing', 'warning $analyzerSeparator The parameter \'child\' is required', - '1 warning found.', + '1 issue found.', ], toolExit: true, ); @@ -124,7 +124,7 @@ void main() { - only_throw_errors '''); - /// Analyze in the current directory - no arguments + // Analyze in the current directory - no arguments await runCommand( command: new AnalyzeCommand(workingDirectory: tempDir), arguments: ['analyze'], @@ -132,12 +132,46 @@ void main() { 'Analyzing', 'warning $analyzerSeparator The parameter \'child\' is required', 'lint $analyzerSeparator Only throw instances of classes extending either Exception or Error', - '1 warning and 1 lint found.', + '2 issues found.', ], toolExit: true, ); }); + testUsingContext('flutter analyze no duplicate issues', () async { + final Directory tempDir = fs.systemTempDirectory.createTempSync('analyze_once_test_').absolute; + + try { + final File foo = fs.file(fs.path.join(tempDir.path, 'foo.dart')); + foo.writeAsStringSync(''' +import 'bar.dart'; + +foo() => bar(); +'''); + + final File bar = fs.file(fs.path.join(tempDir.path, 'bar.dart')); + bar.writeAsStringSync(''' +import 'dart:async'; // unused + +void bar() { +} +'''); + + // Analyze in the current directory - no arguments + await runCommand( + command: new AnalyzeCommand(workingDirectory: tempDir), + arguments: ['analyze'], + statusTextContains: [ + 'Analyzing', + '1 issue found.', + ], + toolExit: true, + ); + } finally { + tempDir.deleteSync(recursive: true); + } + }); + // Analyze a specific file outside the current directory testUsingContext('flutter analyze one file with local options', () async { await runCommand( @@ -147,7 +181,7 @@ void main() { 'Analyzing', 'warning $analyzerSeparator The parameter \'child\' is required', 'lint $analyzerSeparator Only throw instances of classes extending either Exception or Error', - '1 warning and 1 lint found.', + '2 issues found.', ], toolExit: true, );