| 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/util/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 import 'package:unittest/unittest.dart'; | |
| 19 | |
| 20 void main() { | |
| 21 test("success", () {}); | |
| 22 } | |
| 23 """; | |
| 24 | |
| 25 final _failure = """ | |
| 26 import 'dart:async'; | |
| 27 | |
| 28 import 'package:unittest/unittest.dart'; | |
| 29 | |
| 30 void main() { | |
| 31 test("failure", () => throw new TestFailure("oh no")); | |
| 32 } | |
| 33 """; | |
| 34 | |
| 35 final _usage = """ | |
| 36 Usage: pub run unittest:unittest [files or directories...] | |
| 37 | |
| 38 -h, --help Shows this usage information. | |
| 39 --[no-]color Whether to use terminal colors. | |
| 40 (auto-detected by default) | |
| 41 """; | |
| 42 | |
| 43 void main() { | |
| 44 setUp(() { | |
| 45 _sandbox = Directory.systemTemp.createTempSync('unittest_').path; | |
| 46 }); | |
| 47 | |
| 48 tearDown(() { | |
| 49 new Directory(_sandbox).deleteSync(recursive: true); | |
| 50 }); | |
| 51 | |
| 52 test("prints help information", () { | |
| 53 var result = _runUnittest(["--help"]); | |
| 54 expect(result.stdout, equals(""" | |
| 55 Runs tests in this package. | |
| 56 | |
| 57 $_usage""")); | |
| 58 expect(result.exitCode, equals(exit_codes.success)); | |
| 59 }); | |
| 60 | |
| 61 group("fails gracefully if", () { | |
| 62 test("an invalid option is passed", () { | |
| 63 var result = _runUnittest(["--asdf"]); | |
| 64 expect(result.stderr, equals(""" | |
| 65 Could not find an option named "asdf". | |
| 66 | |
| 67 $_usage""")); | |
| 68 expect(result.exitCode, equals(exit_codes.usage)); | |
| 69 }); | |
| 70 | |
| 71 test("a non-existent file is passed", () { | |
| 72 var result = _runUnittest(["file"]); | |
| 73 expect(result.stderr, equals('Failed to load "file": Does not exist.\n')); | |
| 74 expect(result.exitCode, equals(exit_codes.data)); | |
| 75 }); | |
| 76 | |
| 77 test("the default directory doesn't exist", () { | |
| 78 var result = _runUnittest([]); | |
| 79 expect(result.stderr, equals( | |
| 80 'Failed to load "test": No test files were passed and the default ' | |
| 81 'directory doesn\'t exist.\n')); | |
| 82 expect(result.exitCode, equals(exit_codes.data)); | |
| 83 }); | |
| 84 | |
| 85 test("a test file fails to load", () { | |
| 86 var testPath = p.join(_sandbox, "test.dart"); | |
| 87 new File(testPath).writeAsStringSync("invalid Dart file"); | |
| 88 var result = _runUnittest(["test.dart"]); | |
| 89 | |
| 90 expect(result.stderr, equals( | |
| 91 'Failed to load "${p.relative(testPath, from: _sandbox)}":\n' | |
| 92 "line 1 pos 1: unexpected token 'invalid'\n" | |
| 93 "invalid Dart file\n" | |
| 94 "^\n")); | |
| 95 expect(result.exitCode, equals(exit_codes.data)); | |
| 96 }); | |
| 97 | |
| 98 test("a test file throws", () { | |
| 99 var testPath = p.join(_sandbox, "test.dart"); | |
| 100 new File(testPath).writeAsStringSync("void main() => throw 'oh no';"); | |
| 101 | |
| 102 var result = _runUnittest(["test.dart"]); | |
| 103 expect(result.stderr, startsWith( | |
| 104 'Failed to load "${p.relative(testPath, from: _sandbox)}": oh no\n')); | |
| 105 expect(result.exitCode, equals(exit_codes.data)); | |
| 106 }); | |
| 107 | |
| 108 test("a test file doesn't have a main defined", () { | |
| 109 var testPath = p.join(_sandbox, "test.dart"); | |
| 110 new File(testPath).writeAsStringSync("void foo() {}"); | |
| 111 | |
| 112 var result = _runUnittest(["test.dart"]); | |
| 113 expect(result.stderr, startsWith( | |
| 114 'Failed to load "${p.relative(testPath, from: _sandbox)}": No ' | |
| 115 'top-level main() function defined.\n')); | |
| 116 expect(result.exitCode, equals(exit_codes.data)); | |
| 117 }); | |
| 118 | |
| 119 test("a test file has a non-function main", () { | |
| 120 var testPath = p.join(_sandbox, "test.dart"); | |
| 121 new File(testPath).writeAsStringSync("int main;"); | |
| 122 | |
| 123 var result = _runUnittest(["test.dart"]); | |
| 124 expect(result.stderr, startsWith( | |
| 125 'Failed to load "${p.relative(testPath, from: _sandbox)}": Top-level ' | |
| 126 'main getter is not a function.\n')); | |
| 127 expect(result.exitCode, equals(exit_codes.data)); | |
| 128 }); | |
| 129 | |
| 130 test("a test file has a main with arguments", () { | |
| 131 var testPath = p.join(_sandbox, "test.dart"); | |
| 132 new File(testPath).writeAsStringSync("void main(arg) {}"); | |
| 133 | |
| 134 var result = _runUnittest(["test.dart"]); | |
| 135 expect(result.stderr, startsWith( | |
| 136 'Failed to load "${p.relative(testPath, from: _sandbox)}": Top-level ' | |
| 137 'main() function takes arguments.\n')); | |
| 138 expect(result.exitCode, equals(exit_codes.data)); | |
| 139 }); | |
| 140 | |
| 141 // TODO(nweiz): test what happens when a test file is unreadable once issue | |
| 142 // 15078 is fixed. | |
| 143 }); | |
| 144 | |
| 145 group("runs successful tests", () { | |
| 146 test("defined in a single file", () { | |
| 147 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_success); | |
| 148 var result = _runUnittest(["test.dart"]); | |
| 149 expect(result.exitCode, equals(0)); | |
| 150 }); | |
| 151 | |
| 152 test("defined in a directory", () { | |
| 153 for (var i = 0; i < 3; i++) { | |
| 154 new File(p.join(_sandbox, "${i}_test.dart")) | |
| 155 .writeAsStringSync(_success); | |
| 156 } | |
| 157 | |
| 158 var result = _runUnittest(["."]); | |
| 159 expect(result.exitCode, equals(0)); | |
| 160 }); | |
| 161 | |
| 162 test("defaulting to the test directory", () { | |
| 163 new Directory(p.join(_sandbox, "test")).createSync(); | |
| 164 for (var i = 0; i < 3; i++) { | |
| 165 new File(p.join(_sandbox, "test", "${i}_test.dart")) | |
| 166 .writeAsStringSync(_success); | |
| 167 } | |
| 168 | |
| 169 var result = _runUnittest([]); | |
| 170 expect(result.exitCode, equals(0)); | |
| 171 }); | |
| 172 | |
| 173 test("directly", () { | |
| 174 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_success); | |
| 175 var result = _runDart([ | |
| 176 "--package-root=${p.join(packageDir, 'packages')}", | |
| 177 "test.dart" | |
| 178 ]); | |
| 179 expect(result.stdout, contains("All tests passed!")); | |
| 180 }); | |
| 181 }); | |
| 182 | |
| 183 group("runs failing tests", () { | |
| 184 test("defined in a single file", () { | |
| 185 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_failure); | |
| 186 var result = _runUnittest(["test.dart"]); | |
| 187 expect(result.exitCode, equals(1)); | |
| 188 }); | |
| 189 | |
| 190 test("defined in a directory", () { | |
| 191 for (var i = 0; i < 3; i++) { | |
| 192 new File(p.join(_sandbox, "${i}_test.dart")) | |
| 193 .writeAsStringSync(_failure); | |
| 194 } | |
| 195 | |
| 196 var result = _runUnittest(["."]); | |
| 197 expect(result.exitCode, equals(1)); | |
| 198 }); | |
| 199 | |
| 200 test("defaulting to the test directory", () { | |
| 201 new Directory(p.join(_sandbox, "test")).createSync(); | |
| 202 for (var i = 0; i < 3; i++) { | |
| 203 new File(p.join(_sandbox, "test", "${i}_test.dart")) | |
| 204 .writeAsStringSync(_failure); | |
| 205 } | |
| 206 | |
| 207 var result = _runUnittest([]); | |
| 208 expect(result.exitCode, equals(1)); | |
| 209 }); | |
| 210 | |
| 211 test("directly", () { | |
| 212 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_failure); | |
| 213 var result = _runDart([ | |
| 214 "--package-root=${p.join(packageDir, 'packages')}", | |
| 215 "test.dart" | |
| 216 ]); | |
| 217 expect(result.stdout, contains("Some tests failed.")); | |
| 218 }); | |
| 219 }); | |
| 220 | |
| 221 group("flags", () { | |
| 222 test("with the --color flag, uses colors", () { | |
| 223 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_failure); | |
| 224 var result = _runUnittest(["--color", "test.dart"]); | |
| 225 // This is the color code for red. | |
| 226 expect(result.stdout, contains("\u001b[31m")); | |
| 227 }); | |
| 228 }); | |
| 229 } | |
| 230 | |
| 231 ProcessResult _runUnittest(List<String> args) => | |
| 232 runUnittest(args, workingDirectory: _sandbox); | |
| 233 | |
| 234 ProcessResult _runDart(List<String> args) => | |
| 235 runDart(args, workingDirectory: _sandbox); | |
| OLD | NEW |