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 @TestOn("vm") |
| 6 |
| 7 import 'dart:convert'; |
| 8 import 'dart:io'; |
| 9 |
| 10 import 'package:path/path.dart' as p; |
| 11 import 'package:test/src/util/exit_codes.dart' as exit_codes; |
| 12 import 'package:test/test.dart'; |
| 13 |
| 14 import '../io.dart'; |
| 15 |
| 16 final _lines = UTF8.decoder.fuse(const LineSplitter()); |
| 17 |
| 18 final _servingRegExp = |
| 19 new RegExp(r'^Serving myapp [a-z]+ on http://localhost:(\d+)$'); |
| 20 |
| 21 String _sandbox; |
| 22 |
| 23 void main() { |
| 24 setUp(() { |
| 25 _sandbox = Directory.systemTemp.createTempSync('test_').path; |
| 26 |
| 27 new File(p.join(_sandbox, "pubspec.yaml")).writeAsStringSync(""" |
| 28 name: myapp |
| 29 dependencies: |
| 30 barback: any |
| 31 test: {path: ${p.current}} |
| 32 transformers: |
| 33 - myapp: |
| 34 \$include: test/**_test.dart |
| 35 - test/pub_serve: |
| 36 \$include: test/**_test.dart |
| 37 """); |
| 38 |
| 39 new Directory(p.join(_sandbox, "test")).createSync(); |
| 40 |
| 41 new File(p.join(_sandbox, "test", "my_test.dart")).writeAsStringSync(""" |
| 42 import 'package:test/test.dart'; |
| 43 |
| 44 void main() { |
| 45 test("test", () => expect(true, isTrue)); |
| 46 } |
| 47 """); |
| 48 }); |
| 49 |
| 50 tearDown(() { |
| 51 new Directory(_sandbox).deleteSync(recursive: true); |
| 52 }); |
| 53 |
| 54 group("with transformed tests", () { |
| 55 setUp(() { |
| 56 new Directory(p.join(_sandbox, "lib")).createSync(); |
| 57 |
| 58 new File(p.join(_sandbox, "lib", "myapp.dart")).writeAsStringSync(""" |
| 59 import 'package:barback/barback.dart'; |
| 60 |
| 61 class MyTransformer extends Transformer { |
| 62 final allowedExtensions = '.dart'; |
| 63 |
| 64 MyTransformer.asPlugin(); |
| 65 |
| 66 Future apply(Transform transform) { |
| 67 return transform.primaryInput.readAsString().then((contents) { |
| 68 print("contents: \$contents"); |
| 69 print("new contents: \${contents.replaceAll("isFalse", "isTrue")}"); |
| 70 transform.addOutput(new Asset.fromString( |
| 71 transform.primaryInput.id, |
| 72 contents.replaceAll("isFalse", "isTrue"))); |
| 73 }); |
| 74 } |
| 75 } |
| 76 """); |
| 77 |
| 78 var pubGetResult = runPub(['get'], workingDirectory: _sandbox); |
| 79 expect(pubGetResult.exitCode, equals(0)); |
| 80 }); |
| 81 |
| 82 test("runs those tests in the VM", () { |
| 83 return startPub(['serve', '--port', '0'], workingDirectory: _sandbox) |
| 84 .then((process) { |
| 85 return _lines.bind(process.stdout) |
| 86 .firstWhere(_servingRegExp.hasMatch) |
| 87 .then((line) { |
| 88 var match = _servingRegExp.firstMatch(line); |
| 89 |
| 90 try { |
| 91 var result = runUnittest(['--pub-serve=${match[1]}'], |
| 92 workingDirectory: _sandbox); |
| 93 expect(result.exitCode, equals(0)); |
| 94 expect(result.stdout, contains('+1: All tests passed!')); |
| 95 } finally { |
| 96 process.kill(); |
| 97 } |
| 98 }); |
| 99 }); |
| 100 }); |
| 101 |
| 102 test("runs those tests in the browser", () { |
| 103 return startPub(['serve', '--port', '0'], |
| 104 workingDirectory: _sandbox) |
| 105 .then((process) { |
| 106 return _lines.bind(process.stdout) |
| 107 .firstWhere(_servingRegExp.hasMatch) |
| 108 .then((line) { |
| 109 var match = _servingRegExp.firstMatch(line); |
| 110 |
| 111 try { |
| 112 var result = runUnittest( |
| 113 ['--pub-serve=${match[1]}', '-p', 'chrome'], |
| 114 workingDirectory: _sandbox); |
| 115 expect(result.exitCode, equals(0)); |
| 116 expect(result.stdout, contains('+1: All tests passed!')); |
| 117 } finally { |
| 118 process.kill(); |
| 119 } |
| 120 }); |
| 121 }); |
| 122 }); |
| 123 |
| 124 test("gracefully handles pub serve running on the wrong directory for " |
| 125 "VM tests", () { |
| 126 new Directory(p.join(_sandbox, "web")).createSync(); |
| 127 |
| 128 return startPub(['serve', '--port', '0', 'web'], |
| 129 workingDirectory: _sandbox) |
| 130 .then((process) { |
| 131 return _lines.bind(process.stdout) |
| 132 .firstWhere(_servingRegExp.hasMatch) |
| 133 .then((line) { |
| 134 var match = _servingRegExp.firstMatch(line); |
| 135 |
| 136 try { |
| 137 var result = runUnittest(['--pub-serve=${match[1]}'], |
| 138 workingDirectory: _sandbox); |
| 139 expect(result.stderr, contains( |
| 140 'Failed to load "test/my_test.dart":')); |
| 141 expect(result.stderr, contains('404 Not Found')); |
| 142 expect(result.stderr, contains( |
| 143 'Make sure "pub serve" is serving the test/ directory.')); |
| 144 expect(result.exitCode, equals(exit_codes.data)); |
| 145 } finally { |
| 146 process.kill(); |
| 147 } |
| 148 }); |
| 149 }); |
| 150 }); |
| 151 |
| 152 test("gracefully handles pub serve running on the wrong directory for " |
| 153 "browser tests", () { |
| 154 new Directory(p.join(_sandbox, "web")).createSync(); |
| 155 |
| 156 return startPub(['serve', '--port', '0', 'web'], |
| 157 workingDirectory: _sandbox) |
| 158 .then((process) { |
| 159 return _lines.bind(process.stdout) |
| 160 .firstWhere(_servingRegExp.hasMatch) |
| 161 .then((line) { |
| 162 var match = _servingRegExp.firstMatch(line); |
| 163 |
| 164 try { |
| 165 var result = runUnittest( |
| 166 ['--pub-serve=${match[1]}', '-p', 'chrome'], |
| 167 workingDirectory: _sandbox); |
| 168 expect(result.stderr, contains( |
| 169 'Failed to load "test/my_test.dart":')); |
| 170 expect(result.stderr, contains('404 Not Found')); |
| 171 expect(result.stderr, contains( |
| 172 'Make sure "pub serve" is serving the test/ directory.')); |
| 173 expect(result.exitCode, equals(exit_codes.data)); |
| 174 } finally { |
| 175 process.kill(); |
| 176 } |
| 177 }); |
| 178 }); |
| 179 }); |
| 180 |
| 181 test("gracefully handles unconfigured transformers", () { |
| 182 new File(p.join(_sandbox, "pubspec.yaml")).writeAsStringSync(""" |
| 183 name: myapp |
| 184 dependencies: |
| 185 barback: any |
| 186 test: {path: ${p.current}} |
| 187 """); |
| 188 |
| 189 return startPub(['serve', '--port', '0'], |
| 190 workingDirectory: _sandbox) |
| 191 .then((process) { |
| 192 return _lines.bind(process.stdout) |
| 193 .firstWhere(_servingRegExp.hasMatch) |
| 194 .then((line) { |
| 195 var match = _servingRegExp.firstMatch(line); |
| 196 |
| 197 try { |
| 198 var result = runUnittest(['--pub-serve=${match[1]}'], |
| 199 workingDirectory: _sandbox); |
| 200 expect(result.exitCode, equals(exit_codes.data)); |
| 201 expect(result.stderr, equals(''' |
| 202 When using --pub-serve, you must include the "test/pub_serve" transformer in |
| 203 your pubspec: |
| 204 |
| 205 transformers: |
| 206 - test/pub_serve: |
| 207 \$include: test/**_test.dart |
| 208 ''')); |
| 209 } finally { |
| 210 process.kill(); |
| 211 } |
| 212 }); |
| 213 }); |
| 214 }); |
| 215 }); |
| 216 |
| 217 test("gracefully handles pub serve not running for VM tests", () { |
| 218 var result = runUnittest(['--pub-serve=54321'], |
| 219 workingDirectory: _sandbox); |
| 220 expect(result.stderr, equals(''' |
| 221 Failed to load "test/my_test.dart": |
| 222 Error getting http://localhost:54321/my_test.vm_test.dart: Connection refused |
| 223 Make sure "pub serve" is running. |
| 224 ''')); |
| 225 expect(result.exitCode, equals(exit_codes.data)); |
| 226 }); |
| 227 |
| 228 test("gracefully handles pub serve not running for browser tests", () { |
| 229 var result = runUnittest(['--pub-serve=54321', '-p', 'chrome'], |
| 230 workingDirectory: _sandbox); |
| 231 expect(result.stderr, equals(''' |
| 232 Failed to load "test/my_test.dart": |
| 233 Error getting http://localhost:54321/my_test.browser_test.dart.js: Connection re
fused (errno 111) |
| 234 Make sure "pub serve" is running. |
| 235 ''')); |
| 236 expect(result.exitCode, equals(exit_codes.data)); |
| 237 }); |
| 238 |
| 239 test("gracefully handles a test file not being in test/", () { |
| 240 new File(p.join(_sandbox, 'test/my_test.dart')) |
| 241 .copySync(p.join(_sandbox, 'my_test.dart')); |
| 242 |
| 243 var result = runUnittest(['--pub-serve=54321', 'my_test.dart'], |
| 244 workingDirectory: _sandbox); |
| 245 expect(result.stderr, equals( |
| 246 'Failed to load "my_test.dart": When using "pub serve", all test files ' |
| 247 'must be in test/.\n')); |
| 248 }); |
| 249 } |
OLD | NEW |