OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /** |
| 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 |
| 8 * pub, and then validate the results. This library provides an API to build |
| 9 * tests like that. |
| 10 */ |
| 11 #library('test_pub'); |
| 12 |
| 13 #import('dart:io'); |
| 14 |
| 15 #import('../../../lib/unittest/unittest_vm.dart'); |
| 16 #import('../../lib/file_system.dart'); |
| 17 |
| 18 void testOutput(String description, List<String> pubArgs, String expected, |
| 19 [int exitCode = 0]) { |
| 20 asyncTest(description, 1, () { |
| 21 // Find a dart executable we can use to run pub. Uses the one that the |
| 22 // test infrastructure uses. |
| 23 final scriptDir = new File(new Options().script).directorySync().path; |
| 24 final platform = new Platform().operatingSystem(); |
| 25 final dartBin = joinPaths(scriptDir, |
| 26 '../../../tools/testing/bin/$platform/dart'); |
| 27 |
| 28 // Find the main pub entrypoint. |
| 29 final pubPath = joinPaths(scriptDir, '../../pub/pub.dart'); |
| 30 |
| 31 final args = [pubPath]; |
| 32 args.addAll(pubArgs); |
| 33 |
| 34 final process = new Process.start(dartBin, args); |
| 35 final outStream = new StringInputStream(process.stdout); |
| 36 final output = <String>[]; |
| 37 bool processDone = false; |
| 38 |
| 39 checkComplete() { |
| 40 if (!outStream.closed) return; |
| 41 if (!processDone) return; |
| 42 |
| 43 _validateOutput(expected, output); |
| 44 callbackDone(); |
| 45 } |
| 46 |
| 47 process.stderr.pipe(stderr, close: false); |
| 48 |
| 49 outStream.onLine = () { |
| 50 output.add(outStream.readLine()); |
| 51 }; |
| 52 |
| 53 outStream.onClosed = checkComplete; |
| 54 |
| 55 process.onError = (error) { |
| 56 Expect.fail('Failed to run pub: $error'); |
| 57 processDone = true; |
| 58 }; |
| 59 |
| 60 process.onExit = (actualExitCode) { |
| 61 Expect.equals(actualExitCode, exitCode, |
| 62 'Pub returned exit code $actualExitCode, expected $exitCode.'); |
| 63 processDone = true; |
| 64 checkComplete(); |
| 65 }; |
| 66 }); |
| 67 } |
| 68 |
| 69 /** |
| 70 * Compares the [actual] output from running pub with [expectedText]. Ignores |
| 71 * leading and trailing whitespaces differences and tries to report the |
| 72 * offending difference in a nice way. |
| 73 */ |
| 74 void _validateOutput(String expectedText, List<String> actual) { |
| 75 final expected = expectedText.split('\n'); |
| 76 |
| 77 final length = Math.min(expected.length, actual.length); |
| 78 for (var i = 0; i < length; i++) { |
| 79 if (expected[i].trim() != actual[i].trim()) { |
| 80 Expect.fail('Output line ${i + 1} was: ${actual[i]}\nexpected: ${expected[
i]}'); |
| 81 } |
| 82 } |
| 83 |
| 84 if (expected.length > actual.length) { |
| 85 final message = new StringBuffer(); |
| 86 message.add('Missing expected output:\n'); |
| 87 for (var i = actual.length; i < expected.length; i++) { |
| 88 message.add(expected[i]); |
| 89 message.add('\n'); |
| 90 } |
| 91 |
| 92 Expect.fail(message.toString()); |
| 93 } |
| 94 |
| 95 if (expected.length < actual.length) { |
| 96 final message = new StringBuffer(); |
| 97 message.add('Unexpected output:\n'); |
| 98 for (var i = expected.length; i < actual.length; i++) { |
| 99 message.add(actual[i]); |
| 100 message.add('\n'); |
| 101 } |
| 102 |
| 103 Expect.fail(message.toString()); |
| 104 } |
| 105 } |
OLD | NEW |