Skip to content

Commit

Permalink
run_make_support: rename Command::stdin to stdin_buf and add `std…
Browse files Browse the repository at this point in the history
…{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`.
  • Loading branch information
jieyouxu committed Sep 5, 2024
1 parent 009e738 commit 2031eac
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 22 deletions.
68 changes: 55 additions & 13 deletions src/tools/run-make-support/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,58 @@ use crate::{
#[derive(Debug)]
pub struct Command {
cmd: StdCommand,
stdin: Option<Box<[u8]>>,
// Convience for providing a quick stdin buffer.
stdin_buf: Option<Box<[u8]>>,

// Configurations for child process's std{in,out,err} handles.
stdin: Option<Stdio>,
stdout: Option<Stdio>,
stderr: Option<Stdio>,

drop_bomb: DropBomb,
}

impl Command {
#[track_caller]
pub fn new<P: AsRef<OsStr>>(program: P) -> Self {
let program = program.as_ref();
Self { cmd: StdCommand::new(program), stdin: None, drop_bomb: DropBomb::arm(program) }
Self {
cmd: StdCommand::new(program),
stdin_buf: None,
drop_bomb: DropBomb::arm(program),
stdin: None,
stdout: None,
stderr: None,
}
}

/// Specify a stdin input buffer. This is a convenience helper,
pub fn stdin_buf<I: AsRef<[u8]>>(&mut self, input: I) -> &mut Self {
self.stdin_buf = Some(input.as_ref().to_vec().into_boxed_slice());
self
}

/// Configuration for the child process’s standard input (stdin) handle.
///
/// See [`std::process::Command::stdin`].
pub fn stdin<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
self.stdin = Some(cfg.into());
self
}

/// Configuration for the child process’s standard output (stdout) handle.
///
/// See [`std::process::Command::stdout`].
pub fn stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
self.stdout = Some(cfg.into());
self
}

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

Expand Down Expand Up @@ -105,6 +143,8 @@ impl Command {
}

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

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

/// Run the command but do not check its exit status.
/// Only use if you explicitly don't care about the exit status.
/// Prefer to use [`Self::run`] and [`Self::run_fail`]
/// whenever possible.
/// Run the command but do not check its exit status. Only use if you explicitly don't care
/// about the exit status.
///
/// Prefer to use [`Self::run`] and [`Self::run_fail`] whenever possible.
#[track_caller]
pub fn run_unchecked(&mut self) -> CompletedProcess {
self.command_output()
Expand All @@ -137,11 +179,11 @@ impl Command {
fn command_output(&mut self) -> CompletedProcess {
self.drop_bomb.defuse();
// let's make sure we piped all the input and outputs
self.cmd.stdin(Stdio::piped());
self.cmd.stdout(Stdio::piped());
self.cmd.stderr(Stdio::piped());
self.cmd.stdin(self.stdin.take().unwrap_or(Stdio::piped()));
self.cmd.stdout(self.stdout.take().unwrap_or(Stdio::piped()));
self.cmd.stderr(self.stderr.take().unwrap_or(Stdio::piped()));

let output = if let Some(input) = &self.stdin {
let output = if let Some(input) = &self.stdin_buf {
let mut child = self.cmd.spawn().unwrap();

{
Expand Down
7 changes: 4 additions & 3 deletions src/tools/run-make-support/src/external_deps/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,10 @@ impl LlvmFilecheck {
Self { cmd }
}

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

Expand Down
6 changes: 3 additions & 3 deletions src/tools/run-make-support/src/external_deps/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ impl Rustc {
self
}

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

Expand Down
6 changes: 3 additions & 3 deletions src/tools/run-make-support/src/external_deps/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ impl Rustdoc {
self
}

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

Expand Down
2 changes: 2 additions & 0 deletions src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ pub mod rfs {
}

// Re-exports of third-party library crates.
// tidy-alphabetical-start
pub use bstr;
pub use gimli;
pub use libc;
pub use object;
pub use regex;
pub use serde_json;
pub use wasmparser;
// tidy-alphabetical-end

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

0 comments on commit 2031eac

Please sign in to comment.