From 8d61cb245d10bb2a61928cfa8651b1e88e7b115a Mon Sep 17 00:00:00 2001 From: Lukas Pustina Date: Fri, 18 Mar 2016 16:11:37 +0100 Subject: [PATCH 1/6] Extends rustdoc on how to caputure output --- src/libstd/process.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/libstd/process.rs b/src/libstd/process.rs index 5813d82a315..b21f745764c 100644 --- a/src/libstd/process.rs +++ b/src/libstd/process.rs @@ -499,6 +499,29 @@ impl Child { /// before waiting. This helps avoid deadlock: it ensures that the /// child does not block waiting for input from the parent, while /// the parent waits for the child to exit. + /// + /// By default, stdin, stdout and stderr are inherited from the parent. + /// In order to capture the output into this `Result` it is + /// necessary to create new pipes between parent and child. Use + /// `stdout(Stdio::piped())` or `stdout(Stdio::piped())`, respectively. + /// + /// # Examples + /// + /// ```should_panic + /// use std::process::{Command, Stdio}; + /// + /// let mut child = Command::new("/bin/cat") + /// .stdout(Stdio::piped()) + /// .arg("file.txt") + /// .spawn() + /// .unwrap_or_else(|e| { panic!("failed to execute child: {}", e) }); + /// + /// let ecode = child.wait_with_output() + /// .unwrap_or_else(|e| { panic!("failed to wait on child: {}", e) }); + /// + /// assert!(ecode.success()); + /// ``` + /// #[stable(feature = "process", since = "1.0.0")] pub fn wait_with_output(mut self) -> io::Result { drop(self.stdin.take()); From 3b5cfa3ecf4e44b251ce121ab5dcf53c35de8eea Mon Sep 17 00:00:00 2001 From: Lukas Pustina Date: Fri, 18 Mar 2016 18:44:02 +0100 Subject: [PATCH 2/6] Tags new test as `no_run` and uses expect() --- src/libstd/process.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libstd/process.rs b/src/libstd/process.rs index b21f745764c..9907cf32b35 100644 --- a/src/libstd/process.rs +++ b/src/libstd/process.rs @@ -507,17 +507,16 @@ impl Child { /// /// # Examples /// - /// ```should_panic + /// ```no_run /// use std::process::{Command, Stdio}; /// /// let mut child = Command::new("/bin/cat") /// .stdout(Stdio::piped()) /// .arg("file.txt") /// .spawn() - /// .unwrap_or_else(|e| { panic!("failed to execute child: {}", e) }); + /// .expect("failed to execute child"); /// - /// let ecode = child.wait_with_output() - /// .unwrap_or_else(|e| { panic!("failed to wait on child: {}", e) }); + /// let ecode = child.wait_with_output().expect("failed to wait on child"); /// /// assert!(ecode.success()); /// ``` From aefbbc79a3d0c082e19fe3368604dab91333f8a9 Mon Sep 17 00:00:00 2001 From: Lukas Pustina Date: Sat, 19 Mar 2016 10:41:13 +0100 Subject: [PATCH 3/6] Revert "Tags new test as `no_run` and uses expect()" - After discussing with @alexcrichton, the initial commit has been fine. This reverts commit 3b5cfa3ecf4e44b251ce121ab5dcf53c35de8eea. --- src/libstd/process.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libstd/process.rs b/src/libstd/process.rs index 9907cf32b35..b21f745764c 100644 --- a/src/libstd/process.rs +++ b/src/libstd/process.rs @@ -507,16 +507,17 @@ impl Child { /// /// # Examples /// - /// ```no_run + /// ```should_panic /// use std::process::{Command, Stdio}; /// /// let mut child = Command::new("/bin/cat") /// .stdout(Stdio::piped()) /// .arg("file.txt") /// .spawn() - /// .expect("failed to execute child"); + /// .unwrap_or_else(|e| { panic!("failed to execute child: {}", e) }); /// - /// let ecode = child.wait_with_output().expect("failed to wait on child"); + /// let ecode = child.wait_with_output() + /// .unwrap_or_else(|e| { panic!("failed to wait on child: {}", e) }); /// /// assert!(ecode.success()); /// ``` From 45517947ba9a69260e5b3f4fe54a9aa21ed703eb Mon Sep 17 00:00:00 2001 From: Lukas Pustina Date: Sat, 19 Mar 2016 21:07:47 +0100 Subject: [PATCH 4/6] Fixes 2. stdout to stderr in rustdoc --- src/libstd/process.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/process.rs b/src/libstd/process.rs index b21f745764c..1b6993f1940 100644 --- a/src/libstd/process.rs +++ b/src/libstd/process.rs @@ -503,7 +503,7 @@ impl Child { /// By default, stdin, stdout and stderr are inherited from the parent. /// In order to capture the output into this `Result` it is /// necessary to create new pipes between parent and child. Use - /// `stdout(Stdio::piped())` or `stdout(Stdio::piped())`, respectively. + /// `stdout(Stdio::piped())` or `stderr(Stdio::piped())`, respectively. /// /// # Examples /// From 0dd5f67f4af2f071fa435dc0487d3678d3cf40dd Mon Sep 17 00:00:00 2001 From: Lukas Pustina Date: Mon, 21 Mar 2016 14:17:17 +0100 Subject: [PATCH 5/6] Adjusts all rust doc test to use `expect` and `should_panic` - All Rust Doc tests execute the same command `/bin/cat file.txt` which `should_panic` on all platforms consistently, because either `/bin/cat` or `file.txt` do not exist. --- src/libstd/process.rs | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/libstd/process.rs b/src/libstd/process.rs index 1b6993f1940..d58e8ebab58 100644 --- a/src/libstd/process.rs +++ b/src/libstd/process.rs @@ -38,10 +38,10 @@ use sys_common::{AsInner, AsInnerMut, FromInner, IntoInner}; /// let mut child = Command::new("/bin/cat") /// .arg("file.txt") /// .spawn() -/// .unwrap_or_else(|e| { panic!("failed to execute child: {}", e) }); +/// .expect("failed to execute child"); /// /// let ecode = child.wait() -/// .unwrap_or_else(|e| { panic!("failed to wait on child: {}", e) }); +/// .expect("failed to wait on child"); /// /// assert!(ecode.success()); /// ``` @@ -195,7 +195,8 @@ impl FromInner for ChildStderr { /// .arg("-c") /// .arg("echo hello") /// .output() -/// .unwrap_or_else(|e| { panic!("failed to execute process: {}", e) }); +/// .expect("failed to execute proces"); +/// /// let hello = output.stdout; /// ``` #[stable(feature = "process", since = "1.0.0")] @@ -305,11 +306,10 @@ impl Command { /// /// # Examples /// - /// ``` + /// ```should_panic /// use std::process::Command; - /// let output = Command::new("cat").arg("foo.txt").output().unwrap_or_else(|e| { - /// panic!("failed to execute process: {}", e) - /// }); + /// let output = Command::new("/bin/cat").arg("file.txt").output() + /// .expect("failed to execute process"); /// /// println!("status: {}", output.status); /// println!("stdout: {}", String::from_utf8_lossy(&output.stdout)); @@ -328,12 +328,11 @@ impl Command { /// /// # Examples /// - /// ``` + /// ```should_panic /// use std::process::Command; /// - /// let status = Command::new("ls").status().unwrap_or_else(|e| { - /// panic!("failed to execute process: {}", e) - /// }); + /// let status = Command::new("/bin/cat").arg("file.txt").status() + /// .expect("failed to execute process"); /// /// println!("process exited with: {}", status); /// ``` @@ -511,13 +510,13 @@ impl Child { /// use std::process::{Command, Stdio}; /// /// let mut child = Command::new("/bin/cat") - /// .stdout(Stdio::piped()) /// .arg("file.txt") + /// .stdout(Stdio::piped()) /// .spawn() - /// .unwrap_or_else(|e| { panic!("failed to execute child: {}", e) }); + /// .expect("failed to execute child"); /// /// let ecode = child.wait_with_output() - /// .unwrap_or_else(|e| { panic!("failed to wait on child: {}", e) }); + /// .expect("failed to wait on child"); /// /// assert!(ecode.success()); /// ``` From 561337223fc5aa6d97457bdd133f7404b6ae562a Mon Sep 17 00:00:00 2001 From: Lukas Pustina Date: Tue, 22 Mar 2016 15:19:24 +0100 Subject: [PATCH 6/6] Fixes test which are now run due to should_panic Since I changed no_run to should_panic on some tests, the were run but two lacked an actual assertion. Further, I missed to check the return type on another test. --- src/libstd/process.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libstd/process.rs b/src/libstd/process.rs index d58e8ebab58..a542bf2c8d2 100644 --- a/src/libstd/process.rs +++ b/src/libstd/process.rs @@ -314,6 +314,8 @@ impl Command { /// println!("status: {}", output.status); /// println!("stdout: {}", String::from_utf8_lossy(&output.stdout)); /// println!("stderr: {}", String::from_utf8_lossy(&output.stderr)); + /// + /// assert!(output.status.success()); /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn output(&mut self) -> io::Result { @@ -335,6 +337,8 @@ impl Command { /// .expect("failed to execute process"); /// /// println!("process exited with: {}", status); + /// + /// assert!(status.success()); /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn status(&mut self) -> io::Result { @@ -518,7 +522,7 @@ impl Child { /// let ecode = child.wait_with_output() /// .expect("failed to wait on child"); /// - /// assert!(ecode.success()); + /// assert!(ecode.status.success()); /// ``` /// #[stable(feature = "process", since = "1.0.0")]