Skip to content

Commit 2031eac

Browse files
committed
run_make_support: rename Command::stdin to stdin_buf and add std{in,out,err} config helpers
Previously `Command::stdin` was actually just a stdin buffer helper, but this is different from `std::process::Command::stdin`. This is needlessly confusing, and blocks support to add `std{in,out,err}` config that tests may want to use to e.g. redirect to `/dev/ptmx`.
1 parent 009e738 commit 2031eac

File tree

5 files changed

+67
-22
lines changed

5 files changed

+67
-22
lines changed

Diff for: src/tools/run-make-support/src/command.rs

+55-13
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,58 @@ use crate::{
2929
#[derive(Debug)]
3030
pub struct Command {
3131
cmd: StdCommand,
32-
stdin: Option<Box<[u8]>>,
32+
// Convience for providing a quick stdin buffer.
33+
stdin_buf: Option<Box<[u8]>>,
34+
35+
// Configurations for child process's std{in,out,err} handles.
36+
stdin: Option<Stdio>,
37+
stdout: Option<Stdio>,
38+
stderr: Option<Stdio>,
39+
3340
drop_bomb: DropBomb,
3441
}
3542

3643
impl Command {
3744
#[track_caller]
3845
pub fn new<P: AsRef<OsStr>>(program: P) -> Self {
3946
let program = program.as_ref();
40-
Self { cmd: StdCommand::new(program), stdin: None, drop_bomb: DropBomb::arm(program) }
47+
Self {
48+
cmd: StdCommand::new(program),
49+
stdin_buf: None,
50+
drop_bomb: DropBomb::arm(program),
51+
stdin: None,
52+
stdout: None,
53+
stderr: None,
54+
}
55+
}
56+
57+
/// Specify a stdin input buffer. This is a convenience helper,
58+
pub fn stdin_buf<I: AsRef<[u8]>>(&mut self, input: I) -> &mut Self {
59+
self.stdin_buf = Some(input.as_ref().to_vec().into_boxed_slice());
60+
self
61+
}
62+
63+
/// Configuration for the child process’s standard input (stdin) handle.
64+
///
65+
/// See [`std::process::Command::stdin`].
66+
pub fn stdin<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
67+
self.stdin = Some(cfg.into());
68+
self
69+
}
70+
71+
/// Configuration for the child process’s standard output (stdout) handle.
72+
///
73+
/// See [`std::process::Command::stdout`].
74+
pub fn stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
75+
self.stdout = Some(cfg.into());
76+
self
4177
}
4278

43-
/// Specify a stdin input
44-
pub fn stdin<I: AsRef<[u8]>>(&mut self, input: I) -> &mut Self {
45-
self.stdin = Some(input.as_ref().to_vec().into_boxed_slice());
79+
/// Configuration for the child process’s standard error (stderr) handle.
80+
///
81+
/// See [`std::process::Command::stderr`].
82+
pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
83+
self.stderr = Some(cfg.into());
4684
self
4785
}
4886

@@ -105,6 +143,8 @@ impl Command {
105143
}
106144

107145
/// Run the constructed command and assert that it is successfully run.
146+
///
147+
/// By default, std{in,out,err} are [`Stdio::piped()`].
108148
#[track_caller]
109149
pub fn run(&mut self) -> CompletedProcess {
110150
let output = self.command_output();
@@ -115,6 +155,8 @@ impl Command {
115155
}
116156

117157
/// Run the constructed command and assert that it does not successfully run.
158+
///
159+
/// By default, std{in,out,err} are [`Stdio::piped()`].
118160
#[track_caller]
119161
pub fn run_fail(&mut self) -> CompletedProcess {
120162
let output = self.command_output();
@@ -124,10 +166,10 @@ impl Command {
124166
output
125167
}
126168

127-
/// Run the command but do not check its exit status.
128-
/// Only use if you explicitly don't care about the exit status.
129-
/// Prefer to use [`Self::run`] and [`Self::run_fail`]
130-
/// whenever possible.
169+
/// Run the command but do not check its exit status. Only use if you explicitly don't care
170+
/// about the exit status.
171+
///
172+
/// Prefer to use [`Self::run`] and [`Self::run_fail`] whenever possible.
131173
#[track_caller]
132174
pub fn run_unchecked(&mut self) -> CompletedProcess {
133175
self.command_output()
@@ -137,11 +179,11 @@ impl Command {
137179
fn command_output(&mut self) -> CompletedProcess {
138180
self.drop_bomb.defuse();
139181
// let's make sure we piped all the input and outputs
140-
self.cmd.stdin(Stdio::piped());
141-
self.cmd.stdout(Stdio::piped());
142-
self.cmd.stderr(Stdio::piped());
182+
self.cmd.stdin(self.stdin.take().unwrap_or(Stdio::piped()));
183+
self.cmd.stdout(self.stdout.take().unwrap_or(Stdio::piped()));
184+
self.cmd.stderr(self.stderr.take().unwrap_or(Stdio::piped()));
143185

144-
let output = if let Some(input) = &self.stdin {
186+
let output = if let Some(input) = &self.stdin_buf {
145187
let mut child = self.cmd.spawn().unwrap();
146188

147189
{

Diff for: src/tools/run-make-support/src/external_deps/llvm.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,10 @@ impl LlvmFilecheck {
227227
Self { cmd }
228228
}
229229

230-
/// Pipe a read file into standard input containing patterns that will be matched against the .patterns(path) call.
231-
pub fn stdin<I: AsRef<[u8]>>(&mut self, input: I) -> &mut Self {
232-
self.cmd.stdin(input);
230+
/// Provide a buffer representing standard input containing patterns that will be matched
231+
/// against the `.patterns(path)` call.
232+
pub fn stdin_buf<I: AsRef<[u8]>>(&mut self, input: I) -> &mut Self {
233+
self.cmd.stdin_buf(input);
233234
self
234235
}
235236

Diff for: src/tools/run-make-support/src/external_deps/rustc.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,9 @@ impl Rustc {
291291
self
292292
}
293293

294-
/// Specify a stdin input
295-
pub fn stdin<I: AsRef<[u8]>>(&mut self, input: I) -> &mut Self {
296-
self.cmd.stdin(input);
294+
/// Specify a stdin input buffer.
295+
pub fn stdin_buf<I: AsRef<[u8]>>(&mut self, input: I) -> &mut Self {
296+
self.cmd.stdin_buf(input);
297297
self
298298
}
299299

Diff for: src/tools/run-make-support/src/external_deps/rustdoc.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ impl Rustdoc {
8585
self
8686
}
8787

88-
/// Specify a stdin input
89-
pub fn stdin<I: AsRef<[u8]>>(&mut self, input: I) -> &mut Self {
90-
self.cmd.stdin(input);
88+
/// Specify a stdin input buffer.
89+
pub fn stdin_buf<I: AsRef<[u8]>>(&mut self, input: I) -> &mut Self {
90+
self.cmd.stdin_buf(input);
9191
self
9292
}
9393

Diff for: src/tools/run-make-support/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ pub mod rfs {
3434
}
3535

3636
// Re-exports of third-party library crates.
37+
// tidy-alphabetical-start
3738
pub use bstr;
3839
pub use gimli;
3940
pub use libc;
4041
pub use object;
4142
pub use regex;
4243
pub use serde_json;
4344
pub use wasmparser;
45+
// tidy-alphabetical-end
4446

4547
// Re-exports of external dependencies.
4648
pub use external_deps::{c_build, cc, clang, htmldocck, llvm, python, rustc, rustdoc};

0 commit comments

Comments
 (0)