OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /// Test infrastructure for testing pub. Unlike typical unit tests, most pub | 5 /// Test infrastructure for testing pub. Unlike typical unit tests, most pub |
6 /// tests are integration tests that stage some stuff on the file system, run | 6 /// tests are integration tests that stage some stuff on the file system, run |
7 /// pub, and then validate the results. This library provides an API to build | 7 /// pub, and then validate the results. This library provides an API to build |
8 /// tests like that. | 8 /// tests like that. |
9 library test_pub; | 9 library test_pub; |
10 | 10 |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 /// The path of the packages directory in the mock app used for tests. Relative | 256 /// The path of the packages directory in the mock app used for tests. Relative |
257 /// to the sandbox directory. | 257 /// to the sandbox directory. |
258 final String packagesPath = "$appPath/packages"; | 258 final String packagesPath = "$appPath/packages"; |
259 | 259 |
260 /// Set to true when the current batch of scheduled events should be aborted. | 260 /// Set to true when the current batch of scheduled events should be aborted. |
261 bool _abortScheduled = false; | 261 bool _abortScheduled = false; |
262 | 262 |
263 /// Enum identifying a pub command that can be run with a well-defined success | 263 /// Enum identifying a pub command that can be run with a well-defined success |
264 /// output. | 264 /// output. |
265 class RunCommand { | 265 class RunCommand { |
266 static final get = new RunCommand('get', 'Got dependencies!'); | 266 static final get = new RunCommand('get', new RegExp(r'Got dependencies!$')); |
267 static final upgrade = new RunCommand('upgrade', 'Dependencies upgraded!'); | 267 static final upgrade = new RunCommand('upgrade', new RegExp( |
| 268 r'(No dependencies changed\.|Changed \d+ dependenc(y|ies)!)$')); |
268 | 269 |
269 final String name; | 270 final String name; |
270 final RegExp success; | 271 final RegExp success; |
271 RunCommand(this.name, String message) | 272 RunCommand(this.name, this.success); |
272 : success = new RegExp("$message\$"); | |
273 } | 273 } |
274 | 274 |
275 /// Many tests validate behavior that is the same between pub get and | 275 /// Many tests validate behavior that is the same between pub get and |
276 /// upgrade have the same behavior. Instead of duplicating those tests, this | 276 /// upgrade have the same behavior. Instead of duplicating those tests, this |
277 /// takes a callback that defines get/upgrade agnostic tests and runs them | 277 /// takes a callback that defines get/upgrade agnostic tests and runs them |
278 /// with both commands. | 278 /// with both commands. |
279 void forBothPubGetAndUpgrade(void callback(RunCommand command)) { | 279 void forBothPubGetAndUpgrade(void callback(RunCommand command)) { |
280 group(RunCommand.get.name, () => callback(RunCommand.get)); | 280 group(RunCommand.get.name, () => callback(RunCommand.get)); |
281 group(RunCommand.upgrade.name, () => callback(RunCommand.upgrade)); | 281 group(RunCommand.upgrade.name, () => callback(RunCommand.upgrade)); |
282 } | 282 } |
283 | 283 |
284 /// Schedules an invocation of pub [command] and validates that it completes | 284 /// Schedules an invocation of pub [command] and validates that it completes |
285 /// in an expected way. | 285 /// in an expected way. |
286 /// | 286 /// |
287 /// By default, this validates that the command completes successfully and | 287 /// By default, this validates that the command completes successfully and |
288 /// understands the normal output of a successful pub command. If [warning] is | 288 /// understands the normal output of a successful pub command. If [warning] is |
289 /// given, it expects the command to complete successfully *and* print | 289 /// given, it expects the command to complete successfully *and* print |
290 /// [warning] to stderr. If [error] is given, it expects the command to *only* | 290 /// [warning] to stderr. If [error] is given, it expects the command to *only* |
291 /// print [error] to stderr. | 291 /// print [error] to stderr. |
292 // TODO(rnystrom): Clean up other tests to call this when possible. | 292 // TODO(rnystrom): Clean up other tests to call this when possible. |
293 void pubCommand(RunCommand command, {Iterable<String> args, Pattern error, | 293 void pubCommand(RunCommand command, |
294 Pattern warning}) { | 294 {Iterable<String> args, Pattern output, Pattern error, Pattern warning}) { |
295 if (error != null && warning != null) { | 295 if (error != null && warning != null) { |
296 throw new ArgumentError("Cannot pass both 'error' and 'warning'."); | 296 throw new ArgumentError("Cannot pass both 'error' and 'warning'."); |
297 } | 297 } |
298 | 298 |
299 var allArgs = [command.name]; | 299 var allArgs = [command.name]; |
300 if (args != null) allArgs.addAll(args); | 300 if (args != null) allArgs.addAll(args); |
301 | 301 |
302 var output = command.success; | 302 if (output == null) output = command.success; |
303 | 303 |
304 var exitCode = null; | 304 var exitCode = null; |
305 if (error != null) exitCode = 1; | 305 if (error != null) exitCode = 1; |
306 | 306 |
307 // No success output on an error. | 307 // No success output on an error. |
308 if (error != null) output = null; | 308 if (error != null) output = null; |
309 if (warning != null) error = warning; | 309 if (warning != null) error = warning; |
310 | 310 |
311 schedulePub(args: allArgs, output: output, error: error, exitCode: exitCode); | 311 schedulePub(args: allArgs, output: output, error: error, exitCode: exitCode); |
312 } | 312 } |
313 | 313 |
314 void pubGet({Iterable<String> args, Pattern error, | 314 void pubGet({Iterable<String> args, Pattern error, |
315 Pattern warning}) { | 315 Pattern warning}) { |
316 pubCommand(RunCommand.get, args: args, error: error, warning: warning); | 316 pubCommand(RunCommand.get, args: args, error: error, warning: warning); |
317 } | 317 } |
318 | 318 |
319 void pubUpgrade({Iterable<String> args, Pattern error, | 319 void pubUpgrade({Iterable<String> args, Pattern output, Pattern error, |
320 Pattern warning}) { | 320 Pattern warning}) { |
321 pubCommand(RunCommand.upgrade, args: args, error: error, warning: warning); | 321 pubCommand(RunCommand.upgrade, args: args, output: output, error: error, |
| 322 warning: warning); |
322 } | 323 } |
323 | 324 |
324 /// Defines an integration test. The [body] should schedule a series of | 325 /// Defines an integration test. The [body] should schedule a series of |
325 /// operations which will be run asynchronously. | 326 /// operations which will be run asynchronously. |
326 void integration(String description, void body()) => | 327 void integration(String description, void body()) => |
327 _integration(description, body, test); | 328 _integration(description, body, test); |
328 | 329 |
329 /// Like [integration], but causes only this test to run. | 330 /// Like [integration], but causes only this test to run. |
330 void solo_integration(String description, void body()) => | 331 void solo_integration(String description, void body()) => |
331 _integration(description, body, solo_test); | 332 _integration(description, body, solo_test); |
(...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
857 bool matches(item, Map matchState) { | 858 bool matches(item, Map matchState) { |
858 if (item is! Pair) return false; | 859 if (item is! Pair) return false; |
859 return _firstMatcher.matches(item.first, matchState) && | 860 return _firstMatcher.matches(item.first, matchState) && |
860 _lastMatcher.matches(item.last, matchState); | 861 _lastMatcher.matches(item.last, matchState); |
861 } | 862 } |
862 | 863 |
863 Description describe(Description description) { | 864 Description describe(Description description) { |
864 description.addAll("(", ", ", ")", [_firstMatcher, _lastMatcher]); | 865 description.addAll("(", ", ", ")", [_firstMatcher, _lastMatcher]); |
865 } | 866 } |
866 } | 867 } |
OLD | NEW |