Rust: process::ExitStatus should have some kind of constructor

Created on 3 Apr 2016  路  8Comments  路  Source: rust-lang/rust

Because ExitStatus has a private field it is impossible to create one in user code, one must actually execute a process. This prevents testing/mocking. My use case is that I have some function which runs a process and generates data, however, for testing I want to mock the test run and just return a success exit status and some data. I can't do that because I can't create my own ExitStatus objects.

At the least there should be a way to create a success ExitStatus. Preferably, there would be a way to create various fail statuses too.

B-unstable T-libs final-comment-period

Most helpful comment

馃敂 This issue is now entering a cycle-long final comment period for stabilization 馃敂

All 8 comments

Should the constructor just take an i32?

I've also wanted a basically "raw constructor" in the past. I'd be fine adding some std::os extensions which allow construction from a c_int or DWORD (i32 and u32)

+1
I鈥檇 like to extend Command, but I cannot reuse Output for return value because I can鈥檛 construct ExitStatus. :(

I've opened a PR for this over at https://github.com/rust-lang/rust/pull/33224

Great, thanks! :+1:

This would help n00bs like me who start out by copying the example code and tweaking it:

let output = Command::new("sh")
                     .arg("-c")
                     .arg("echo hello")
                     .output()
                     .unwrap_or_else(|e| { panic!("failed to execute process: {}", e) });

This example is a special case that only happens to work if the unwrap_or_else contains a panic! or something else that immediately exits. Otherwise you need to create your own Output struct to return which is currently impossible.

馃敂 This issue is now entering a cycle-long final comment period for stabilization 馃敂

Discussed recently, the libs team decided to stabilize.

Was this page helpful?
0 / 5 - 0 ratings