| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 test.test.io; | 5 library test.test.io; |
| 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 p; | 10 import 'package:path/path.dart' as p; |
| 11 import 'package:test/src/util/io.dart'; | 11 import 'package:test/src/util/io.dart'; |
| 12 | 12 |
| 13 /// The path to the root directory of the `test` package. | 13 /// The path to the root directory of the `test` package. |
| 14 final String packageDir = p.dirname(p.dirname(libraryPath(#test.test.io))); | 14 final String packageDir = p.dirname(p.dirname(libraryPath(#test.test.io))); |
| 15 | 15 |
| 16 /// The path to the `pub` executable in the current Dart SDK. | 16 /// The path to the `pub` executable in the current Dart SDK. |
| 17 final _pubPath = p.absolute(p.join( | 17 final _pubPath = p.absolute(p.join( |
| 18 p.dirname(Platform.executable), | 18 p.dirname(Platform.executable), |
| 19 Platform.isWindows ? 'pub.bat' : 'pub')); | 19 Platform.isWindows ? 'pub.bat' : 'pub')); |
| 20 | 20 |
| 21 /// The platform-specific message emitted when a nonexistent file is loaded. |
| 22 final String noSuchFileMessage = Platform.isWindows |
| 23 ? "The system cannot find the file specified." |
| 24 : "No such file or directory"; |
| 25 |
| 21 /// Runs the test executable with the package root set properly. | 26 /// Runs the test executable with the package root set properly. |
| 22 ProcessResult runTest(List<String> args, {String workingDirectory, | 27 ProcessResult runTest(List<String> args, {String workingDirectory, |
| 23 Map<String, String> environment}) { | 28 Map<String, String> environment}) { |
| 24 var allArgs = [ | 29 var allArgs = [ |
| 25 p.absolute(p.join(packageDir, 'bin/test.dart')), | 30 p.absolute(p.join(packageDir, 'bin/test.dart')), |
| 26 "--package-root=${p.join(packageDir, 'packages')}" | 31 "--package-root=${p.join(packageDir, 'packages')}" |
| 27 ]..addAll(args); | 32 ]..addAll(args); |
| 28 | 33 |
| 29 if (environment == null) environment = {}; | 34 if (environment == null) environment = {}; |
| 30 environment.putIfAbsent("_UNITTEST_USE_COLOR", () => "false"); | 35 environment.putIfAbsent("_UNITTEST_USE_COLOR", () => "false"); |
| 31 | 36 |
| 32 // TODO(nweiz): Use ScheduledProcess once it's compatible. | 37 // TODO(nweiz): Use ScheduledProcess once it's compatible. |
| 33 return runDart(allArgs, workingDirectory: workingDirectory, | 38 return runDart(allArgs, workingDirectory: workingDirectory, |
| 34 environment: environment); | 39 environment: environment); |
| 35 } | 40 } |
| 36 | 41 |
| 37 /// Runs Dart. | 42 /// Runs Dart. |
| 38 ProcessResult runDart(List<String> args, {String workingDirectory, | 43 ProcessResult runDart(List<String> args, {String workingDirectory, |
| 39 Map<String, String> environment}) { | 44 Map<String, String> environment}) { |
| 40 var allArgs = Platform.executableArguments.map((arg) { | 45 var allArgs = Platform.executableArguments.map((arg) { |
| 41 // The package root might be relative, so we need to make it absolute if | 46 // The package root might be relative, so we need to make it absolute if |
| 42 // we're going to run in a different working directory. | 47 // we're going to run in a different working directory. |
| 43 if (!arg.startsWith("--package-root=")) return arg; | 48 if (!arg.startsWith("--package-root=")) return arg; |
| 44 return "--package-root=" + | 49 return "--package-root=" + |
| 45 p.absolute(arg.substring("--package-root=".length)); | 50 p.absolute(arg.substring("--package-root=".length)); |
| 46 }).toList()..addAll(args); | 51 }).toList()..addAll(args); |
| 47 | 52 |
| 48 // TODO(nweiz): Use ScheduledProcess once it's compatible. | 53 // TODO(nweiz): Use ScheduledProcess once it's compatible. |
| 49 return Process.runSync(p.absolute(Platform.executable), allArgs, | 54 return new _NormalizedProcessResult(Process.runSync( |
| 50 workingDirectory: workingDirectory, environment: environment); | 55 p.absolute(Platform.executable), allArgs, |
| 56 workingDirectory: workingDirectory, environment: environment)); |
| 51 } | 57 } |
| 52 | 58 |
| 53 /// Runs Pub. | 59 /// Runs Pub. |
| 54 ProcessResult runPub(List<String> args, {String workingDirectory, | 60 ProcessResult runPub(List<String> args, {String workingDirectory, |
| 55 Map<String, String> environment}) { | 61 Map<String, String> environment}) { |
| 56 // TODO(nweiz): Use ScheduledProcess once it's compatible. | 62 // TODO(nweiz): Use ScheduledProcess once it's compatible. |
| 57 return Process.runSync(_pubPath, args, | 63 return new _NormalizedProcessResult(Process.runSync( |
| 58 workingDirectory: workingDirectory, environment: environment); | 64 _pubPath, args, |
| 65 workingDirectory: workingDirectory, environment: environment)); |
| 59 } | 66 } |
| 60 | 67 |
| 61 /// Starts the test executable with the package root set properly. | 68 /// Starts the test executable with the package root set properly. |
| 62 Future<Process> startTest(List<String> args, {String workingDirectory, | 69 Future<Process> startTest(List<String> args, {String workingDirectory, |
| 63 Map<String, String> environment}) { | 70 Map<String, String> environment}) { |
| 64 var allArgs = [ | 71 var allArgs = [ |
| 65 p.absolute(p.join(packageDir, 'bin/test.dart')), | 72 p.absolute(p.join(packageDir, 'bin/test.dart')), |
| 66 "--package-root=${p.join(packageDir, 'packages')}" | 73 "--package-root=${p.join(packageDir, 'packages')}" |
| 67 ]..addAll(args); | 74 ]..addAll(args); |
| 68 | 75 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 83 workingDirectory: workingDirectory, environment: environment); | 90 workingDirectory: workingDirectory, environment: environment); |
| 84 } | 91 } |
| 85 | 92 |
| 86 /// Starts Pub. | 93 /// Starts Pub. |
| 87 Future<Process> startPub(List<String> args, {String workingDirectory, | 94 Future<Process> startPub(List<String> args, {String workingDirectory, |
| 88 Map<String, String> environment}) { | 95 Map<String, String> environment}) { |
| 89 // TODO(nweiz): Use ScheduledProcess once it's compatible. | 96 // TODO(nweiz): Use ScheduledProcess once it's compatible. |
| 90 return Process.start(_pubPath, args, | 97 return Process.start(_pubPath, args, |
| 91 workingDirectory: workingDirectory, environment: environment); | 98 workingDirectory: workingDirectory, environment: environment); |
| 92 } | 99 } |
| 100 |
| 101 /// A wrapper around [ProcessResult] that normalizes the newline format across |
| 102 /// operating systems. |
| 103 class _NormalizedProcessResult implements ProcessResult { |
| 104 final ProcessResult _inner; |
| 105 |
| 106 int get exitCode => _inner.exitCode; |
| 107 int get pid => _inner.pid; |
| 108 |
| 109 final String stdout; |
| 110 final String stderr; |
| 111 |
| 112 _NormalizedProcessResult(ProcessResult inner) |
| 113 : _inner = inner, |
| 114 stdout = Platform.isWindows |
| 115 ? inner.stdout.replaceAll("\r\n", "\n") |
| 116 : inner.stdout, |
| 117 stderr = Platform.isWindows |
| 118 ? inner.stderr.replaceAll("\r\n", "\n") |
| 119 : inner.stderr; |
| 120 } |
| OLD | NEW |