]> git.lizzy.rs Git - rust.git/blob - src/libstd/process.rs
CStr::from_bytes
[rust.git] / src / libstd / process.rs
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.
4 //
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.
10
11 //! Working with processes.
12
13 #![stable(feature = "process", since = "1.0.0")]
14
15 use prelude::v1::*;
16 use io::prelude::*;
17
18 use ffi::OsStr;
19 use fmt;
20 use io;
21 use path::Path;
22 use str;
23 use sys::pipe::AnonPipe;
24 use sys::process as imp;
25 use sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
26 use thread::{self, JoinHandle};
27
28 /// Representation of a running or exited child process.
29 ///
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.
33 ///
34 /// # Examples
35 ///
36 /// ```should_panic
37 /// use std::process::Command;
38 ///
39 /// let mut child = Command::new("/bin/cat")
40 ///                         .arg("file.txt")
41 ///                         .spawn()
42 ///                         .unwrap_or_else(|e| { panic!("failed to execute child: {}", e) });
43 ///
44 /// let ecode = child.wait()
45 ///                  .unwrap_or_else(|e| { panic!("failed to wait on child: {}", e) });
46 ///
47 /// assert!(ecode.success());
48 /// ```
49 ///
50 /// # Note
51 ///
52 /// Take note that there is no implementation of
53 /// [`Drop`](../../core/ops/trait.Drop.html) for child processes, so if you
54 /// do not ensure the `Child` has exited then it will continue to run, even
55 /// after the `Child` handle to the child process has gone out of scope.
56 ///
57 /// Calling `wait` (or other functions that wrap around it) will make the
58 /// parent process wait until the child has actually exited before continuing.
59 #[stable(feature = "process", since = "1.0.0")]
60 pub struct Child {
61     handle: imp::Process,
62
63     /// The handle for writing to the child's stdin, if it has been captured
64     #[stable(feature = "process", since = "1.0.0")]
65     pub stdin: Option<ChildStdin>,
66
67     /// The handle for reading from the child's stdout, if it has been captured
68     #[stable(feature = "process", since = "1.0.0")]
69     pub stdout: Option<ChildStdout>,
70
71     /// The handle for reading from the child's stderr, if it has been captured
72     #[stable(feature = "process", since = "1.0.0")]
73     pub stderr: Option<ChildStderr>,
74 }
75
76 impl AsInner<imp::Process> for Child {
77     fn as_inner(&self) -> &imp::Process { &self.handle }
78 }
79
80 impl FromInner<(imp::Process, imp::StdioPipes)> for Child {
81     fn from_inner((handle, io): (imp::Process, imp::StdioPipes)) -> Child {
82         Child {
83             handle: handle,
84             stdin: io.stdin.map(ChildStdin::from_inner),
85             stdout: io.stdout.map(ChildStdout::from_inner),
86             stderr: io.stderr.map(ChildStderr::from_inner),
87         }
88     }
89 }
90
91 impl IntoInner<imp::Process> for Child {
92     fn into_inner(self) -> imp::Process { self.handle }
93 }
94
95 /// A handle to a child process's stdin
96 #[stable(feature = "process", since = "1.0.0")]
97 pub struct ChildStdin {
98     inner: AnonPipe
99 }
100
101 #[stable(feature = "process", since = "1.0.0")]
102 impl Write for ChildStdin {
103     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
104         self.inner.write(buf)
105     }
106
107     fn flush(&mut self) -> io::Result<()> {
108         Ok(())
109     }
110 }
111
112 impl AsInner<AnonPipe> for ChildStdin {
113     fn as_inner(&self) -> &AnonPipe { &self.inner }
114 }
115
116 impl IntoInner<AnonPipe> for ChildStdin {
117     fn into_inner(self) -> AnonPipe { self.inner }
118 }
119
120 impl FromInner<AnonPipe> for ChildStdin {
121     fn from_inner(pipe: AnonPipe) -> ChildStdin {
122         ChildStdin { inner: pipe }
123     }
124 }
125
126 /// A handle to a child process's stdout
127 #[stable(feature = "process", since = "1.0.0")]
128 pub struct ChildStdout {
129     inner: AnonPipe
130 }
131
132 #[stable(feature = "process", since = "1.0.0")]
133 impl Read for ChildStdout {
134     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
135         self.inner.read(buf)
136     }
137 }
138
139 impl AsInner<AnonPipe> for ChildStdout {
140     fn as_inner(&self) -> &AnonPipe { &self.inner }
141 }
142
143 impl IntoInner<AnonPipe> for ChildStdout {
144     fn into_inner(self) -> AnonPipe { self.inner }
145 }
146
147 impl FromInner<AnonPipe> for ChildStdout {
148     fn from_inner(pipe: AnonPipe) -> ChildStdout {
149         ChildStdout { inner: pipe }
150     }
151 }
152
153 /// A handle to a child process's stderr
154 #[stable(feature = "process", since = "1.0.0")]
155 pub struct ChildStderr {
156     inner: AnonPipe
157 }
158
159 #[stable(feature = "process", since = "1.0.0")]
160 impl Read for ChildStderr {
161     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
162         self.inner.read(buf)
163     }
164 }
165
166 impl AsInner<AnonPipe> for ChildStderr {
167     fn as_inner(&self) -> &AnonPipe { &self.inner }
168 }
169
170 impl IntoInner<AnonPipe> for ChildStderr {
171     fn into_inner(self) -> AnonPipe { self.inner }
172 }
173
174 impl FromInner<AnonPipe> for ChildStderr {
175     fn from_inner(pipe: AnonPipe) -> ChildStderr {
176         ChildStderr { inner: pipe }
177     }
178 }
179
180 /// The `Command` type acts as a process builder, providing fine-grained control
181 /// over how a new process should be spawned. A default configuration can be
182 /// generated using `Command::new(program)`, where `program` gives a path to the
183 /// program to be executed. Additional builder methods allow the configuration
184 /// to be changed (for example, by adding arguments) prior to spawning:
185 ///
186 /// ```
187 /// use std::process::Command;
188 ///
189 /// let output = Command::new("sh")
190 ///                      .arg("-c")
191 ///                      .arg("echo hello")
192 ///                      .output()
193 ///                      .unwrap_or_else(|e| { panic!("failed to execute process: {}", e) });
194 /// let hello = output.stdout;
195 /// ```
196 #[stable(feature = "process", since = "1.0.0")]
197 pub struct Command {
198     inner: imp::Command,
199 }
200
201 impl Command {
202     /// Constructs a new `Command` for launching the program at
203     /// path `program`, with the following default configuration:
204     ///
205     /// * No arguments to the program
206     /// * Inherit the current process's environment
207     /// * Inherit the current process's working directory
208     /// * Inherit stdin/stdout/stderr for `spawn` or `status`, but create pipes for `output`
209     ///
210     /// Builder methods are provided to change these defaults and
211     /// otherwise configure the process.
212     #[stable(feature = "process", since = "1.0.0")]
213     pub fn new<S: AsRef<OsStr>>(program: S) -> Command {
214         Command { inner: imp::Command::new(program.as_ref()) }
215     }
216
217     /// Add an argument to pass to the program.
218     #[stable(feature = "process", since = "1.0.0")]
219     pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command {
220         self.inner.arg(arg.as_ref());
221         self
222     }
223
224     /// Add multiple arguments to pass to the program.
225     #[stable(feature = "process", since = "1.0.0")]
226     pub fn args<S: AsRef<OsStr>>(&mut self, args: &[S]) -> &mut Command {
227         for arg in args {
228             self.arg(arg.as_ref());
229         }
230         self
231     }
232
233     /// Inserts or updates an environment variable mapping.
234     ///
235     /// Note that environment variable names are case-insensitive (but case-preserving) on Windows,
236     /// and case-sensitive on all other platforms.
237     #[stable(feature = "process", since = "1.0.0")]
238     pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command
239         where K: AsRef<OsStr>, V: AsRef<OsStr>
240     {
241         self.inner.env(key.as_ref(), val.as_ref());
242         self
243     }
244
245     /// Removes an environment variable mapping.
246     #[stable(feature = "process", since = "1.0.0")]
247     pub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Command {
248         self.inner.env_remove(key.as_ref());
249         self
250     }
251
252     /// Clears the entire environment map for the child process.
253     #[stable(feature = "process", since = "1.0.0")]
254     pub fn env_clear(&mut self) -> &mut Command {
255         self.inner.env_clear();
256         self
257     }
258
259     /// Sets the working directory for the child process.
260     #[stable(feature = "process", since = "1.0.0")]
261     pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Command {
262         self.inner.cwd(dir.as_ref().as_ref());
263         self
264     }
265
266     /// Configuration for the child process's stdin handle (file descriptor 0).
267     #[stable(feature = "process", since = "1.0.0")]
268     pub fn stdin(&mut self, cfg: Stdio) -> &mut Command {
269         self.inner.stdin(cfg.0);
270         self
271     }
272
273     /// Configuration for the child process's stdout handle (file descriptor 1).
274     #[stable(feature = "process", since = "1.0.0")]
275     pub fn stdout(&mut self, cfg: Stdio) -> &mut Command {
276         self.inner.stdout(cfg.0);
277         self
278     }
279
280     /// Configuration for the child process's stderr handle (file descriptor 2).
281     #[stable(feature = "process", since = "1.0.0")]
282     pub fn stderr(&mut self, cfg: Stdio) -> &mut Command {
283         self.inner.stderr(cfg.0);
284         self
285     }
286
287     /// Executes the command as a child process, returning a handle to it.
288     ///
289     /// By default, stdin, stdout and stderr are inherited from the parent.
290     #[stable(feature = "process", since = "1.0.0")]
291     pub fn spawn(&mut self) -> io::Result<Child> {
292         self.inner.spawn(imp::Stdio::Inherit).map(Child::from_inner)
293     }
294
295     /// Executes the command as a child process, waiting for it to finish and
296     /// collecting all of its output.
297     ///
298     /// By default, stdin, stdout and stderr are captured (and used to
299     /// provide the resulting output).
300     ///
301     /// # Examples
302     ///
303     /// ```
304     /// use std::process::Command;
305     /// let output = Command::new("cat").arg("foo.txt").output().unwrap_or_else(|e| {
306     ///     panic!("failed to execute process: {}", e)
307     /// });
308     ///
309     /// println!("status: {}", output.status);
310     /// println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
311     /// println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
312     /// ```
313     #[stable(feature = "process", since = "1.0.0")]
314     pub fn output(&mut self) -> io::Result<Output> {
315         self.inner.spawn(imp::Stdio::MakePipe).map(Child::from_inner)
316             .and_then(|p| p.wait_with_output())
317     }
318
319     /// Executes a command as a child process, waiting for it to finish and
320     /// collecting its exit status.
321     ///
322     /// By default, stdin, stdout and stderr are inherited from the parent.
323     ///
324     /// # Examples
325     ///
326     /// ```
327     /// use std::process::Command;
328     ///
329     /// let status = Command::new("ls").status().unwrap_or_else(|e| {
330     ///     panic!("failed to execute process: {}", e)
331     /// });
332     ///
333     /// println!("process exited with: {}", status);
334     /// ```
335     #[stable(feature = "process", since = "1.0.0")]
336     pub fn status(&mut self) -> io::Result<ExitStatus> {
337         self.spawn().and_then(|mut p| p.wait())
338     }
339 }
340
341 #[stable(feature = "rust1", since = "1.0.0")]
342 impl fmt::Debug for Command {
343     /// Format the program and arguments of a Command for display. Any
344     /// non-utf8 data is lossily converted using the utf8 replacement
345     /// character.
346     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
347         self.inner.fmt(f)
348     }
349 }
350
351 impl AsInner<imp::Command> for Command {
352     fn as_inner(&self) -> &imp::Command { &self.inner }
353 }
354
355 impl AsInnerMut<imp::Command> for Command {
356     fn as_inner_mut(&mut self) -> &mut imp::Command { &mut self.inner }
357 }
358
359 /// The output of a finished process.
360 #[derive(PartialEq, Eq, Clone)]
361 #[stable(feature = "process", since = "1.0.0")]
362 pub struct Output {
363     /// The status (exit code) of the process.
364     #[stable(feature = "process", since = "1.0.0")]
365     pub status: ExitStatus,
366     /// The data that the process wrote to stdout.
367     #[stable(feature = "process", since = "1.0.0")]
368     pub stdout: Vec<u8>,
369     /// The data that the process wrote to stderr.
370     #[stable(feature = "process", since = "1.0.0")]
371     pub stderr: Vec<u8>,
372 }
373
374 // If either stderr or stdout are valid utf8 strings it prints the valid
375 // strings, otherwise it prints the byte sequence instead
376 #[stable(feature = "process_output_debug", since = "1.7.0")]
377 impl fmt::Debug for Output {
378     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
379
380         let stdout_utf8 = str::from_utf8(&self.stdout);
381         let stdout_debug: &fmt::Debug = match stdout_utf8 {
382             Ok(ref str) => str,
383             Err(_) => &self.stdout
384         };
385
386         let stderr_utf8 = str::from_utf8(&self.stderr);
387         let stderr_debug: &fmt::Debug = match stderr_utf8 {
388             Ok(ref str) => str,
389             Err(_) => &self.stderr
390         };
391
392         fmt.debug_struct("Output")
393             .field("status", &self.status)
394             .field("stdout", stdout_debug)
395             .field("stderr", stderr_debug)
396             .finish()
397     }
398 }
399
400 /// Describes what to do with a standard I/O stream for a child process.
401 #[stable(feature = "process", since = "1.0.0")]
402 pub struct Stdio(imp::Stdio);
403
404 impl Stdio {
405     /// A new pipe should be arranged to connect the parent and child processes.
406     #[stable(feature = "process", since = "1.0.0")]
407     pub fn piped() -> Stdio { Stdio(imp::Stdio::MakePipe) }
408
409     /// The child inherits from the corresponding parent descriptor.
410     #[stable(feature = "process", since = "1.0.0")]
411     pub fn inherit() -> Stdio { Stdio(imp::Stdio::Inherit) }
412
413     /// This stream will be ignored. This is the equivalent of attaching the
414     /// stream to `/dev/null`
415     #[stable(feature = "process", since = "1.0.0")]
416     pub fn null() -> Stdio { Stdio(imp::Stdio::Null) }
417 }
418
419 impl FromInner<imp::Stdio> for Stdio {
420     fn from_inner(inner: imp::Stdio) -> Stdio {
421         Stdio(inner)
422     }
423 }
424
425 /// Describes the result of a process after it has terminated.
426 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
427 #[stable(feature = "process", since = "1.0.0")]
428 pub struct ExitStatus(imp::ExitStatus);
429
430 impl ExitStatus {
431     /// Was termination successful? Signal termination not considered a success,
432     /// and success is defined as a zero exit status.
433     #[stable(feature = "process", since = "1.0.0")]
434     pub fn success(&self) -> bool {
435         self.0.success()
436     }
437
438     /// Returns the exit code of the process, if any.
439     ///
440     /// On Unix, this will return `None` if the process was terminated
441     /// by a signal; `std::os::unix` provides an extension trait for
442     /// extracting the signal and other details from the `ExitStatus`.
443     #[stable(feature = "process", since = "1.0.0")]
444     pub fn code(&self) -> Option<i32> {
445         self.0.code()
446     }
447 }
448
449 impl AsInner<imp::ExitStatus> for ExitStatus {
450     fn as_inner(&self) -> &imp::ExitStatus { &self.0 }
451 }
452
453 #[stable(feature = "process", since = "1.0.0")]
454 impl fmt::Display for ExitStatus {
455     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
456         self.0.fmt(f)
457     }
458 }
459
460 impl Child {
461     /// Forces the child to exit. This is equivalent to sending a
462     /// SIGKILL on unix platforms.
463     #[stable(feature = "process", since = "1.0.0")]
464     pub fn kill(&mut self) -> io::Result<()> {
465         self.handle.kill()
466     }
467
468     /// Returns the OS-assigned process identifier associated with this child.
469     #[stable(feature = "process_id", since = "1.3.0")]
470     pub fn id(&self) -> u32 {
471         self.handle.id()
472     }
473
474     /// Waits for the child to exit completely, returning the status that it
475     /// exited with. This function will continue to have the same return value
476     /// after it has been called at least once.
477     ///
478     /// The stdin handle to the child process, if any, will be closed
479     /// before waiting. This helps avoid deadlock: it ensures that the
480     /// child does not block waiting for input from the parent, while
481     /// the parent waits for the child to exit.
482     #[stable(feature = "process", since = "1.0.0")]
483     pub fn wait(&mut self) -> io::Result<ExitStatus> {
484         drop(self.stdin.take());
485         self.handle.wait().map(ExitStatus)
486     }
487
488     /// Simultaneously waits for the child to exit and collect all remaining
489     /// output on the stdout/stderr handles, returning an `Output`
490     /// instance.
491     ///
492     /// The stdin handle to the child process, if any, will be closed
493     /// before waiting. This helps avoid deadlock: it ensures that the
494     /// child does not block waiting for input from the parent, while
495     /// the parent waits for the child to exit.
496     #[stable(feature = "process", since = "1.0.0")]
497     pub fn wait_with_output(mut self) -> io::Result<Output> {
498         drop(self.stdin.take());
499         fn read<R>(mut input: R) -> JoinHandle<io::Result<Vec<u8>>>
500             where R: Read + Send + 'static
501         {
502             thread::spawn(move || {
503                 let mut ret = Vec::new();
504                 input.read_to_end(&mut ret).map(|_| ret)
505             })
506         }
507         let stdout = self.stdout.take().map(read);
508         let stderr = self.stderr.take().map(read);
509         let status = try!(self.wait());
510         let stdout = stdout.and_then(|t| t.join().unwrap().ok());
511         let stderr = stderr.and_then(|t| t.join().unwrap().ok());
512
513         Ok(Output {
514             status: status,
515             stdout: stdout.unwrap_or(Vec::new()),
516             stderr: stderr.unwrap_or(Vec::new()),
517         })
518     }
519 }
520
521 /// Terminates the current process with the specified exit code.
522 ///
523 /// This function will never return and will immediately terminate the current
524 /// process. The exit code is passed through to the underlying OS and will be
525 /// available for consumption by another process.
526 ///
527 /// Note that because this function never returns, and that it terminates the
528 /// process, no destructors on the current stack or any other thread's stack
529 /// will be run. If a clean shutdown is needed it is recommended to only call
530 /// this function at a known point where there are no more destructors left
531 /// to run.
532 #[stable(feature = "rust1", since = "1.0.0")]
533 pub fn exit(code: i32) -> ! {
534     ::sys_common::cleanup();
535     ::sys::os::exit(code)
536 }
537
538 #[cfg(test)]
539 mod tests {
540     use prelude::v1::*;
541     use io::prelude::*;
542
543     use io::ErrorKind;
544     use str;
545     use super::{Command, Output, Stdio};
546
547     // FIXME(#10380) these tests should not all be ignored on android.
548
549     #[test]
550     #[cfg_attr(target_os = "android", ignore)]
551     fn smoke() {
552         let p = Command::new("true").spawn();
553         assert!(p.is_ok());
554         let mut p = p.unwrap();
555         assert!(p.wait().unwrap().success());
556     }
557
558     #[test]
559     #[cfg_attr(target_os = "android", ignore)]
560     fn smoke_failure() {
561         match Command::new("if-this-is-a-binary-then-the-world-has-ended").spawn() {
562             Ok(..) => panic!(),
563             Err(..) => {}
564         }
565     }
566
567     #[test]
568     #[cfg_attr(target_os = "android", ignore)]
569     fn exit_reported_right() {
570         let p = Command::new("false").spawn();
571         assert!(p.is_ok());
572         let mut p = p.unwrap();
573         assert!(p.wait().unwrap().code() == Some(1));
574         drop(p.wait());
575     }
576
577     #[test]
578     #[cfg(unix)]
579     #[cfg_attr(target_os = "android", ignore)]
580     fn signal_reported_right() {
581         use os::unix::process::ExitStatusExt;
582
583         let mut p = Command::new("/bin/sh")
584                             .arg("-c").arg("read a")
585                             .stdin(Stdio::piped())
586                             .spawn().unwrap();
587         p.kill().unwrap();
588         match p.wait().unwrap().signal() {
589             Some(9) => {},
590             result => panic!("not terminated by signal 9 (instead, {:?})",
591                              result),
592         }
593     }
594
595     pub fn run_output(mut cmd: Command) -> String {
596         let p = cmd.spawn();
597         assert!(p.is_ok());
598         let mut p = p.unwrap();
599         assert!(p.stdout.is_some());
600         let mut ret = String::new();
601         p.stdout.as_mut().unwrap().read_to_string(&mut ret).unwrap();
602         assert!(p.wait().unwrap().success());
603         return ret;
604     }
605
606     #[test]
607     #[cfg_attr(target_os = "android", ignore)]
608     fn stdout_works() {
609         let mut cmd = Command::new("echo");
610         cmd.arg("foobar").stdout(Stdio::piped());
611         assert_eq!(run_output(cmd), "foobar\n");
612     }
613
614     #[test]
615     #[cfg_attr(any(windows, target_os = "android"), ignore)]
616     fn set_current_dir_works() {
617         let mut cmd = Command::new("/bin/sh");
618         cmd.arg("-c").arg("pwd")
619            .current_dir("/")
620            .stdout(Stdio::piped());
621         assert_eq!(run_output(cmd), "/\n");
622     }
623
624     #[test]
625     #[cfg_attr(any(windows, target_os = "android"), ignore)]
626     fn stdin_works() {
627         let mut p = Command::new("/bin/sh")
628                             .arg("-c").arg("read line; echo $line")
629                             .stdin(Stdio::piped())
630                             .stdout(Stdio::piped())
631                             .spawn().unwrap();
632         p.stdin.as_mut().unwrap().write("foobar".as_bytes()).unwrap();
633         drop(p.stdin.take());
634         let mut out = String::new();
635         p.stdout.as_mut().unwrap().read_to_string(&mut out).unwrap();
636         assert!(p.wait().unwrap().success());
637         assert_eq!(out, "foobar\n");
638     }
639
640
641     #[test]
642     #[cfg_attr(target_os = "android", ignore)]
643     #[cfg(unix)]
644     fn uid_works() {
645         use os::unix::prelude::*;
646         use libc;
647         let mut p = Command::new("/bin/sh")
648                             .arg("-c").arg("true")
649                             .uid(unsafe { libc::getuid() })
650                             .gid(unsafe { libc::getgid() })
651                             .spawn().unwrap();
652         assert!(p.wait().unwrap().success());
653     }
654
655     #[test]
656     #[cfg_attr(target_os = "android", ignore)]
657     #[cfg(unix)]
658     fn uid_to_root_fails() {
659         use os::unix::prelude::*;
660         use libc;
661
662         // if we're already root, this isn't a valid test. Most of the bots run
663         // as non-root though (android is an exception).
664         if unsafe { libc::getuid() == 0 } { return }
665         assert!(Command::new("/bin/ls").uid(0).gid(0).spawn().is_err());
666     }
667
668     #[test]
669     #[cfg_attr(target_os = "android", ignore)]
670     fn test_process_status() {
671         let mut status = Command::new("false").status().unwrap();
672         assert!(status.code() == Some(1));
673
674         status = Command::new("true").status().unwrap();
675         assert!(status.success());
676     }
677
678     #[test]
679     fn test_process_output_fail_to_start() {
680         match Command::new("/no-binary-by-this-name-should-exist").output() {
681             Err(e) => assert_eq!(e.kind(), ErrorKind::NotFound),
682             Ok(..) => panic!()
683         }
684     }
685
686     #[test]
687     #[cfg_attr(target_os = "android", ignore)]
688     fn test_process_output_output() {
689         let Output {status, stdout, stderr}
690              = Command::new("echo").arg("hello").output().unwrap();
691         let output_str = str::from_utf8(&stdout).unwrap();
692
693         assert!(status.success());
694         assert_eq!(output_str.trim().to_string(), "hello");
695         assert_eq!(stderr, Vec::new());
696     }
697
698     #[test]
699     #[cfg_attr(target_os = "android", ignore)]
700     fn test_process_output_error() {
701         let Output {status, stdout, stderr}
702              = Command::new("mkdir").arg(".").output().unwrap();
703
704         assert!(status.code() == Some(1));
705         assert_eq!(stdout, Vec::new());
706         assert!(!stderr.is_empty());
707     }
708
709     #[test]
710     #[cfg_attr(target_os = "android", ignore)]
711     fn test_finish_once() {
712         let mut prog = Command::new("false").spawn().unwrap();
713         assert!(prog.wait().unwrap().code() == Some(1));
714     }
715
716     #[test]
717     #[cfg_attr(target_os = "android", ignore)]
718     fn test_finish_twice() {
719         let mut prog = Command::new("false").spawn().unwrap();
720         assert!(prog.wait().unwrap().code() == Some(1));
721         assert!(prog.wait().unwrap().code() == Some(1));
722     }
723
724     #[test]
725     #[cfg_attr(target_os = "android", ignore)]
726     fn test_wait_with_output_once() {
727         let prog = Command::new("echo").arg("hello").stdout(Stdio::piped())
728             .spawn().unwrap();
729         let Output {status, stdout, stderr} = prog.wait_with_output().unwrap();
730         let output_str = str::from_utf8(&stdout).unwrap();
731
732         assert!(status.success());
733         assert_eq!(output_str.trim().to_string(), "hello");
734         assert_eq!(stderr, Vec::new());
735     }
736
737     #[cfg(all(unix, not(target_os="android")))]
738     pub fn env_cmd() -> Command {
739         Command::new("env")
740     }
741     #[cfg(target_os="android")]
742     pub fn env_cmd() -> Command {
743         let mut cmd = Command::new("/system/bin/sh");
744         cmd.arg("-c").arg("set");
745         cmd
746     }
747
748     #[cfg(windows)]
749     pub fn env_cmd() -> Command {
750         let mut cmd = Command::new("cmd");
751         cmd.arg("/c").arg("set");
752         cmd
753     }
754
755     #[test]
756     fn test_inherit_env() {
757         use env;
758
759         let result = env_cmd().output().unwrap();
760         let output = String::from_utf8(result.stdout).unwrap();
761
762         for (ref k, ref v) in env::vars() {
763             // don't check android RANDOM variables
764             if cfg!(target_os = "android") && *k == "RANDOM" {
765                 continue
766             }
767
768             // Windows has hidden environment variables whose names start with
769             // equals signs (`=`). Those do not show up in the output of the
770             // `set` command.
771             assert!((cfg!(windows) && k.starts_with("=")) ||
772                     k.starts_with("DYLD") ||
773                     output.contains(&format!("{}={}", *k, *v)) ||
774                     output.contains(&format!("{}='{}'", *k, *v)),
775                     "output doesn't contain `{}={}`\n{}",
776                     k, v, output);
777         }
778     }
779
780     #[test]
781     fn test_override_env() {
782         use env;
783
784         // In some build environments (such as chrooted Nix builds), `env` can
785         // only be found in the explicitly-provided PATH env variable, not in
786         // default places such as /bin or /usr/bin. So we need to pass through
787         // PATH to our sub-process.
788         let mut cmd = env_cmd();
789         cmd.env_clear().env("RUN_TEST_NEW_ENV", "123");
790         if let Some(p) = env::var_os("PATH") {
791             cmd.env("PATH", &p);
792         }
793         let result = cmd.output().unwrap();
794         let output = String::from_utf8_lossy(&result.stdout).to_string();
795
796         assert!(output.contains("RUN_TEST_NEW_ENV=123"),
797                 "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output);
798     }
799
800     #[test]
801     fn test_add_to_env() {
802         let result = env_cmd().env("RUN_TEST_NEW_ENV", "123").output().unwrap();
803         let output = String::from_utf8_lossy(&result.stdout).to_string();
804
805         assert!(output.contains("RUN_TEST_NEW_ENV=123"),
806                 "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output);
807     }
808
809     // Regression tests for #30858.
810     #[test]
811     fn test_interior_nul_in_progname_is_error() {
812         match Command::new("has-some-\0\0s-inside").spawn() {
813             Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
814             Ok(_) => panic!(),
815         }
816     }
817
818     #[test]
819     fn test_interior_nul_in_arg_is_error() {
820         match Command::new("echo").arg("has-some-\0\0s-inside").spawn() {
821             Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
822             Ok(_) => panic!(),
823         }
824     }
825
826     #[test]
827     fn test_interior_nul_in_args_is_error() {
828         match Command::new("echo").args(&["has-some-\0\0s-inside"]).spawn() {
829             Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
830             Ok(_) => panic!(),
831         }
832     }
833
834     #[test]
835     fn test_interior_nul_in_current_dir_is_error() {
836         match Command::new("echo").current_dir("has-some-\0\0s-inside").spawn() {
837             Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
838             Ok(_) => panic!(),
839         }
840     }
841
842     // Regression tests for #30862.
843     #[test]
844     fn test_interior_nul_in_env_key_is_error() {
845         match env_cmd().env("has-some-\0\0s-inside", "value").spawn() {
846             Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
847             Ok(_) => panic!(),
848         }
849     }
850
851     #[test]
852     fn test_interior_nul_in_env_value_is_error() {
853         match env_cmd().env("key", "has-some-\0\0s-inside").spawn() {
854             Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
855             Ok(_) => panic!(),
856         }
857     }
858 }