Skip to main content

Capture a potentially interesting Rust compiler output

ID
0dee0e0
date
2021-11-27 08:11:15+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
169bb40
message
Capture a potentially interesting Rust compiler output

$ cargo test
   Compiling dominant_colours v1.0.0 (/Users/alexwlchan/repos/dominant_colours)
error[E0106]: missing lifetime specifier
   --> src/main.rs:167:17
    |
167 |         stdout: &str,
    |                 ^ expected named lifetime parameter
    |
help: consider introducing a named lifetime parameter
    |
165 |     struct DcOutput<'a> {
166 |         exit_code: i32,
167 |         stdout: &'a str,
    |

error[E0106]: missing lifetime specifier
   --> src/main.rs:168:17
    |
168 |         stderr: &str,
    |                 ^ expected named lifetime parameter
    |
help: consider introducing a named lifetime parameter
    |
165 |     struct DcOutput<'a> {
166 |         exit_code: i32,
167 |         stdout: &str,
168 |         stderr: &'a str,
    |

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0106`.
error: could not compile `dominant_colours`

To learn more, run the command again with --verbose.
changed files
1 file, 18 additions, 6 deletions

Changed files

src/main.rs (6344) → src/main.rs (6598)

diff --git a/src/main.rs b/src/main.rs
index 76faf81..c8c63bd 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -157,9 +157,15 @@ mod tests {
     fn it_fails_if_you_pass_an_invalid_count() {
         let output = get_failure(&["./src/tests/red.png", "--count=NaN"]);
 
-        assert_eq!(output.status.code().unwrap(), 1);
-        assert_eq!(str::from_utf8(&output.stdout).unwrap(), "");
-        assert_eq!(str::from_utf8(&output.stderr).unwrap(), "error: Invalid value: The argument 'NaN' isn't a valid value\n");
+        assert_eq!(output.exit_code, 1);
+        assert_eq!(output.stdout, "");
+        assert_eq!(output.stderr, "error: Invalid value: The argument 'NaN' isn't a valid value\n");
+    }
+
+    struct DcOutput {
+        exit_code: i32,
+        stdout: &str,
+        stderr: &str,
     }
 
     fn get_success(args: &[&str]) -> Output {
@@ -173,13 +179,19 @@ mod tests {
             .to_owned()
     }
 
-    fn get_failure(args: &[&str]) -> Output {
+    fn get_failure(args: &[&str]) -> DcOutput {
         let mut cmd = Command::cargo_bin("dominant_colours").unwrap();
-        cmd
+        let output = cmd
             .args(args)
             .unwrap_err()
             .as_output()
             .unwrap()
-            .to_owned()
+            .to_owned();
+
+        DcOutput {
+            exit_code: output.status.code().unwrap(),
+            stdout: str::from_utf8(&output.stdout).unwrap(),
+            stderr: str::from_utf8(&output.stderr).unwrap(),
+        }
     }
 }