Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: utils/tests/pub/test_pub.dart

Issue 10382094: Refactor the pub tests. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rename guard to guardAsync Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « utils/tests/pub/pub_test.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 10 matching lines...) Expand all
21 */ 21 */
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 void testPub(String description, [List<Descriptor> cache, Descriptor app, 31 /**
32 List<String> args, List<Descriptor> expectedPackageDir, 32 * The path of the package cache directory used for tests. Relative to the
33 List<Descriptor> sdk, String output, int exitCode = 0]) { 33 * sandbox directory.
34 asyncTest(description, 1, () { 34 */
35 var createdSandboxDir; 35 final String cachePath = "cache";
36 var createdAppDir;
37 var createdSdkDir;
38 36
39 deleteSandboxIfCreated() { 37 /**
40 if (createdSandboxDir != null) { 38 * The path of the mock SDK directory used for tests. Relative to the sandbox
41 deleteDir(createdSandboxDir).then((_) { 39 * directory.
42 callbackDone(); 40 */
43 }); 41 final String sdkPath = "sdk";
44 } else { 42
45 callbackDone(); 43 /**
46 } 44 * The path of the mock app directory used for tests. Relative to the sandbox
45 * directory.
46 */
47 final String appPath = "myapp";
48
49 /**
50 * The path of the packages directory in the mock app used for tests. Relative
51 * to the sandbox directory.
52 */
53 final String packagesPath = "$appPath/packages";
54
55 /**
56 * The type for callbacks that will be fired during [runPub]. Takes the sandbox
57 * directory as a parameter.
58 */
59 typedef Future _ScheduledEvent(Directory parentDir);
60
61 /**
62 * The list of events that are scheduled to run after the sandbox directory has
63 * been created but before Pub is run.
64 */
65 List<_ScheduledEvent> _scheduledBeforePub;
66
67 /**
68 * The list of events that are scheduled to run after Pub has been run.
69 */
70 List<_ScheduledEvent> _scheduledAfterPub;
71
72 void runPub([List<String> args, String output, int exitCode = 0]) {
73 var createdSandboxDir;
74
75 var asyncDone = expectAsync0(() {});
76
77 deleteSandboxIfCreated(onDeleted()) {
78 _scheduledBeforePub = null;
79 _scheduledAfterPub = null;
80 if (createdSandboxDir != null) {
81 deleteDir(createdSandboxDir).then((_) => onDeleted());
82 } else {
83 onDeleted();
47 } 84 }
85 }
48 86
49 final future = _setUpSandbox().chain((sandboxDir) { 87 String pathInSandbox(path) => join(getFullPath(createdSandboxDir), path);
50 createdSandboxDir = sandboxDir;
51 return _setUpApp(sandboxDir, app);
52 }).chain((appDir) {
53 createdAppDir = appDir;
54 return _setUpSdk(createdSandboxDir, sdk);
55 }).chain((sdkDir) {
56 createdSdkDir = sdkDir;
57 return _setUpCache(createdSandboxDir, cache);
58 }).chain((cacheDir) {
59 var workingDir;
60 if (createdAppDir != null) workingDir = createdAppDir.path;
61 88
62 if (cacheDir != null) { 89 final future = _setUpSandbox().chain((sandboxDir) {
63 // TODO(rnystrom): Hack in the cache directory path. Should pass this 90 createdSandboxDir = sandboxDir;
64 // in using environment var once #752 is done. 91 return _runScheduled(sandboxDir, _scheduledBeforePub);
65 args.add('--cachedir=${getFullPath(cacheDir)}'); 92 }).chain((_) {
66 } 93 return ensureDir(pathInSandbox(appPath));
94 }).chain((_) {
95 // TODO(rnystrom): Hack in the cache directory path. Should pass this
96 // in using environment var once #752 is done.
97 args.add('--cachedir=${pathInSandbox(cachePath)}');
67 98
68 if (createdSdkDir != null) { 99 // TODO(rnystrom): Hack in the SDK path. Should pass this in using
69 // TODO(rnystrom): Hack in the SDK path. Should pass this in using 100 // environment var once #752 is done.
70 // environment var once #752 is done. 101 args.add('--sdkdir=${pathInSandbox(sdkPath)}');
71 args.add('--sdkdir=${getFullPath(createdSdkDir)}');
72 }
73 102
74 return _runPub(args, workingDir); 103 return _runPub(args, pathInSandbox(appPath));
75 }).chain((result) { 104 }).chain((result) {
76 _validateOutput(output, result.stdout); 105 _validateOutput(output, result.stdout);
77 106
78 Expect.equals(result.stderr.length, 0, 107 Expect.equals(result.stderr.length, 0,
79 'Did not expect any output on stderr, and got:\n' + 108 'Did not expect any output on stderr, and got:\n' +
80 Strings.join(result.stderr, '\n')); 109 Strings.join(result.stderr, '\n'));
81 110
82 Expect.equals(result.exitCode, exitCode, 111 Expect.equals(result.exitCode, exitCode,
83 'Pub returned exit code ${result.exitCode}, expected $exitCode.'); 112 'Pub returned exit code ${result.exitCode}, expected $exitCode.');
84 113
85 return _validateExpectedPackages(createdAppDir, expectedPackageDir); 114 return _runScheduled(createdSandboxDir, _scheduledAfterPub);
115 });
116
117 future.then((_) {
118 deleteSandboxIfCreated(asyncDone);
119 });
120
121 future.handleException((error) {
122 // If an error occurs during testing, delete the sandbox, throw the error so
123 // that the test framework sees it, then finally call asyncDone so that the
124 // test framework knows we're done doing asynchronous stuff.
125 deleteSandboxIfCreated(() {
126 guardAsync(() { throw error; }, asyncDone);
86 }); 127 });
87 128 return true;
88 future.then((error) {
89 // Null means there were no errors.
90 if (error != null) Expect.fail(error);
91
92 deleteSandboxIfCreated();
93 });
94
95 future.handleException((error) {
96 deleteSandboxIfCreated();
97 // If we encounter an error, we want to pass it to the test framework. In
98 // order to get the stack trace information, we need to re-throw and
99 // re-catch it.
100 try {
101 throw error;
102 } catch (var e, var stack) {
103 reportTestError('$e', '$stack');
104 }
105 return true;
106 });
107 }); 129 });
108 } 130 }
109 131
110 Future<Directory> _setUpSandbox() { 132 Future<Directory> _setUpSandbox() {
111 return createTempDir('pub-test-sandbox-'); 133 return createTempDir('pub-test-sandbox-');
112 } 134 }
113 135
114 Future _setUpCache(Directory sandboxDir, List<Descriptor> cache) { 136 _runScheduled(Directory parentDir, List<_ScheduledEvent> scheduled) {
115 // No cache. 137 if (scheduled == null) return new Future.immediate(null);
116 if (cache == null) return new Future.immediate(null); 138 var future = Futures.wait(scheduled.map((event) => event(parentDir)));
117 139 scheduled.clear();
118 return dir('pub-cache', cache).create(sandboxDir); 140 return future;
119 }
120
121 Future _setUpApp(Directory sandboxDir, Descriptor app) {
122 // No app directory.
123 if (app == null) return new Future.immediate(null);
124
125 return app.create(sandboxDir);
126 }
127
128 Future _setUpSdk(Directory sandboxDir, List<Descriptor> sdk) {
129 // No SDK directory.
130 if (sdk == null) return new Future.immediate(null);
131
132 return dir('sdk', sdk).create(sandboxDir);
133 } 141 }
134 142
135 Future<ProcessResult> _runPub(List<String> pubArgs, String workingDir) { 143 Future<ProcessResult> _runPub(List<String> pubArgs, String workingDir) {
136 // Find a dart executable we can use to run pub. Uses the one that the 144 // Find a dart executable we can use to run pub. Uses the one that the
137 // test infrastructure uses. 145 // test infrastructure uses.
138 final scriptDir = new File(new Options().script).directorySync().path; 146 final scriptDir = new File(new Options().script).directorySync().path;
139 final platform = Platform.operatingSystem; 147 final platform = Platform.operatingSystem;
140 final dartBin = join(scriptDir, '../../../tools/testing/bin/$platform/dart'); 148 final dartBin = join(scriptDir, '../../../tools/testing/bin/$platform/dart');
141 149
142 // Find the main pub entrypoint. 150 // Find the main pub entrypoint.
143 final pubPath = fs.joinPaths(scriptDir, '../../pub/pub.dart'); 151 final pubPath = fs.joinPaths(scriptDir, '../../pub/pub.dart');
144 152
145 final args = ['--enable-type-checks', '--enable-asserts', pubPath]; 153 final args = ['--enable-type-checks', '--enable-asserts', pubPath];
146 args.addAll(pubArgs); 154 args.addAll(pubArgs);
147 155
148 return runProcess(dartBin, args, workingDir); 156 return runProcess(dartBin, args, workingDir);
149 } 157 }
150 158
151 /** 159 /**
152 * Validates the contents of the "packages" directory inside [appDir] against
153 * [expectedPackageDir].
154 */
155 Future<String> _validateExpectedPackages(Directory appDir,
156 List<Descriptor> expectedPackageDir) {
157 // No expectation.
158 if (expectedPackageDir == null) return new Future.immediate(null);
159
160 return dir('packages', expectedPackageDir).validate(appDir.path);
161 }
162
163 /**
164 * Compares the [actual] output from running pub with [expectedText]. Ignores 160 * Compares the [actual] output from running pub with [expectedText]. Ignores
165 * leading and trailing whitespace differences and tries to report the 161 * leading and trailing whitespace differences and tries to report the
166 * offending difference in a nice way. 162 * offending difference in a nice way.
167 */ 163 */
168 void _validateOutput(String expectedText, List<String> actual) { 164 void _validateOutput(String expectedText, List<String> actual) {
169 final expected = expectedText.split('\n'); 165 final expected = expectedText.split('\n');
170 166
171 // 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
172 // 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
173 // to expect zero lines of output, not a single empty line. 169 // to expect zero lines of output, not a single empty line.
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 215
220 /** 216 /**
221 * Creates the file or directory within [dir]. Returns a [Future] that is 217 * Creates the file or directory within [dir]. Returns a [Future] that is
222 * completed after the creation is done. 218 * completed after the creation is done.
223 */ 219 */
224 abstract Future create(dir); 220 abstract Future create(dir);
225 221
226 /** 222 /**
227 * Validates that this descriptor correctly matches the corresponding file 223 * Validates that this descriptor correctly matches the corresponding file
228 * 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
229 * the entry is valid, or a message describing the error if it failed. 225 * the entry is valid, or throws an error if it failed.
230 */ 226 */
231 abstract Future<String> validate(String dir); 227 abstract Future validate(String dir);
228
229 /**
230 * Schedules the directory to be created before Pub is run with [runPub]. The
231 * directory will be created relative to the sandbox directory.
232 */
233 void scheduleCreate() => _scheduleBeforePub(create);
234
235 /**
236 * Schedules the directory to be validated after Pub is run with [runPub]. The
237 * directory will be validated relative to the sandbox directory.
238 */
239 void scheduleValidate() =>
240 _scheduleAfterPub((parentDir) => validate(parentDir));
232 } 241 }
233 242
234 /** 243 /**
235 * Describes a file. These are used both for setting up an expected directory 244 * Describes a file. These are used both for setting up an expected directory
236 * tree before running a test, and for validating that the file system matches 245 * tree before running a test, and for validating that the file system matches
237 * some expectations after running it. 246 * some expectations after running it.
238 */ 247 */
239 class FileDescriptor extends Descriptor { 248 class FileDescriptor extends Descriptor {
240 /** 249 /**
241 * The text contents of the file. 250 * The text contents of the file.
242 */ 251 */
243 final String contents; 252 final String contents;
244 253
245 FileDescriptor(String name, this.contents) : super(name); 254 FileDescriptor(String name, this.contents) : super(name);
246 255
247 /** 256 /**
248 * Creates the file within [dir]. Returns a [Future] that is completed after 257 * Creates the file within [dir]. Returns a [Future] that is completed after
249 * the creation is done. 258 * the creation is done.
250 */ 259 */
251 Future<File> create(dir) { 260 Future<File> create(dir) {
252 return writeTextFile(join(dir, name), contents); 261 return writeTextFile(join(dir, name), contents);
253 } 262 }
254 263
255 /** 264 /**
256 * Validates that this file correctly matches the actual file at [path]. 265 * Validates that this file correctly matches the actual file at [path].
257 */ 266 */
258 Future<String> validate(String path) { 267 Future validate(String path) {
259 path = join(path, name); 268 path = join(path, name);
260 return fileExists(path).chain((exists) { 269 return fileExists(path).chain((exists) {
261 if (!exists) { 270 if (!exists) Expect.fail('Expected file $path does not exist.');
262 return new Future.immediate('Expected file $path does not exist.');
263 }
264 271
265 return readTextFile(path).transform((text) { 272 return readTextFile(path).transform((text) {
266 if (text == contents) return null; 273 if (text == contents) return null;
267 274
268 return 'File $path should contain:\n\n$contents\n\n' 275 Expect.fail('File $path should contain:\n\n$contents\n\n'
269 'but contained:\n\n$text'; 276 'but contained:\n\n$text');
270 }); 277 });
271 }); 278 });
272 } 279 }
273 } 280 }
274 281
275 /** 282 /**
276 * Describes a directory and its contents. These are used both for setting up 283 * Describes a directory and its contents. These are used both for setting up
277 * an expected directory tree before running a test, and for validating that 284 * an expected directory tree before running a test, and for validating that
278 * the file system matches some expectations after running it. 285 * the file system matches some expectations after running it.
279 */ 286 */
(...skipping 28 matching lines...) Expand all
308 315
309 return completer.future; 316 return completer.future;
310 } 317 }
311 318
312 /** 319 /**
313 * Validates that the directory at [path] contains all of the expected 320 * Validates that the directory at [path] contains all of the expected
314 * contents in this descriptor. Note that this does *not* check that the 321 * contents in this descriptor. Note that this does *not* check that the
315 * directory doesn't contain other unexpected stuff, just that it *does* 322 * directory doesn't contain other unexpected stuff, just that it *does*
316 * contain the stuff we do expect. 323 * contain the stuff we do expect.
317 */ 324 */
318 Future<String> validate(String path) { 325 Future validate(String path) {
319 // Validate each of the items in this directory. 326 // Validate each of the items in this directory.
320 final entryFutures = contents.map( 327 final entryFutures = contents.map(
321 (entry) => entry.validate(join(path, name))); 328 (entry) => entry.validate(join(path, name)));
322 329
323 // If they are all valid, the directory is valid. 330 // If they are all valid, the directory is valid.
324 return Futures.wait(entryFutures).transform((entries) { 331 return Futures.wait(entryFutures).transform((entries) => null);
325 for (final entry in entries) {
326 if (entry != null) return entry;
327 }
328
329 // If we got here, all of the sub-entries were valid.
330 return null;
331 });
332 } 332 }
333 } 333 }
334
335 /**
336 * Schedules a callback to be called before Pub is run with [runPub].
337 */
338 void _scheduleBeforePub(_ScheduledEvent event) {
339 if (_scheduledBeforePub == null) _scheduledBeforePub = [];
340 _scheduledBeforePub.add(event);
341 }
342
343 /**
344 * Schedules a callback to be called after Pub is run with [runPub].
345 */
346 void _scheduleAfterPub(_ScheduledEvent event) {
347 if (_scheduledAfterPub == null) _scheduledAfterPub = [];
348 _scheduledAfterPub.add(event);
349 }
OLDNEW
« no previous file with comments | « utils/tests/pub/pub_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698