forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#122460 - jieyouxu:rmake-example-refactor, r…
…=Nilstrieb Rework rmake support library API ### Take 1: Strongly-typed API Context: rust-lang#122448 (comment) > My 2 cents: from my experience with writing similar "test DSLs", I would suggest to create these helpers as soon as possible in the process (basically the first time someone needs them, not only after N similar usages), and basically treat any imperative code in these high-level tests as a maintenance burden, basically making them as declarative as possible. Otherwise it might be a bit annoying to keep refactoring the tests later once such helpers are available. > > I would even discourage the arg method and create explicit methods for setting things like unpretty, the output file etc., but this might be more controversial, as it will make the invoked command-line arguments more opaque. cc ``@Kobzol`` for the testing DSL suggestion. Example: ```rs let output = Rustc::new() .input_file("main.rs") .emit(&[EmitKind::Metadata]) .extern_("stable", &stable_path) .output(); ``` ### Take 2: xshell-based macro API Example: ```rs let sh = Shell::new()?; let stable_path = stable_path.to_string_lossy(); let output = cmd!(sh, "rustc main.rs --emit=metadata --extern stable={stable_path}").output()?; ``` ### Take 3: Weakly-typed API with a few helper methods ```rs let output = Rustc::new() .input("main.rs") .emit("metadata") .extern_("stable", &stable_path) .output(); ```
- Loading branch information
Showing
15 changed files
with
240 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use std::env; | ||
use std::path::{Path, PathBuf}; | ||
use std::process::{Command, Output}; | ||
|
||
use super::handle_failed_output; | ||
|
||
fn run_common(bin_name: &str) -> (Command, Output) { | ||
let target = env::var("TARGET").unwrap(); | ||
|
||
let bin_name = | ||
if target.contains("windows") { format!("{}.exe", bin_name) } else { bin_name.to_owned() }; | ||
|
||
let mut bin_path = PathBuf::new(); | ||
bin_path.push(env::var("TMPDIR").unwrap()); | ||
bin_path.push(&bin_name); | ||
let ld_lib_path_envvar = env::var("LD_LIB_PATH_ENVVAR").unwrap(); | ||
let mut cmd = Command::new(bin_path); | ||
cmd.env(&ld_lib_path_envvar, { | ||
let mut paths = vec![]; | ||
paths.push(PathBuf::from(env::var("TMPDIR").unwrap())); | ||
for p in env::split_paths(&env::var("TARGET_RPATH_ENV").unwrap()) { | ||
paths.push(p.to_path_buf()); | ||
} | ||
for p in env::split_paths(&env::var(&ld_lib_path_envvar).unwrap()) { | ||
paths.push(p.to_path_buf()); | ||
} | ||
env::join_paths(paths.iter()).unwrap() | ||
}); | ||
|
||
if target.contains("windows") { | ||
let mut paths = vec![]; | ||
for p in env::split_paths(&std::env::var("PATH").unwrap_or(String::new())) { | ||
paths.push(p.to_path_buf()); | ||
} | ||
paths.push(Path::new(&std::env::var("TARGET_RPATH_DIR").unwrap()).to_path_buf()); | ||
cmd.env("PATH", env::join_paths(paths.iter()).unwrap()); | ||
} | ||
|
||
let output = cmd.output().unwrap(); | ||
(cmd, output) | ||
} | ||
|
||
/// Run a built binary and make sure it succeeds. | ||
#[track_caller] | ||
pub fn run(bin_name: &str) -> Output { | ||
let caller_location = std::panic::Location::caller(); | ||
let caller_line_number = caller_location.line(); | ||
|
||
let (cmd, output) = run_common(bin_name); | ||
if !output.status.success() { | ||
handle_failed_output(&format!("{:#?}", cmd), output, caller_line_number); | ||
} | ||
output | ||
} | ||
|
||
/// Run a built binary and make sure it fails. | ||
#[track_caller] | ||
pub fn run_fail(bin_name: &str) -> Output { | ||
let caller_location = std::panic::Location::caller(); | ||
let caller_line_number = caller_location.line(); | ||
|
||
let (cmd, output) = run_common(bin_name); | ||
if output.status.success() { | ||
handle_failed_output(&format!("{:#?}", cmd), output, caller_line_number); | ||
} | ||
output | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.