| 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 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 /// directory. | 228 /// directory. |
| 229 final String appPath = "myapp"; | 229 final String appPath = "myapp"; |
| 230 | 230 |
| 231 /// The path of the packages directory in the mock app used for tests. Relative | 231 /// The path of the packages directory in the mock app used for tests. Relative |
| 232 /// to the sandbox directory. | 232 /// to the sandbox directory. |
| 233 final String packagesPath = "$appPath/packages"; | 233 final String packagesPath = "$appPath/packages"; |
| 234 | 234 |
| 235 /// Set to true when the current batch of scheduled events should be aborted. | 235 /// Set to true when the current batch of scheduled events should be aborted. |
| 236 bool _abortScheduled = false; | 236 bool _abortScheduled = false; |
| 237 | 237 |
| 238 /// Enum identifying a pub command that can be run with a well-defined success |
| 239 /// output. |
| 240 class RunCommand { |
| 241 static final install = new RunCommand('install', |
| 242 new RegExp("Dependencies installed!\$")); |
| 243 |
| 244 static final update = new RunCommand('update', |
| 245 new RegExp("Dependencies updated!\$")); |
| 246 |
| 247 final String name; |
| 248 final RegExp success; |
| 249 RunCommand(this.name, this.success); |
| 250 } |
| 251 |
| 252 /// Many tests validate behavior that is the same between pub install and |
| 253 /// update have the same behavior. Instead of duplicating those tests, this |
| 254 /// takes a callback that defines install/update agnostic tests and runs them |
| 255 /// with both commands. |
| 256 void forBothPubInstallAndUpdate(void callback(RunCommand command)) { |
| 257 group(RunCommand.install.name, () => callback(RunCommand.install)); |
| 258 group(RunCommand.update.name, () => callback(RunCommand.update)); |
| 259 } |
| 260 |
| 261 /// Schedules an invocation of pub [command] and validates that it completes |
| 262 /// in an expected way. |
| 263 /// |
| 264 /// By default, this validates that the command completes successfully and |
| 265 /// understands the normal output of a successful pub command. If [warning] is |
| 266 /// given, it expects the command to complete successfully *and* print |
| 267 /// [warning] to stderr. If [error] is given, it expects the command to *only* |
| 268 /// print [error] to stderr. |
| 269 // TODO(rnystrom): Clean up other tests to call this when possible. |
| 270 void pubCommand(RunCommand command, {Iterable<String> args, Pattern error, |
| 271 Pattern warning}) { |
| 272 if (error != null && warning != null) { |
| 273 throw new ArgumentError("Cannot pass both 'error' and 'warning'."); |
| 274 } |
| 275 |
| 276 var allArgs = [command.name]; |
| 277 if (args != null) allArgs.addAll(args); |
| 278 |
| 279 var output = command.success; |
| 280 |
| 281 var exitCode = null; |
| 282 if (error != null) exitCode = 1; |
| 283 |
| 284 // No success output on an error. |
| 285 if (error != null) output = null; |
| 286 if (warning != null) error = warning; |
| 287 |
| 288 schedulePub(args: allArgs, output: output, error: error, exitCode: exitCode); |
| 289 } |
| 290 |
| 291 void pubInstall({Iterable<String> args, Pattern error, |
| 292 Pattern warning}) { |
| 293 pubCommand(RunCommand.install, args: args, error: error, warning: warning); |
| 294 } |
| 295 |
| 296 void pubUpdate({Iterable<String> args, Pattern error, |
| 297 Pattern warning}) { |
| 298 pubCommand(RunCommand.update, args: args, error: error, warning: warning); |
| 299 } |
| 300 |
| 238 /// Defines an integration test. The [body] should schedule a series of | 301 /// Defines an integration test. The [body] should schedule a series of |
| 239 /// operations which will be run asynchronously. | 302 /// operations which will be run asynchronously. |
| 240 void integration(String description, void body()) => | 303 void integration(String description, void body()) => |
| 241 _integration(description, body, test); | 304 _integration(description, body, test); |
| 242 | 305 |
| 243 /// Like [integration], but causes only this test to run. | 306 /// Like [integration], but causes only this test to run. |
| 244 void solo_integration(String description, void body()) => | 307 void solo_integration(String description, void body()) => |
| 245 _integration(description, body, solo_test); | 308 _integration(description, body, solo_test); |
| 246 | 309 |
| 247 void _integration(String description, void body(), [Function testFn]) { | 310 void _integration(String description, void body(), [Function testFn]) { |
| (...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 680 bool matches(item, MatchState matchState) { | 743 bool matches(item, MatchState matchState) { |
| 681 if (item is! Pair) return false; | 744 if (item is! Pair) return false; |
| 682 return _firstMatcher.matches(item.first, matchState) && | 745 return _firstMatcher.matches(item.first, matchState) && |
| 683 _lastMatcher.matches(item.last, matchState); | 746 _lastMatcher.matches(item.last, matchState); |
| 684 } | 747 } |
| 685 | 748 |
| 686 Description describe(Description description) { | 749 Description describe(Description description) { |
| 687 description.addAll("(", ", ", ")", [_firstMatcher, _lastMatcher]); | 750 description.addAll("(", ", ", ")", [_firstMatcher, _lastMatcher]); |
| 688 } | 751 } |
| 689 } | 752 } |
| OLD | NEW |