| 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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 * The list of events that are scheduled to run after the sandbox directory has | 68 * The list of events that are scheduled to run after the sandbox directory has |
| 69 * been created but before Pub is run. | 69 * been created but before Pub is run. |
| 70 */ | 70 */ |
| 71 List<_ScheduledEvent> _scheduledBeforePub; | 71 List<_ScheduledEvent> _scheduledBeforePub; |
| 72 | 72 |
| 73 /** | 73 /** |
| 74 * The list of events that are scheduled to run after Pub has been run. | 74 * The list of events that are scheduled to run after Pub has been run. |
| 75 */ | 75 */ |
| 76 List<_ScheduledEvent> _scheduledAfterPub; | 76 List<_ScheduledEvent> _scheduledAfterPub; |
| 77 | 77 |
| 78 void runPub([List<String> args, Pattern output, int exitCode = 0]) { | 78 void runPub([List<String> args, Pattern output, Pattern error, |
| 79 int exitCode = 0]) { |
| 79 var createdSandboxDir; | 80 var createdSandboxDir; |
| 80 | 81 |
| 81 var asyncDone = expectAsync0(() {}); | 82 var asyncDone = expectAsync0(() {}); |
| 82 | 83 |
| 83 deleteSandboxIfCreated(onDeleted()) { | 84 deleteSandboxIfCreated(onDeleted()) { |
| 84 _scheduledBeforePub = null; | 85 _scheduledBeforePub = null; |
| 85 _scheduledAfterPub = null; | 86 _scheduledAfterPub = null; |
| 86 if (createdSandboxDir != null) { | 87 if (createdSandboxDir != null) { |
| 87 deleteDir(createdSandboxDir).then((_) => onDeleted()); | 88 deleteDir(createdSandboxDir).then((_) => onDeleted()); |
| 88 } else { | 89 } else { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 101 // TODO(rnystrom): Hack in the cache directory path. Should pass this | 102 // TODO(rnystrom): Hack in the cache directory path. Should pass this |
| 102 // in using environment var once #752 is done. | 103 // in using environment var once #752 is done. |
| 103 args.add('--cachedir=${pathInSandbox(cachePath)}'); | 104 args.add('--cachedir=${pathInSandbox(cachePath)}'); |
| 104 | 105 |
| 105 // TODO(rnystrom): Hack in the SDK path. Should pass this in using | 106 // TODO(rnystrom): Hack in the SDK path. Should pass this in using |
| 106 // environment var once #752 is done. | 107 // environment var once #752 is done. |
| 107 args.add('--sdkdir=${pathInSandbox(sdkPath)}'); | 108 args.add('--sdkdir=${pathInSandbox(sdkPath)}'); |
| 108 | 109 |
| 109 return _runPub(args, pathInSandbox(appPath)); | 110 return _runPub(args, pathInSandbox(appPath)); |
| 110 }).chain((result) { | 111 }).chain((result) { |
| 111 Expect.equals(result.stderr.length, 0, | 112 _validateOutput(output, result.stdout); |
| 112 'Did not expect any output on stderr, and got:\n' + | 113 _validateOutput(error, result.stderr); |
| 113 Strings.join(result.stderr, '\n')); | |
| 114 | 114 |
| 115 Expect.equals(result.exitCode, exitCode, | 115 Expect.equals(result.exitCode, exitCode, |
| 116 'Pub returned exit code ${result.exitCode}, expected $exitCode.'); | 116 'Pub returned exit code ${result.exitCode}, expected $exitCode.'); |
| 117 | 117 |
| 118 _validateOutput(output, result.stdout); | |
| 119 | |
| 120 return _runScheduled(createdSandboxDir, _scheduledAfterPub); | 118 return _runScheduled(createdSandboxDir, _scheduledAfterPub); |
| 121 }); | 119 }); |
| 122 | 120 |
| 123 future.then((_) { | 121 future.then((_) { |
| 124 deleteSandboxIfCreated(asyncDone); | 122 deleteSandboxIfCreated(asyncDone); |
| 125 }); | 123 }); |
| 126 | 124 |
| 127 future.handleException((error) { | 125 future.handleException((error) { |
| 128 // If an error occurs during testing, delete the sandbox, throw the error so | 126 // If an error occurs during testing, delete the sandbox, throw the error so |
| 129 // that the test framework sees it, then finally call asyncDone so that the | 127 // that the test framework sees it, then finally call asyncDone so that the |
| 130 // test framework knows we're done doing asynchronous stuff. | 128 // test framework knows we're done doing asynchronous stuff. |
| 131 deleteSandboxIfCreated(() { | 129 deleteSandboxIfCreated(() { |
| 132 guardAsync(() { throw error; }, asyncDone); | 130 guardAsync(() { throw error; }, asyncDone); |
| 133 }); | 131 }); |
| 134 return true; | 132 return true; |
| 135 }); | 133 }); |
| 136 } | 134 } |
| 137 | 135 |
| 136 |
| 137 /** |
| 138 * Wraps a test that needs git in order to run. This validates that the test is |
| 139 * running on a builbot in which case we expect git to be installed. If we are |
| 140 * not running on the buildbot, we will instead see if git is installed and |
| 141 * skip the test if not. This way, users don't need to have git installed to |
| 142 * run the tests locally (unless they actually care about the pub git tests). |
| 143 */ |
| 144 void withGit(void callback()) { |
| 145 isGitInstalled.then(expectAsync1((installed) { |
| 146 if (installed || Platform.environment.containsKey('BUILDBOT_BUILDERNAME')) { |
| 147 callback(); |
| 148 } |
| 149 })); |
| 150 } |
| 151 |
| 138 Future<Directory> _setUpSandbox() { | 152 Future<Directory> _setUpSandbox() { |
| 139 return createTempDir('pub-test-sandbox-'); | 153 return createTempDir('pub-test-sandbox-'); |
| 140 } | 154 } |
| 141 | 155 |
| 142 _runScheduled(Directory parentDir, List<_ScheduledEvent> scheduled) { | 156 _runScheduled(Directory parentDir, List<_ScheduledEvent> scheduled) { |
| 143 if (scheduled == null) return new Future.immediate(null); | 157 if (scheduled == null) return new Future.immediate(null); |
| 144 var future = Futures.wait(scheduled.map((event) => event(parentDir))); | 158 var future = Futures.wait(scheduled.map((event) => event(parentDir))); |
| 145 scheduled.clear(); | 159 scheduled.clear(); |
| 146 return future; | 160 return future; |
| 147 } | 161 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 163 return runProcess(dartBin, args, workingDir); | 177 return runProcess(dartBin, args, workingDir); |
| 164 } | 178 } |
| 165 | 179 |
| 166 /** | 180 /** |
| 167 * Compares the [actual] output from running pub with [expected]. For [String] | 181 * Compares the [actual] output from running pub with [expected]. For [String] |
| 168 * patterns, ignores leading and trailing whitespace differences and tries to | 182 * patterns, ignores leading and trailing whitespace differences and tries to |
| 169 * report the offending difference in a nice way. For other [Pattern]s, just | 183 * report the offending difference in a nice way. For other [Pattern]s, just |
| 170 * reports whether the output contained the pattern. | 184 * reports whether the output contained the pattern. |
| 171 */ | 185 */ |
| 172 void _validateOutput(Pattern expected, List<String> actual) { | 186 void _validateOutput(Pattern expected, List<String> actual) { |
| 187 if (expected == null) return; |
| 188 |
| 173 if (expected is String) return _validateOutputString(expected, actual); | 189 if (expected is String) return _validateOutputString(expected, actual); |
| 174 var actualText = Strings.join(actual, "\n"); | 190 var actualText = Strings.join(actual, "\n"); |
| 175 if (actualText.contains(expected)) return; | 191 if (actualText.contains(expected)) return; |
| 176 Expect.fail('Expected output to match "$expected", was:\n$actualText'); | 192 Expect.fail('Expected output to match "$expected", was:\n$actualText'); |
| 177 } | 193 } |
| 178 | 194 |
| 179 void _validateOutputString(String expectedText, List<String> actual) { | 195 void _validateOutputString(String expectedText, List<String> actual) { |
| 180 final expected = expectedText.split('\n'); | 196 final expected = expectedText.split('\n'); |
| 181 | 197 |
| 182 // Strip off the last line. This lets us have expected multiline strings | 198 // Strip off the last line. This lets us have expected multiline strings |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 _scheduledBeforePub.add(event); | 401 _scheduledBeforePub.add(event); |
| 386 } | 402 } |
| 387 | 403 |
| 388 /** | 404 /** |
| 389 * Schedules a callback to be called after Pub is run with [runPub]. | 405 * Schedules a callback to be called after Pub is run with [runPub]. |
| 390 */ | 406 */ |
| 391 void _scheduleAfterPub(_ScheduledEvent event) { | 407 void _scheduleAfterPub(_ScheduledEvent event) { |
| 392 if (_scheduledAfterPub == null) _scheduledAfterPub = []; | 408 if (_scheduledAfterPub == null) _scheduledAfterPub = []; |
| 393 _scheduledAfterPub.add(event); | 409 _scheduledAfterPub.add(event); |
| 394 } | 410 } |
| OLD | NEW |