| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** | 5 /** |
| 6 * Test infrastructure for testing pub. Unlike typical unit tests, most pub | 6 * Test infrastructure for testing pub. Unlike typical unit tests, most pub |
| 7 * tests are integration tests that stage some stuff on the file system, run | 7 * tests are integration tests that stage some stuff on the file system, run |
| 8 * pub, and then validate the results. This library provides an API to build | 8 * pub, and then validate the results. This library provides an API to build |
| 9 * tests like that. | 9 * tests like that. |
| 10 */ | 10 */ |
| (...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 559 var dir = new Options().script; | 559 var dir = new Options().script; |
| 560 while (basename(dir) != 'pub') dir = dirname(dir); | 560 while (basename(dir) != 'pub') dir = dirname(dir); |
| 561 | 561 |
| 562 return getFullPath(dir); | 562 return getFullPath(dir); |
| 563 } | 563 } |
| 564 | 564 |
| 565 /** | 565 /** |
| 566 * Schedules a call to the Pub command-line utility. Runs Pub with [args] and | 566 * Schedules a call to the Pub command-line utility. Runs Pub with [args] and |
| 567 * validates that its results match [output], [error], and [exitCode]. | 567 * validates that its results match [output], [error], and [exitCode]. |
| 568 */ | 568 */ |
| 569 void schedulePub({List<String> args, Pattern output, Pattern error, | 569 void schedulePub({List args, Pattern output, Pattern error, |
| 570 Future<Uri> tokenEndpoint, int exitCode: 0}) { | 570 Future<Uri> tokenEndpoint, int exitCode: 0}) { |
| 571 _schedule((sandboxDir) { | 571 _schedule((sandboxDir) { |
| 572 return _doPub(runProcess, sandboxDir, args, tokenEndpoint) | 572 return _doPub(runProcess, sandboxDir, args, tokenEndpoint) |
| 573 .transform((result) { | 573 .transform((result) { |
| 574 var failures = []; | 574 var failures = []; |
| 575 | 575 |
| 576 _validateOutput(failures, 'stdout', output, result.stdout); | 576 _validateOutput(failures, 'stdout', output, result.stdout); |
| 577 _validateOutput(failures, 'stderr', error, result.stderr); | 577 _validateOutput(failures, 'stderr', error, result.stderr); |
| 578 | 578 |
| 579 if (result.exitCode != exitCode) { | 579 if (result.exitCode != exitCode) { |
| 580 failures.add( | 580 failures.add( |
| 581 'Pub returned exit code ${result.exitCode}, expected $exitCode.'); | 581 'Pub returned exit code ${result.exitCode}, expected $exitCode.'); |
| 582 } | 582 } |
| 583 | 583 |
| 584 if (failures.length > 0) { | 584 if (failures.length > 0) { |
| 585 if (error == null) { | 585 if (error == null) { |
| 586 // If we aren't validating the error, still show it on failure. | 586 // If we aren't validating the error, still show it on failure. |
| 587 failures.add('Pub stderr:'); | 587 failures.add('Pub stderr:'); |
| 588 failures.addAll(result.stderr.map((line) => '| $line')); | 588 failures.addAll(result.stderr.map((line) => '| $line')); |
| 589 } | 589 } |
| 590 | 590 |
| 591 throw new ExpectException(Strings.join(failures, '\n')); | 591 throw new ExpectException(Strings.join(failures, '\n')); |
| 592 } | 592 } |
| 593 | 593 |
| 594 return null; | 594 return null; |
| 595 }); | 595 }); |
| 596 }); | 596 }); |
| 597 } | 597 } |
| 598 | 598 |
| 599 /** | 599 /// A shorthand for [schedulePub] and [run] when no validation needs to be done |
| 600 * A shorthand for [schedulePub] and [run] when no validation needs to be done | 600 /// after Pub has been run. |
| 601 * after Pub has been run. | 601 /// |
| 602 */ | 602 /// Any futures in [args] will be resolved before the process is started. |
| 603 void runPub({List<String> args, Pattern output, Pattern error, | 603 void runPub({List args, Pattern output, Pattern error, int exitCode: 0}) { |
| 604 int exitCode: 0}) { | |
| 605 schedulePub(args: args, output: output, error: error, exitCode: exitCode); | 604 schedulePub(args: args, output: output, error: error, exitCode: exitCode); |
| 606 run(); | 605 run(); |
| 607 } | 606 } |
| 608 | 607 |
| 609 /// Starts a Pub process and returns a [ScheduledProcess] that supports | 608 /// Starts a Pub process and returns a [ScheduledProcess] that supports |
| 610 /// interaction with that process. | 609 /// interaction with that process. |
| 611 ScheduledProcess startPub({List<String> args}) { | 610 /// |
| 611 /// Any futures in [args] will be resolved before the process is started. |
| 612 ScheduledProcess startPub({List args, Future<Uri> tokenEndpoint}) { |
| 612 var process = _scheduleValue((sandboxDir) => | 613 var process = _scheduleValue((sandboxDir) => |
| 613 _doPub(startProcess, sandboxDir, args)); | 614 _doPub(startProcess, sandboxDir, args, tokenEndpoint)); |
| 614 return new ScheduledProcess("pub", process); | 615 return new ScheduledProcess("pub", process); |
| 615 } | 616 } |
| 616 | 617 |
| 617 /// Like [startPub], but runs `pub lish` in particular with [server] used both | 618 /// Like [startPub], but runs `pub lish` in particular with [server] used both |
| 618 /// as the OAuth2 server (with "/token" as the token endpoint) and as the | 619 /// as the OAuth2 server (with "/token" as the token endpoint) and as the |
| 619 /// package server. | 620 /// package server. |
| 620 ScheduledProcess startPubLish(ScheduledServer server, {List<String> args}) { | 621 /// |
| 621 var process = _scheduleValue((sandboxDir) { | 622 /// Any futures in [args] will be resolved before the process is started. |
| 622 return server.url.chain((url) { | 623 ScheduledProcess startPubLish(ScheduledServer server, {List args}) { |
| 623 var tokenEndpoint = url.resolve('/token'); | 624 var tokenEndpoint = server.url.transform((url) => |
| 624 if (args == null) args = []; | 625 url.resolve('/token').toString()); |
| 625 args = flatten(['lish', '--server', url.toString(), args]); | 626 if (args == null) args = []; |
| 626 return _doPub(startProcess, sandboxDir, args, tokenEndpoint); | 627 args = flatten(['lish', '--server', tokenEndpoint, args]); |
| 627 }); | 628 return startPub(args: args, tokenEndpoint: tokenEndpoint); |
| 628 }); | |
| 629 return new ScheduledProcess("pub lish", process); | |
| 630 } | 629 } |
| 631 | 630 |
| 632 /// Handles the beginning confirmation process for uploading a packages. | 631 /// Handles the beginning confirmation process for uploading a packages. |
| 633 /// Ensures that the right output is shown and then enters "y" to confirm the | 632 /// Ensures that the right output is shown and then enters "y" to confirm the |
| 634 /// upload. | 633 /// upload. |
| 635 void confirmPublish(ScheduledProcess pub) { | 634 void confirmPublish(ScheduledProcess pub) { |
| 636 // TODO(rnystrom): This is overly specific and inflexible regarding different | 635 // TODO(rnystrom): This is overly specific and inflexible regarding different |
| 637 // test packages. Should validate this a little more loosely. | 636 // test packages. Should validate this a little more loosely. |
| 638 expectLater(pub.nextLine(), equals('Publishing "test_pkg" 1.0.0:')); | 637 expectLater(pub.nextLine(), equals('Publishing "test_pkg" 1.0.0:')); |
| 639 expectLater(pub.nextLine(), equals("|-- LICENSE")); | 638 expectLater(pub.nextLine(), equals("|-- LICENSE")); |
| 640 expectLater(pub.nextLine(), equals("|-- lib")); | 639 expectLater(pub.nextLine(), equals("|-- lib")); |
| 641 expectLater(pub.nextLine(), equals("| '-- test_pkg.dart")); | 640 expectLater(pub.nextLine(), equals("| '-- test_pkg.dart")); |
| 642 expectLater(pub.nextLine(), equals("'-- pubspec.yaml")); | 641 expectLater(pub.nextLine(), equals("'-- pubspec.yaml")); |
| 643 expectLater(pub.nextLine(), equals("")); | 642 expectLater(pub.nextLine(), equals("")); |
| 644 | 643 |
| 645 pub.writeLine("y"); | 644 pub.writeLine("y"); |
| 646 } | 645 } |
| 647 | 646 |
| 648 | 647 |
| 649 /// Calls [fn] with appropriately modified arguments to run a pub process. [fn] | 648 /// Calls [fn] with appropriately modified arguments to run a pub process. [fn] |
| 650 /// should have the same signature as [startProcess], except that the returned | 649 /// should have the same signature as [startProcess], except that the returned |
| 651 /// [Future] may have a type other than [Process]. | 650 /// [Future] may have a type other than [Process]. |
| 652 Future _doPub(Function fn, sandboxDir, List<String> args, Uri tokenEndpoint) { | 651 Future _doPub(Function fn, sandboxDir, List args, Future<Uri> tokenEndpoint) { |
| 653 String pathInSandbox(path) => join(getFullPath(sandboxDir), path); | 652 String pathInSandbox(path) => join(getFullPath(sandboxDir), path); |
| 654 | 653 |
| 655 return ensureDir(pathInSandbox(appPath)).chain((_) { | 654 return Futures.wait([ |
| 655 ensureDir(pathInSandbox(appPath)), |
| 656 _awaitObject(args), |
| 657 tokenEndpoint == null ? new Future.immediate(null) : tokenEndpoint |
| 658 ]).chain((results) { |
| 659 var args = results[1]; |
| 660 var tokenEndpoint = results[2]; |
| 656 // Find a Dart executable we can use to spawn. Use the same one that was | 661 // Find a Dart executable we can use to spawn. Use the same one that was |
| 657 // used to run this script itself. | 662 // used to run this script itself. |
| 658 var dartBin = new Options().executable; | 663 var dartBin = new Options().executable; |
| 659 | 664 |
| 660 // If the executable looks like a path, get its full path. That way we | 665 // If the executable looks like a path, get its full path. That way we |
| 661 // can still find it when we spawn it with a different working directory. | 666 // can still find it when we spawn it with a different working directory. |
| 662 if (dartBin.contains(Platform.pathSeparator)) { | 667 if (dartBin.contains(Platform.pathSeparator)) { |
| 663 dartBin = new File(dartBin).fullPathSync(); | 668 dartBin = new File(dartBin).fullPathSync(); |
| 664 } | 669 } |
| 665 | 670 |
| (...skipping 992 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1658 /// calling [completion] is unnecessary. | 1663 /// calling [completion] is unnecessary. |
| 1659 void expectLater(Future actual, matcher, {String reason, | 1664 void expectLater(Future actual, matcher, {String reason, |
| 1660 FailureHandler failureHandler, bool verbose: false}) { | 1665 FailureHandler failureHandler, bool verbose: false}) { |
| 1661 _schedule((_) { | 1666 _schedule((_) { |
| 1662 return actual.transform((value) { | 1667 return actual.transform((value) { |
| 1663 expect(value, matcher, reason: reason, failureHandler: failureHandler, | 1668 expect(value, matcher, reason: reason, failureHandler: failureHandler, |
| 1664 verbose: false); | 1669 verbose: false); |
| 1665 }); | 1670 }); |
| 1666 }); | 1671 }); |
| 1667 } | 1672 } |
| OLD | NEW |