| 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 11 matching lines...) Expand all Loading... |
| 22 FileDescriptor file(String name, String contents) => | 22 FileDescriptor file(String name, String contents) => |
| 23 new FileDescriptor(name, contents); | 23 new FileDescriptor(name, contents); |
| 24 | 24 |
| 25 /** | 25 /** |
| 26 * Creates a new [DirectoryDescriptor] with [name] and [contents]. | 26 * Creates a new [DirectoryDescriptor] with [name] and [contents]. |
| 27 */ | 27 */ |
| 28 DirectoryDescriptor dir(String name, [List<Descriptor> contents]) => | 28 DirectoryDescriptor dir(String name, [List<Descriptor> contents]) => |
| 29 new DirectoryDescriptor(name, contents); | 29 new DirectoryDescriptor(name, contents); |
| 30 | 30 |
| 31 /** | 31 /** |
| 32 * Creates a new [GitRepoDescriptor] with [name] and [contents]. | |
| 33 */ | |
| 34 DirectoryDescriptor git(String name, [List<Descriptor> contents]) => | |
| 35 new GitRepoDescriptor(name, contents); | |
| 36 | |
| 37 /** | |
| 38 * The path of the package cache directory used for tests. Relative to the | 32 * The path of the package cache directory used for tests. Relative to the |
| 39 * sandbox directory. | 33 * sandbox directory. |
| 40 */ | 34 */ |
| 41 final String cachePath = "cache"; | 35 final String cachePath = "cache"; |
| 42 | 36 |
| 43 /** | 37 /** |
| 44 * The path of the mock SDK directory used for tests. Relative to the sandbox | 38 * The path of the mock SDK directory used for tests. Relative to the sandbox |
| 45 * directory. | 39 * directory. |
| 46 */ | 40 */ |
| 47 final String sdkPath = "sdk"; | 41 final String sdkPath = "sdk"; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 68 * The list of events that are scheduled to run after the sandbox directory has | 62 * The list of events that are scheduled to run after the sandbox directory has |
| 69 * been created but before Pub is run. | 63 * been created but before Pub is run. |
| 70 */ | 64 */ |
| 71 List<_ScheduledEvent> _scheduledBeforePub; | 65 List<_ScheduledEvent> _scheduledBeforePub; |
| 72 | 66 |
| 73 /** | 67 /** |
| 74 * The list of events that are scheduled to run after Pub has been run. | 68 * The list of events that are scheduled to run after Pub has been run. |
| 75 */ | 69 */ |
| 76 List<_ScheduledEvent> _scheduledAfterPub; | 70 List<_ScheduledEvent> _scheduledAfterPub; |
| 77 | 71 |
| 78 void runPub([List<String> args, Pattern output, int exitCode = 0]) { | 72 void runPub([List<String> args, String output, int exitCode = 0]) { |
| 79 var createdSandboxDir; | 73 var createdSandboxDir; |
| 80 | 74 |
| 81 var asyncDone = expectAsync0(() {}); | 75 var asyncDone = expectAsync0(() {}); |
| 82 | 76 |
| 83 deleteSandboxIfCreated(onDeleted()) { | 77 deleteSandboxIfCreated(onDeleted()) { |
| 84 _scheduledBeforePub = null; | 78 _scheduledBeforePub = null; |
| 85 _scheduledAfterPub = null; | 79 _scheduledAfterPub = null; |
| 86 if (createdSandboxDir != null) { | 80 if (createdSandboxDir != null) { |
| 87 deleteDir(createdSandboxDir).then((_) => onDeleted()); | 81 deleteDir(createdSandboxDir).then((_) => onDeleted()); |
| 88 } else { | 82 } else { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 // Find the main pub entrypoint. | 150 // Find the main pub entrypoint. |
| 157 final pubPath = fs.joinPaths(scriptDir, '../../pub/pub.dart'); | 151 final pubPath = fs.joinPaths(scriptDir, '../../pub/pub.dart'); |
| 158 | 152 |
| 159 final args = ['--enable-type-checks', '--enable-asserts', pubPath]; | 153 final args = ['--enable-type-checks', '--enable-asserts', pubPath]; |
| 160 args.addAll(pubArgs); | 154 args.addAll(pubArgs); |
| 161 | 155 |
| 162 return runProcess(dartBin, args, workingDir); | 156 return runProcess(dartBin, args, workingDir); |
| 163 } | 157 } |
| 164 | 158 |
| 165 /** | 159 /** |
| 166 * Compares the [actual] output from running pub with [expected]. For [String] | 160 * Compares the [actual] output from running pub with [expectedText]. Ignores |
| 167 * patterns, ignores leading and trailing whitespace differences and tries to | 161 * leading and trailing whitespace differences and tries to report the |
| 168 * report the offending difference in a nice way. For other [Pattern]s, just | 162 * offending difference in a nice way. |
| 169 * reports whether the output contained the pattern. | |
| 170 */ | 163 */ |
| 171 void _validateOutput(Pattern expected, List<String> actual) { | 164 void _validateOutput(String expectedText, List<String> actual) { |
| 172 if (expected is String) return _validateOutputString(expected, actual); | |
| 173 var actualText = Strings.join(actual, "\n"); | |
| 174 if (actualText.contains(expected)) return; | |
| 175 Expect.fail('Expected output to match "$expected", was:\n$actualText'); | |
| 176 } | |
| 177 | |
| 178 void _validateOutputString(String expectedText, List<String> actual) { | |
| 179 final expected = expectedText.split('\n'); | 165 final expected = expectedText.split('\n'); |
| 180 | 166 |
| 181 // Strip off the last line. This lets us have expected multiline strings | 167 // Strip off the last line. This lets us have expected multiline strings |
| 182 // where the closing ''' is on its own line. It also fixes '' expected output | 168 // where the closing ''' is on its own line. It also fixes '' expected output |
| 183 // to expect zero lines of output, not a single empty line. | 169 // to expect zero lines of output, not a single empty line. |
| 184 expected.removeLast(); | 170 expected.removeLast(); |
| 185 | 171 |
| 186 final length = Math.min(expected.length, actual.length); | 172 final length = Math.min(expected.length, actual.length); |
| 187 for (var i = 0; i < length; i++) { | 173 for (var i = 0; i < length; i++) { |
| 188 if (expected[i].trim() != actual[i].trim()) { | 174 if (expected[i].trim() != actual[i].trim()) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 * Validates that this descriptor correctly matches the corresponding file | 223 * Validates that this descriptor correctly matches the corresponding file |
| 238 * system entry within [dir]. Returns a [Future] that completes to `null` if | 224 * system entry within [dir]. Returns a [Future] that completes to `null` if |
| 239 * the entry is valid, or throws an error if it failed. | 225 * the entry is valid, or throws an error if it failed. |
| 240 */ | 226 */ |
| 241 abstract Future validate(String dir); | 227 abstract Future validate(String dir); |
| 242 | 228 |
| 243 /** | 229 /** |
| 244 * Schedules the directory to be created before Pub is run with [runPub]. The | 230 * Schedules the directory to be created before Pub is run with [runPub]. The |
| 245 * directory will be created relative to the sandbox directory. | 231 * directory will be created relative to the sandbox directory. |
| 246 */ | 232 */ |
| 247 // TODO(nweiz): Use implicit closurization once issue 2984 is fixed. | 233 void scheduleCreate() => _scheduleBeforePub(create); |
| 248 void scheduleCreate() => _scheduleBeforePub((dir) => this.create(dir)); | |
| 249 | 234 |
| 250 /** | 235 /** |
| 251 * Schedules the directory to be validated after Pub is run with [runPub]. The | 236 * Schedules the directory to be validated after Pub is run with [runPub]. The |
| 252 * directory will be validated relative to the sandbox directory. | 237 * directory will be validated relative to the sandbox directory. |
| 253 */ | 238 */ |
| 254 void scheduleValidate() => | 239 void scheduleValidate() => |
| 255 _scheduleAfterPub((parentDir) => validate(parentDir.path)); | 240 _scheduleAfterPub((parentDir) => validate(parentDir.path)); |
| 256 } | 241 } |
| 257 | 242 |
| 258 /** | 243 /** |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 // Validate each of the items in this directory. | 326 // Validate each of the items in this directory. |
| 342 final entryFutures = contents.map( | 327 final entryFutures = contents.map( |
| 343 (entry) => entry.validate(join(path, name))); | 328 (entry) => entry.validate(join(path, name))); |
| 344 | 329 |
| 345 // If they are all valid, the directory is valid. | 330 // If they are all valid, the directory is valid. |
| 346 return Futures.wait(entryFutures).transform((entries) => null); | 331 return Futures.wait(entryFutures).transform((entries) => null); |
| 347 } | 332 } |
| 348 } | 333 } |
| 349 | 334 |
| 350 /** | 335 /** |
| 351 * Describes a Git repository and its contents. | |
| 352 */ | |
| 353 class GitRepoDescriptor extends DirectoryDescriptor { | |
| 354 GitRepoDescriptor(String name, List<Descriptor> contents) | |
| 355 : super(name, contents); | |
| 356 | |
| 357 /** | |
| 358 * Creates the Git repository and commits the contents. | |
| 359 */ | |
| 360 Future<Directory> create(parentDir) { | |
| 361 var workingDir; | |
| 362 Future runGit(List<String> args) { | |
| 363 return runProcess('git', args, workingDir: workingDir.path). | |
| 364 transform((result) { | |
| 365 if (!result.success) throw "Error running git: ${result.stderr}"; | |
| 366 return null; | |
| 367 }); | |
| 368 } | |
| 369 | |
| 370 return super.create(parentDir).chain((rootDir) { | |
| 371 workingDir = rootDir; | |
| 372 return runGit(['init']); | |
| 373 }).chain((_) => runGit(['add', '.'])) | |
| 374 .chain((_) => runGit(['commit', '-m', 'initial commit'])) | |
| 375 .transform((_) => workingDir); | |
| 376 } | |
| 377 } | |
| 378 | |
| 379 /** | |
| 380 * Schedules a callback to be called before Pub is run with [runPub]. | 336 * Schedules a callback to be called before Pub is run with [runPub]. |
| 381 */ | 337 */ |
| 382 void _scheduleBeforePub(_ScheduledEvent event) { | 338 void _scheduleBeforePub(_ScheduledEvent event) { |
| 383 if (_scheduledBeforePub == null) _scheduledBeforePub = []; | 339 if (_scheduledBeforePub == null) _scheduledBeforePub = []; |
| 384 _scheduledBeforePub.add(event); | 340 _scheduledBeforePub.add(event); |
| 385 } | 341 } |
| 386 | 342 |
| 387 /** | 343 /** |
| 388 * Schedules a callback to be called after Pub is run with [runPub]. | 344 * Schedules a callback to be called after Pub is run with [runPub]. |
| 389 */ | 345 */ |
| 390 void _scheduleAfterPub(_ScheduledEvent event) { | 346 void _scheduleAfterPub(_ScheduledEvent event) { |
| 391 if (_scheduledAfterPub == null) _scheduledAfterPub = []; | 347 if (_scheduledAfterPub == null) _scheduledAfterPub = []; |
| 392 _scheduledAfterPub.add(event); | 348 _scheduledAfterPub.add(event); |
| 393 } | 349 } |
| OLD | NEW |