OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'dart:io'; |
| 6 |
| 7 import 'package:path/path.dart' as p; |
| 8 import 'package:unittest/src/exit_codes.dart' as exit_codes; |
| 9 import 'package:unittest/unittest.dart'; |
| 10 |
| 11 import 'io.dart'; |
| 12 |
| 13 String _sandbox; |
| 14 |
| 15 final _success = """ |
| 16 import 'dart:async'; |
| 17 |
| 18 void main() { |
| 19 var declarer = Zone.current[#unittest.declarer]; |
| 20 declarer.test("success", () {}); |
| 21 } |
| 22 """; |
| 23 |
| 24 final _failure = """ |
| 25 import 'dart:async'; |
| 26 |
| 27 import 'package:unittest/unittest.dart'; |
| 28 |
| 29 void main() { |
| 30 var declarer = Zone.current[#unittest.declarer]; |
| 31 declarer.test("failure", () => throw new TestFailure("oh no")); |
| 32 } |
| 33 """; |
| 34 |
| 35 void main() { |
| 36 setUp(() { |
| 37 _sandbox = Directory.systemTemp.createTempSync('unittest_').path; |
| 38 }); |
| 39 |
| 40 tearDown(() { |
| 41 new Directory(_sandbox).deleteSync(recursive: true); |
| 42 }); |
| 43 |
| 44 test("prints help information", () { |
| 45 var result = _runUnittest(["--help"]); |
| 46 expect(result.stdout, equals(""" |
| 47 Runs tests in this package. |
| 48 |
| 49 Usage: pub run unittest:unittest [files or directories...] |
| 50 |
| 51 -h, --help Shows this usage information. |
| 52 """)); |
| 53 expect(result.exitCode, equals(exit_codes.success)); |
| 54 }); |
| 55 |
| 56 group("fails gracefully if", () { |
| 57 test("an invalid option is passed", () { |
| 58 var result = _runUnittest(["--asdf"]); |
| 59 expect(result.stderr, equals(""" |
| 60 Could not find an option named "asdf". |
| 61 |
| 62 Usage: pub run unittest:unittest [files or directories...] |
| 63 |
| 64 -h, --help Shows this usage information. |
| 65 """)); |
| 66 expect(result.exitCode, equals(exit_codes.usage)); |
| 67 }); |
| 68 |
| 69 test("a non-existent file is passed", () { |
| 70 var result = _runUnittest(["file"]); |
| 71 expect(result.stderr, equals('Failed to load "file": Does not exist.\n')); |
| 72 expect(result.exitCode, equals(exit_codes.data)); |
| 73 }); |
| 74 |
| 75 test("the default directory doesn't exist", () { |
| 76 var result = _runUnittest([]); |
| 77 expect(result.stderr, equals( |
| 78 'Failed to load "test": No test files were passed and the default ' |
| 79 'directory doesn\'t exist.\n')); |
| 80 expect(result.exitCode, equals(exit_codes.data)); |
| 81 }); |
| 82 |
| 83 test("a test file fails to load", () { |
| 84 var testPath = p.join(_sandbox, "test.dart"); |
| 85 new File(testPath).writeAsStringSync("invalid Dart file"); |
| 86 var result = _runUnittest(["test.dart"]); |
| 87 |
| 88 expect(result.stderr, equals( |
| 89 'Failed to load "${p.relative(testPath, from: _sandbox)}":\n' |
| 90 "line 1 pos 1: unexpected token 'invalid'\n" |
| 91 "invalid Dart file\n" |
| 92 "^\n")); |
| 93 expect(result.exitCode, equals(exit_codes.data)); |
| 94 }); |
| 95 |
| 96 test("a test file throws", () { |
| 97 var testPath = p.join(_sandbox, "test.dart"); |
| 98 new File(testPath).writeAsStringSync("void main() => throw 'oh no';"); |
| 99 |
| 100 var result = _runUnittest(["test.dart"]); |
| 101 expect(result.stderr, startsWith( |
| 102 'Failed to load "${p.relative(testPath, from: _sandbox)}": oh no\n')); |
| 103 expect(result.exitCode, equals(exit_codes.data)); |
| 104 }); |
| 105 |
| 106 test("a test file doesn't have a main defined", () { |
| 107 var testPath = p.join(_sandbox, "test.dart"); |
| 108 new File(testPath).writeAsStringSync("void foo() {}"); |
| 109 |
| 110 var result = _runUnittest(["test.dart"]); |
| 111 expect(result.stderr, startsWith( |
| 112 'Failed to load "${p.relative(testPath, from: _sandbox)}": No ' |
| 113 'top-level main() function defined.\n')); |
| 114 expect(result.exitCode, equals(exit_codes.data)); |
| 115 }); |
| 116 |
| 117 test("a test file has a non-function main", () { |
| 118 var testPath = p.join(_sandbox, "test.dart"); |
| 119 new File(testPath).writeAsStringSync("int main;"); |
| 120 |
| 121 var result = _runUnittest(["test.dart"]); |
| 122 expect(result.stderr, startsWith( |
| 123 'Failed to load "${p.relative(testPath, from: _sandbox)}": Top-level ' |
| 124 'main getter is not a function.\n')); |
| 125 expect(result.exitCode, equals(exit_codes.data)); |
| 126 }); |
| 127 |
| 128 test("a test file has a main with arguments", () { |
| 129 var testPath = p.join(_sandbox, "test.dart"); |
| 130 new File(testPath).writeAsStringSync("void main(arg) {}"); |
| 131 |
| 132 var result = _runUnittest(["test.dart"]); |
| 133 expect(result.stderr, startsWith( |
| 134 'Failed to load "${p.relative(testPath, from: _sandbox)}": Top-level ' |
| 135 'main() function takes arguments.\n')); |
| 136 expect(result.exitCode, equals(exit_codes.data)); |
| 137 }); |
| 138 |
| 139 // TODO(nweiz): test what happens when a test file is unreadable once issue |
| 140 // 15078 is fixed. |
| 141 }); |
| 142 |
| 143 group("runs successful tests", () { |
| 144 test("defined in a single file", () { |
| 145 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_success); |
| 146 var result = _runUnittest(["test.dart"]); |
| 147 expect(result.exitCode, equals(0)); |
| 148 }); |
| 149 |
| 150 test("defined in a directory", () { |
| 151 for (var i = 0; i < 3; i++) { |
| 152 new File(p.join(_sandbox, "${i}_test.dart")) |
| 153 .writeAsStringSync(_success); |
| 154 } |
| 155 |
| 156 var result = _runUnittest(["."]); |
| 157 expect(result.exitCode, equals(0)); |
| 158 }); |
| 159 |
| 160 test("defaulting to the test directory", () { |
| 161 new Directory(p.join(_sandbox, "test")).createSync(); |
| 162 for (var i = 0; i < 3; i++) { |
| 163 new File(p.join(_sandbox, "test", "${i}_test.dart")) |
| 164 .writeAsStringSync(_success); |
| 165 } |
| 166 |
| 167 var result = _runUnittest([]); |
| 168 expect(result.exitCode, equals(0)); |
| 169 }); |
| 170 }); |
| 171 |
| 172 group("runs failing tests", () { |
| 173 test("defined in a single file", () { |
| 174 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_failure); |
| 175 var result = _runUnittest(["test.dart"]); |
| 176 expect(result.exitCode, equals(1)); |
| 177 }); |
| 178 |
| 179 test("defined in a directory", () { |
| 180 for (var i = 0; i < 3; i++) { |
| 181 new File(p.join(_sandbox, "${i}_test.dart")) |
| 182 .writeAsStringSync(_failure); |
| 183 } |
| 184 |
| 185 var result = _runUnittest(["."]); |
| 186 expect(result.exitCode, equals(1)); |
| 187 }); |
| 188 |
| 189 test("defaulting to the test directory", () { |
| 190 new Directory(p.join(_sandbox, "test")).createSync(); |
| 191 for (var i = 0; i < 3; i++) { |
| 192 new File(p.join(_sandbox, "test", "${i}_test.dart")) |
| 193 .writeAsStringSync(_failure); |
| 194 } |
| 195 |
| 196 var result = _runUnittest([]); |
| 197 expect(result.exitCode, equals(1)); |
| 198 }); |
| 199 }); |
| 200 } |
| 201 |
| 202 ProcessResult _runUnittest(List<String> args) => |
| 203 runUnittest(args, workingDirectory: _sandbox); |
OLD | NEW |