diff --git a/.github/scripts/tests.sh b/.github/scripts/tests.sh index 39ef1afa..f2a4453e 100755 --- a/.github/scripts/tests.sh +++ b/.github/scripts/tests.sh @@ -3,17 +3,25 @@ set -euxo pipefail unset CARGO_INSTALL_ROOT -unset CARGO_HOME -# Install binaries using cargo-binstall -# shellcheck disable=SC2086 -"./$1" binstall --log-level debug --no-confirm \ - b3sum \ - cargo-release \ - cargo-binstall \ - cargo-watch \ - miniserve \ - sccache +crates="b3sum cargo-release cargo-binstall cargo-watch miniserve sccache" + +if [ "$2" = "Windows" ]; then + # Install binaries using cargo-binstall + # shellcheck disable=SC2086 + "./$1" --log-level debug --no-confirm $crates +else + export CARGO_HOME=/tmp/cargo-home-for-test + export PATH="$CARGO_HOME/bin:$PATH" + + mkdir -p "$CARGO_HOME/bin" + # Copy it to bin to test use of env var `CARGO` + cp "./$1" "$CARGO_HOME/bin/cargo-binstall" + + # Install binaries using cargo-binstall + # shellcheck disable=SC2086 + cargo binstall --log-level debug --no-confirm $crates +fi # Test that the installed binaries can be run b3sum --version @@ -69,7 +77,7 @@ cargo binstall --help >/dev/null # to force failure if falling back to source # FIXME: remove/replace once #136 lands -PATH="$test_resources/fake-cargo:$PATH" +export PATH="$test_resources/fake-cargo:$PATH" # Test default GitLab pkg-url templates "./$1" binstall \ diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 401e9868..a5740149 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -51,6 +51,4 @@ jobs: - name: Test shell: bash run: .github/scripts/tests.sh ${{ matrix.bin }} ${{ runner.os }} - env: - CARGO_HOME: /tmp/cargo-home-for-test/ diff --git a/crates/binstalk/src/ops/install.rs b/crates/binstalk/src/ops/install.rs index 8b9cb3de..973cd655 100644 --- a/crates/binstalk/src/ops/install.rs +++ b/crates/binstalk/src/ops/install.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::{borrow::Cow, env, ffi::OsStr, sync::Arc}; use cargo_toml::Package; use compact_str::CompactString; @@ -110,12 +110,18 @@ async fn install_from_source( ) -> Result<(), BinstallError> { let jobserver_client = lazy_jobserver_client.get().await?; + let cargo = env::var_os("CARGO") + .map(Cow::Owned) + .unwrap_or_else(|| Cow::Borrowed(OsStr::new("cargo"))); + debug!( - "Running `cargo install {} --version {} --target {target}`", - package.name, package.version + "Running `{} install {} --version {} --target {target}`", + cargo.to_string_lossy(), + package.name, + package.version ); - let mut cmd = Command::new("cargo"); + let mut cmd = Command::new(cargo); cmd.arg("install") .arg(&package.name) diff --git a/crates/detect-targets/src/detect.rs b/crates/detect-targets/src/detect.rs index da023365..6200e425 100644 --- a/crates/detect-targets/src/detect.rs +++ b/crates/detect-targets/src/detect.rs @@ -1,4 +1,7 @@ use std::{ + borrow::Cow, + env, + ffi::OsStr, io::{BufRead, Cursor}, process::{Output, Stdio}, }; @@ -65,8 +68,17 @@ pub async fn detect_targets() -> Vec { /// Figure out what the host target is using `rustc`. /// If `rustc` is absent, then it would return `None`. +/// +/// If environment variable `CARGO` is present, then +/// `$CARGO -vV` will be run instead. +/// +/// Otherwise, it will run `rustc -vV` to detect target. async fn get_target_from_rustc() -> Option { - let Output { status, stdout, .. } = Command::new("rustc") + let cmd = env::var_os("CARGO") + .map(Cow::Owned) + .unwrap_or_else(|| Cow::Borrowed(OsStr::new("rustc"))); + + let Output { status, stdout, .. } = Command::new(cmd) .arg("-vV") .stdin(Stdio::null()) .stdout(Stdio::piped()) diff --git a/crates/detect-targets/src/lib.rs b/crates/detect-targets/src/lib.rs index 2e74eb17..a9a5c5c4 100644 --- a/crates/detect-targets/src/lib.rs +++ b/crates/detect-targets/src/lib.rs @@ -1,5 +1,11 @@ //! Detect the target at the runtime. //! +//! It runs `$CARGO -vV` if environment variable `CARGO` is present +//! for cargo subcommands, otherwise it would try running `rustc -vV`. +//! +//! If both `rustc` isn't present on the system, it will fallback +//! to using syscalls plus `ldd` on Linux to detect targets. +//! //! Example use cases: //! - The binary is built with musl libc to run on anywhere, but //! the runtime supports glibc.