|
| 1 | +use std::path::PathBuf; |
| 2 | +use std::process::Command; |
| 3 | +use std::sync::Once; |
| 4 | + |
| 5 | +static BUILD_PRQLC: Once = Once::new(); |
| 6 | + |
| 7 | +/// Return a `Command` that runs the `prqlc` binary with color/backtrace |
| 8 | +/// stripped so snapshot tests are deterministic. |
| 9 | +/// |
| 10 | +/// When `CARGO_BIN_EXE_prqlc` is set (integration tests), it uses that path. |
| 11 | +/// Otherwise it locates the binary relative to the test executable and builds |
| 12 | +/// it if necessary — `cargo test` for a bin-only crate does not produce the |
| 13 | +/// non-test binary automatically. |
| 14 | +pub fn prqlc_command() -> Command { |
| 15 | + let bin = prqlc_bin_path(); |
| 16 | + let mut cmd = Command::new(bin); |
| 17 | + normalize_prqlc(&mut cmd); |
| 18 | + cmd |
| 19 | +} |
| 20 | + |
| 21 | +fn prqlc_bin_path() -> PathBuf { |
| 22 | + if let Some(bin) = std::env::var_os("CARGO_BIN_EXE_prqlc") { |
| 23 | + return PathBuf::from(bin); |
| 24 | + } |
| 25 | + |
| 26 | + // Locate the target directory from the test binary path. |
| 27 | + let test_bin = std::env::current_exe().expect("cannot determine test binary path"); |
| 28 | + let mut dir = test_bin.parent().unwrap().to_path_buf(); |
| 29 | + if dir.ends_with("deps") { |
| 30 | + dir.pop(); |
| 31 | + } |
| 32 | + |
| 33 | + let bin_name = if cfg!(windows) { "prqlc.exe" } else { "prqlc" }; |
| 34 | + let bin_path = dir.join(bin_name); |
| 35 | + |
| 36 | + // `cargo test` for a [[bin]]-only crate builds the test harness (in deps/) |
| 37 | + // but does NOT build the actual binary. Build it on demand if missing. |
| 38 | + if !bin_path.exists() { |
| 39 | + BUILD_PRQLC.call_once(|| { |
| 40 | + let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string()); |
| 41 | + let status = Command::new(cargo) |
| 42 | + .args(["build", "--bin", "prqlc"]) |
| 43 | + .status() |
| 44 | + .expect("failed to run `cargo build --bin prqlc`"); |
| 45 | + assert!(status.success(), "failed to build prqlc binary"); |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + bin_path |
| 50 | +} |
| 51 | + |
| 52 | +fn normalize_prqlc(cmd: &mut Command) -> &mut Command { |
| 53 | + cmd |
| 54 | + // We set `CLICOLOR_FORCE` in CI to force color output, but we don't want `prqlc` to |
| 55 | + // output color for our snapshot tests. And it seems to override the |
| 56 | + // `--color=never` flag. |
| 57 | + .env_remove("CLICOLOR_FORCE") |
| 58 | + .env("NO_COLOR", "1") |
| 59 | + .args(["--color=never"]) |
| 60 | + // We don't want the tests to be affected by the user's `RUST_BACKTRACE` setting. |
| 61 | + .env_remove("RUST_BACKTRACE") |
| 62 | + .env_remove("RUST_LOG") |
| 63 | +} |
0 commit comments