1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Working with processes.
13 #![stable(feature = "process", since = "1.0.0")]
14 #![allow(non_upper_case_globals)]
21 use io
::{self, Error, ErrorKind}
;
23 use sys
::pipe
::{self, AnonPipe}
;
24 use sys
::process
as imp
;
25 use sys_common
::{AsInner, AsInnerMut, FromInner, IntoInner}
;
26 use thread
::{self, JoinHandle}
;
28 /// Representation of a running or exited child process.
30 /// This structure is used to represent and manage child processes. A child
31 /// process is created via the `Command` struct, which configures the spawning
32 /// process and can itself be constructed using a builder-style interface.
37 /// use std::process::Command;
39 /// let mut child = Command::new("/bin/cat")
42 /// .unwrap_or_else(|e| { panic!("failed to execute child: {}", e) });
44 /// let ecode = child.wait()
45 /// .unwrap_or_else(|e| { panic!("failed to wait on child: {}", e) });
47 /// assert!(ecode.success());
49 #[stable(feature = "process", since = "1.0.0")]
53 /// None until wait() or wait_with_output() is called.
54 status
: Option
<imp
::ExitStatus
>,
56 /// The handle for writing to the child's stdin, if it has been captured
57 #[stable(feature = "process", since = "1.0.0")]
58 pub stdin
: Option
<ChildStdin
>,
60 /// The handle for reading from the child's stdout, if it has been captured
61 #[stable(feature = "process", since = "1.0.0")]
62 pub stdout
: Option
<ChildStdout
>,
64 /// The handle for reading from the child's stderr, if it has been captured
65 #[stable(feature = "process", since = "1.0.0")]
66 pub stderr
: Option
<ChildStderr
>,
69 impl AsInner
<imp
::Process
> for Child
{
70 fn as_inner(&self) -> &imp
::Process { &self.handle }
73 impl IntoInner
<imp
::Process
> for Child
{
74 fn into_inner(self) -> imp
::Process { self.handle }
77 /// A handle to a child process's stdin
78 #[stable(feature = "process", since = "1.0.0")]
79 pub struct ChildStdin
{
83 #[stable(feature = "process", since = "1.0.0")]
84 impl Write
for ChildStdin
{
85 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> {
89 fn flush(&mut self) -> io
::Result
<()> {
94 impl AsInner
<AnonPipe
> for ChildStdin
{
95 fn as_inner(&self) -> &AnonPipe { &self.inner }
98 impl IntoInner
<AnonPipe
> for ChildStdin
{
99 fn into_inner(self) -> AnonPipe { self.inner }
102 /// A handle to a child process's stdout
103 #[stable(feature = "process", since = "1.0.0")]
104 pub struct ChildStdout
{
108 #[stable(feature = "process", since = "1.0.0")]
109 impl Read
for ChildStdout
{
110 fn read(&mut self, buf
: &mut [u8]) -> io
::Result
<usize> {
115 impl AsInner
<AnonPipe
> for ChildStdout
{
116 fn as_inner(&self) -> &AnonPipe { &self.inner }
119 impl IntoInner
<AnonPipe
> for ChildStdout
{
120 fn into_inner(self) -> AnonPipe { self.inner }
123 /// A handle to a child process's stderr
124 #[stable(feature = "process", since = "1.0.0")]
125 pub struct ChildStderr
{
129 #[stable(feature = "process", since = "1.0.0")]
130 impl Read
for ChildStderr
{
131 fn read(&mut self, buf
: &mut [u8]) -> io
::Result
<usize> {
136 impl AsInner
<AnonPipe
> for ChildStderr
{
137 fn as_inner(&self) -> &AnonPipe { &self.inner }
140 impl IntoInner
<AnonPipe
> for ChildStderr
{
141 fn into_inner(self) -> AnonPipe { self.inner }
144 /// The `Command` type acts as a process builder, providing fine-grained control
145 /// over how a new process should be spawned. A default configuration can be
146 /// generated using `Command::new(program)`, where `program` gives a path to the
147 /// program to be executed. Additional builder methods allow the configuration
148 /// to be changed (for example, by adding arguments) prior to spawning:
151 /// use std::process::Command;
153 /// let output = Command::new("sh")
155 /// .arg("echo hello")
157 /// .unwrap_or_else(|e| { panic!("failed to execute process: {}", e) });
158 /// let hello = output.stdout;
160 #[stable(feature = "process", since = "1.0.0")]
164 // Details explained in the builder methods
165 stdin
: Option
<Stdio
>,
166 stdout
: Option
<Stdio
>,
167 stderr
: Option
<Stdio
>,
171 /// Constructs a new `Command` for launching the program at
172 /// path `program`, with the following default configuration:
174 /// * No arguments to the program
175 /// * Inherit the current process's environment
176 /// * Inherit the current process's working directory
177 /// * Inherit stdin/stdout/stderr for `spawn` or `status`, but create pipes for `output`
179 /// Builder methods are provided to change these defaults and
180 /// otherwise configure the process.
181 #[stable(feature = "process", since = "1.0.0")]
182 pub fn new
<S
: AsRef
<OsStr
>>(program
: S
) -> Command
{
184 inner
: imp
::Command
::new(program
.as_ref()),
191 /// Add an argument to pass to the program.
192 #[stable(feature = "process", since = "1.0.0")]
193 pub fn arg
<S
: AsRef
<OsStr
>>(&mut self, arg
: S
) -> &mut Command
{
194 self.inner
.arg(arg
.as_ref());
198 /// Add multiple arguments to pass to the program.
199 #[stable(feature = "process", since = "1.0.0")]
200 pub fn args
<S
: AsRef
<OsStr
>>(&mut self, args
: &[S
]) -> &mut Command
{
201 self.inner
.args(args
.iter().map(AsRef
::as_ref
));
205 /// Inserts or updates an environment variable mapping.
207 /// Note that environment variable names are case-insensitive (but case-preserving) on Windows,
208 /// and case-sensitive on all other platforms.
209 #[stable(feature = "process", since = "1.0.0")]
210 pub fn env
<K
, V
>(&mut self, key
: K
, val
: V
) -> &mut Command
211 where K
: AsRef
<OsStr
>, V
: AsRef
<OsStr
>
213 self.inner
.env(key
.as_ref(), val
.as_ref());
217 /// Removes an environment variable mapping.
218 #[stable(feature = "process", since = "1.0.0")]
219 pub fn env_remove
<K
: AsRef
<OsStr
>>(&mut self, key
: K
) -> &mut Command
{
220 self.inner
.env_remove(key
.as_ref());
224 /// Clears the entire environment map for the child process.
225 #[stable(feature = "process", since = "1.0.0")]
226 pub fn env_clear(&mut self) -> &mut Command
{
227 self.inner
.env_clear();
231 /// Sets the working directory for the child process.
232 #[stable(feature = "process", since = "1.0.0")]
233 pub fn current_dir
<P
: AsRef
<path
::Path
>>(&mut self, dir
: P
) -> &mut Command
{
234 self.inner
.cwd(dir
.as_ref().as_ref());
238 /// Configuration for the child process's stdin handle (file descriptor 0).
239 #[stable(feature = "process", since = "1.0.0")]
240 pub fn stdin(&mut self, cfg
: Stdio
) -> &mut Command
{
241 self.stdin
= Some(cfg
);
245 /// Configuration for the child process's stdout handle (file descriptor 1).
246 #[stable(feature = "process", since = "1.0.0")]
247 pub fn stdout(&mut self, cfg
: Stdio
) -> &mut Command
{
248 self.stdout
= Some(cfg
);
252 /// Configuration for the child process's stderr handle (file descriptor 2).
253 #[stable(feature = "process", since = "1.0.0")]
254 pub fn stderr(&mut self, cfg
: Stdio
) -> &mut Command
{
255 self.stderr
= Some(cfg
);
259 fn spawn_inner(&self, default_io
: StdioImp
) -> io
::Result
<Child
> {
260 let default_io
= Stdio(default_io
);
262 // See comment on `setup_io` for what `_drop_later` is.
263 let (their_stdin
, our_stdin
, _drop_later
) = try
!(
264 setup_io(self.stdin
.as_ref().unwrap_or(&default_io
), true)
266 let (their_stdout
, our_stdout
, _drop_later
) = try
!(
267 setup_io(self.stdout
.as_ref().unwrap_or(&default_io
), false)
269 let (their_stderr
, our_stderr
, _drop_later
) = try
!(
270 setup_io(self.stderr
.as_ref().unwrap_or(&default_io
), false)
273 match imp
::Process
::spawn(&self.inner
, their_stdin
, their_stdout
,
276 Ok(handle
) => Ok(Child
{
279 stdin
: our_stdin
.map(|fd
| ChildStdin { inner: fd }
),
280 stdout
: our_stdout
.map(|fd
| ChildStdout { inner: fd }
),
281 stderr
: our_stderr
.map(|fd
| ChildStderr { inner: fd }
),
286 /// Executes the command as a child process, returning a handle to it.
288 /// By default, stdin, stdout and stderr are inherited from the parent.
289 #[stable(feature = "process", since = "1.0.0")]
290 pub fn spawn(&mut self) -> io
::Result
<Child
> {
291 self.spawn_inner(StdioImp
::Inherit
)
294 /// Executes the command as a child process, waiting for it to finish and
295 /// collecting all of its output.
297 /// By default, stdin, stdout and stderr are captured (and used to
298 /// provide the resulting output).
303 /// use std::process::Command;
304 /// let output = Command::new("cat").arg("foo.txt").output().unwrap_or_else(|e| {
305 /// panic!("failed to execute process: {}", e)
308 /// println!("status: {}", output.status);
309 /// println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
310 /// println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
312 #[stable(feature = "process", since = "1.0.0")]
313 pub fn output(&mut self) -> io
::Result
<Output
> {
314 self.spawn_inner(StdioImp
::MakePipe
).and_then(|p
| p
.wait_with_output())
317 /// Executes a command as a child process, waiting for it to finish and
318 /// collecting its exit status.
320 /// By default, stdin, stdout and stderr are inherited from the parent.
325 /// use std::process::Command;
327 /// let status = Command::new("ls").status().unwrap_or_else(|e| {
328 /// panic!("failed to execute process: {}", e)
331 /// println!("process exited with: {}", status);
333 #[stable(feature = "process", since = "1.0.0")]
334 pub fn status(&mut self) -> io
::Result
<ExitStatus
> {
335 self.spawn().and_then(|mut p
| p
.wait())
339 #[stable(feature = "rust1", since = "1.0.0")]
340 impl fmt
::Debug
for Command
{
341 /// Format the program and arguments of a Command for display. Any
342 /// non-utf8 data is lossily converted using the utf8 replacement
344 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
345 try
!(write
!(f
, "{:?}", self.inner
.program
));
346 for arg
in &self.inner
.args
{
347 try
!(write
!(f
, " {:?}", arg
));
353 impl AsInner
<imp
::Command
> for Command
{
354 fn as_inner(&self) -> &imp
::Command { &self.inner }
357 impl AsInnerMut
<imp
::Command
> for Command
{
358 fn as_inner_mut(&mut self) -> &mut imp
::Command { &mut self.inner }
361 // Takes a `Stdio` configuration (this module) and whether the to-be-owned
362 // handle will be readable.
364 // Returns a triple of (stdio to spawn with, stdio to store, stdio to drop). The
365 // stdio to spawn with is passed down to the `sys` module and indicates how the
366 // stdio stream should be set up. The "stdio to store" is an object which
367 // should be returned in the `Child` that makes its way out. The "stdio to drop"
368 // represents the raw value of "stdio to spawn with", but is the owned variant
369 // for it. This needs to be dropped after the child spawns
370 fn setup_io(io
: &Stdio
, readable
: bool
)
371 -> io
::Result
<(imp
::Stdio
, Option
<AnonPipe
>, Option
<AnonPipe
>)>
374 StdioImp
::MakePipe
=> {
375 let (reader
, writer
) = try
!(pipe
::anon_pipe());
377 (imp
::Stdio
::Raw(reader
.raw()), Some(writer
), Some(reader
))
379 (imp
::Stdio
::Raw(writer
.raw()), Some(reader
), Some(writer
))
382 StdioImp
::Raw(ref owned
) => (imp
::Stdio
::Raw(owned
.raw()), None
, None
),
383 StdioImp
::Inherit
=> (imp
::Stdio
::Inherit
, None
, None
),
384 StdioImp
::None
=> (imp
::Stdio
::None
, None
, None
),
388 /// The output of a finished process.
389 #[derive(PartialEq, Eq, Clone)]
390 #[stable(feature = "process", since = "1.0.0")]
392 /// The status (exit code) of the process.
393 #[stable(feature = "process", since = "1.0.0")]
394 pub status
: ExitStatus
,
395 /// The data that the process wrote to stdout.
396 #[stable(feature = "process", since = "1.0.0")]
398 /// The data that the process wrote to stderr.
399 #[stable(feature = "process", since = "1.0.0")]
403 /// Describes what to do with a standard I/O stream for a child process.
404 #[stable(feature = "process", since = "1.0.0")]
405 pub struct Stdio(StdioImp
);
407 // The internal enum for stdio setup; see below for descriptions.
416 /// A new pipe should be arranged to connect the parent and child processes.
417 #[stable(feature = "process", since = "1.0.0")]
418 pub fn piped() -> Stdio { Stdio(StdioImp::MakePipe) }
420 /// The child inherits from the corresponding parent descriptor.
421 #[stable(feature = "process", since = "1.0.0")]
422 pub fn inherit() -> Stdio { Stdio(StdioImp::Inherit) }
424 /// This stream will be ignored. This is the equivalent of attaching the
425 /// stream to `/dev/null`
426 #[stable(feature = "process", since = "1.0.0")]
427 pub fn null() -> Stdio { Stdio(StdioImp::None) }
430 impl FromInner
<imp
::RawStdio
> for Stdio
{
431 fn from_inner(inner
: imp
::RawStdio
) -> Stdio
{
432 Stdio(StdioImp
::Raw(inner
))
436 /// Describes the result of a process after it has terminated.
437 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
438 #[stable(feature = "process", since = "1.0.0")]
439 pub struct ExitStatus(imp
::ExitStatus
);
442 /// Was termination successful? Signal termination not considered a success,
443 /// and success is defined as a zero exit status.
444 #[stable(feature = "process", since = "1.0.0")]
445 pub fn success(&self) -> bool
{
449 /// Returns the exit code of the process, if any.
451 /// On Unix, this will return `None` if the process was terminated
452 /// by a signal; `std::os::unix` provides an extension trait for
453 /// extracting the signal and other details from the `ExitStatus`.
454 #[stable(feature = "process", since = "1.0.0")]
455 pub fn code(&self) -> Option
<i32> {
460 impl AsInner
<imp
::ExitStatus
> for ExitStatus
{
461 fn as_inner(&self) -> &imp
::ExitStatus { &self.0 }
464 #[stable(feature = "process", since = "1.0.0")]
465 impl fmt
::Display
for ExitStatus
{
466 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
472 /// Forces the child to exit. This is equivalent to sending a
473 /// SIGKILL on unix platforms.
474 #[stable(feature = "process", since = "1.0.0")]
475 pub fn kill(&mut self) -> io
::Result
<()> {
476 #[cfg(unix)] fn collect_status(p: &mut Child) {
477 // On Linux (and possibly other unices), a process that has exited will
478 // continue to accept signals because it is "defunct". The delivery of
479 // signals will only fail once the child has been reaped. For this
480 // reason, if the process hasn't exited yet, then we attempt to collect
481 // their status with WNOHANG.
482 if p
.status
.is_none() {
483 match p
.handle
.try_wait() {
484 Some(status
) => { p.status = Some(status); }
489 #[cfg(windows)] fn collect_status(_p: &mut Child) {}
491 collect_status(self);
493 // if the process has finished, and therefore had waitpid called,
494 // and we kill it, then on unix we might ending up killing a
495 // newer process that happens to have the same (re-used) id
496 if self.status
.is_some() {
497 return Err(Error
::new(
498 ErrorKind
::InvalidInput
,
499 "invalid argument: can't kill an exited process",
503 unsafe { self.handle.kill() }
506 /// Returns the OS-assigned process identifier associated with this child.
507 #[stable(feature = "process_id", since = "1.3.0")]
508 pub fn id(&self) -> u32 {
512 /// Waits for the child to exit completely, returning the status that it
513 /// exited with. This function will continue to have the same return value
514 /// after it has been called at least once.
516 /// The stdin handle to the child process, if any, will be closed
517 /// before waiting. This helps avoid deadlock: it ensures that the
518 /// child does not block waiting for input from the parent, while
519 /// the parent waits for the child to exit.
520 #[stable(feature = "process", since = "1.0.0")]
521 pub fn wait(&mut self) -> io
::Result
<ExitStatus
> {
522 drop(self.stdin
.take());
524 Some(code
) => Ok(ExitStatus(code
)),
526 let status
= try
!(self.handle
.wait());
527 self.status
= Some(status
);
528 Ok(ExitStatus(status
))
533 /// Simultaneously waits for the child to exit and collect all remaining
534 /// output on the stdout/stderr handles, returning an `Output`
537 /// The stdin handle to the child process, if any, will be closed
538 /// before waiting. This helps avoid deadlock: it ensures that the
539 /// child does not block waiting for input from the parent, while
540 /// the parent waits for the child to exit.
541 #[stable(feature = "process", since = "1.0.0")]
542 pub fn wait_with_output(mut self) -> io
::Result
<Output
> {
543 drop(self.stdin
.take());
544 fn read
<R
>(mut input
: R
) -> JoinHandle
<io
::Result
<Vec
<u8>>>
545 where R
: Read
+ Send
+ '
static
547 thread
::spawn(move || {
548 let mut ret
= Vec
::new();
549 input
.read_to_end(&mut ret
).map(|_
| ret
)
552 let stdout
= self.stdout
.take().map(read
);
553 let stderr
= self.stderr
.take().map(read
);
554 let status
= try
!(self.wait());
555 let stdout
= stdout
.and_then(|t
| t
.join().unwrap().ok());
556 let stderr
= stderr
.and_then(|t
| t
.join().unwrap().ok());
560 stdout
: stdout
.unwrap_or(Vec
::new()),
561 stderr
: stderr
.unwrap_or(Vec
::new()),
566 /// Terminates the current process with the specified exit code.
568 /// This function will never return and will immediately terminate the current
569 /// process. The exit code is passed through to the underlying OS and will be
570 /// available for consumption by another process.
572 /// Note that because this function never returns, and that it terminates the
573 /// process, no destructors on the current stack or any other thread's stack
574 /// will be run. If a clean shutdown is needed it is recommended to only call
575 /// this function at a known point where there are no more destructors left
577 #[stable(feature = "rust1", since = "1.0.0")]
578 pub fn exit(code
: i32) -> ! {
579 ::sys_common
::cleanup();
580 ::sys
::os
::exit(code
)
590 use super::{Command, Output, Stdio}
;
592 // FIXME(#10380) these tests should not all be ignored on android.
594 #[cfg(not(target_os="android"))]
597 let p
= Command
::new("true").spawn();
599 let mut p
= p
.unwrap();
600 assert
!(p
.wait().unwrap().success());
603 #[cfg(not(target_os="android"))]
606 match Command
::new("if-this-is-a-binary-then-the-world-has-ended").spawn() {
612 #[cfg(not(target_os="android"))]
614 fn exit_reported_right() {
615 let p
= Command
::new("false").spawn();
617 let mut p
= p
.unwrap();
618 assert
!(p
.wait().unwrap().code() == Some(1));
622 #[cfg(all(unix, not(target_os="android")))]
624 fn signal_reported_right() {
625 use os
::unix
::process
::ExitStatusExt
;
627 let mut p
= Command
::new("/bin/sh")
628 .arg("-c").arg("read a")
629 .stdin(Stdio
::piped())
632 match p
.wait().unwrap().signal() {
634 result
=> panic
!("not terminated by signal 9 (instead, {:?})",
639 pub fn run_output(mut cmd
: Command
) -> String
{
642 let mut p
= p
.unwrap();
643 assert
!(p
.stdout
.is_some());
644 let mut ret
= String
::new();
645 p
.stdout
.as_mut().unwrap().read_to_string(&mut ret
).unwrap();
646 assert
!(p
.wait().unwrap().success());
650 #[cfg(not(target_os="android"))]
653 let mut cmd
= Command
::new("echo");
654 cmd
.arg("foobar").stdout(Stdio
::piped());
655 assert_eq
!(run_output(cmd
), "foobar\n");
658 #[cfg(all(unix, not(target_os="android")))]
660 fn set_current_dir_works() {
661 let mut cmd
= Command
::new("/bin/sh");
662 cmd
.arg("-c").arg("pwd")
664 .stdout(Stdio
::piped());
665 assert_eq
!(run_output(cmd
), "/\n");
668 #[cfg(all(unix, not(target_os="android")))]
671 let mut p
= Command
::new("/bin/sh")
672 .arg("-c").arg("read line; echo $line")
673 .stdin(Stdio
::piped())
674 .stdout(Stdio
::piped())
676 p
.stdin
.as_mut().unwrap().write("foobar".as_bytes()).unwrap();
677 drop(p
.stdin
.take());
678 let mut out
= String
::new();
679 p
.stdout
.as_mut().unwrap().read_to_string(&mut out
).unwrap();
680 assert
!(p
.wait().unwrap().success());
681 assert_eq
!(out
, "foobar\n");
685 #[cfg(all(unix, not(target_os="android")))]
688 use os
::unix
::prelude
::*;
690 let mut p
= Command
::new("/bin/sh")
691 .arg("-c").arg("true")
692 .uid(unsafe { libc::getuid() }
)
693 .gid(unsafe { libc::getgid() }
)
695 assert
!(p
.wait().unwrap().success());
698 #[cfg(all(unix, not(target_os="android")))]
700 fn uid_to_root_fails() {
701 use os
::unix
::prelude
::*;
704 // if we're already root, this isn't a valid test. Most of the bots run
705 // as non-root though (android is an exception).
706 if unsafe { libc::getuid() == 0 } { return }
707 assert
!(Command
::new("/bin/ls").uid(0).gid(0).spawn().is_err());
710 #[cfg(not(target_os="android"))]
712 fn test_process_status() {
713 let mut status
= Command
::new("false").status().unwrap();
714 assert
!(status
.code() == Some(1));
716 status
= Command
::new("true").status().unwrap();
717 assert
!(status
.success());
721 fn test_process_output_fail_to_start() {
722 match Command
::new("/no-binary-by-this-name-should-exist").output() {
723 Err(e
) => assert_eq
!(e
.kind(), ErrorKind
::NotFound
),
728 #[cfg(not(target_os="android"))]
730 fn test_process_output_output() {
731 let Output {status, stdout, stderr}
732 = Command
::new("echo").arg("hello").output().unwrap();
733 let output_str
= str::from_utf8(&stdout
).unwrap();
735 assert
!(status
.success());
736 assert_eq
!(output_str
.trim().to_string(), "hello");
737 assert_eq
!(stderr
, Vec
::new());
740 #[cfg(not(target_os="android"))]
742 fn test_process_output_error() {
743 let Output {status, stdout, stderr}
744 = Command
::new("mkdir").arg(".").output().unwrap();
746 assert
!(status
.code() == Some(1));
747 assert_eq
!(stdout
, Vec
::new());
748 assert
!(!stderr
.is_empty());
751 #[cfg(not(target_os="android"))]
753 fn test_finish_once() {
754 let mut prog
= Command
::new("false").spawn().unwrap();
755 assert
!(prog
.wait().unwrap().code() == Some(1));
758 #[cfg(not(target_os="android"))]
760 fn test_finish_twice() {
761 let mut prog
= Command
::new("false").spawn().unwrap();
762 assert
!(prog
.wait().unwrap().code() == Some(1));
763 assert
!(prog
.wait().unwrap().code() == Some(1));
766 #[cfg(not(target_os="android"))]
768 fn test_wait_with_output_once() {
769 let prog
= Command
::new("echo").arg("hello").stdout(Stdio
::piped())
771 let Output {status, stdout, stderr}
= prog
.wait_with_output().unwrap();
772 let output_str
= str::from_utf8(&stdout
).unwrap();
774 assert
!(status
.success());
775 assert_eq
!(output_str
.trim().to_string(), "hello");
776 assert_eq
!(stderr
, Vec
::new());
779 #[cfg(all(unix, not(target_os="android")))]
780 pub fn env_cmd() -> Command
{
783 #[cfg(target_os="android")]
784 pub fn env_cmd() -> Command
{
785 let mut cmd
= Command
::new("/system/bin/sh");
786 cmd
.arg("-c").arg("set");
791 pub fn env_cmd() -> Command
{
792 let mut cmd
= Command
::new("cmd");
793 cmd
.arg("/c").arg("set");
797 #[cfg(not(target_os="android"))]
799 fn test_inherit_env() {
802 let result
= env_cmd().output().unwrap();
803 let output
= String
::from_utf8(result
.stdout
).unwrap();
805 for (ref k
, ref v
) in env
::vars() {
806 // Windows has hidden environment variables whose names start with
807 // equals signs (`=`). Those do not show up in the output of the
809 assert
!((cfg
!(windows
) && k
.starts_with("=")) ||
810 k
.starts_with("DYLD") ||
811 output
.contains(&format
!("{}={}", *k
, *v
)),
812 "output doesn't contain `{}={}`\n{}",
816 #[cfg(target_os="android")]
818 fn test_inherit_env() {
821 let mut result
= env_cmd().output().unwrap();
822 let output
= String
::from_utf8(result
.stdout
).unwrap();
824 for (ref k
, ref v
) in env
::vars() {
825 // don't check android RANDOM variables
826 if *k
!= "RANDOM".to_string() {
827 assert
!(output
.contains(&format
!("{}={}",
830 output
.contains(&format
!("{}=\'{}\'",
838 fn test_override_env() {
841 // In some build environments (such as chrooted Nix builds), `env` can
842 // only be found in the explicitly-provided PATH env variable, not in
843 // default places such as /bin or /usr/bin. So we need to pass through
844 // PATH to our sub-process.
845 let mut cmd
= env_cmd();
846 cmd
.env_clear().env("RUN_TEST_NEW_ENV", "123");
847 if let Some(p
) = env
::var_os("PATH") {
850 let result
= cmd
.output().unwrap();
851 let output
= String
::from_utf8_lossy(&result
.stdout
).to_string();
853 assert
!(output
.contains("RUN_TEST_NEW_ENV=123"),
854 "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output
);
858 fn test_add_to_env() {
859 let result
= env_cmd().env("RUN_TEST_NEW_ENV", "123").output().unwrap();
860 let output
= String
::from_utf8_lossy(&result
.stdout
).to_string();
862 assert
!(output
.contains("RUN_TEST_NEW_ENV=123"),
863 "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output
);