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. | 5 /// Test infrastructure for testing pub. |
6 /// | 6 /// |
7 /// Unlike typical unit tests, most pub tests are integration tests that stage | 7 /// Unlike typical unit tests, most pub tests are integration tests that stage |
8 /// some stuff on the file system, run pub, and then validate the results. This | 8 /// some stuff on the file system, run pub, and then validate the results. This |
9 /// library provides an API to build tests like that. | 9 /// library provides an API to build tests like that. |
10 library test_pub; | 10 library test_pub; |
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 pub.writeLine("y"); | 479 pub.writeLine("y"); |
480 } | 480 } |
481 | 481 |
482 /// Gets the absolute path to [relPath], which is a relative path in the test | 482 /// Gets the absolute path to [relPath], which is a relative path in the test |
483 /// sandbox. | 483 /// sandbox. |
484 String _pathInSandbox(String relPath) { | 484 String _pathInSandbox(String relPath) { |
485 return p.join(p.absolute(sandboxDir), relPath); | 485 return p.join(p.absolute(sandboxDir), relPath); |
486 } | 486 } |
487 | 487 |
488 /// Gets the environment variables used to run pub in a test context. | 488 /// Gets the environment variables used to run pub in a test context. |
489 Map getPubTestEnvironment([String tokenEndpoint]) { | 489 Future<Map> getPubTestEnvironment([String tokenEndpoint]) async { |
490 var environment = {}; | 490 var environment = {}; |
491 environment['_PUB_TESTING'] = 'true'; | 491 environment['_PUB_TESTING'] = 'true'; |
492 environment['PUB_CACHE'] = _pathInSandbox(cachePath); | 492 environment['PUB_CACHE'] = _pathInSandbox(cachePath); |
493 | 493 |
494 // Ensure a known SDK version is set for the tests that rely on that. | 494 // Ensure a known SDK version is set for the tests that rely on that. |
495 environment['_PUB_TEST_SDK_VERSION'] = "0.1.2+3"; | 495 environment['_PUB_TEST_SDK_VERSION'] = "0.1.2+3"; |
496 | 496 |
497 if (tokenEndpoint != null) { | 497 if (tokenEndpoint != null) { |
498 environment['_PUB_TEST_TOKEN_ENDPOINT'] = tokenEndpoint.toString(); | 498 environment['_PUB_TEST_TOKEN_ENDPOINT'] = tokenEndpoint.toString(); |
499 } | 499 } |
500 | 500 |
| 501 if (_hasServer) { |
| 502 return port.then((p) { |
| 503 environment['PUB_HOSTED_URL'] = "http://localhost:$p"; |
| 504 return environment; |
| 505 }); |
| 506 } |
| 507 |
501 return environment; | 508 return environment; |
502 } | 509 } |
503 | 510 |
504 /// Starts a Pub process and returns a [ScheduledProcess] that supports | 511 /// Starts a Pub process and returns a [ScheduledProcess] that supports |
505 /// interaction with that process. | 512 /// interaction with that process. |
506 /// | 513 /// |
507 /// Any futures in [args] will be resolved before the process is started. | 514 /// Any futures in [args] will be resolved before the process is started. |
508 /// | 515 /// |
509 /// If [environment] is given, any keys in it will override the environment | 516 /// If [environment] is given, any keys in it will override the environment |
510 /// variables passed to the spawned process. | 517 /// variables passed to the spawned process. |
(...skipping 16 matching lines...) Expand all Loading... |
527 // to be manually updated when changing code before running the tests. | 534 // to be manually updated when changing code before running the tests. |
528 // Otherwise, you will test against stale data. | 535 // Otherwise, you will test against stale data. |
529 // | 536 // |
530 // Using the snapshot makes running the tests much faster, which is why we | 537 // Using the snapshot makes running the tests much faster, which is why we |
531 // make this trade-off. | 538 // make this trade-off. |
532 var pubPath = p.join(p.dirname(dartBin), 'snapshots/pub.dart.snapshot'); | 539 var pubPath = p.join(p.dirname(dartBin), 'snapshots/pub.dart.snapshot'); |
533 var dartArgs = [pubPath, '--verbose']; | 540 var dartArgs = [pubPath, '--verbose']; |
534 dartArgs.addAll(args); | 541 dartArgs.addAll(args); |
535 | 542 |
536 if (tokenEndpoint == null) tokenEndpoint = new Future.value(); | 543 if (tokenEndpoint == null) tokenEndpoint = new Future.value(); |
537 var environmentFuture = tokenEndpoint.then((tokenEndpoint) { | 544 var environmentFuture = tokenEndpoint |
538 var pubEnvironment = getPubTestEnvironment(tokenEndpoint); | 545 .then((tokenEndpoint) => getPubTestEnvironment(tokenEndpoint)) |
539 | 546 .then((pubEnvironment) { |
540 // If there is a server running, tell pub what its URL is so hosted | |
541 // dependencies will look there. | |
542 if (_hasServer) { | |
543 return port.then((p) { | |
544 pubEnvironment['PUB_HOSTED_URL'] = "http://localhost:$p"; | |
545 return pubEnvironment; | |
546 }); | |
547 } | |
548 | |
549 return pubEnvironment; | |
550 }).then((pubEnvironment) { | |
551 if (environment != null) pubEnvironment.addAll(environment); | 547 if (environment != null) pubEnvironment.addAll(environment); |
552 return pubEnvironment; | 548 return pubEnvironment; |
553 }); | 549 }); |
554 | 550 |
555 return new PubProcess.start(dartBin, dartArgs, environment: environmentFuture, | 551 return new PubProcess.start(dartBin, dartArgs, environment: environmentFuture, |
556 workingDirectory: _pathInSandbox(appPath), | 552 workingDirectory: _pathInSandbox(appPath), |
557 description: args.isEmpty ? 'pub' : 'pub ${args.first}'); | 553 description: args.isEmpty ? 'pub' : 'pub ${args.first}'); |
558 } | 554 } |
559 | 555 |
560 /// A subclass of [ScheduledProcess] that parses pub's verbose logging output | 556 /// A subclass of [ScheduledProcess] that parses pub's verbose logging output |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
847 'downloads': 0, | 843 'downloads': 0, |
848 'created': '2012-09-25T18:38:28.685260', | 844 'created': '2012-09-25T18:38:28.685260', |
849 'libraries': ['$name.dart'], | 845 'libraries': ['$name.dart'], |
850 'uploader': ['nweiz@google.com'] | 846 'uploader': ['nweiz@google.com'] |
851 }); | 847 }); |
852 } | 848 } |
853 | 849 |
854 return map; | 850 return map; |
855 } | 851 } |
856 | 852 |
| 853 /// Returns the name of the shell script for a binstub named [name]. |
| 854 /// |
| 855 /// Adds a ".bat" extension on Windows. |
| 856 String binStubName(String name) => Platform.isWindows ? '$name.bat' : name; |
| 857 |
857 /// Compares the [actual] output from running pub with [expected]. | 858 /// Compares the [actual] output from running pub with [expected]. |
858 /// | 859 /// |
859 /// If [expected] is a [String], ignores leading and trailing whitespace | 860 /// If [expected] is a [String], ignores leading and trailing whitespace |
860 /// differences and tries to report the offending difference in a nice way. | 861 /// differences and tries to report the offending difference in a nice way. |
861 /// | 862 /// |
862 /// If it's a [RegExp] or [Matcher], just reports whether the output matches. | 863 /// If it's a [RegExp] or [Matcher], just reports whether the output matches. |
863 void _validateOutput(List<String> failures, String pipe, expected, | 864 void _validateOutput(List<String> failures, String pipe, expected, |
864 String actual) { | 865 String actual) { |
865 if (expected == null) return; | 866 if (expected == null) return; |
866 | 867 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
977 _lastMatcher.matches(item.last, matchState); | 978 _lastMatcher.matches(item.last, matchState); |
978 } | 979 } |
979 | 980 |
980 Description describe(Description description) { | 981 Description describe(Description description) { |
981 return description.addAll("(", ", ", ")", [_firstMatcher, _lastMatcher]); | 982 return description.addAll("(", ", ", ")", [_firstMatcher, _lastMatcher]); |
982 } | 983 } |
983 } | 984 } |
984 | 985 |
985 /// A [StreamMatcher] that matches multiple lines of output. | 986 /// A [StreamMatcher] that matches multiple lines of output. |
986 StreamMatcher emitsLines(String output) => inOrder(output.split("\n")); | 987 StreamMatcher emitsLines(String output) => inOrder(output.split("\n")); |
OLD | NEW |