OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 @TestOn("vm") | 5 @TestOn("vm") |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 import 'dart:math' as math; | 8 import 'dart:math' as math; |
9 | 9 |
10 import 'package:path/path.dart' as p; | 10 import 'package:path/path.dart' as p; |
11 import 'package:test/src/util/exit_codes.dart' as exit_codes; | |
12 import 'package:test/src/util/io.dart'; | 11 import 'package:test/src/util/io.dart'; |
13 import 'package:test/test.dart'; | 12 import 'package:test/test.dart'; |
14 | 13 |
15 import '../io.dart'; | 14 import '../io.dart'; |
16 | 15 |
17 String _sandbox; | 16 String _sandbox; |
18 | 17 |
19 final _success = """ | 18 final _success = """ |
20 import 'dart:async'; | 19 import 'dart:async'; |
21 | 20 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 var result = _runUnittest(["--asdf"]); | 79 var result = _runUnittest(["--asdf"]); |
81 expect(result.stderr, equals(""" | 80 expect(result.stderr, equals(""" |
82 Could not find an option named "asdf". | 81 Could not find an option named "asdf". |
83 | 82 |
84 $_usage""")); | 83 $_usage""")); |
85 expect(result.exitCode, equals(exit_codes.usage)); | 84 expect(result.exitCode, equals(exit_codes.usage)); |
86 }); | 85 }); |
87 | 86 |
88 test("a non-existent file is passed", () { | 87 test("a non-existent file is passed", () { |
89 var result = _runUnittest(["file"]); | 88 var result = _runUnittest(["file"]); |
90 expect(result.stderr, equals('Failed to load "file": Does not exist.\n')); | 89 expect(result.stdout, allOf([ |
91 expect(result.exitCode, equals(exit_codes.data)); | 90 contains('-1: load error'), |
| 91 contains('Failed to load "file": Does not exist.') |
| 92 ])); |
| 93 expect(result.exitCode, equals(1)); |
92 }); | 94 }); |
93 | 95 |
94 test("the default directory doesn't exist", () { | 96 test("the default directory doesn't exist", () { |
95 var result = _runUnittest([]); | 97 var result = _runUnittest([]); |
96 expect(result.stderr, equals( | 98 expect(result.stderr, equals(""" |
97 'Failed to load "test": No test files were passed and the default ' | 99 No test files were passed and the default "test/" directory doesn't exist. |
98 'directory doesn\'t exist.\n')); | 100 |
| 101 $_usage""")); |
99 expect(result.exitCode, equals(exit_codes.data)); | 102 expect(result.exitCode, equals(exit_codes.data)); |
100 }); | 103 }); |
101 | 104 |
102 test("a test file fails to load", () { | 105 test("a test file fails to load", () { |
103 var testPath = p.join(_sandbox, "test.dart"); | 106 var testPath = p.join(_sandbox, "test.dart"); |
104 new File(testPath).writeAsStringSync("invalid Dart file"); | 107 new File(testPath).writeAsStringSync("invalid Dart file"); |
105 var result = _runUnittest(["test.dart"]); | 108 var result = _runUnittest(["test.dart"]); |
106 | 109 |
107 expect(result.stderr, equals( | 110 expect(result.stdout, allOf([ |
108 'Failed to load "${p.relative(testPath, from: _sandbox)}":\n' | 111 contains('-1: load error'), |
109 "line 1 pos 1: unexpected token 'invalid'\n" | 112 contains( |
110 "invalid Dart file\n" | 113 ' Failed to load "${p.relative(testPath, from: _sandbox)}":\n' |
111 "^\n")); | 114 " line 1 pos 1: unexpected token 'invalid'\n" |
112 expect(result.exitCode, equals(exit_codes.data)); | 115 " invalid Dart file\n" |
| 116 " ^\n") |
| 117 ])); |
| 118 expect(result.exitCode, equals(1)); |
113 }); | 119 }); |
114 | 120 |
115 // This is slightly different from the above test because it's an error | 121 // This is slightly different from the above test because it's an error |
116 // that's caught first by the analyzer when it's used to parse the file. | 122 // that's caught first by the analyzer when it's used to parse the file. |
117 test("a test file fails to parse", () { | 123 test("a test file fails to parse", () { |
118 var testPath = p.join(_sandbox, "test.dart"); | 124 var testPath = p.join(_sandbox, "test.dart"); |
119 new File(testPath).writeAsStringSync("@TestOn)"); | 125 new File(testPath).writeAsStringSync("@TestOn)"); |
120 var result = _runUnittest(["test.dart"]); | 126 var result = _runUnittest(["test.dart"]); |
121 | 127 |
122 expect(result.stderr, equals( | 128 expect(result.stdout, allOf([ |
123 'Failed to load "${p.relative(testPath, from: _sandbox)}":\n' | 129 contains('-1: load error'), |
124 "line 1 pos 8: unexpected token ')'\n" | 130 contains( |
125 "@TestOn)\n" | 131 ' Failed to load "${p.relative(testPath, from: _sandbox)}":\n' |
126 " ^\n")); | 132 " line 1 pos 8: unexpected token ')'\n" |
127 expect(result.exitCode, equals(exit_codes.data)); | 133 " @TestOn)\n" |
| 134 " ^\n") |
| 135 ])); |
| 136 expect(result.exitCode, equals(1)); |
128 }); | 137 }); |
129 | 138 |
130 test("an annotation's structure is invalid", () { | 139 test("an annotation's structure is invalid", () { |
131 var testPath = p.join(_sandbox, "test.dart"); | 140 var testPath = p.join(_sandbox, "test.dart"); |
132 new File(testPath).writeAsStringSync("@TestOn()\nlibrary foo;"); | 141 new File(testPath).writeAsStringSync("@TestOn()\nlibrary foo;"); |
133 var result = _runUnittest(["test.dart"]); | 142 var result = _runUnittest(["test.dart"]); |
134 | 143 |
135 expect(result.stderr, equals( | 144 expect(result.stdout, allOf([ |
136 'Failed to load "${p.relative(testPath, from: _sandbox)}":\n' | 145 contains('-1: load error'), |
137 "Error on line 1, column 8: TestOn takes one argument.\n" | 146 contains( |
138 "@TestOn()\n" | 147 ' Failed to load "${p.relative(testPath, from: _sandbox)}":\n' |
139 " ^^\n")); | 148 " Error on line 1, column 8: TestOn takes one argument.\n" |
140 expect(result.exitCode, equals(exit_codes.data)); | 149 " @TestOn()\n" |
| 150 " ^^\n") |
| 151 ])); |
| 152 expect(result.exitCode, equals(1)); |
141 }); | 153 }); |
142 | 154 |
143 test("an annotation's contents are invalid", () { | 155 test("an annotation's contents are invalid", () { |
144 var testPath = p.join(_sandbox, "test.dart"); | 156 var testPath = p.join(_sandbox, "test.dart"); |
145 new File(testPath).writeAsStringSync("@TestOn('zim')\nlibrary foo;"); | 157 new File(testPath).writeAsStringSync("@TestOn('zim')\nlibrary foo;"); |
146 var result = _runUnittest(["test.dart"]); | 158 var result = _runUnittest(["test.dart"]); |
147 | 159 |
148 expect(result.stderr, equals( | 160 expect(result.stdout, allOf([ |
149 'Failed to load "${p.relative(testPath, from: _sandbox)}":\n' | 161 contains('-1: load error'), |
150 "Error on line 1, column 10: Undefined variable.\n" | 162 contains( |
151 "@TestOn('zim')\n" | 163 ' Failed to load "${p.relative(testPath, from: _sandbox)}":\n' |
152 " ^^^\n")); | 164 " Error on line 1, column 10: Undefined variable.\n" |
153 expect(result.exitCode, equals(exit_codes.data)); | 165 " @TestOn('zim')\n" |
| 166 " ^^^\n") |
| 167 ])); |
| 168 expect(result.exitCode, equals(1)); |
154 }); | 169 }); |
155 | 170 |
156 test("a test file throws", () { | 171 test("a test file throws", () { |
157 var testPath = p.join(_sandbox, "test.dart"); | 172 var testPath = p.join(_sandbox, "test.dart"); |
158 new File(testPath).writeAsStringSync("void main() => throw 'oh no';"); | 173 new File(testPath).writeAsStringSync("void main() => throw 'oh no';"); |
159 | 174 |
160 var result = _runUnittest(["test.dart"]); | 175 var result = _runUnittest(["test.dart"]); |
161 expect(result.stderr, startsWith( | 176 expect(result.stdout, allOf([ |
162 'Failed to load "${p.relative(testPath, from: _sandbox)}": oh no\n')); | 177 contains('-1: load error'), |
163 expect(result.exitCode, equals(exit_codes.data)); | 178 contains( |
| 179 'Failed to load "${p.relative(testPath, from: _sandbox)}": oh no') |
| 180 ])); |
| 181 expect(result.exitCode, equals(1)); |
164 }); | 182 }); |
165 | 183 |
166 test("a test file doesn't have a main defined", () { | 184 test("a test file doesn't have a main defined", () { |
167 var testPath = p.join(_sandbox, "test.dart"); | 185 var testPath = p.join(_sandbox, "test.dart"); |
168 new File(testPath).writeAsStringSync("void foo() {}"); | 186 new File(testPath).writeAsStringSync("void foo() {}"); |
169 | 187 |
170 var result = _runUnittest(["test.dart"]); | 188 var result = _runUnittest(["test.dart"]); |
171 expect(result.stderr, startsWith( | 189 expect(result.stdout, allOf([ |
172 'Failed to load "${p.relative(testPath, from: _sandbox)}": No ' | 190 contains('-1: load error'), |
173 'top-level main() function defined.\n')); | 191 contains( |
174 expect(result.exitCode, equals(exit_codes.data)); | 192 'Failed to load "${p.relative(testPath, from: _sandbox)}": No ' |
| 193 'top-level main() function defined.') |
| 194 ])); |
| 195 expect(result.exitCode, equals(1)); |
175 }); | 196 }); |
176 | 197 |
177 test("a test file has a non-function main", () { | 198 test("a test file has a non-function main", () { |
178 var testPath = p.join(_sandbox, "test.dart"); | 199 var testPath = p.join(_sandbox, "test.dart"); |
179 new File(testPath).writeAsStringSync("int main;"); | 200 new File(testPath).writeAsStringSync("int main;"); |
180 | 201 |
181 var result = _runUnittest(["test.dart"]); | 202 var result = _runUnittest(["test.dart"]); |
182 expect(result.stderr, startsWith( | 203 expect(result.stdout, allOf([ |
183 'Failed to load "${p.relative(testPath, from: _sandbox)}": Top-level ' | 204 contains('-1: load error'), |
184 'main getter is not a function.\n')); | 205 contains( |
185 expect(result.exitCode, equals(exit_codes.data)); | 206 'Failed to load "${p.relative(testPath, from: _sandbox)}": ' |
| 207 'Top-level main getter is not a function.') |
| 208 ])); |
| 209 expect(result.exitCode, equals(1)); |
186 }); | 210 }); |
187 | 211 |
188 test("a test file has a main with arguments", () { | 212 test("a test file has a main with arguments", () { |
189 var testPath = p.join(_sandbox, "test.dart"); | 213 var testPath = p.join(_sandbox, "test.dart"); |
190 new File(testPath).writeAsStringSync("void main(arg) {}"); | 214 new File(testPath).writeAsStringSync("void main(arg) {}"); |
191 | 215 |
192 var result = _runUnittest(["test.dart"]); | 216 var result = _runUnittest(["test.dart"]); |
193 expect(result.stderr, startsWith( | 217 expect(result.stdout, allOf([ |
194 'Failed to load "${p.relative(testPath, from: _sandbox)}": Top-level ' | 218 contains('-1: load error'), |
195 'main() function takes arguments.\n')); | 219 contains( |
196 expect(result.exitCode, equals(exit_codes.data)); | 220 'Failed to load "${p.relative(testPath, from: _sandbox)}": ' |
| 221 'Top-level main() function takes arguments.') |
| 222 ])); |
| 223 expect(result.exitCode, equals(1)); |
| 224 }); |
| 225 |
| 226 test("multiple load errors occur", () { |
| 227 var testPath = p.join(_sandbox, "test.dart"); |
| 228 new File(testPath).writeAsStringSync("invalid Dart file"); |
| 229 var result = _runUnittest(["test.dart", "nonexistent.dart"]); |
| 230 |
| 231 expect(result.stdout, allOf([ |
| 232 contains('test.dart: load error'), |
| 233 contains( |
| 234 ' Failed to load "test.dart":\n' |
| 235 " line 1 pos 1: unexpected token 'invalid'\n" |
| 236 " invalid Dart file\n" |
| 237 " ^\n"), |
| 238 contains('nonexistent.dart: load error'), |
| 239 contains('Failed to load "nonexistent.dart": Does not exist.') |
| 240 ])); |
197 }); | 241 }); |
198 | 242 |
199 // TODO(nweiz): test what happens when a test file is unreadable once issue | 243 // TODO(nweiz): test what happens when a test file is unreadable once issue |
200 // 15078 is fixed. | 244 // 15078 is fixed. |
201 }); | 245 }); |
202 | 246 |
203 group("runs successful tests", () { | 247 group("runs successful tests", () { |
204 test("defined in a single file", () { | 248 test("defined in a single file", () { |
205 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_success); | 249 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_success); |
206 var result = _runUnittest(["test.dart"]); | 250 var result = _runUnittest(["test.dart"]); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 test("directly", () { | 313 test("directly", () { |
270 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_failure); | 314 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_failure); |
271 var result = _runDart([ | 315 var result = _runDart([ |
272 "--package-root=${p.join(packageDir, 'packages')}", | 316 "--package-root=${p.join(packageDir, 'packages')}", |
273 "test.dart" | 317 "test.dart" |
274 ]); | 318 ]); |
275 expect(result.stdout, contains("Some tests failed.")); | 319 expect(result.stdout, contains("Some tests failed.")); |
276 }); | 320 }); |
277 }); | 321 }); |
278 | 322 |
| 323 test("runs tests even when a file fails to load", () { |
| 324 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_success); |
| 325 var result = _runUnittest(["test.dart", "nonexistent.dart"]); |
| 326 expect(result.stdout, contains("+1 -1: Some tests failed.")); |
| 327 expect(result.exitCode, equals(1)); |
| 328 }); |
| 329 |
279 group("flags:", () { | 330 group("flags:", () { |
280 test("with the --color flag, uses colors", () { | 331 test("with the --color flag, uses colors", () { |
281 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_failure); | 332 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_failure); |
282 var result = _runUnittest(["--color", "test.dart"]); | 333 var result = _runUnittest(["--color", "test.dart"]); |
283 // This is the color code for red. | 334 // This is the color code for red. |
284 expect(result.stdout, contains("\u001b[31m")); | 335 expect(result.stdout, contains("\u001b[31m")); |
285 }); | 336 }); |
286 | 337 |
287 group("with the --name flag,", () { | 338 group("with the --name flag,", () { |
288 test("selects tests with matching names", () { | 339 test("selects tests with matching names", () { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 }); | 373 }); |
323 | 374 |
324 test("produces an error when no tests match", () { | 375 test("produces an error when no tests match", () { |
325 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_success); | 376 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_success); |
326 | 377 |
327 var result = _runUnittest(["--name", "no match", "test.dart"]); | 378 var result = _runUnittest(["--name", "no match", "test.dart"]); |
328 expect(result.stderr, | 379 expect(result.stderr, |
329 contains('No tests match regular expression "no match".')); | 380 contains('No tests match regular expression "no match".')); |
330 expect(result.exitCode, equals(exit_codes.data)); | 381 expect(result.exitCode, equals(exit_codes.data)); |
331 }); | 382 }); |
| 383 |
| 384 test("doesn't filter out load exceptions", () { |
| 385 var result = _runUnittest(["--name", "name", "file"]); |
| 386 expect(result.stdout, allOf([ |
| 387 contains('-1: load error'), |
| 388 contains('Failed to load "file": Does not exist.') |
| 389 ])); |
| 390 expect(result.exitCode, equals(1)); |
| 391 }); |
332 }); | 392 }); |
333 | 393 |
334 group("with the --plain-name flag,", () { | 394 group("with the --plain-name flag,", () { |
335 test("selects tests with matching names", () { | 395 test("selects tests with matching names", () { |
336 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(""" | 396 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(""" |
337 import 'dart:async'; | 397 import 'dart:async'; |
338 | 398 |
339 import 'package:test/test.dart'; | 399 import 'package:test/test.dart'; |
340 | 400 |
341 void main() { | 401 void main() { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 }); | 438 }); |
379 }); | 439 }); |
380 }); | 440 }); |
381 } | 441 } |
382 | 442 |
383 ProcessResult _runUnittest(List<String> args) => | 443 ProcessResult _runUnittest(List<String> args) => |
384 runUnittest(args, workingDirectory: _sandbox); | 444 runUnittest(args, workingDirectory: _sandbox); |
385 | 445 |
386 ProcessResult _runDart(List<String> args) => | 446 ProcessResult _runDart(List<String> args) => |
387 runDart(args, workingDirectory: _sandbox); | 447 runDart(args, workingDirectory: _sandbox); |
OLD | NEW |