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")]
23 use sys
::pipe
::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());
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.
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")]
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
>,
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
>,
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
>,
76 impl AsInner
<imp
::Process
> for Child
{
77 fn as_inner(&self) -> &imp
::Process { &self.handle }
80 impl FromInner
<(imp
::Process
, imp
::StdioPipes
)> for Child
{
81 fn from_inner((handle
, io
): (imp
::Process
, imp
::StdioPipes
)) -> Child
{
84 stdin
: io
.stdin
.map(ChildStdin
::from_inner
),
85 stdout
: io
.stdout
.map(ChildStdout
::from_inner
),
86 stderr
: io
.stderr
.map(ChildStderr
::from_inner
),
91 impl IntoInner
<imp
::Process
> for Child
{
92 fn into_inner(self) -> imp
::Process { self.handle }
95 /// A handle to a child process's stdin
96 #[stable(feature = "process", since = "1.0.0")]
97 pub struct ChildStdin
{
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
)
107 fn flush(&mut self) -> io
::Result
<()> {
112 impl AsInner
<AnonPipe
> for ChildStdin
{
113 fn as_inner(&self) -> &AnonPipe { &self.inner }
116 impl IntoInner
<AnonPipe
> for ChildStdin
{
117 fn into_inner(self) -> AnonPipe { self.inner }
120 impl FromInner
<AnonPipe
> for ChildStdin
{
121 fn from_inner(pipe
: AnonPipe
) -> ChildStdin
{
122 ChildStdin { inner: pipe }
126 /// A handle to a child process's stdout
127 #[stable(feature = "process", since = "1.0.0")]
128 pub struct ChildStdout
{
132 #[stable(feature = "process", since = "1.0.0")]
133 impl Read
for ChildStdout
{
134 fn read(&mut self, buf
: &mut [u8]) -> io
::Result
<usize> {
139 impl AsInner
<AnonPipe
> for ChildStdout
{
140 fn as_inner(&self) -> &AnonPipe { &self.inner }
143 impl IntoInner
<AnonPipe
> for ChildStdout
{
144 fn into_inner(self) -> AnonPipe { self.inner }
147 impl FromInner
<AnonPipe
> for ChildStdout
{
148 fn from_inner(pipe
: AnonPipe
) -> ChildStdout
{
149 ChildStdout { inner: pipe }
153 /// A handle to a child process's stderr
154 #[stable(feature = "process", since = "1.0.0")]
155 pub struct ChildStderr
{
159 #[stable(feature = "process", since = "1.0.0")]
160 impl Read
for ChildStderr
{
161 fn read(&mut self, buf
: &mut [u8]) -> io
::Result
<usize> {
166 impl AsInner
<AnonPipe
> for ChildStderr
{
167 fn as_inner(&self) -> &AnonPipe { &self.inner }
170 impl IntoInner
<AnonPipe
> for ChildStderr
{
171 fn into_inner(self) -> AnonPipe { self.inner }
174 impl FromInner
<AnonPipe
> for ChildStderr
{
175 fn from_inner(pipe
: AnonPipe
) -> ChildStderr
{
176 ChildStderr { inner: pipe }
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:
187 /// use std::process::Command;
189 /// let output = Command::new("sh")
191 /// .arg("echo hello")
193 /// .unwrap_or_else(|e| { panic!("failed to execute process: {}", e) });
194 /// let hello = output.stdout;
196 #[stable(feature = "process", since = "1.0.0")]
202 /// Constructs a new `Command` for launching the program at
203 /// path `program`, with the following default configuration:
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`
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()) }
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());
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
{
228 self.arg(arg
.as_ref());
233 /// Inserts or updates an environment variable mapping.
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
>
241 self.inner
.env(key
.as_ref(), val
.as_ref());
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());
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();
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());
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);
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);
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);
287 /// Executes the command as a child process, returning a handle to it.
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
)
295 /// Executes the command as a child process, waiting for it to finish and
296 /// collecting all of its output.
298 /// By default, stdin, stdout and stderr are captured (and used to
299 /// provide the resulting output).
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)
309 /// println!("status: {}", output.status);
310 /// println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
311 /// println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
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())
319 /// Executes a command as a child process, waiting for it to finish and
320 /// collecting its exit status.
322 /// By default, stdin, stdout and stderr are inherited from the parent.
327 /// use std::process::Command;
329 /// let status = Command::new("ls").status().unwrap_or_else(|e| {
330 /// panic!("failed to execute process: {}", e)
333 /// println!("process exited with: {}", status);
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())
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
346 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
351 impl AsInner
<imp
::Command
> for Command
{
352 fn as_inner(&self) -> &imp
::Command { &self.inner }
355 impl AsInnerMut
<imp
::Command
> for Command
{
356 fn as_inner_mut(&mut self) -> &mut imp
::Command { &mut self.inner }
359 /// The output of a finished process.
360 #[derive(PartialEq, Eq, Clone)]
361 #[stable(feature = "process", since = "1.0.0")]
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")]
369 /// The data that the process wrote to stderr.
370 #[stable(feature = "process", since = "1.0.0")]
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
{
380 let stdout_utf8
= str::from_utf8(&self.stdout
);
381 let stdout_debug
: &fmt
::Debug
= match stdout_utf8
{
383 Err(_
) => &self.stdout
386 let stderr_utf8
= str::from_utf8(&self.stderr
);
387 let stderr_debug
: &fmt
::Debug
= match stderr_utf8
{
389 Err(_
) => &self.stderr
392 fmt
.debug_struct("Output")
393 .field("status", &self.status
)
394 .field("stdout", stdout_debug
)
395 .field("stderr", stderr_debug
)
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
);
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) }
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) }
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) }
419 impl FromInner
<imp
::Stdio
> for Stdio
{
420 fn from_inner(inner
: imp
::Stdio
) -> Stdio
{
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
);
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
{
438 /// Returns the exit code of the process, if any.
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> {
449 impl AsInner
<imp
::ExitStatus
> for ExitStatus
{
450 fn as_inner(&self) -> &imp
::ExitStatus { &self.0 }
453 #[stable(feature = "process", since = "1.0.0")]
454 impl fmt
::Display
for ExitStatus
{
455 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
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
<()> {
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 {
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.
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
)
488 /// Simultaneously waits for the child to exit and collect all remaining
489 /// output on the stdout/stderr handles, returning an `Output`
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
502 thread
::spawn(move || {
503 let mut ret
= Vec
::new();
504 input
.read_to_end(&mut ret
).map(|_
| ret
)
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());
515 stdout
: stdout
.unwrap_or(Vec
::new()),
516 stderr
: stderr
.unwrap_or(Vec
::new()),
521 /// Terminates the current process with the specified exit code.
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.
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
532 #[stable(feature = "rust1", since = "1.0.0")]
533 pub fn exit(code
: i32) -> ! {
534 ::sys_common
::cleanup();
535 ::sys
::os
::exit(code
)
545 use super::{Command, Output, Stdio}
;
547 // FIXME(#10380) these tests should not all be ignored on android.
550 #[cfg_attr(target_os = "android", ignore)]
552 let p
= Command
::new("true").spawn();
554 let mut p
= p
.unwrap();
555 assert
!(p
.wait().unwrap().success());
559 #[cfg_attr(target_os = "android", ignore)]
561 match Command
::new("if-this-is-a-binary-then-the-world-has-ended").spawn() {
568 #[cfg_attr(target_os = "android", ignore)]
569 fn exit_reported_right() {
570 let p
= Command
::new("false").spawn();
572 let mut p
= p
.unwrap();
573 assert
!(p
.wait().unwrap().code() == Some(1));
579 #[cfg_attr(target_os = "android", ignore)]
580 fn signal_reported_right() {
581 use os
::unix
::process
::ExitStatusExt
;
583 let mut p
= Command
::new("/bin/sh")
584 .arg("-c").arg("read a")
585 .stdin(Stdio
::piped())
588 match p
.wait().unwrap().signal() {
590 result
=> panic
!("not terminated by signal 9 (instead, {:?})",
595 pub fn run_output(mut cmd
: Command
) -> String
{
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());
607 #[cfg_attr(target_os = "android", ignore)]
609 let mut cmd
= Command
::new("echo");
610 cmd
.arg("foobar").stdout(Stdio
::piped());
611 assert_eq
!(run_output(cmd
), "foobar\n");
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")
620 .stdout(Stdio
::piped());
621 assert_eq
!(run_output(cmd
), "/\n");
625 #[cfg_attr(any(windows, target_os = "android"), ignore)]
627 let mut p
= Command
::new("/bin/sh")
628 .arg("-c").arg("read line; echo $line")
629 .stdin(Stdio
::piped())
630 .stdout(Stdio
::piped())
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");
642 #[cfg_attr(target_os = "android", ignore)]
645 use os
::unix
::prelude
::*;
647 let mut p
= Command
::new("/bin/sh")
648 .arg("-c").arg("true")
649 .uid(unsafe { libc::getuid() }
)
650 .gid(unsafe { libc::getgid() }
)
652 assert
!(p
.wait().unwrap().success());
656 #[cfg_attr(target_os = "android", ignore)]
658 fn uid_to_root_fails() {
659 use os
::unix
::prelude
::*;
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());
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));
674 status
= Command
::new("true").status().unwrap();
675 assert
!(status
.success());
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
),
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();
693 assert
!(status
.success());
694 assert_eq
!(output_str
.trim().to_string(), "hello");
695 assert_eq
!(stderr
, Vec
::new());
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();
704 assert
!(status
.code() == Some(1));
705 assert_eq
!(stdout
, Vec
::new());
706 assert
!(!stderr
.is_empty());
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));
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));
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())
729 let Output {status, stdout, stderr}
= prog
.wait_with_output().unwrap();
730 let output_str
= str::from_utf8(&stdout
).unwrap();
732 assert
!(status
.success());
733 assert_eq
!(output_str
.trim().to_string(), "hello");
734 assert_eq
!(stderr
, Vec
::new());
737 #[cfg(all(unix, not(target_os="android")))]
738 pub fn env_cmd() -> Command
{
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");
749 pub fn env_cmd() -> Command
{
750 let mut cmd
= Command
::new("cmd");
751 cmd
.arg("/c").arg("set");
756 fn test_inherit_env() {
759 let result
= env_cmd().output().unwrap();
760 let output
= String
::from_utf8(result
.stdout
).unwrap();
762 for (ref k
, ref v
) in env
::vars() {
763 // don't check android RANDOM variables
764 if cfg
!(target_os
= "android") && *k
== "RANDOM" {
768 // Windows has hidden environment variables whose names start with
769 // equals signs (`=`). Those do not show up in the output of the
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{}",
781 fn test_override_env() {
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") {
793 let result
= cmd
.output().unwrap();
794 let output
= String
::from_utf8_lossy(&result
.stdout
).to_string();
796 assert
!(output
.contains("RUN_TEST_NEW_ENV=123"),
797 "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output
);
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();
805 assert
!(output
.contains("RUN_TEST_NEW_ENV=123"),
806 "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output
);
809 // Regression tests for #30858.
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
),
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
),
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
),
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
),
842 // Regression tests for #30862.
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
),
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
),