Skip to content

Commit a4ffa2c

Browse files
committed
Command docs: Replace many spawn with status and an extra check
All these examples called `spawn` and then didn't wait for the child (!) Fix this by having them call .status() rather than .spawn(), and assert that the status was success. Signed-off-by: Ian Jackson <[email protected]>
1 parent 51cbc01 commit a4ffa2c

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed

library/std/src/process.rs

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -525,9 +525,11 @@ impl Command {
525525
/// ```no_run
526526
/// use std::process::Command;
527527
///
528-
/// Command::new("sh")
529-
/// .spawn()
528+
/// let status = Command::new("sh")
529+
/// .status()
530530
/// .expect("sh command failed to start");
531+
///
532+
/// assert!(status.success());
531533
/// ```
532534
#[stable(feature = "process", since = "1.0.0")]
533535
pub fn new<S: AsRef<OsStr>>(program: S) -> Command {
@@ -569,11 +571,13 @@ impl Command {
569571
/// ```no_run
570572
/// use std::process::Command;
571573
///
572-
/// Command::new("ls")
574+
/// let status = Command::new("ls")
573575
/// .arg("-l")
574576
/// .arg("-a")
575-
/// .spawn()
577+
/// .status()
576578
/// .expect("ls command failed to start");
579+
///
580+
/// assert!(status.success());
577581
/// ```
578582
#[stable(feature = "process", since = "1.0.0")]
579583
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command {
@@ -599,10 +603,12 @@ impl Command {
599603
/// ```no_run
600604
/// use std::process::Command;
601605
///
602-
/// Command::new("ls")
606+
/// let status = Command::new("ls")
603607
/// .args(&["-l", "-a"])
604-
/// .spawn()
608+
/// .status()
605609
/// .expect("ls command failed to start");
610+
///
611+
/// assert!(status.success());
606612
/// ```
607613
#[stable(feature = "process", since = "1.0.0")]
608614
pub fn args<I, S>(&mut self, args: I) -> &mut Command
@@ -628,10 +634,12 @@ impl Command {
628634
/// ```no_run
629635
/// use std::process::Command;
630636
///
631-
/// Command::new("ls")
637+
/// let status = Command::new("ls")
632638
/// .env("PATH", "/bin")
633-
/// .spawn()
639+
/// .status()
634640
/// .expect("ls command failed to start");
641+
///
642+
/// assert!(status.success());
635643
/// ```
636644
#[stable(feature = "process", since = "1.0.0")]
637645
pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command
@@ -659,13 +667,15 @@ impl Command {
659667
/// k == "TERM" || k == "TZ" || k == "LANG" || k == "PATH"
660668
/// ).collect();
661669
///
662-
/// Command::new("printenv")
670+
/// let status = Command::new("printenv")
663671
/// .stdin(Stdio::null())
664672
/// .stdout(Stdio::inherit())
665673
/// .env_clear()
666674
/// .envs(&filtered_env)
667-
/// .spawn()
675+
/// .status()
668676
/// .expect("printenv failed to start");
677+
///
678+
/// assert!(status.success());
669679
/// ```
670680
#[stable(feature = "command_envs", since = "1.19.0")]
671681
pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Command
@@ -689,10 +699,12 @@ impl Command {
689699
/// ```no_run
690700
/// use std::process::Command;
691701
///
692-
/// Command::new("ls")
702+
/// let status = Command::new("ls")
693703
/// .env_remove("PATH")
694-
/// .spawn()
704+
/// .status()
695705
/// .expect("ls command failed to start");
706+
///
707+
/// assert!(status.success());
696708
/// ```
697709
#[stable(feature = "process", since = "1.0.0")]
698710
pub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Command {
@@ -709,10 +721,12 @@ impl Command {
709721
/// ```no_run
710722
/// use std::process::Command;
711723
///
712-
/// Command::new("ls")
724+
/// let status = Command::new("ls")
713725
/// .env_clear()
714-
/// .spawn()
726+
/// .status()
715727
/// .expect("ls command failed to start");
728+
///
729+
/// assert!(status.success());
716730
/// ```
717731
#[stable(feature = "process", since = "1.0.0")]
718732
pub fn env_clear(&mut self) -> &mut Command {
@@ -737,10 +751,12 @@ impl Command {
737751
/// ```no_run
738752
/// use std::process::Command;
739753
///
740-
/// Command::new("ls")
754+
/// let status = Command::new("ls")
741755
/// .current_dir("/bin")
742-
/// .spawn()
756+
/// .status()
743757
/// .expect("ls command failed to start");
758+
///
759+
/// assert!(status.success());
744760
/// ```
745761
///
746762
/// [`canonicalize`]: crate::fs::canonicalize
@@ -765,10 +781,12 @@ impl Command {
765781
/// ```no_run
766782
/// use std::process::{Command, Stdio};
767783
///
768-
/// Command::new("ls")
784+
/// let status = Command::new("ls")
769785
/// .stdin(Stdio::null())
770-
/// .spawn()
786+
/// .status()
771787
/// .expect("ls command failed to start");
788+
///
789+
/// assert!(status.success());
772790
/// ```
773791
#[stable(feature = "process", since = "1.0.0")]
774792
pub fn stdin<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
@@ -791,10 +809,12 @@ impl Command {
791809
/// ```no_run
792810
/// use std::process::{Command, Stdio};
793811
///
794-
/// Command::new("ls")
812+
/// let status = Command::new("ls")
795813
/// .stdout(Stdio::null())
796-
/// .spawn()
814+
/// .status()
797815
/// .expect("ls command failed to start");
816+
///
817+
/// assert!(status.success());
798818
/// ```
799819
#[stable(feature = "process", since = "1.0.0")]
800820
pub fn stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
@@ -817,10 +837,12 @@ impl Command {
817837
/// ```no_run
818838
/// use std::process::{Command, Stdio};
819839
///
820-
/// Command::new("ls")
840+
/// let status = Command::new("ls")
821841
/// .stderr(Stdio::null())
822-
/// .spawn()
842+
/// .status()
823843
/// .expect("ls command failed to start");
844+
///
845+
/// assert!(status.success());
824846
/// ```
825847
#[stable(feature = "process", since = "1.0.0")]
826848
pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {

0 commit comments

Comments
 (0)