OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 | 4 |
5 library testing.analyze; | 5 library testing.analyze; |
6 | 6 |
7 import 'dart:async' show Stream, Future; | 7 import 'dart:async' show Stream, Future; |
8 | 8 |
9 import 'dart:convert' show LineSplitter, UTF8; | 9 import 'dart:convert' show LineSplitter, UTF8; |
10 | 10 |
11 import 'dart:io' show File, Process; | 11 import 'dart:io' show File, Process; |
12 | 12 |
13 import '../testing.dart' show dartSdk; | 13 import '../testing.dart' show startDart; |
14 | 14 |
15 import 'log.dart' show isVerbose; | 15 import 'log.dart' show isVerbose; |
16 | 16 |
17 import 'suite.dart' show Suite; | 17 import 'suite.dart' show Suite; |
18 | 18 |
19 class Analyze extends Suite { | 19 class Analyze extends Suite { |
20 final Uri analysisOptions; | 20 final Uri analysisOptions; |
21 | 21 |
22 final List<Uri> uris; | 22 final List<Uri> uris; |
23 | 23 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 stream.transform(UTF8.decoder).transform(new LineSplitter()); | 100 stream.transform(UTF8.decoder).transform(new LineSplitter()); |
101 await for (String line in lines) { | 101 await for (String line in lines) { |
102 yield new AnalyzerDiagnostic.fromLine(line); | 102 yield new AnalyzerDiagnostic.fromLine(line); |
103 } | 103 } |
104 } | 104 } |
105 | 105 |
106 /// Run dartanalyzer on all tests in [uris]. | 106 /// Run dartanalyzer on all tests in [uris]. |
107 Future<Null> analyzeUris(Uri analysisOptions, Uri packages, List<Uri> uris, | 107 Future<Null> analyzeUris(Uri analysisOptions, Uri packages, List<Uri> uris, |
108 List<RegExp> exclude) async { | 108 List<RegExp> exclude) async { |
109 if (uris.isEmpty) return; | 109 if (uris.isEmpty) return; |
110 const String analyzerPath = "bin/dartanalyzer"; | 110 const String analyzerPath = "pkg/analyzer_cli/bin/analyzer.dart"; |
111 Uri analyzer = dartSdk.resolve(analyzerPath); | 111 Uri analyzer = Uri.base.resolve(analyzerPath); |
112 if (!await new File.fromUri(analyzer).exists()) { | 112 if (!await new File.fromUri(analyzer).exists()) { |
113 throw "Couldn't find '$analyzerPath' in '${dartSdk.toFilePath()}'"; | 113 throw "Couldn't find '$analyzerPath' in '${Uri.base.toFilePath()}'"; |
114 } | 114 } |
115 List<String> arguments = <String>[ | 115 List<String> arguments = <String>[ |
116 "--packages=${packages.toFilePath()}", | 116 "--packages=${packages.toFilePath()}", |
117 "--package-warnings", | 117 "--package-warnings", |
118 "--format=machine", | 118 "--format=machine", |
119 ]; | 119 ]; |
120 if (analysisOptions != null) { | 120 if (analysisOptions != null) { |
121 arguments.add("--options=${analysisOptions.toFilePath()}"); | 121 arguments.add("--options=${analysisOptions.toFilePath()}"); |
122 } | 122 } |
123 arguments.addAll(uris.map((Uri uri) => uri.toFilePath())); | 123 arguments.addAll(uris.map((Uri uri) => uri.toFilePath())); |
124 if (isVerbose) { | 124 if (isVerbose) { |
125 print("Running:\n ${analyzer.toFilePath()} ${arguments.join(' ')}"); | 125 print("Running:\n ${analyzer.toFilePath()} ${arguments.join(' ')}"); |
126 } else { | 126 } else { |
127 print("Running dartanalyzer."); | 127 print("Running dartanalyzer."); |
128 } | 128 } |
129 Stopwatch sw = new Stopwatch()..start(); | 129 Stopwatch sw = new Stopwatch()..start(); |
130 Process process = await Process.start(analyzer.toFilePath(), arguments); | 130 Process process = await startDart(analyzer, arguments); |
131 process.stdin.close(); | 131 process.stdin.close(); |
132 Future stdoutFuture = parseAnalyzerOutput(process.stdout).toList(); | 132 Future stdoutFuture = parseAnalyzerOutput(process.stdout).toList(); |
133 Future stderrFuture = parseAnalyzerOutput(process.stderr).toList(); | 133 Future stderrFuture = parseAnalyzerOutput(process.stderr).toList(); |
134 await process.exitCode; | 134 await process.exitCode; |
135 List<AnalyzerDiagnostic> diagnostics = <AnalyzerDiagnostic>[]; | 135 List<AnalyzerDiagnostic> diagnostics = <AnalyzerDiagnostic>[]; |
136 diagnostics.addAll(await stdoutFuture); | 136 diagnostics.addAll(await stdoutFuture); |
137 diagnostics.addAll(await stderrFuture); | 137 diagnostics.addAll(await stderrFuture); |
138 bool hasOutput = false; | 138 bool hasOutput = false; |
139 Set<String> seen = new Set<String>(); | 139 Set<String> seen = new Set<String>(); |
140 for (AnalyzerDiagnostic diagnostic in diagnostics) { | 140 for (AnalyzerDiagnostic diagnostic in diagnostics) { |
141 String path = diagnostic.uri.path; | 141 String path = diagnostic.uri.path; |
142 if (exclude.any((RegExp r) => path.contains(r))) continue; | 142 if (exclude.any((RegExp r) => path.contains(r))) continue; |
143 String message = "$diagnostic"; | 143 String message = "$diagnostic"; |
144 if (seen.add(message)) { | 144 if (seen.add(message)) { |
145 hasOutput = true; | 145 hasOutput = true; |
146 print(message); | 146 print(message); |
147 } | 147 } |
148 } | 148 } |
149 if (hasOutput) { | 149 if (hasOutput) { |
150 throw "Non-empty output from analyzer."; | 150 throw "Non-empty output from analyzer."; |
151 } | 151 } |
152 sw.stop(); | 152 sw.stop(); |
153 print("Running analyzer took: ${sw.elapsed}."); | 153 print("Running analyzer took: ${sw.elapsed}."); |
154 } | 154 } |
OLD | NEW |