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