Skip to content

Commit 60c73ec

Browse files
committed
run-make-support: introduce macro for common methods to avoid repetition
Add a helper macro for adding common methods to command wrappers. Common methods include helpers that delegate to `Command` and running methods. - `arg` and `args` (delegates to `Command`) - `env`, `env_remove` and `env_clear` (delegates to `Command`) - `output`, `run` and `run_fail` This helps to avoid needing to copy-pasta / reimplement these common methods on a new command wrapper, which hopefully reduces the friction for run-make test writers wanting to introduce new command wrappers.
1 parent 1a99015 commit 60c73ec

File tree

1 file changed

+96
-0
lines changed
  • src/tools/run-make-support/src

1 file changed

+96
-0
lines changed

src/tools/run-make-support/src/lib.rs

+96
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,99 @@ fn handle_failed_output(cmd: &Command, output: Output, caller_line_number: u32)
115115
eprintln!("=== STDERR ===\n{}\n\n", String::from_utf8(output.stderr).unwrap());
116116
std::process::exit(1)
117117
}
118+
119+
macro_rules! impl_common_helpers {
120+
($wrapper: ident) => {
121+
impl $wrapper {
122+
/// Specify an environment variable.
123+
pub fn env<K, V>(&mut self, key: K, value: V) -> &mut Self
124+
where
125+
K: AsRef<::std::ffi::OsStr>,
126+
V: AsRef<::std::ffi::OsStr>,
127+
{
128+
self.cmd.env(key, value);
129+
self
130+
}
131+
132+
/// Remove an environmental variable.
133+
pub fn env_remove<K>(&mut self, key: K) -> &mut Self
134+
where
135+
K: AsRef<::std::ffi::OsStr>,
136+
{
137+
self.cmd.env_remove(key);
138+
self
139+
}
140+
141+
/// Clear all environmental variables.
142+
pub fn env_var(&mut self) -> &mut Self {
143+
self.cmd.env_clear();
144+
self
145+
}
146+
147+
/// Generic command argument provider. Prefer specific helper methods if possible.
148+
/// Note that for some executables, arguments might be platform specific. For C/C++
149+
/// compilers, arguments might be platform *and* compiler specific.
150+
pub fn arg<S>(&mut self, arg: S) -> &mut Self
151+
where
152+
S: AsRef<::std::ffi::OsStr>,
153+
{
154+
self.cmd.arg(arg);
155+
self
156+
}
157+
158+
/// Generic command arguments provider. Prefer specific helper methods if possible.
159+
/// Note that for some executables, arguments might be platform specific. For C/C++
160+
/// compilers, arguments might be platform *and* compiler specific.
161+
pub fn args<S>(&mut self, args: &[S]) -> &mut Self
162+
where
163+
S: AsRef<::std::ffi::OsStr>,
164+
{
165+
self.cmd.args(args);
166+
self
167+
}
168+
169+
/// Inspect what the underlying [`Command`][::std::process::Command] is up to the
170+
/// current construction.
171+
pub fn inspect<I>(&mut self, inspector: I) -> &mut Self
172+
where
173+
I: FnOnce(&::std::process::Command),
174+
{
175+
inspector(&self.cmd);
176+
self
177+
}
178+
179+
/// Get the [`Output`][::std::process::Output] of the finished process.
180+
pub fn output(&mut self) -> ::std::process::Output {
181+
self.cmd.output().expect("failed to get output of finished process")
182+
}
183+
184+
/// Run the constructed command and assert that it is successfully run.
185+
#[track_caller]
186+
pub fn run(&mut self) -> ::std::process::Output {
187+
let caller_location = ::std::panic::Location::caller();
188+
let caller_line_number = caller_location.line();
189+
190+
let output = self.cmd.output().unwrap();
191+
if !output.status.success() {
192+
handle_failed_output(&self.cmd, output, caller_line_number);
193+
}
194+
output
195+
}
196+
197+
/// Run the constructed command and assert that it does not successfully run.
198+
#[track_caller]
199+
pub fn run_fail(&mut self) -> ::std::process::Output {
200+
let caller_location = ::std::panic::Location::caller();
201+
let caller_line_number = caller_location.line();
202+
203+
let output = self.cmd.output().unwrap();
204+
if output.status.success() {
205+
handle_failed_output(&self.cmd, output, caller_line_number);
206+
}
207+
output
208+
}
209+
}
210+
};
211+
}
212+
213+
pub(crate) use impl_common_helpers;

0 commit comments

Comments
 (0)