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 void main() { |
| 26 setUp(() { |
| 27 _sandbox = Directory.systemTemp.createTempSync('unittest_').path; |
| 28 }); |
| 29 |
| 30 tearDown(() { |
| 31 new Directory(_sandbox).deleteSync(recursive: true); |
| 32 }); |
| 33 |
| 34 group("fails gracefully if", () { |
| 35 test("a test file fails to compile", () { |
| 36 var testPath = p.join(_sandbox, "test.dart"); |
| 37 new File(testPath).writeAsStringSync("invalid Dart file"); |
| 38 var result = _runUnittest(["-p", "chrome", "test.dart"]); |
| 39 |
| 40 expect(result.stdout, |
| 41 contains("Expected a declaration, but got 'invalid'")); |
| 42 expect(result.stderr, equals( |
| 43 'Failed to load "${p.relative(testPath, from: _sandbox)}": dart2js ' |
| 44 'failed.\n')); |
| 45 expect(result.exitCode, equals(exit_codes.data)); |
| 46 }); |
| 47 |
| 48 test("a test file throws", () { |
| 49 var testPath = p.join(_sandbox, "test.dart"); |
| 50 new File(testPath).writeAsStringSync("void main() => throw 'oh no';"); |
| 51 |
| 52 var result = _runUnittest(["-p", "chrome", "test.dart"]); |
| 53 expect(result.stderr, startsWith( |
| 54 'Failed to load "${p.relative(testPath, from: _sandbox)}": oh no\n')); |
| 55 expect(result.exitCode, equals(exit_codes.data)); |
| 56 }); |
| 57 |
| 58 test("a test file doesn't have a main defined", () { |
| 59 var testPath = p.join(_sandbox, "test.dart"); |
| 60 new File(testPath).writeAsStringSync("void foo() {}"); |
| 61 |
| 62 var result = _runUnittest(["-p", "chrome", "test.dart"]); |
| 63 expect(result.stderr, startsWith( |
| 64 'Failed to load "${p.relative(testPath, from: _sandbox)}": No ' |
| 65 'top-level main() function defined.\n')); |
| 66 expect(result.exitCode, equals(exit_codes.data)); |
| 67 }); |
| 68 |
| 69 test("a test file has a non-function main", () { |
| 70 var testPath = p.join(_sandbox, "test.dart"); |
| 71 new File(testPath).writeAsStringSync("int main;"); |
| 72 |
| 73 var result = _runUnittest(["-p", "chrome", "test.dart"]); |
| 74 expect(result.stderr, startsWith( |
| 75 'Failed to load "${p.relative(testPath, from: _sandbox)}": Top-level ' |
| 76 'main getter is not a function.\n')); |
| 77 expect(result.exitCode, equals(exit_codes.data)); |
| 78 }); |
| 79 |
| 80 test("a test file has a main with arguments", () { |
| 81 var testPath = p.join(_sandbox, "test.dart"); |
| 82 new File(testPath).writeAsStringSync("void main(arg) {}"); |
| 83 |
| 84 var result = _runUnittest(["-p", "chrome", "test.dart"]); |
| 85 expect(result.stderr, startsWith( |
| 86 'Failed to load "${p.relative(testPath, from: _sandbox)}": Top-level ' |
| 87 'main() function takes arguments.\n')); |
| 88 expect(result.exitCode, equals(exit_codes.data)); |
| 89 }); |
| 90 |
| 91 // TODO(nweiz): test what happens when a test file is unreadable once issue |
| 92 // 15078 is fixed. |
| 93 }); |
| 94 |
| 95 group("runs successful tests", () { |
| 96 test("on the browser only", () { |
| 97 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_success); |
| 98 var result = _runUnittest(["-p", "chrome", "test.dart"]); |
| 99 expect(result.exitCode, equals(0)); |
| 100 }); |
| 101 |
| 102 test("on the browser and the VM", () { |
| 103 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_success); |
| 104 var result = _runUnittest(["-p", "chrome", "-p", "vm", "test.dart"]); |
| 105 expect(result.exitCode, equals(0)); |
| 106 }); |
| 107 }); |
| 108 |
| 109 group("runs failing tests", () { |
| 110 test("on the browser only", () { |
| 111 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(""" |
| 112 import 'dart:async'; |
| 113 |
| 114 import 'package:unittest/unittest.dart'; |
| 115 |
| 116 void main() { |
| 117 test("failure", () => throw new TestFailure("oh no")); |
| 118 } |
| 119 """); |
| 120 var result = _runUnittest(["-p", "chrome", "test.dart"]); |
| 121 expect(result.exitCode, equals(1)); |
| 122 }); |
| 123 |
| 124 test("that fail only on the browser", () { |
| 125 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(""" |
| 126 import 'dart:async'; |
| 127 |
| 128 import 'package:path/path.dart' as p; |
| 129 import 'package:unittest/unittest.dart'; |
| 130 |
| 131 void main() { |
| 132 test("test", () { |
| 133 if (p.style == p.Style.url) throw new TestFailure("oh no"); |
| 134 }); |
| 135 } |
| 136 """); |
| 137 var result = _runUnittest(["-p", "chrome", "-p", "vm", "test.dart"]); |
| 138 expect(result.exitCode, equals(1)); |
| 139 }); |
| 140 |
| 141 test("that fail only on the VM", () { |
| 142 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(""" |
| 143 import 'dart:async'; |
| 144 |
| 145 import 'package:path/path.dart' as p; |
| 146 import 'package:unittest/unittest.dart'; |
| 147 |
| 148 void main() { |
| 149 test("test", () { |
| 150 if (p.style != p.Style.url) throw new TestFailure("oh no"); |
| 151 }); |
| 152 } |
| 153 """); |
| 154 var result = _runUnittest(["-p", "chrome", "-p", "vm", "test.dart"]); |
| 155 expect(result.exitCode, equals(1)); |
| 156 }); |
| 157 }); |
| 158 } |
| 159 |
| 160 ProcessResult _runUnittest(List<String> args) => |
| 161 runUnittest(args, workingDirectory: _sandbox); |
OLD | NEW |