Chromium Code Reviews| 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 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 499 future.chain((_) => cleanup()).then((_) { | 499 future.chain((_) => cleanup()).then((_) { |
| 500 asyncDone(); | 500 asyncDone(); |
| 501 }); | 501 }); |
| 502 } | 502 } |
| 503 | 503 |
| 504 /// Get the path to the root "util/test/pub" directory containing the pub tests. | 504 /// Get the path to the root "util/test/pub" directory containing the pub tests. |
| 505 String get testDirectory { | 505 String get testDirectory { |
| 506 var dir = new Path.fromNative(new Options().script); | 506 var dir = new Path.fromNative(new Options().script); |
| 507 while (dir.filename != 'pub') dir = dir.directoryPath; | 507 while (dir.filename != 'pub') dir = dir.directoryPath; |
| 508 | 508 |
| 509 return dir.toNativePath(); | 509 return new File(dir.toNativePath()).fullPathSync(); |
| 510 } | 510 } |
| 511 | 511 |
| 512 /** | 512 /** |
| 513 * Schedules a call to the Pub command-line utility. Runs Pub with [args] and | 513 * Schedules a call to the Pub command-line utility. Runs Pub with [args] and |
| 514 * validates that its results match [output], [error], and [exitCode]. | 514 * validates that its results match [output], [error], and [exitCode]. |
| 515 */ | 515 */ |
| 516 void schedulePub([List<String> args, Pattern output, Pattern error, | 516 void schedulePub([List<String> args, Pattern output, Pattern error, |
| 517 int exitCode = 0]) { | 517 int exitCode = 0]) { |
| 518 _schedule((sandboxDir) { | 518 _schedule((sandboxDir) { |
| 519 String pathInSandbox(path) => join(getFullPath(sandboxDir), path); | 519 String pathInSandbox(path) => join(getFullPath(sandboxDir), path); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 534 | 534 |
| 535 var dartArgs = | 535 var dartArgs = |
| 536 ['--enable-type-checks', '--enable-asserts', pubPath, '--trace']; | 536 ['--enable-type-checks', '--enable-asserts', pubPath, '--trace']; |
| 537 dartArgs.addAll(args); | 537 dartArgs.addAll(args); |
| 538 | 538 |
| 539 var environment = new Map.from(Platform.environment); | 539 var environment = new Map.from(Platform.environment); |
| 540 environment['PUB_CACHE'] = pathInSandbox(cachePath); | 540 environment['PUB_CACHE'] = pathInSandbox(cachePath); |
| 541 environment['DART_SDK'] = pathInSandbox(sdkPath); | 541 environment['DART_SDK'] = pathInSandbox(sdkPath); |
| 542 | 542 |
| 543 return runProcess(dartBin, dartArgs, workingDir: pathInSandbox(appPath), | 543 return runProcess(dartBin, dartArgs, workingDir: pathInSandbox(appPath), |
| 544 environment: environment, pipeStdout: output == null, | 544 environment: environment); |
| 545 pipeStderr: error == null); | |
| 546 }).transform((result) { | 545 }).transform((result) { |
| 547 _validateOutput(output, result.stdout); | 546 var failures = []; |
| 548 _validateOutput(error, result.stderr); | |
| 549 | 547 |
| 550 Expect.equals(result.exitCode, exitCode, | 548 _validateOutput(failures, 'stdout', output, result.stdout); |
| 551 'Pub returned exit code ${result.exitCode}, expected $exitCode.'); | 549 _validateOutput(failures, 'stderr', error, result.stderr); |
| 550 | |
| 551 if (result.exitCode != exitCode) { | |
| 552 failures.add( | |
| 553 'Pub returned exit code ${result.exitCode}, expected $exitCode.'); | |
| 554 } | |
| 555 | |
| 556 if (failures.length > 0) { | |
| 557 if (error == null) { | |
| 558 // If we aren't validating the error, still show it on failure. | |
| 559 failures.add('Pub stderr:'); | |
| 560 failures.addAll(result.stderr.map((line) => '| $line')); | |
| 561 } | |
| 562 | |
| 563 throw new ExpectException(Strings.join(failures, '\n')); | |
| 564 } | |
| 552 | 565 |
| 553 return null; | 566 return null; |
| 554 }); | 567 }); |
| 555 }); | 568 }); |
| 556 } | 569 } |
| 557 | 570 |
| 558 /** | 571 /** |
| 559 * A shorthand for [schedulePub] and [run] when no validation needs to be done | 572 * A shorthand for [schedulePub] and [run] when no validation needs to be done |
| 560 * after Pub has been run. | 573 * after Pub has been run. |
| 561 */ | 574 */ |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 610 | 623 |
| 611 return runNextEvent(null); | 624 return runNextEvent(null); |
| 612 } | 625 } |
| 613 | 626 |
| 614 /** | 627 /** |
| 615 * Compares the [actual] output from running pub with [expected]. For [String] | 628 * Compares the [actual] output from running pub with [expected]. For [String] |
| 616 * patterns, ignores leading and trailing whitespace differences and tries to | 629 * patterns, ignores leading and trailing whitespace differences and tries to |
| 617 * report the offending difference in a nice way. For other [Pattern]s, just | 630 * report the offending difference in a nice way. For other [Pattern]s, just |
| 618 * reports whether the output contained the pattern. | 631 * reports whether the output contained the pattern. |
| 619 */ | 632 */ |
| 620 void _validateOutput(Pattern expected, List<String> actual) { | 633 void _validateOutput(List<String> failures, String pipe, Pattern expected, |
| 634 List<String> actual) { | |
| 621 if (expected == null) return; | 635 if (expected == null) return; |
| 622 | 636 |
| 623 if (expected is String) return _validateOutputString(expected, actual); | 637 if (expected is RegExp) { |
|
nweiz
2012/10/09 00:06:39
I'd rather short-circuit than have so much nested
Bob Nystrom
2012/10/15 20:52:15
I reordered this to have a positive "is RegExp" te
| |
| 624 var actualText = Strings.join(actual, "\n"); | 638 var actualText = Strings.join(actual, "\n"); |
| 625 if (actualText.contains(expected)) return; | 639 if (!actualText.contains(expected)) { |
| 626 Expect.fail('Expected output to match "$expected", was:\n$actualText'); | 640 if (actual.length == 0) { |
| 641 failures.add('Expected $pipe to match "${expected.pattern}" but got none .'); | |
|
nweiz
2012/10/09 00:06:39
Line length.
Bob Nystrom
2012/10/15 20:52:15
Done.
| |
| 642 } else { | |
| 643 failures.add('Expected $pipe to match "${expected.pattern}" but got:'); | |
| 644 failures.addAll(actual.map((line) => '| $line')); | |
| 645 } | |
| 646 } | |
| 647 | |
| 648 return; | |
| 649 } | |
| 650 | |
| 651 _validateOutputString(failures, pipe, expected, actual); | |
| 627 } | 652 } |
| 628 | 653 |
| 629 void _validateOutputString(String expectedText, List<String> actual) { | 654 void _validateOutputString(List<String> failures, String pipe, |
| 655 String expectedText, List<String> actual) { | |
| 630 final expected = expectedText.split('\n'); | 656 final expected = expectedText.split('\n'); |
| 631 | 657 |
| 632 // Strip off the last line. This lets us have expected multiline strings | 658 // Strip off the last line. This lets us have expected multiline strings |
| 633 // where the closing ''' is on its own line. It also fixes '' expected output | 659 // where the closing ''' is on its own line. It also fixes '' expected output |
| 634 // to expect zero lines of output, not a single empty line. | 660 // to expect zero lines of output, not a single empty line. |
| 635 expected.removeLast(); | 661 expected.removeLast(); |
| 636 | 662 |
| 637 final length = min(expected.length, actual.length); | 663 var results = []; |
| 664 var failed = false; | |
| 665 | |
| 666 // Compare them line by line to see which ones match. | |
| 667 var length = max(expected.length, actual.length); | |
| 638 for (var i = 0; i < length; i++) { | 668 for (var i = 0; i < length; i++) { |
| 639 if (expected[i].trim() != actual[i].trim()) { | 669 if (i >= actual.length) { |
| 640 Expect.fail( | 670 // Missing output. |
| 641 'Output line ${i + 1} was: ${actual[i]}\nexpected: ${expected[i]}'); | 671 failed = true; |
| 672 results.add('? ${expected[i]}'); | |
| 673 } else if (i >= expected.length) { | |
| 674 // Unexpected extra output. | |
| 675 failed = true; | |
| 676 results.add('X ${actual[i]}'); | |
| 677 } else { | |
| 678 var expectedLine = expected[i].trim(); | |
| 679 var actualLine = actual[i].trim(); | |
| 680 | |
| 681 if (expectedLine != actualLine) { | |
| 682 // Mismatched lines. | |
| 683 failed = true; | |
| 684 results.add('X ${actual[i]}'); | |
| 685 } else { | |
| 686 // Output is OK, but include it in case other lines are wrong. | |
| 687 results.add('| ${actual[i]}'); | |
| 688 } | |
| 642 } | 689 } |
| 643 } | 690 } |
| 644 | 691 |
| 645 if (expected.length > actual.length) { | 692 // If any lines mismatched, show the expected and actual. |
| 646 final message = new StringBuffer(); | 693 if (failed) { |
| 647 message.add('Missing expected output:\n'); | 694 failures.add('Expected $pipe:'); |
| 648 for (var i = actual.length; i < expected.length; i++) { | 695 failures.addAll(expected.map((line) => '| $line')); |
| 649 message.add(expected[i]); | 696 failures.add('Got:'); |
| 650 message.add('\n'); | 697 failures.addAll(results); |
| 651 } | |
| 652 | |
| 653 Expect.fail(message.toString()); | |
| 654 } | |
| 655 | |
| 656 if (expected.length < actual.length) { | |
| 657 final message = new StringBuffer(); | |
| 658 message.add('Unexpected output:\n'); | |
| 659 for (var i = expected.length; i < actual.length; i++) { | |
| 660 message.add(actual[i]); | |
| 661 message.add('\n'); | |
| 662 } | |
| 663 | |
| 664 Expect.fail(message.toString()); | |
| 665 } | 698 } |
| 666 } | 699 } |
| 667 | 700 |
| 668 /** | 701 /** |
| 669 * Base class for [FileDescriptor] and [DirectoryDescriptor] so that a | 702 * Base class for [FileDescriptor] and [DirectoryDescriptor] so that a |
| 670 * directory can contain a heterogeneous collection of files and | 703 * directory can contain a heterogeneous collection of files and |
| 671 * subdirectories. | 704 * subdirectories. |
| 672 */ | 705 */ |
| 673 abstract class Descriptor { | 706 abstract class Descriptor { |
| 674 /** | 707 /** |
| (...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1166 } | 1199 } |
| 1167 | 1200 |
| 1168 /** | 1201 /** |
| 1169 * Schedules a callback to be called after Pub is run with [runPub], even if it | 1202 * Schedules a callback to be called after Pub is run with [runPub], even if it |
| 1170 * fails. | 1203 * fails. |
| 1171 */ | 1204 */ |
| 1172 void _scheduleCleanup(_ScheduledEvent event) { | 1205 void _scheduleCleanup(_ScheduledEvent event) { |
| 1173 if (_scheduledCleanup == null) _scheduledCleanup = []; | 1206 if (_scheduledCleanup == null) _scheduledCleanup = []; |
| 1174 _scheduledCleanup.add(event); | 1207 _scheduledCleanup.add(event); |
| 1175 } | 1208 } |
| OLD | NEW |