| 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 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 pub.stdout.expect(emitsLines( | 469 pub.stdout.expect(emitsLines( |
| 470 "|-- LICENSE\n" | 470 "|-- LICENSE\n" |
| 471 "|-- lib\n" | 471 "|-- lib\n" |
| 472 "| '-- test_pkg.dart\n" | 472 "| '-- test_pkg.dart\n" |
| 473 "'-- pubspec.yaml\n" | 473 "'-- pubspec.yaml\n" |
| 474 "\n" | 474 "\n" |
| 475 "Looks great! Are you ready to upload your package (y/n)?")); | 475 "Looks great! Are you ready to upload your package (y/n)?")); |
| 476 pub.writeLine("y"); | 476 pub.writeLine("y"); |
| 477 } | 477 } |
| 478 | 478 |
| 479 /// Gets the absolute path to [relPath], which is a relative path in the test |
| 480 /// sandbox. |
| 481 String _pathInSandbox(String relPath) { |
| 482 return p.join(p.absolute(sandboxDir), relPath); |
| 483 } |
| 484 |
| 485 /// Gets the environment variables used to run pub in a test context. |
| 486 Map getPubTestEnvironment([Uri tokenEndpoint]) { |
| 487 var environment = {}; |
| 488 environment['_PUB_TESTING'] = 'true'; |
| 489 environment['PUB_CACHE'] = _pathInSandbox(cachePath); |
| 490 |
| 491 // Ensure a known SDK version is set for the tests that rely on that. |
| 492 environment['_PUB_TEST_SDK_VERSION'] = "0.1.2+3"; |
| 493 |
| 494 if (tokenEndpoint != null) { |
| 495 environment['_PUB_TEST_TOKEN_ENDPOINT'] = |
| 496 tokenEndpoint.toString(); |
| 497 } |
| 498 |
| 499 return environment; |
| 500 } |
| 501 |
| 479 /// Starts a Pub process and returns a [ScheduledProcess] that supports | 502 /// Starts a Pub process and returns a [ScheduledProcess] that supports |
| 480 /// interaction with that process. | 503 /// interaction with that process. |
| 481 /// | 504 /// |
| 482 /// Any futures in [args] will be resolved before the process is started. | 505 /// Any futures in [args] will be resolved before the process is started. |
| 483 ScheduledProcess startPub({List args, Future<Uri> tokenEndpoint}) { | 506 ScheduledProcess startPub({List args, Future<Uri> tokenEndpoint}) { |
| 484 String pathInSandbox(String relPath) { | 507 ensureDir(_pathInSandbox(appPath)); |
| 485 return p.join(p.absolute(sandboxDir), relPath); | |
| 486 } | |
| 487 | |
| 488 ensureDir(pathInSandbox(appPath)); | |
| 489 | 508 |
| 490 // Find a Dart executable we can use to spawn. Use the same one that was | 509 // Find a Dart executable we can use to spawn. Use the same one that was |
| 491 // used to run this script itself. | 510 // used to run this script itself. |
| 492 var dartBin = Platform.executable; | 511 var dartBin = Platform.executable; |
| 493 | 512 |
| 494 // If the executable looks like a path, get its full path. That way we | 513 // If the executable looks like a path, get its full path. That way we |
| 495 // can still find it when we spawn it with a different working directory. | 514 // can still find it when we spawn it with a different working directory. |
| 496 if (dartBin.contains(Platform.pathSeparator)) { | 515 if (dartBin.contains(Platform.pathSeparator)) { |
| 497 dartBin = p.absolute(dartBin); | 516 dartBin = p.absolute(dartBin); |
| 498 } | 517 } |
| 499 | 518 |
| 500 // Always run pub from a snapshot. Since we require the SDK to be built, the | 519 // Always run pub from a snapshot. Since we require the SDK to be built, the |
| 501 // snapshot should be there. Note that this *does* mean that the snapshot has | 520 // snapshot should be there. Note that this *does* mean that the snapshot has |
| 502 // to be manually updated when changing code before running the tests. | 521 // to be manually updated when changing code before running the tests. |
| 503 // Otherwise, you will test against stale data. | 522 // Otherwise, you will test against stale data. |
| 504 // | 523 // |
| 505 // Using the snapshot makes running the tests much faster, which is why we | 524 // Using the snapshot makes running the tests much faster, which is why we |
| 506 // make this trade-off. | 525 // make this trade-off. |
| 507 var pubPath = p.join(p.dirname(dartBin), 'snapshots/pub.dart.snapshot'); | 526 var pubPath = p.join(p.dirname(dartBin), 'snapshots/pub.dart.snapshot'); |
| 508 var dartArgs = [pubPath, '--verbose']; | 527 var dartArgs = [pubPath, '--verbose']; |
| 509 dartArgs.addAll(args); | 528 dartArgs.addAll(args); |
| 510 | 529 |
| 511 if (tokenEndpoint == null) tokenEndpoint = new Future.value(); | 530 if (tokenEndpoint == null) tokenEndpoint = new Future.value(); |
| 512 var environmentFuture = tokenEndpoint.then((tokenEndpoint) { | 531 var environmentFuture = tokenEndpoint.then((tokenEndpoint) { |
| 513 var environment = {}; | 532 var environment = getPubTestEnvironment(tokenEndpoint); |
| 514 environment['_PUB_TESTING'] = 'true'; | |
| 515 environment['PUB_CACHE'] = pathInSandbox(cachePath); | |
| 516 | |
| 517 // Ensure a known SDK version is set for the tests that rely on that. | |
| 518 environment['_PUB_TEST_SDK_VERSION'] = "0.1.2+3"; | |
| 519 | |
| 520 if (tokenEndpoint != null) { | |
| 521 environment['_PUB_TEST_TOKEN_ENDPOINT'] = | |
| 522 tokenEndpoint.toString(); | |
| 523 } | |
| 524 | 533 |
| 525 // If there is a server running, tell pub what its URL is so hosted | 534 // If there is a server running, tell pub what its URL is so hosted |
| 526 // dependencies will look there. | 535 // dependencies will look there. |
| 527 if (_hasServer) { | 536 if (_hasServer) { |
| 528 return port.then((p) { | 537 return port.then((p) { |
| 529 environment['PUB_HOSTED_URL'] = "http://localhost:$p"; | 538 environment['PUB_HOSTED_URL'] = "http://localhost:$p"; |
| 530 return environment; | 539 return environment; |
| 531 }); | 540 }); |
| 532 } | 541 } |
| 533 | 542 |
| 534 return environment; | 543 return environment; |
| 535 }); | 544 }); |
| 536 | 545 |
| 537 return new PubProcess.start(dartBin, dartArgs, environment: environmentFuture, | 546 return new PubProcess.start(dartBin, dartArgs, environment: environmentFuture, |
| 538 workingDirectory: pathInSandbox(appPath), | 547 workingDirectory: _pathInSandbox(appPath), |
| 539 description: args.isEmpty ? 'pub' : 'pub ${args.first}'); | 548 description: args.isEmpty ? 'pub' : 'pub ${args.first}'); |
| 540 } | 549 } |
| 541 | 550 |
| 542 /// A subclass of [ScheduledProcess] that parses pub's verbose logging output | 551 /// A subclass of [ScheduledProcess] that parses pub's verbose logging output |
| 543 /// and makes [stdout] and [stderr] work as though pub weren't running in | 552 /// and makes [stdout] and [stderr] work as though pub weren't running in |
| 544 /// verbose mode. | 553 /// verbose mode. |
| 545 class PubProcess extends ScheduledProcess { | 554 class PubProcess extends ScheduledProcess { |
| 546 Stream<Pair<log.Level, String>> _log; | 555 Stream<Pair<log.Level, String>> _log; |
| 547 Stream<String> _stdout; | 556 Stream<String> _stdout; |
| 548 Stream<String> _stderr; | 557 Stream<String> _stderr; |
| (...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 959 _lastMatcher.matches(item.last, matchState); | 968 _lastMatcher.matches(item.last, matchState); |
| 960 } | 969 } |
| 961 | 970 |
| 962 Description describe(Description description) { | 971 Description describe(Description description) { |
| 963 return description.addAll("(", ", ", ")", [_firstMatcher, _lastMatcher]); | 972 return description.addAll("(", ", ", ")", [_firstMatcher, _lastMatcher]); |
| 964 } | 973 } |
| 965 } | 974 } |
| 966 | 975 |
| 967 /// A [StreamMatcher] that matches multiple lines of output. | 976 /// A [StreamMatcher] that matches multiple lines of output. |
| 968 StreamMatcher emitsLines(String output) => inOrder(output.split("\n")); | 977 StreamMatcher emitsLines(String output) => inOrder(output.split("\n")); |
| OLD | NEW |