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 477 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
488 pub.writeLine("y"); | 488 pub.writeLine("y"); |
489 } | 489 } |
490 | 490 |
491 /// Gets the absolute path to [relPath], which is a relative path in the test | 491 /// Gets the absolute path to [relPath], which is a relative path in the test |
492 /// sandbox. | 492 /// sandbox. |
493 String _pathInSandbox(String relPath) { | 493 String _pathInSandbox(String relPath) { |
494 return p.join(p.absolute(sandboxDir), relPath); | 494 return p.join(p.absolute(sandboxDir), relPath); |
495 } | 495 } |
496 | 496 |
497 /// Gets the environment variables used to run pub in a test context. | 497 /// Gets the environment variables used to run pub in a test context. |
498 Map getPubTestEnvironment([String tokenEndpoint]) { | 498 Future<Map> getPubTestEnvironment([String tokenEndpoint]) { |
499 var environment = {}; | 499 final completer0 = new Completer(); |
500 environment['_PUB_TESTING'] = 'true'; | 500 scheduleMicrotask(() { |
501 environment['PUB_CACHE'] = _pathInSandbox(cachePath); | 501 try { |
502 | 502 var environment = {}; |
503 // Ensure a known SDK version is set for the tests that rely on that. | 503 environment['_PUB_TESTING'] = 'true'; |
504 environment['_PUB_TEST_SDK_VERSION'] = "0.1.2+3"; | 504 environment['PUB_CACHE'] = _pathInSandbox(cachePath); |
505 | 505 environment['_PUB_TEST_SDK_VERSION'] = "0.1.2+3"; |
506 if (tokenEndpoint != null) { | 506 join0() { |
507 environment['_PUB_TEST_TOKEN_ENDPOINT'] = tokenEndpoint.toString(); | 507 join1() { |
508 } | 508 completer0.complete(environment); |
509 | 509 } |
510 return environment; | 510 if (_hasServer) { |
| 511 completer0.complete(port.then(((p) { |
| 512 environment['PUB_HOSTED_URL'] = "http://localhost:$p"; |
| 513 return environment; |
| 514 }))); |
| 515 } else { |
| 516 join1(); |
| 517 } |
| 518 } |
| 519 if (tokenEndpoint != null) { |
| 520 environment['_PUB_TEST_TOKEN_ENDPOINT'] = tokenEndpoint.toString(); |
| 521 join0(); |
| 522 } else { |
| 523 join0(); |
| 524 } |
| 525 } catch (e, s) { |
| 526 completer0.completeError(e, s); |
| 527 } |
| 528 }); |
| 529 return completer0.future; |
511 } | 530 } |
512 | 531 |
513 /// Starts a Pub process and returns a [ScheduledProcess] that supports | 532 /// Starts a Pub process and returns a [ScheduledProcess] that supports |
514 /// interaction with that process. | 533 /// interaction with that process. |
515 /// | 534 /// |
516 /// Any futures in [args] will be resolved before the process is started. | 535 /// Any futures in [args] will be resolved before the process is started. |
517 /// | 536 /// |
518 /// If [environment] is given, any keys in it will override the environment | 537 /// If [environment] is given, any keys in it will override the environment |
519 /// variables passed to the spawned process. | 538 /// variables passed to the spawned process. |
520 ScheduledProcess startPub({List args, Future<String> tokenEndpoint, Map<String, | 539 ScheduledProcess startPub({List args, Future<String> tokenEndpoint, Map<String, |
(...skipping 15 matching lines...) Expand all Loading... |
536 // to be manually updated when changing code before running the tests. | 555 // to be manually updated when changing code before running the tests. |
537 // Otherwise, you will test against stale data. | 556 // Otherwise, you will test against stale data. |
538 // | 557 // |
539 // Using the snapshot makes running the tests much faster, which is why we | 558 // Using the snapshot makes running the tests much faster, which is why we |
540 // make this trade-off. | 559 // make this trade-off. |
541 var pubPath = p.join(p.dirname(dartBin), 'snapshots/pub.dart.snapshot'); | 560 var pubPath = p.join(p.dirname(dartBin), 'snapshots/pub.dart.snapshot'); |
542 var dartArgs = [pubPath, '--verbose']; | 561 var dartArgs = [pubPath, '--verbose']; |
543 dartArgs.addAll(args); | 562 dartArgs.addAll(args); |
544 | 563 |
545 if (tokenEndpoint == null) tokenEndpoint = new Future.value(); | 564 if (tokenEndpoint == null) tokenEndpoint = new Future.value(); |
546 var environmentFuture = tokenEndpoint.then((tokenEndpoint) { | 565 var environmentFuture = tokenEndpoint.then( |
547 var pubEnvironment = getPubTestEnvironment(tokenEndpoint); | 566 (tokenEndpoint) => getPubTestEnvironment(tokenEndpoint)).then((pubEnvironm
ent) { |
548 | |
549 // If there is a server running, tell pub what its URL is so hosted | |
550 // dependencies will look there. | |
551 if (_hasServer) { | |
552 return port.then((p) { | |
553 pubEnvironment['PUB_HOSTED_URL'] = "http://localhost:$p"; | |
554 return pubEnvironment; | |
555 }); | |
556 } | |
557 | |
558 return pubEnvironment; | |
559 }).then((pubEnvironment) { | |
560 if (environment != null) pubEnvironment.addAll(environment); | 567 if (environment != null) pubEnvironment.addAll(environment); |
561 return pubEnvironment; | 568 return pubEnvironment; |
562 }); | 569 }); |
563 | 570 |
564 return new PubProcess.start( | 571 return new PubProcess.start( |
565 dartBin, | 572 dartBin, |
566 dartArgs, | 573 dartArgs, |
567 environment: environmentFuture, | 574 environment: environmentFuture, |
568 workingDirectory: _pathInSandbox(appPath), | 575 workingDirectory: _pathInSandbox(appPath), |
569 description: args.isEmpty ? 'pub' : 'pub ${args.first}'); | 576 description: args.isEmpty ? 'pub' : 'pub ${args.first}'); |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
861 'downloads': 0, | 868 'downloads': 0, |
862 'created': '2012-09-25T18:38:28.685260', | 869 'created': '2012-09-25T18:38:28.685260', |
863 'libraries': ['$name.dart'], | 870 'libraries': ['$name.dart'], |
864 'uploader': ['nweiz@google.com'] | 871 'uploader': ['nweiz@google.com'] |
865 }); | 872 }); |
866 } | 873 } |
867 | 874 |
868 return map; | 875 return map; |
869 } | 876 } |
870 | 877 |
| 878 /// Returns the name of the shell script for a binstub named [name]. |
| 879 /// |
| 880 /// Adds a ".bat" extension on Windows. |
| 881 String binStubName(String name) => Platform.isWindows ? '$name.bat' : name; |
| 882 |
871 /// Compares the [actual] output from running pub with [expected]. | 883 /// Compares the [actual] output from running pub with [expected]. |
872 /// | 884 /// |
873 /// If [expected] is a [String], ignores leading and trailing whitespace | 885 /// If [expected] is a [String], ignores leading and trailing whitespace |
874 /// differences and tries to report the offending difference in a nice way. | 886 /// differences and tries to report the offending difference in a nice way. |
875 /// | 887 /// |
876 /// If it's a [RegExp] or [Matcher], just reports whether the output matches. | 888 /// If it's a [RegExp] or [Matcher], just reports whether the output matches. |
877 void _validateOutput(List<String> failures, String pipe, expected, | 889 void _validateOutput(List<String> failures, String pipe, expected, |
878 String actual) { | 890 String actual) { |
879 if (expected == null) return; | 891 if (expected == null) return; |
880 | 892 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
991 _lastMatcher.matches(item.last, matchState); | 1003 _lastMatcher.matches(item.last, matchState); |
992 } | 1004 } |
993 | 1005 |
994 Description describe(Description description) { | 1006 Description describe(Description description) { |
995 return description.addAll("(", ", ", ")", [_firstMatcher, _lastMatcher]); | 1007 return description.addAll("(", ", ", ")", [_firstMatcher, _lastMatcher]); |
996 } | 1008 } |
997 } | 1009 } |
998 | 1010 |
999 /// A [StreamMatcher] that matches multiple lines of output. | 1011 /// A [StreamMatcher] that matches multiple lines of output. |
1000 StreamMatcher emitsLines(String output) => inOrder(output.split("\n")); | 1012 StreamMatcher emitsLines(String output) => inOrder(output.split("\n")); |
OLD | NEW |