Skip to content

Commit

Permalink
Add unit tests for cargo-auditable.
Browse files Browse the repository at this point in the history
  • Loading branch information
duckinator committed Nov 4, 2024
1 parent c0759ff commit aa196ca
Showing 1 changed file with 104 additions and 25 deletions.
129 changes: 104 additions & 25 deletions cargo-dist/src/build/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,33 +156,15 @@ impl<'a> DistGraphBuilder<'a> {
}
}

/// Build a cargo target
pub fn build_cargo_target(
dist_graph: &DistGraph,
manifest: &mut DistManifest,
fn make_build_cargo_target_command(
cargo_cmd: &String,
rustflags: &String,
target: &CargoBuildStep,
) -> DistResult<()> {
let cargo = dist_graph.tools.cargo()?;

eprint!(
"building cargo target ({}/{}",
target.target_triple, target.profile
);

let mut rustflags = target.rustflags.clone();
let mut desired_extra_env = vec![];
let skip_brewfile = env::var("DO_NOT_USE_BREWFILE").is_ok();
if !skip_brewfile {
if let Some(env_output) = fetch_brew_env(dist_graph, &target.working_dir)? {
let brew_env = parse_env(&env_output)?;
desired_extra_env = select_brew_env(&brew_env);
rustflags = determine_brew_rustflags(&rustflags, &brew_env);
}
}

let mut command = Cmd::new(&cargo.cmd, "build your app with Cargo");
auditable: bool
) -> Cmd {
let mut command = Cmd::new(cargo_cmd, "build your app with Cargo");

if dist_graph.config.builds.cargo.cargo_auditable {
if auditable {
eprint!(" auditable");
command.arg("auditable");
}
Expand Down Expand Up @@ -225,6 +207,37 @@ pub fn build_cargo_target(
eprintln!(" --package={})", package);
}
}

command
}

/// Build a cargo target
pub fn build_cargo_target(
dist_graph: &DistGraph,
manifest: &mut DistManifest,
target: &CargoBuildStep,
) -> DistResult<()> {
let cargo = dist_graph.tools.cargo()?;

eprint!(
"building cargo target ({}/{}",
target.target_triple, target.profile
);

let mut rustflags = target.rustflags.clone();
let mut desired_extra_env = vec![];
let skip_brewfile = env::var("DO_NOT_USE_BREWFILE").is_ok();
if !skip_brewfile {
if let Some(env_output) = fetch_brew_env(dist_graph, &target.working_dir)? {
let brew_env = parse_env(&env_output)?;
desired_extra_env = select_brew_env(&brew_env);
rustflags = determine_brew_rustflags(&rustflags, &brew_env);
}
}

let auditable = dist_graph.config.builds.cargo.cargo_auditable;
let mut command = make_build_cargo_target_command(&cargo.cmd, &rustflags, &target, auditable);

Check failure on line 239 in cargo-dist/src/build/cargo.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

error: this expression creates a reference which is immediately dereferenced by the compiler --> cargo-dist/src/build/cargo.rs:239:79 | 239 | let mut command = make_build_cargo_target_command(&cargo.cmd, &rustflags, &target, auditable); | ^^^^^^^ help: change this to: `target` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `-D clippy::needless-borrow` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::needless_borrow)]`

Check failure on line 239 in cargo-dist/src/build/cargo.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

error: this expression creates a reference which is immediately dereferenced by the compiler --> cargo-dist/src/build/cargo.rs:239:79 | 239 | let mut command = make_build_cargo_target_command(&cargo.cmd, &rustflags, &target, auditable); | ^^^^^^^ help: change this to: `target` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `-D clippy::needless-borrow` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::needless_borrow)]`

// If we generated any extra environment variables to
// inject into the environment, apply them now.
command.envs(desired_extra_env);
Expand Down Expand Up @@ -280,3 +293,69 @@ pub fn rustup_toolchain(dist_graph: &DistGraph, cmd: &RustupStep) -> DistResult<
fn determine_brew_rustflags(base_rustflags: &str, environment: &SortedMap<&str, &str>) -> String {
format!("{base_rustflags} {}", calculate_ldflags(environment))
}

#[cfg(test)]
mod tests {
use crate::{CargoBuildStep, TargetTriple};
use crate::tasks::{CargoTargetFeatures, CargoTargetFeatureList, CargoTargetPackages};
use super::make_build_cargo_target_command;

#[test]
fn build_command_not_auditable() {
let cargo_cmd = "cargo".to_string();
let rustflags = "--some-rust-flag".to_string();
let auditable = false;

let features = CargoTargetFeatures {
default_features: true,
features: CargoTargetFeatureList::default(),
};
let target = CargoBuildStep {
expected_binaries: vec![],
features: features,

Check failure on line 315 in cargo-dist/src/build/cargo.rs

View workflow job for this annotation

GitHub Actions / clippy

redundant field names in struct initialization

error: redundant field names in struct initialization --> cargo-dist/src/build/cargo.rs:315:13 | 315 | features: features, | ^^^^^^^^^^^^^^^^^^ help: replace it with: `features` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names = note: `-D clippy::redundant-field-names` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::redundant_field_names)]`
package: CargoTargetPackages::Workspace,
profile: "release".to_string(),
rustflags: "--this-rust-flag-gets-ignored".to_string(),
target_triple: TargetTriple::new("x86_64-unknown-linux-gnu".to_string()),
working_dir: ".".to_string().into(), // this feels mildly cursed -duckinator.
};

let cmd = make_build_cargo_target_command(&cargo_cmd, &rustflags, &target, auditable);

let mut args = cmd.inner.get_args();

let arg1 = args.next().unwrap().to_str().unwrap();
assert_eq!(arg1, "build");
}

#[test]
fn build_command_auditable() {
let cargo_cmd = "cargo".to_string();
let rustflags = "--some-rust-flag".to_string();
let auditable = true;

let features = CargoTargetFeatures {
default_features: true,
features: CargoTargetFeatureList::default(),
};
let target = CargoBuildStep {
expected_binaries: vec![],
features: features,

Check failure on line 343 in cargo-dist/src/build/cargo.rs

View workflow job for this annotation

GitHub Actions / clippy

redundant field names in struct initialization

error: redundant field names in struct initialization --> cargo-dist/src/build/cargo.rs:343:13 | 343 | features: features, | ^^^^^^^^^^^^^^^^^^ help: replace it with: `features` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names
package: CargoTargetPackages::Workspace,
profile: "release".to_string(),
rustflags: "--this-rust-flag-gets-ignored".to_string(),
target_triple: TargetTriple::new("x86_64-unknown-linux-gnu".to_string()),
working_dir: ".".to_string().into(), // this feels mildly cursed -duckinator.
};

let cmd = make_build_cargo_target_command(&cargo_cmd, &rustflags, &target, auditable);

let mut args = cmd.inner.get_args();

let arg1 = args.next().unwrap().to_str().unwrap();
assert_eq!(arg1, "auditable");

let arg2 = args.next().unwrap().to_str().unwrap();
assert_eq!(arg2, "build");
}
}

0 comments on commit aa196ca

Please sign in to comment.