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', r'Got dependencies!$'); |
267 static final upgrade = new RunCommand('upgrade', 'Dependencies upgraded!'); | 267 static final upgrade = new RunCommand('upgrade', |
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, String pattern) |
nweiz
2013/12/11 06:48:32
If you're going to take a pattern, take a Pattern.
Bob Nystrom
2013/12/11 22:36:59
Done.
| |
272 : success = new RegExp("$message\$"); | 273 : success = new RegExp(pattern); |
273 } | 274 } |
274 | 275 |
275 /// Many tests validate behavior that is the same between pub get and | 276 /// Many tests validate behavior that is the same between pub get and |
276 /// upgrade have the same behavior. Instead of duplicating those tests, this | 277 /// upgrade have the same behavior. Instead of duplicating those tests, this |
277 /// takes a callback that defines get/upgrade agnostic tests and runs them | 278 /// takes a callback that defines get/upgrade agnostic tests and runs them |
278 /// with both commands. | 279 /// with both commands. |
279 void forBothPubGetAndUpgrade(void callback(RunCommand command)) { | 280 void forBothPubGetAndUpgrade(void callback(RunCommand command)) { |
280 group(RunCommand.get.name, () => callback(RunCommand.get)); | 281 group(RunCommand.get.name, () => callback(RunCommand.get)); |
281 group(RunCommand.upgrade.name, () => callback(RunCommand.upgrade)); | 282 group(RunCommand.upgrade.name, () => callback(RunCommand.upgrade)); |
282 } | 283 } |
283 | 284 |
284 /// Schedules an invocation of pub [command] and validates that it completes | 285 /// Schedules an invocation of pub [command] and validates that it completes |
285 /// in an expected way. | 286 /// in an expected way. |
286 /// | 287 /// |
287 /// By default, this validates that the command completes successfully and | 288 /// By default, this validates that the command completes successfully and |
288 /// understands the normal output of a successful pub command. If [warning] is | 289 /// understands the normal output of a successful pub command. If [warning] is |
289 /// given, it expects the command to complete successfully *and* print | 290 /// given, it expects the command to complete successfully *and* print |
290 /// [warning] to stderr. If [error] is given, it expects the command to *only* | 291 /// [warning] to stderr. If [error] is given, it expects the command to *only* |
291 /// print [error] to stderr. | 292 /// print [error] to stderr. |
292 // TODO(rnystrom): Clean up other tests to call this when possible. | 293 // TODO(rnystrom): Clean up other tests to call this when possible. |
293 void pubCommand(RunCommand command, {Iterable<String> args, Pattern error, | 294 void pubCommand(RunCommand command, |
294 Pattern warning}) { | 295 {Iterable<String> args, Pattern output, Pattern error, Pattern warning}) { |
295 if (error != null && warning != null) { | 296 if (error != null && warning != null) { |
296 throw new ArgumentError("Cannot pass both 'error' and 'warning'."); | 297 throw new ArgumentError("Cannot pass both 'error' and 'warning'."); |
297 } | 298 } |
298 | 299 |
299 var allArgs = [command.name]; | 300 var allArgs = [command.name]; |
300 if (args != null) allArgs.addAll(args); | 301 if (args != null) allArgs.addAll(args); |
301 | 302 |
302 var output = command.success; | 303 if (output == null) output = command.success; |
303 | 304 |
304 var exitCode = null; | 305 var exitCode = null; |
305 if (error != null) exitCode = 1; | 306 if (error != null) exitCode = 1; |
306 | 307 |
307 // No success output on an error. | 308 // No success output on an error. |
308 if (error != null) output = null; | 309 if (error != null) output = null; |
309 if (warning != null) error = warning; | 310 if (warning != null) error = warning; |
310 | 311 |
311 schedulePub(args: allArgs, output: output, error: error, exitCode: exitCode); | 312 schedulePub(args: allArgs, output: output, error: error, exitCode: exitCode); |
312 } | 313 } |
313 | 314 |
314 void pubGet({Iterable<String> args, Pattern error, | 315 void pubGet({Iterable<String> args, Pattern error, |
315 Pattern warning}) { | 316 Pattern warning}) { |
316 pubCommand(RunCommand.get, args: args, error: error, warning: warning); | 317 pubCommand(RunCommand.get, args: args, error: error, warning: warning); |
317 } | 318 } |
318 | 319 |
319 void pubUpgrade({Iterable<String> args, Pattern error, | 320 void pubUpgrade({Iterable<String> args, Pattern output, Pattern error, |
320 Pattern warning}) { | 321 Pattern warning}) { |
321 pubCommand(RunCommand.upgrade, args: args, error: error, warning: warning); | 322 pubCommand(RunCommand.upgrade, args: args, output: output, error: error, |
323 warning: warning); | |
322 } | 324 } |
323 | 325 |
324 /// Defines an integration test. The [body] should schedule a series of | 326 /// Defines an integration test. The [body] should schedule a series of |
325 /// operations which will be run asynchronously. | 327 /// operations which will be run asynchronously. |
326 void integration(String description, void body()) => | 328 void integration(String description, void body()) => |
327 _integration(description, body, test); | 329 _integration(description, body, test); |
328 | 330 |
329 /// Like [integration], but causes only this test to run. | 331 /// Like [integration], but causes only this test to run. |
330 void solo_integration(String description, void body()) => | 332 void solo_integration(String description, void body()) => |
331 _integration(description, body, solo_test); | 333 _integration(description, body, solo_test); |
(...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
857 bool matches(item, Map matchState) { | 859 bool matches(item, Map matchState) { |
858 if (item is! Pair) return false; | 860 if (item is! Pair) return false; |
859 return _firstMatcher.matches(item.first, matchState) && | 861 return _firstMatcher.matches(item.first, matchState) && |
860 _lastMatcher.matches(item.last, matchState); | 862 _lastMatcher.matches(item.last, matchState); |
861 } | 863 } |
862 | 864 |
863 Description describe(Description description) { | 865 Description describe(Description description) { |
864 description.addAll("(", ", ", ")", [_firstMatcher, _lastMatcher]); | 866 description.addAll("(", ", ", ")", [_firstMatcher, _lastMatcher]); |
865 } | 867 } |
866 } | 868 } |
OLD | NEW |