| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library scheduled_process_test; | 5 library scheduled_process_test; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:path/path.dart' as path; | 10 import 'package:path/path.dart' as path; |
| 11 import 'package:scheduled_test/scheduled_process.dart'; | 11 import 'package:scheduled_test/scheduled_process.dart'; |
| 12 import 'package:scheduled_test/scheduled_stream.dart'; |
| 12 import 'package:scheduled_test/scheduled_test.dart'; | 13 import 'package:scheduled_test/scheduled_test.dart'; |
| 13 | 14 |
| 14 import 'metatest.dart'; | 15 import 'metatest.dart'; |
| 15 import 'utils.dart'; | 16 import 'utils.dart'; |
| 16 | 17 |
| 17 void main(_, message) { | 18 void main(_, message) { |
| 18 initMetatest(message); | 19 initMetatest(message); |
| 19 | 20 |
| 20 setUpTimeout(); | 21 setUpTimeout(); |
| 21 | 22 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 | 122 |
| 122 expectTestsPass("a process that ends while waiting for stdout shouldn't " | 123 expectTestsPass("a process that ends while waiting for stdout shouldn't " |
| 123 "block the test", () { | 124 "block the test", () { |
| 124 var errors; | 125 var errors; |
| 125 test('test 1', () { | 126 test('test 1', () { |
| 126 currentSchedule.onException.schedule(() { | 127 currentSchedule.onException.schedule(() { |
| 127 errors = currentSchedule.errors; | 128 errors = currentSchedule.errors; |
| 128 }); | 129 }); |
| 129 | 130 |
| 130 var process = startDartProcess(''); | 131 var process = startDartProcess(''); |
| 131 expect(process.nextLine(), completion(equals('hello'))); | 132 process.stdout.expect('hello'); |
| 132 expect(process.nextLine(), completion(equals('world'))); | 133 process.stdout.expect('world'); |
| 133 process.shouldExit(0); | 134 process.shouldExit(0); |
| 134 }); | 135 }); |
| 135 | 136 |
| 136 test('test 2', () { | 137 test('test 2', () { |
| 137 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | 138 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); |
| 138 expect(errors.length, anyOf(1, 2)); | 139 expect(errors.length, anyOf(1, 2)); |
| 139 expect(errors[0].error, isStateError); | 140 expect(errors[0].error, new isInstanceOf<TestFailure>()); |
| 140 expect(errors[0].error.message, equals("No elements")); | 141 expect(errors[0].error.message, equals( |
| 142 "Expected: 'hello'\n" |
| 143 " Emitted: \n" |
| 144 " Which: unexpected end of stream")); |
| 141 | 145 |
| 142 // Whether or not this error appears depends on how quickly the "no | 146 // Whether or not this error appears depends on how quickly the "no |
| 143 // elements" error is handled. | 147 // elements" error is handled. |
| 144 if (errors.length == 2) { | 148 if (errors.length == 2) { |
| 145 expect(errors[1].error.toString(), matches(r"^Process " | 149 expect(errors[1].error.toString(), matches(r"^Process " |
| 146 r"'[^']+[\\/]dart(\.exe)? [^']+' ended earlier than scheduled with " | 150 r"'[^']+[\\/]dart(\.exe)? [^']+' ended earlier than scheduled with " |
| 147 r"exit code 0\.")); | 151 r"exit code 0\.")); |
| 148 } | 152 } |
| 149 }); | 153 }); |
| 150 }, passing: ['test 2']); | 154 }, passing: ['test 2']); |
| 151 | 155 |
| 152 expectTestsPass("a process that ends during the task immediately before it's " | 156 expectTestsPass("a process that ends during the task immediately before it's " |
| 153 "scheduled to end shouldn't cause an error", () { | 157 "scheduled to end shouldn't cause an error", () { |
| 154 test('test', () { | 158 test('test', () { |
| 155 var process = startDartProcess('stdin.toList();'); | 159 var process = startDartProcess('stdin.toList();'); |
| 156 process.closeStdin(); | 160 process.closeStdin(); |
| 157 // Unfortunately, sleeping for a second seems like the best way of | 161 // Unfortunately, sleeping for a second seems like the best way of |
| 158 // guaranteeing that the process ends during this task. | 162 // guaranteeing that the process ends during this task. |
| 159 schedule(() => new Future.delayed(new Duration(seconds: 1))); | 163 schedule(() => new Future.delayed(new Duration(seconds: 1))); |
| 160 process.shouldExit(0); | 164 process.shouldExit(0); |
| 161 }); | 165 }); |
| 162 }); | 166 }); |
| 163 | 167 |
| 164 expectTestsPass("nextLine returns the next line of stdout from the process", | 168 expectTestsPass("stdout exposes the standard output from the process", () { |
| 165 () { | |
| 166 test('test', () { | 169 test('test', () { |
| 167 var process = startDartProcess(r'print("hello\n\nworld"); print("hi");'); | 170 var process = startDartProcess(r'print("hello\n\nworld"); print("hi");'); |
| 168 expect(process.nextLine(), completion(equals('hello'))); | 171 process.stdout.expect('hello'); |
| 169 expect(process.nextLine(), completion(equals(''))); | 172 process.stdout.expect(''); |
| 170 expect(process.nextLine(), completion(equals('world'))); | 173 process.stdout.expect('world'); |
| 171 expect(process.nextLine(), completion(equals('hi'))); | 174 process.stdout.expect('hi'); |
| 175 process.stdout.expect(isDone); |
| 172 process.shouldExit(0); | 176 process.shouldExit(0); |
| 173 }); | 177 }); |
| 174 }); | 178 }); |
| 175 | 179 |
| 176 expectTestsPass("nextLine throws an error if there's no more stdout", () { | 180 expectTestsPass("stderr exposes the stderr from the process", () { |
| 177 var errors; | |
| 178 test('test 1', () { | |
| 179 currentSchedule.onException.schedule(() { | |
| 180 errors = currentSchedule.errors; | |
| 181 }); | |
| 182 | |
| 183 var process = startDartProcess('print("hello");'); | |
| 184 expect(process.nextLine(), completion(equals('hello'))); | |
| 185 expect(process.nextLine(), completion(equals('world'))); | |
| 186 process.shouldExit(0); | |
| 187 }); | |
| 188 | |
| 189 test('test 2', () { | |
| 190 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 191 expect(errors.length, anyOf(1, 2)); | |
| 192 expect(errors[0].error, isStateError); | |
| 193 expect(errors[0].error.message, equals("No elements")); | |
| 194 | |
| 195 // Whether or not this error appears depends on how quickly the "no | |
| 196 // elements" error is handled. | |
| 197 if (errors.length == 2) { | |
| 198 expect(errors[1].error.toString(), matches(r"^Process " | |
| 199 r"'[^']+[\\/]dart(\.exe)? [^']+' ended earlier than scheduled with " | |
| 200 r"exit code 0\.")); | |
| 201 } | |
| 202 }); | |
| 203 }, passing: ['test 2']); | |
| 204 | |
| 205 expectTestsPass("nextErrLine returns the next line of stderr from the " | |
| 206 "process", () { | |
| 207 test('test', () { | 181 test('test', () { |
| 208 var process = startDartProcess(r''' | 182 var process = startDartProcess(r''' |
| 209 stderr.write("hello\n\nworld\n"); | 183 stderr.write("hello\n\nworld\n"); |
| 210 stderr.write("hi"); | 184 stderr.write("hi"); |
| 211 '''); | 185 '''); |
| 212 expect(process.nextErrLine(), completion(equals('hello'))); | 186 process.stderr.expect('hello'); |
| 213 expect(process.nextErrLine(), completion(equals(''))); | 187 process.stderr.expect(''); |
| 214 expect(process.nextErrLine(), completion(equals('world'))); | 188 process.stderr.expect('world'); |
| 215 expect(process.nextErrLine(), completion(equals('hi'))); | 189 process.stderr.expect('hi'); |
| 190 process.stderr.expect(isDone); |
| 216 process.shouldExit(0); | 191 process.shouldExit(0); |
| 217 }); | 192 }); |
| 218 }); | 193 }); |
| 219 | |
| 220 expectTestsPass("nextErrLine throws an error if there's no more stderr", () { | |
| 221 var errors; | |
| 222 test('test 1', () { | |
| 223 currentSchedule.onException.schedule(() { | |
| 224 errors = currentSchedule.errors; | |
| 225 }); | |
| 226 | |
| 227 var process = startDartProcess(r'stderr.write("hello\n");'); | |
| 228 expect(process.nextErrLine(), completion(equals('hello'))); | |
| 229 expect(process.nextErrLine(), completion(equals('world'))); | |
| 230 process.shouldExit(0); | |
| 231 }); | |
| 232 | |
| 233 test('test 2', () { | |
| 234 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 235 expect(errors.length, anyOf(1, 2)); | |
| 236 expect(errors[0].error, isStateError); | |
| 237 expect(errors[0].error.message, equals("No elements")); | |
| 238 | |
| 239 // Whether or not this error appears depends on how quickly the "no | |
| 240 // elements" error is handled. | |
| 241 if (errors.length == 2) { | |
| 242 expect(errors[1].error.toString(), matches(r"^Process " | |
| 243 r"'[^']+[\\/]dart(\.exe)? [^']+' ended earlier than scheduled with " | |
| 244 r"exit code 0\.")); | |
| 245 } | |
| 246 }); | |
| 247 }, passing: ['test 2']); | |
| 248 | |
| 249 expectTestsPass("remainingStdout returns all the stdout if it's not consumed " | |
| 250 "any other way", () { | |
| 251 test('test', () { | |
| 252 var process = startDartProcess(r'print("hello\n\nworld"); print("hi");'); | |
| 253 process.shouldExit(0); | |
| 254 expect(process.remainingStdout(), | |
| 255 completion(equals("hello\n\nworld\nhi"))); | |
| 256 }); | |
| 257 }); | |
| 258 | |
| 259 expectTestsPass("remainingStdout returns the empty string if there's no " | |
| 260 "stdout", () { | |
| 261 test('test', () { | |
| 262 var process = startDartProcess(r''); | |
| 263 process.shouldExit(0); | |
| 264 expect(process.remainingStdout(), completion(isEmpty)); | |
| 265 }); | |
| 266 }); | |
| 267 | |
| 268 expectTestsPass("remainingStdout returns the remaining stdout after the " | |
| 269 "lines consumed by nextLine", () { | |
| 270 test('test', () { | |
| 271 var process = startDartProcess(r'print("hello\n\nworld"); print("hi");'); | |
| 272 expect(process.nextLine(), completion(equals("hello"))); | |
| 273 expect(process.nextLine(), completion(equals(""))); | |
| 274 process.shouldExit(0); | |
| 275 expect(process.remainingStdout(), completion(equals("world\nhi"))); | |
| 276 }); | |
| 277 }); | |
| 278 | |
| 279 expectTestsPass("remainingStdout can't be called before the process is " | |
| 280 "scheduled to end", () { | |
| 281 test('test', () { | |
| 282 var process = startDartProcess(r''); | |
| 283 expect(process.remainingStdout, throwsA(isStateError)); | |
| 284 process.shouldExit(0); | |
| 285 }); | |
| 286 }); | |
| 287 | |
| 288 expectTestsPass("remainingStderr returns all the stderr if it's not consumed " | |
| 289 "any other way", () { | |
| 290 test('test', () { | |
| 291 var process = startDartProcess(r''' | |
| 292 stderr.write("hello\n\nworld\n"); | |
| 293 stderr.write("hi\n"); | |
| 294 '''); | |
| 295 process.shouldExit(0); | |
| 296 expect(process.remainingStderr(), | |
| 297 completion(equals("hello\n\nworld\nhi"))); | |
| 298 }); | |
| 299 }); | |
| 300 | |
| 301 expectTestsPass("remainingStderr returns the empty string if there's no " | |
| 302 "stderr", () { | |
| 303 test('test', () { | |
| 304 var process = startDartProcess(r''); | |
| 305 process.shouldExit(0); | |
| 306 expect(process.remainingStderr(), completion(isEmpty)); | |
| 307 }); | |
| 308 }); | |
| 309 | |
| 310 expectTestsPass("remainingStderr returns the remaining stderr after the " | |
| 311 "lines consumed by nextLine", () { | |
| 312 test('test', () { | |
| 313 var process = startDartProcess(r''' | |
| 314 stderr.write("hello\n\nworld\n"); | |
| 315 stderr.write("hi\n"); | |
| 316 '''); | |
| 317 expect(process.nextErrLine(), completion(equals("hello"))); | |
| 318 expect(process.nextErrLine(), completion(equals(""))); | |
| 319 process.shouldExit(0); | |
| 320 expect(process.remainingStderr(), completion(equals("world\nhi"))); | |
| 321 }); | |
| 322 }); | |
| 323 | |
| 324 expectTestsPass("remainingStderr can't be called before the process is " | |
| 325 "scheduled to end", () { | |
| 326 test('test', () { | |
| 327 var process = startDartProcess(r''); | |
| 328 expect(process.remainingStderr, throwsA(isStateError)); | |
| 329 process.shouldExit(0); | |
| 330 }); | |
| 331 }); | |
| 332 | 194 |
| 333 expectTestsPass("writeLine schedules a line to be written to the process", | 195 expectTestsPass("writeLine schedules a line to be written to the process", |
| 334 () { | 196 () { |
| 335 test('test', () { | 197 test('test', () { |
| 336 var process = startDartProcess(r''' | 198 var process = startDartProcess(r''' |
| 337 stdinLines.listen((line) => print("> $line")); | 199 stdinLines.listen((line) => print("> $line")); |
| 338 '''); | 200 '''); |
| 339 process.writeLine("hello"); | 201 process.writeLine("hello"); |
| 340 expect(process.nextLine(), completion(equals("> hello"))); | 202 process.stdout.expect("> hello"); |
| 341 process.writeLine("world"); | 203 process.writeLine("world"); |
| 342 expect(process.nextLine(), completion(equals("> world"))); | 204 process.stdout.expect("> world"); |
| 343 process.kill(); | 205 process.kill(); |
| 344 }); | 206 }); |
| 345 }); | 207 }); |
| 346 | 208 |
| 347 expectTestsPass("closeStdin closes the process's stdin stream", () { | 209 expectTestsPass("closeStdin closes the process's stdin stream", () { |
| 348 test('test', () { | 210 test('test', () { |
| 349 var process = startDartProcess(r''' | 211 var process = startDartProcess(r''' |
| 350 stdin.listen((line) => print("> $line"), | 212 stdin.listen((line) => print("> $line"), |
| 351 onDone: () => print("stdin closed")); | 213 onDone: () => print("stdin closed")); |
| 352 '''); | 214 '''); |
| 353 process.closeStdin(); | 215 process.closeStdin(); |
| 354 process.shouldExit(0); | 216 process.shouldExit(0); |
| 355 expect(process.nextLine(), completion(equals('stdin closed'))); | 217 process.stdout.expect('stdin closed'); |
| 356 }); | 218 }); |
| 357 }); | 219 }); |
| 358 } | 220 } |
| 359 | 221 |
| 360 ScheduledProcess startDartProcess(String script) { | 222 ScheduledProcess startDartProcess(String script) { |
| 361 var tempDir = schedule(() => Directory.systemTemp | 223 var tempDir = schedule(() => Directory.systemTemp |
| 362 .createTemp('scheduled_process_test_') | 224 .createTemp('scheduled_process_test_') |
| 363 .then((dir) => dir.path), | 225 .then((dir) => dir.path), |
| 364 'create temp dir'); | 226 'create temp dir'); |
| 365 var dartPath = schedule(() { | 227 var dartPath = schedule(() { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 383 currentSchedule.onComplete.schedule(() { | 245 currentSchedule.onComplete.schedule(() { |
| 384 return tempDir.catchError((_) => null).then((dir) { | 246 return tempDir.catchError((_) => null).then((dir) { |
| 385 if (dir == null) return null; | 247 if (dir == null) return null; |
| 386 return new Directory(dir).delete(recursive: true); | 248 return new Directory(dir).delete(recursive: true); |
| 387 }); | 249 }); |
| 388 }, 'clean up temp dir'); | 250 }, 'clean up temp dir'); |
| 389 | 251 |
| 390 return new ScheduledProcess.start(Platform.executable, | 252 return new ScheduledProcess.start(Platform.executable, |
| 391 ['--checked', dartPath]); | 253 ['--checked', dartPath]); |
| 392 } | 254 } |
| OLD | NEW |