Skip to content

Commit

Permalink
Add BUILD_WRAP_ALLOW environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed May 6, 2024
1 parent aa9d59d commit e432ead
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Installing `build-wrap` requires two steps:

## Environment variables that `build-wrap` reads

- `BUILD_WRAP_ALLOW`: When set to a value other than `0`, `build-wrap` uses the following weakened strategy. If a running a build script under `BUILD_WRAP_CMD` fails, report the failure and rerun the build script normally.

- `BUILD_WRAP_CMD`: Command used to execute a build script. Linux default:

```sh
Expand Down
23 changes: 20 additions & 3 deletions src/util/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ pub fn exec_forwarding_output(mut command: Command, failure_is_error: bool) -> R
std::io::stdout().flush()?;
std::io::stderr().flush()?;

if failure_is_error {
ensure!(output.status.success(), "command failed: {command:?}");
if !output.status.success() {
if failure_is_error {
bail!("command failed: {command:?}");
}
eprintln!("command failed: {command:?}");
}

Ok(output)
Expand Down Expand Up @@ -65,9 +68,19 @@ fn unpack_and_exec(bytes: &[u8]) -> Result<()> {
// They will cause the wrapped build script to be rerun, however.
let expanded_args = split_and_expand(&temp_path)?;

let allow_enabled = enabled("BUILD_WRAP_ALLOW");

let mut command = Command::new(&expanded_args[0]);
command.args(&expanded_args[1..]);
let _: Output = exec_forwarding_output(command, true)?;
let output = exec_forwarding_output(command, !allow_enabled)?;

// smoelius: We should arrive at this `if` with `!output.status.success()` only when
// `BUILD_WRAP_ALLOW` is enabled.
if !output.status.success() {
debug_assert!(allow_enabled);
let command = Command::new(&temp_path);
let _: Output = exec_forwarding_output(command, true)?;
}

drop(temp_path);

Expand Down Expand Up @@ -227,3 +240,7 @@ fn var(key: &str) -> Result<String, env::VarError> {

env::var(key)
}

fn enabled(name: &str) -> bool {
env::var(name).map_or(false, |value| value != "0")
}
20 changes: 20 additions & 0 deletions tests/allow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
pub mod util;

#[test]
fn allow() {
let temp_package = util::temp_package("tests/cases/ping.rs").unwrap();

for allow in [false, true] {
let mut command = util::build_with_build_wrap();
if allow {
command.env("BUILD_WRAP_ALLOW", "1");
}
command.current_dir(&temp_package);

let output = util::exec_forwarding_output(command, false).unwrap();
// smoelius: The command should succeed precisely when `BUILD_SCRIPT_ALLOW` is enabled.
assert_eq!(allow, output.status.success());
let stderr = std::str::from_utf8(&output.stderr).unwrap();
assert!(stderr.contains("command failed"));
}
}

0 comments on commit e432ead

Please sign in to comment.