| 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:async'; |
| 7 import 'dart:io'; | 8 import 'dart:io'; |
| 8 | 9 |
| 9 import 'package:path/path.dart' as p; | 10 import 'package:path/path.dart' as p; |
| 11 import 'package:scheduled_test/descriptor.dart' as d; |
| 12 import 'package:scheduled_test/scheduled_stream.dart'; |
| 13 import 'package:scheduled_test/scheduled_test.dart'; |
| 10 import 'package:test/src/util/exit_codes.dart' as exit_codes; | 14 import 'package:test/src/util/exit_codes.dart' as exit_codes; |
| 11 import 'package:test/test.dart'; | |
| 12 | 15 |
| 13 import '../io.dart'; | 16 import '../io.dart'; |
| 14 | 17 |
| 15 String _sandbox; | 18 /// The `--pub-serve` argument for the test process, based on [pubServePort]. |
| 19 Future<String> get _pubServeArg => |
| 20 pubServePort.then((port) => '--pub-serve=$port'); |
| 16 | 21 |
| 17 void main() { | 22 void main() { |
| 18 setUp(() { | 23 useSandbox(() { |
| 19 _sandbox = Directory.systemTemp.createTempSync('test_').path; | 24 d.file("pubspec.yaml", """ |
| 20 | |
| 21 new File(p.join(_sandbox, "pubspec.yaml")).writeAsStringSync(""" | |
| 22 name: myapp | 25 name: myapp |
| 23 dependencies: | 26 dependencies: |
| 24 barback: any | 27 barback: any |
| 25 test: {path: ${p.current}} | 28 test: {path: ${p.current}} |
| 26 transformers: | 29 transformers: |
| 27 - myapp: | 30 - myapp: |
| 28 \$include: test/**_test.dart | 31 \$include: test/**_test.dart |
| 29 - test/pub_serve: | 32 - test/pub_serve: |
| 30 \$include: test/**_test.dart | 33 \$include: test/**_test.dart |
| 31 dependency_overrides: | 34 dependency_overrides: |
| 32 matcher: '0.12.0-alpha.0' | 35 matcher: '0.12.0-alpha.0' |
| 33 """); | 36 """).create(); |
| 34 | 37 |
| 35 new Directory(p.join(_sandbox, "test")).createSync(); | 38 d.dir("test", [ |
| 36 | 39 d.file("my_test.dart", """ |
| 37 new File(p.join(_sandbox, "test", "my_test.dart")).writeAsStringSync(""" | |
| 38 import 'package:test/test.dart'; | 40 import 'package:test/test.dart'; |
| 39 | 41 |
| 40 void main() { | 42 void main() { |
| 41 test("test", () => expect(true, isTrue)); | 43 test("test", () => expect(true, isTrue)); |
| 42 } | 44 } |
| 43 """); | 45 """) |
| 46 ]).create(); |
| 44 | 47 |
| 45 new Directory(p.join(_sandbox, "lib")).createSync(); | 48 d.dir("lib", [ |
| 46 | 49 d.file("myapp.dart", """ |
| 47 new File(p.join(_sandbox, "lib", "myapp.dart")).writeAsStringSync(""" | |
| 48 import 'package:barback/barback.dart'; | 50 import 'package:barback/barback.dart'; |
| 49 | 51 |
| 50 class MyTransformer extends Transformer { | 52 class MyTransformer extends Transformer { |
| 51 final allowedExtensions = '.dart'; | 53 final allowedExtensions = '.dart'; |
| 52 | 54 |
| 53 MyTransformer.asPlugin(); | 55 MyTransformer.asPlugin(); |
| 54 | 56 |
| 55 Future apply(Transform transform) async { | 57 Future apply(Transform transform) async { |
| 56 var contents = await transform.primaryInput.readAsString(); | 58 var contents = await transform.primaryInput.readAsString(); |
| 57 transform.addOutput(new Asset.fromString( | 59 transform.addOutput(new Asset.fromString( |
| 58 transform.primaryInput.id, | 60 transform.primaryInput.id, |
| 59 contents.replaceAll("isFalse", "isTrue"))); | 61 contents.replaceAll("isFalse", "isTrue"))); |
| 60 } | 62 } |
| 61 } | 63 } |
| 62 """); | 64 """) |
| 65 ]).create(); |
| 63 | 66 |
| 64 var pubGetResult = runPub(['get'], workingDirectory: _sandbox); | 67 runPub(['get']).shouldExit(0); |
| 65 expect(pubGetResult.exitCode, equals(0)); | |
| 66 }); | |
| 67 | |
| 68 tearDown(() { | |
| 69 // On Windows, there's no way to shut down the actual "pub serve" process. | |
| 70 // Killing the process we start will just kill the batch file wrapper (issue | |
| 71 // 23304), not the underlying "pub serve" process. Since that process has | |
| 72 // locks on files in the sandbox, we can't delete the sandbox on Windows | |
| 73 // without errors. | |
| 74 if (Platform.isWindows) return; | |
| 75 | |
| 76 new Directory(_sandbox).deleteSync(recursive: true); | |
| 77 }); | 68 }); |
| 78 | 69 |
| 79 group("with transformed tests", () { | 70 group("with transformed tests", () { |
| 80 setUp(() { | 71 setUp(() { |
| 81 // Give the test a failing assertion that the transformer will convert to | 72 // Give the test a failing assertion that the transformer will convert to |
| 82 // a passing assertion. | 73 // a passing assertion. |
| 83 new File(p.join(_sandbox, "test", "my_test.dart")).writeAsStringSync(""" | 74 d.file("test/my_test.dart", """ |
| 84 import 'package:test/test.dart'; | 75 import 'package:test/test.dart'; |
| 85 | 76 |
| 86 void main() { | 77 void main() { |
| 87 test("test", () => expect(true, isFalse)); | 78 test("test", () => expect(true, isFalse)); |
| 88 } | 79 } |
| 89 """); | 80 """).create(); |
| 90 }); | 81 }); |
| 91 | 82 |
| 92 test("runs those tests in the VM", () async { | 83 test("runs those tests in the VM", () { |
| 93 var pair = await startPubServe(workingDirectory: _sandbox); | 84 var pub = runPubServe(); |
| 94 try { | 85 var test = runTest([_pubServeArg]); |
| 95 var result = runTest(['--pub-serve=${pair.last}'], | 86 test.stdout.expect(consumeThrough(contains('+1: All tests passed!'))); |
| 96 workingDirectory: _sandbox); | 87 test.shouldExit(0); |
| 97 expect(result.exitCode, equals(0)); | 88 pub.kill(); |
| 98 expect(result.stdout, contains('+1: All tests passed!')); | |
| 99 } finally { | |
| 100 pair.first.kill(); | |
| 101 } | |
| 102 }); | 89 }); |
| 103 | 90 |
| 104 test("runs those tests on Chrome", () async { | 91 test("runs those tests on Chrome", () { |
| 105 var pair = await startPubServe(workingDirectory: _sandbox); | 92 var pub = runPubServe(); |
| 106 try { | 93 var test = runTest([_pubServeArg, '-p', 'chrome']); |
| 107 var result = runTest(['--pub-serve=${pair.last}', '-p', 'chrome'], | 94 test.stdout.expect(consumeThrough(contains('+1: All tests passed!'))); |
| 108 workingDirectory: _sandbox); | 95 test.shouldExit(0); |
| 109 expect(result.exitCode, equals(0)); | 96 pub.kill(); |
| 110 expect(result.stdout, contains('+1: All tests passed!')); | |
| 111 } finally { | |
| 112 pair.first.kill(); | |
| 113 } | |
| 114 }); | 97 }); |
| 115 | 98 |
| 116 test("runs those tests on content shell", () async { | 99 test("runs those tests on content shell", () { |
| 117 var pair = await startPubServe(workingDirectory: _sandbox); | 100 var pub = runPubServe(); |
| 118 try { | 101 var test = runTest([_pubServeArg, '-p', 'content-shell']); |
| 119 var result = runTest( | 102 test.stdout.expect(consumeThrough(contains('+1: All tests passed!'))); |
| 120 ['--pub-serve=${pair.last}', '-p', 'content-shell'], | 103 test.shouldExit(0); |
| 121 workingDirectory: _sandbox); | 104 pub.kill(); |
| 122 expect(result.exitCode, equals(0)); | |
| 123 expect(result.stdout, contains('+1: All tests passed!')); | |
| 124 } finally { | |
| 125 pair.first.kill(); | |
| 126 } | |
| 127 }); | 105 }); |
| 128 | 106 |
| 129 test("gracefully handles pub serve running on the wrong directory for " | 107 test("gracefully handles pub serve running on the wrong directory for " |
| 130 "VM tests", () async { | 108 "VM tests", () { |
| 131 new Directory(p.join(_sandbox, "web")).createSync(); | 109 d.dir("web").create(); |
| 132 | 110 |
| 133 var pair = await startPubServe(args: ['web'], workingDirectory: _sandbox); | 111 var pub = runPubServe(args: ['web']); |
| 134 try { | 112 var test = runTest([_pubServeArg]); |
| 135 var result = runTest(['--pub-serve=${pair.last}'], | 113 test.stdout.expect(containsInOrder([ |
| 136 workingDirectory: _sandbox); | 114 '-1: loading ${p.join("test", "my_test.dart")}', |
| 137 expect(result.stdout, allOf([ | 115 'Failed to load "${p.join("test", "my_test.dart")}":', |
| 138 contains('-1: loading ${p.join("test", "my_test.dart")}'), | 116 '404 Not Found', |
| 139 contains('Failed to load "${p.join("test", "my_test.dart")}":'), | 117 'Make sure "pub serve" is serving the test/ directory.' |
| 140 contains('404 Not Found'), | 118 ])); |
| 141 contains('Make sure "pub serve" is serving the test/ directory.') | 119 test.shouldExit(1); |
| 142 ])); | 120 |
| 143 expect(result.exitCode, equals(1)); | 121 pub.kill(); |
| 144 } finally { | |
| 145 pair.first.kill(); | |
| 146 } | |
| 147 }); | 122 }); |
| 148 | 123 |
| 149 test("gracefully handles pub serve running on the wrong directory for " | 124 test("gracefully handles pub serve running on the wrong directory for " |
| 150 "browser tests", () async { | 125 "browser tests", () { |
| 151 new Directory(p.join(_sandbox, "web")).createSync(); | 126 d.dir("web").create(); |
| 152 | 127 |
| 153 var pair = await startPubServe(args: ['web'], workingDirectory: _sandbox); | 128 var pub = runPubServe(args: ['web']); |
| 154 try { | 129 var test = runTest([_pubServeArg, '-p', 'chrome']); |
| 155 var result = runTest(['--pub-serve=${pair.last}', '-p', 'chrome'], | 130 test.stdout.expect(containsInOrder([ |
| 156 workingDirectory: _sandbox); | 131 '-1: compiling ${p.join("test", "my_test.dart")}', |
| 157 expect(result.stdout, allOf([ | 132 'Failed to load "${p.join("test", "my_test.dart")}":', |
| 158 contains('-1: compiling ${p.join("test", "my_test.dart")}'), | 133 '404 Not Found', |
| 159 contains('Failed to load "${p.join("test", "my_test.dart")}":'), | 134 'Make sure "pub serve" is serving the test/ directory.' |
| 160 contains('404 Not Found'), | 135 ])); |
| 161 contains('Make sure "pub serve" is serving the test/ directory.') | 136 test.shouldExit(1); |
| 162 ])); | 137 |
| 163 expect(result.exitCode, equals(1)); | 138 pub.kill(); |
| 164 } finally { | |
| 165 pair.first.kill(); | |
| 166 } | |
| 167 }); | 139 }); |
| 168 | 140 |
| 169 test("gracefully handles unconfigured transformers", () async { | 141 test("gracefully handles unconfigured transformers", () { |
| 170 new File(p.join(_sandbox, "pubspec.yaml")).writeAsStringSync(""" | 142 d.file("pubspec.yaml", """ |
| 171 name: myapp | 143 name: myapp |
| 172 dependencies: | 144 dependencies: |
| 173 barback: any | 145 barback: any |
| 174 test: {path: ${p.current}} | 146 test: {path: ${p.current}} |
| 175 """); | 147 """).create(); |
| 176 | 148 |
| 177 var pair = await startPubServe(workingDirectory: _sandbox); | 149 var pub = runPubServe(); |
| 178 try { | 150 var test = runTest([_pubServeArg]); |
| 179 var result = runTest(['--pub-serve=${pair.last}'], | 151 expectStderrEquals(test, ''' |
| 180 workingDirectory: _sandbox); | |
| 181 expect(result.exitCode, equals(exit_codes.data)); | |
| 182 expect(result.stderr, equals(''' | |
| 183 When using --pub-serve, you must include the "test/pub_serve" transformer in | 152 When using --pub-serve, you must include the "test/pub_serve" transformer in |
| 184 your pubspec: | 153 your pubspec: |
| 185 | 154 |
| 186 transformers: | 155 transformers: |
| 187 - test/pub_serve: | 156 - test/pub_serve: |
| 188 \$include: test/**_test.dart | 157 \$include: test/**_test.dart |
| 189 ''')); | 158 '''); |
| 190 } finally { | 159 test.shouldExit(exit_codes.data); |
| 191 pair.first.kill(); | 160 |
| 192 } | 161 pub.kill(); |
| 193 }); | 162 }); |
| 194 }); | 163 }); |
| 195 | 164 |
| 196 group("uses a custom HTML file", () { | 165 group("uses a custom HTML file", () { |
| 197 setUp(() { | 166 setUp(() { |
| 198 new File(p.join(_sandbox, "test", "test.dart")).writeAsStringSync(""" | 167 d.dir("test", [ |
| 168 d.file("test.dart", """ |
| 199 import 'dart:html'; | 169 import 'dart:html'; |
| 200 | 170 |
| 201 import 'package:test/test.dart'; | 171 import 'package:test/test.dart'; |
| 202 | 172 |
| 203 void main() { | 173 void main() { |
| 204 test("failure", () { | 174 test("failure", () { |
| 205 expect(document.query('#foo'), isNull); | 175 expect(document.query('#foo'), isNull); |
| 206 }); | 176 }); |
| 207 } | 177 } |
| 208 """); | 178 """), |
| 209 | 179 |
| 210 new File(p.join(_sandbox, "test", "test.html")).writeAsStringSync(""" | 180 d.file("test.html", """ |
| 211 <html> | 181 <html> |
| 212 <head> | 182 <head> |
| 213 <link rel='x-dart-test' href='test.dart'> | 183 <link rel='x-dart-test' href='test.dart'> |
| 214 <script src="packages/test/dart.js"></script> | 184 <script src="packages/test/dart.js"></script> |
| 215 </head> | 185 </head> |
| 216 <body> | 186 <body> |
| 217 <div id="foo"></div> | 187 <div id="foo"></div> |
| 218 </body> | 188 </body> |
| 219 """); | 189 """) |
| 190 ]).create(); |
| 220 }); | 191 }); |
| 221 | 192 |
| 222 test("on Chrome", () async { | 193 test("on Chrome", () { |
| 223 var pair = await startPubServe(workingDirectory: _sandbox); | 194 var pub = runPubServe(); |
| 224 try { | 195 var test = runTest([_pubServeArg, '-p', 'chrome']); |
| 225 var result = runTest(['--pub-serve=${pair.last}', '-p', 'chrome'], | 196 test.stdout.expect(consumeThrough(contains('+1: All tests passed!'))); |
| 226 workingDirectory: _sandbox); | 197 test.shouldExit(0); |
| 227 expect(result.exitCode, equals(0)); | 198 pub.kill(); |
| 228 expect(result.stdout, contains('+1: All tests passed!')); | |
| 229 } finally { | |
| 230 pair.first.kill(); | |
| 231 } | |
| 232 }); | 199 }); |
| 233 | 200 |
| 234 test("on content shell", () async { | 201 test("on content shell", () { |
| 235 var pair = await startPubServe(workingDirectory: _sandbox); | 202 var pub = runPubServe(); |
| 236 try { | 203 var test = runTest([_pubServeArg, '-p', 'content-shell']); |
| 237 var result = runTest( | 204 test.stdout.expect(consumeThrough(contains('+1: All tests passed!'))); |
| 238 ['--pub-serve=${pair.last}', '-p', 'content-shell'], | 205 test.shouldExit(0); |
| 239 workingDirectory: _sandbox); | 206 pub.kill(); |
| 240 expect(result.exitCode, equals(0)); | |
| 241 expect(result.stdout, contains('+1: All tests passed!')); | |
| 242 } finally { | |
| 243 pair.first.kill(); | |
| 244 } | |
| 245 }); | 207 }); |
| 246 }); | 208 }); |
| 247 | 209 |
| 248 | |
| 249 group("with a failing test", () { | 210 group("with a failing test", () { |
| 250 setUp(() { | 211 setUp(() { |
| 251 new File(p.join(_sandbox, "test", "my_test.dart")).writeAsStringSync(""" | 212 d.file("test/my_test.dart", """ |
| 252 import 'dart:html'; | 213 import 'dart:html'; |
| 253 | 214 |
| 254 import 'package:test/test.dart'; | 215 import 'package:test/test.dart'; |
| 255 | 216 |
| 256 void main() { | 217 void main() { |
| 257 test("failure", () => throw 'oh no'); | 218 test("failure", () => throw 'oh no'); |
| 258 } | 219 } |
| 259 """); | 220 """).create(); |
| 260 }); | 221 }); |
| 261 | 222 |
| 262 test("dartifies stack traces for JS-compiled tests by default", () async { | 223 test("dartifies stack traces for JS-compiled tests by default", () { |
| 263 var pair = await startPubServe(workingDirectory: _sandbox); | 224 var pub = runPubServe(); |
| 264 try { | 225 var test = runTest([_pubServeArg, '-p', 'chrome', '--verbose-trace']); |
| 265 var result = runTest([ | 226 test.stdout.expect(containsInOrder([ |
| 266 '--pub-serve=${pair.last}', | 227 " main.<fn>", |
| 267 '-p', 'chrome', | 228 "package:test", |
| 268 '--verbose-trace' | 229 "dart:async/zone.dart" |
| 269 ], workingDirectory: _sandbox); | 230 ])); |
| 270 expect(result.stdout, contains(" main.<fn>\n")); | 231 test.shouldExit(1); |
| 271 expect(result.stdout, contains("package:test")); | 232 pub.kill(); |
| 272 expect(result.stdout, contains("dart:async/zone.dart")); | |
| 273 expect(result.exitCode, equals(1)); | |
| 274 } finally { | |
| 275 pair.first.kill(); | |
| 276 } | |
| 277 }); | 233 }); |
| 278 | 234 |
| 279 test("doesn't dartify stack traces for JS-compiled tests with --js-trace", | 235 test("doesn't dartify stack traces for JS-compiled tests with --js-trace", |
| 280 () async { | 236 () { |
| 281 var pair = await startPubServe(workingDirectory: _sandbox); | 237 var pub = runPubServe(); |
| 282 try { | 238 var test = runTest([ |
| 283 var result = runTest([ | 239 _pubServeArg, |
| 284 '--pub-serve=${pair.last}', | 240 '-p', 'chrome', |
| 285 '-p', 'chrome', | 241 '--js-trace', |
| 286 '--js-trace', | 242 '--verbose-trace' |
| 287 '--verbose-trace' | 243 ]); |
| 288 ], workingDirectory: _sandbox); | 244 |
| 289 expect(result.stdout, isNot(contains(" main.<fn>\n"))); | 245 test.stdout.fork().expect(never(endsWith(" main.<fn>"))); |
| 290 expect(result.stdout, isNot(contains("package:test"))); | 246 test.stdout.fork().expect(never(contains("package:test"))); |
| 291 expect(result.stdout, isNot(contains("dart:async/zone.dart"))); | 247 test.stdout.fork().expect(never(contains("dart:async/zone.dart"))); |
| 292 expect(result.exitCode, equals(1)); | 248 test.stdout.expect(consumeThrough(contains("-1: Some tests failed."))); |
| 293 } finally { | 249 test.shouldExit(1); |
| 294 pair.first.kill(); | 250 |
| 295 } | 251 pub.kill(); |
| 296 }); | 252 }); |
| 297 }); | 253 }); |
| 298 | 254 |
| 299 test("gracefully handles pub serve not running for VM tests", () { | 255 test("gracefully handles pub serve not running for VM tests", () { |
| 300 var result = runTest(['--pub-serve=54321'], | 256 var test = runTest(['--pub-serve=54321']); |
| 301 workingDirectory: _sandbox); | 257 test.stdout.expect(containsInOrder([ |
| 302 expect(result.stdout, allOf([ | 258 '-1: loading ${p.join("test", "my_test.dart")}', |
| 303 contains('-1: loading ${p.join("test", "my_test.dart")}'), | 259 'Failed to load "${p.join("test", "my_test.dart")}":', |
| 304 contains(''' | 260 'Error getting http://localhost:54321/my_test.dart.vm_test.dart: ' |
| 305 Failed to load "${p.join("test", "my_test.dart")}": | 261 'Connection refused', |
| 306 Error getting http://localhost:54321/my_test.dart.vm_test.dart: Connection ref
used | 262 'Make sure "pub serve" is running.' |
| 307 Make sure "pub serve" is running.''') | |
| 308 ])); | 263 ])); |
| 309 expect(result.exitCode, equals(1)); | 264 test.shouldExit(1); |
| 310 }); | 265 }); |
| 311 | 266 |
| 312 test("gracefully handles pub serve not running for browser tests", () { | 267 test("gracefully handles pub serve not running for browser tests", () { |
| 313 var result = runTest(['--pub-serve=54321', '-p', 'chrome'], | 268 var test = runTest(['--pub-serve=54321', '-p', 'chrome']); |
| 314 workingDirectory: _sandbox); | |
| 315 var message = Platform.isWindows | 269 var message = Platform.isWindows |
| 316 ? 'The remote computer refused the network connection.' | 270 ? 'The remote computer refused the network connection.' |
| 317 : 'Connection refused (errno '; | 271 : 'Connection refused (errno '; |
| 318 | 272 |
| 319 expect(result.stdout, allOf([ | 273 test.stdout.expect(containsInOrder([ |
| 320 contains('-1: compiling ${p.join("test", "my_test.dart")}'), | 274 '-1: compiling ${p.join("test", "my_test.dart")}', |
| 321 contains('Failed to load "${p.join("test", "my_test.dart")}":'), | 275 'Failed to load "${p.join("test", "my_test.dart")}":', |
| 322 contains('Error getting http://localhost:54321/my_test.dart.browser_test' | 276 'Error getting http://localhost:54321/my_test.dart.browser_test.dart.js' |
| 323 '.dart.js.map: $message'), | 277 '.map: $message', |
| 324 contains('Make sure "pub serve" is running.') | 278 'Make sure "pub serve" is running.' |
| 325 ])); | 279 ])); |
| 326 expect(result.exitCode, equals(1)); | 280 test.shouldExit(1); |
| 327 }); | 281 }); |
| 328 | 282 |
| 329 test("gracefully handles a test file not being in test/", () { | 283 test("gracefully handles a test file not being in test/", () { |
| 330 new File(p.join(_sandbox, 'test/my_test.dart')) | 284 schedule(() { |
| 331 .copySync(p.join(_sandbox, 'my_test.dart')); | 285 new File(p.join(sandbox, 'test/my_test.dart')) |
| 286 .copySync(p.join(sandbox, 'my_test.dart')); |
| 287 }); |
| 332 | 288 |
| 333 var result = runTest(['--pub-serve=54321', 'my_test.dart'], | 289 var test = runTest(['--pub-serve=54321', 'my_test.dart']); |
| 334 workingDirectory: _sandbox); | 290 test.stdout.expect(containsInOrder([ |
| 335 expect(result.stdout, allOf([ | 291 '-1: loading my_test.dart', |
| 336 contains('-1: loading my_test.dart'), | 292 'Failed to load "my_test.dart": When using "pub serve", all test files ' |
| 337 contains( | 293 'must be in test/.' |
| 338 'Failed to load "my_test.dart": When using "pub serve", all test ' | |
| 339 'files must be in test/.\n') | |
| 340 ])); | 294 ])); |
| 341 expect(result.exitCode, equals(1)); | 295 test.shouldExit(1); |
| 342 }); | 296 }); |
| 343 } | 297 } |
| OLD | NEW |