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 | 7 import 'dart:async' show Stream, Future; |
8 Stream, | |
9 Future; | |
10 | 8 |
11 import 'dart:convert' show | 9 import 'dart:convert' show LineSplitter, UTF8; |
12 LineSplitter, | |
13 UTF8; | |
14 | 10 |
15 import 'dart:io' show | 11 import 'dart:io' show File, Process; |
16 File, | |
17 Process; | |
18 | 12 |
19 import '../testing.dart' show | 13 import '../testing.dart' show dartSdk; |
20 dartSdk; | |
21 | 14 |
22 import 'log.dart' show | 15 import 'log.dart' show isVerbose; |
23 isVerbose; | |
24 | 16 |
25 import 'suite.dart' show | 17 import 'suite.dart' show Suite; |
26 Suite; | |
27 | 18 |
28 class Analyze extends Suite { | 19 class Analyze extends Suite { |
29 final Uri analysisOptions; | 20 final Uri analysisOptions; |
30 | 21 |
31 final List<Uri> uris; | 22 final List<Uri> uris; |
32 | 23 |
33 final List<RegExp> exclude; | 24 final List<RegExp> exclude; |
34 | 25 |
35 Analyze(this.analysisOptions, this.uris, this.exclude) | 26 Analyze(this.analysisOptions, this.uris, this.exclude) |
36 : super("analyze", "analyze", null); | 27 : super("analyze", "analyze", null); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 final String message; | 69 final String message; |
79 | 70 |
80 AnalyzerDiagnostic(this.kind, this.detailedKind, this.code, this.uri, | 71 AnalyzerDiagnostic(this.kind, this.detailedKind, this.code, this.uri, |
81 this.line, this.startColumn, this.endColumn, this.message); | 72 this.line, this.startColumn, this.endColumn, this.message); |
82 | 73 |
83 factory AnalyzerDiagnostic.fromLine(String line) { | 74 factory AnalyzerDiagnostic.fromLine(String line) { |
84 List<String> parts = line.split("|"); | 75 List<String> parts = line.split("|"); |
85 if (parts.length != 8) { | 76 if (parts.length != 8) { |
86 throw "Malformed output: $line"; | 77 throw "Malformed output: $line"; |
87 } | 78 } |
88 return new AnalyzerDiagnostic(parts[0], parts[1], parts[2], | 79 return new AnalyzerDiagnostic( |
| 80 parts[0], |
| 81 parts[1], |
| 82 parts[2], |
89 Uri.base.resolve(parts[3]), | 83 Uri.base.resolve(parts[3]), |
90 int.parse(parts[4]), int.parse(parts[5]), int.parse(parts[6]), | 84 int.parse(parts[4]), |
| 85 int.parse(parts[5]), |
| 86 int.parse(parts[6]), |
91 parts[7]); | 87 parts[7]); |
92 } | 88 } |
93 | 89 |
94 String toString() { | 90 String toString() { |
95 return "$uri:$line:$startColumn: " | 91 return "$uri:$line:$startColumn: " |
96 "${kind == 'INFO' ? 'warning: hint' : kind.toLowerCase()}:\n" | 92 "${kind == 'INFO' ? 'warning: hint' : kind.toLowerCase()}:\n" |
97 "[$code] $message"; | 93 "[$code] $message"; |
98 } | 94 } |
99 } | 95 } |
100 | 96 |
101 Stream<AnalyzerDiagnostic> parseAnalyzerOutput( | 97 Stream<AnalyzerDiagnostic> parseAnalyzerOutput( |
102 Stream<List<int>> stream) async* { | 98 Stream<List<int>> stream) async* { |
103 Stream<String> lines = | 99 Stream<String> lines = |
104 stream.transform(UTF8.decoder).transform(new LineSplitter()); | 100 stream.transform(UTF8.decoder).transform(new LineSplitter()); |
105 await for (String line in lines) { | 101 await for (String line in lines) { |
106 yield new AnalyzerDiagnostic.fromLine(line); | 102 yield new AnalyzerDiagnostic.fromLine(line); |
107 } | 103 } |
108 } | 104 } |
109 | 105 |
110 /// Run dartanalyzer on all tests in [uris]. | 106 /// Run dartanalyzer on all tests in [uris]. |
111 Future<Null> analyzeUris( | 107 Future<Null> analyzeUris(Uri analysisOptions, Uri packages, List<Uri> uris, |
112 Uri analysisOptions, Uri packages, List<Uri> uris, | |
113 List<RegExp> exclude) async { | 108 List<RegExp> exclude) async { |
114 if (uris.isEmpty) return; | 109 if (uris.isEmpty) return; |
115 const String analyzerPath = "bin/dartanalyzer"; | 110 const String analyzerPath = "bin/dartanalyzer"; |
116 Uri analyzer = dartSdk.resolve(analyzerPath); | 111 Uri analyzer = dartSdk.resolve(analyzerPath); |
117 if (!await new File.fromUri(analyzer).exists()) { | 112 if (!await new File.fromUri(analyzer).exists()) { |
118 throw "Couldn't find '$analyzerPath' in '${dartSdk.toFilePath()}'"; | 113 throw "Couldn't find '$analyzerPath' in '${dartSdk.toFilePath()}'"; |
119 } | 114 } |
120 List<String> arguments = <String>[ | 115 List<String> arguments = <String>[ |
121 "--packages=${packages.toFilePath()}", | 116 "--packages=${packages.toFilePath()}", |
122 "--package-warnings", | 117 "--package-warnings", |
123 "--format=machine", | 118 "--format=machine", |
124 ]; | 119 ]; |
125 if (analysisOptions != null) { | 120 if (analysisOptions != null) { |
126 arguments.add("--options=${analysisOptions.toFilePath()}"); | 121 arguments.add("--options=${analysisOptions.toFilePath()}"); |
127 } | 122 } |
128 arguments.addAll(uris.map((Uri uri) => uri.toFilePath())); | 123 arguments.addAll(uris.map((Uri uri) => uri.toFilePath())); |
129 if (isVerbose) { | 124 if (isVerbose) { |
130 print("Running:\n ${analyzer.toFilePath()} ${arguments.join(' ')}"); | 125 print("Running:\n ${analyzer.toFilePath()} ${arguments.join(' ')}"); |
131 } else { | 126 } else { |
132 print("Running dartanalyzer."); | 127 print("Running dartanalyzer."); |
133 } | 128 } |
(...skipping 16 matching lines...) Expand all Loading... |
150 hasOutput = true; | 145 hasOutput = true; |
151 print(message); | 146 print(message); |
152 } | 147 } |
153 } | 148 } |
154 if (hasOutput) { | 149 if (hasOutput) { |
155 throw "Non-empty output from analyzer."; | 150 throw "Non-empty output from analyzer."; |
156 } | 151 } |
157 sw.stop(); | 152 sw.stop(); |
158 print("Running analyzer took: ${sw.elapsed}."); | 153 print("Running analyzer took: ${sw.elapsed}."); |
159 } | 154 } |
160 | |
OLD | NEW |