Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/adopt/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ rust_test(
"src/build_output.rs",
"src/defaults.rs",
"src/dependency_sets.rs",
"src/dependency_sets_tests.rs",
"src/error.rs",
"src/families.rs",
"src/git_changes.rs",
Expand Down
1,997 changes: 2 additions & 1,995 deletions cli/adopt/src/dependency_sets.rs

Large diffs are not rendered by default.

1,991 changes: 1,991 additions & 0 deletions cli/adopt/src/dependency_sets_tests.rs

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions cli/cli/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ rust_library(
"src/exec/sync_prune.rs",
"src/exec/test_filter.rs",
"src/exec/test_reports.rs",
"src/exec/test_reports/account.rs",
"src/exec/test_reports/collect.rs",
"src/exec/test_reports/report.rs",
"src/exec/umbrella.rs",
"src/exec/update.rs",
"src/exec/update_add_missing.rs",
Expand Down Expand Up @@ -258,6 +261,7 @@ rust_test(
"src/exec.rs",
"src/exec/affected.rs",
"src/exec/audit.rs",
"src/exec/audit_tests.rs",
"src/exec/audit_accept.rs",
"src/exec/audit_consumer.rs",
"src/exec/audit_cyclonedx.rs",
Expand All @@ -278,6 +282,7 @@ rust_test(
"src/exec/changed_coverage_tests.rs",
"src/exec/clean.rs",
"src/exec/common.rs",
"src/exec/common_tests.rs",
"src/exec/compiler_stderr.rs",
"src/exec/coverage_candidate.rs",
"src/exec/current_dir_guard_tests.rs",
Expand Down Expand Up @@ -335,20 +340,27 @@ rust_test(
"src/exec/test_case_launcher_tests.rs",
"src/exec/test_filter.rs",
"src/exec/test_reports.rs",
"src/exec/test_reports/account.rs",
"src/exec/test_reports/collect.rs",
"src/exec/test_reports/report.rs",
"src/exec/test_reports_tests.rs",
"src/exec/test_support.rs",
"src/exec/text_output_tests.rs",
"src/exec/umbrella.rs",
"src/exec/umbrella_tests.rs",
"src/exec/update.rs",
"src/exec/update_add_missing.rs",
"src/exec/update_advisory.rs",
"src/exec/update_check.rs",
"src/exec/update_consumer.rs",
"src/exec/update_consumer_tests.rs",
"src/exec/update_live.rs",
"src/exec/update_outdated.rs",
"src/exec/update_pr.rs",
"src/exec/update_prune.rs",
"src/exec/update_unused.rs",
"src/exec/update_version.rs",
"src/exec/update_version_tests.rs",
"src/exec/update_version_add.rs",
"src/exec/update_version_add_tests.rs",
"src/exec/workflow.rs",
Expand Down
115 changes: 2 additions & 113 deletions cli/cli/src/exec/audit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,119 +28,8 @@ fn today_utc_from(pinned: Option<String>) -> String {
}

#[cfg(test)]
mod tests {
use super::{
AUDIT_TODAY_ENV, QueryBazel, parse_locked_for_configured, today_utc_from, uncovered_message,
};
use dx_adopt::dependency_sets::{Ecosystem, ResolvedSet};
use dx_plan::resolve::{QueryResult, QueryRunner};
use dx_tool_resolve::Bazel as _;
use std::cell::RefCell;
use std::path::Path;

fn unreadable_lock_error(ecosystem: Ecosystem, lock: &str) -> String {
let set = ResolvedSet {
name: "deps".to_owned(),
ecosystem,
dir: String::new(),
manifests: Vec::new(),
locks: vec![lock.to_owned()],
repository: None,
};
let locks = vec![(lock.to_owned(), "{}".to_owned())];
parse_locked_for_configured(&set, &locks)
.err()
.map(|error| error.to_string())
.unwrap_or_default()
}

#[test]
fn uncovered_message_names_the_set_and_ecosystem() {
let uv = uncovered_message("tools", "uv");
assert!(uv.contains("tools") && uv.contains("(uv)"), "{uv}");
assert!(!uv.contains("PowerShell"), "{uv}");
let shell = uncovered_message("powershell", "powershell");
assert_eq!(
shell,
"failed to assess powershell: no advisory backend for PowerShell Gallery modules; NuGet.org advisories do not apply; accept it with an [[uncovered]] entry in security.toml"
);
}

#[test]
fn uncovered_message_names_luarocks() {
assert_eq!(
uncovered_message("rocks", "luarocks"),
"failed to assess rocks: no advisory backend for LuaRocks rocks; OSV defines no LuaRocks ecosystem (https://ossf.github.io/osv-schema/); accept it with an [[uncovered]] entry in security.toml"
);
}

#[test]
fn set_without_a_readable_lock_fails() {
let cargo = unreadable_lock_error(Ecosystem::Cargo, "cargo-bazel-lock.json");
assert!(
cargo.contains("deps") && cargo.contains("Cargo.lock"),
"{cargo}"
);
let nuget = unreadable_lock_error(Ecosystem::Nuget, "other.lock");
assert!(nuget.contains("paket.lock"), "{nuget}");
let maven = unreadable_lock_error(Ecosystem::Maven, "pins.txt");
assert!(maven.contains(".json"), "{maven}");
}

struct Recorder(RefCell<Vec<Vec<String>>>);

impl QueryRunner for Recorder {
fn run_query(&self, argv: &[String], _cwd: &Path) -> std::io::Result<QueryResult> {
self.0.borrow_mut().push(argv.to_vec());
Ok(QueryResult {
code: Some(0),
stdout: Vec::new(),
stderr: Vec::new(),
})
}
}

#[test]
fn the_tool_cquery_carries_the_shared_settings_and_ends_with_the_label() {
let recorder = Recorder(RefCell::new(Vec::new()));
let bazel = QueryBazel {
query_runner: &recorder,
workspace: Path::new("."),
startup: &[],
};
bazel.cquery_files("//t:tool", &[]).expect("cquery");
let calls = recorder.0.borrow();
let argv = &calls[0];
for setting in dx_plan::plan::shared_settings("cquery") {
assert!(argv.contains(&setting), "{setting}");
}
assert_eq!(argv.last().map(String::as_str), Some("//t:tool"));
}

#[test]
fn a_pinned_date_makes_the_audit_date_reproducible() {
assert_eq!(today_utc_from(Some("2020-01-02".to_owned())), "2020-01-02");
assert_eq!(AUDIT_TODAY_ENV, "DX_AUDIT_TODAY");
}

#[test]
fn an_unusable_pin_falls_back_to_today() {
for bad in [
"",
"2020-1-2",
"2020-01-02T00:00",
"not-a-date",
"2020/01/02x",
] {
let today = today_utc_from(None);
assert_eq!(
today_utc_from(Some(bad.to_owned())),
today,
"{bad:?} is not a pinned date"
);
}
}
}
#[path = "audit_tests.rs"]
mod tests;

#[derive(Debug, thiserror::Error)]
pub(crate) enum AuditError {
Expand Down
111 changes: 111 additions & 0 deletions cli/cli/src/exec/audit_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use super::{
AUDIT_TODAY_ENV, QueryBazel, parse_locked_for_configured, today_utc_from, uncovered_message,
};
use dx_adopt::dependency_sets::{Ecosystem, ResolvedSet};
use dx_plan::resolve::{QueryResult, QueryRunner};
use dx_tool_resolve::Bazel as _;
use std::cell::RefCell;
use std::path::Path;

fn unreadable_lock_error(ecosystem: Ecosystem, lock: &str) -> String {
let set = ResolvedSet {
name: "deps".to_owned(),
ecosystem,
dir: String::new(),
manifests: Vec::new(),
locks: vec![lock.to_owned()],
repository: None,
};
let locks = vec![(lock.to_owned(), "{}".to_owned())];
parse_locked_for_configured(&set, &locks)
.err()
.map(|error| error.to_string())
.unwrap_or_default()
}

#[test]
fn uncovered_message_names_the_set_and_ecosystem() {
let uv = uncovered_message("tools", "uv");
assert!(uv.contains("tools") && uv.contains("(uv)"), "{uv}");
assert!(!uv.contains("PowerShell"), "{uv}");
let shell = uncovered_message("powershell", "powershell");
assert_eq!(
shell,
"failed to assess powershell: no advisory backend for PowerShell Gallery modules; NuGet.org advisories do not apply; accept it with an [[uncovered]] entry in security.toml"
);
}

#[test]
fn uncovered_message_names_luarocks() {
assert_eq!(
uncovered_message("rocks", "luarocks"),
"failed to assess rocks: no advisory backend for LuaRocks rocks; OSV defines no LuaRocks ecosystem (https://ossf.github.io/osv-schema/); accept it with an [[uncovered]] entry in security.toml"
);
}

#[test]
fn set_without_a_readable_lock_fails() {
let cargo = unreadable_lock_error(Ecosystem::Cargo, "cargo-bazel-lock.json");
assert!(
cargo.contains("deps") && cargo.contains("Cargo.lock"),
"{cargo}"
);
let nuget = unreadable_lock_error(Ecosystem::Nuget, "other.lock");
assert!(nuget.contains("paket.lock"), "{nuget}");
let maven = unreadable_lock_error(Ecosystem::Maven, "pins.txt");
assert!(maven.contains(".json"), "{maven}");
}

struct Recorder(RefCell<Vec<Vec<String>>>);

impl QueryRunner for Recorder {
fn run_query(&self, argv: &[String], _cwd: &Path) -> std::io::Result<QueryResult> {
self.0.borrow_mut().push(argv.to_vec());
Ok(QueryResult {
code: Some(0),
stdout: Vec::new(),
stderr: Vec::new(),
})
}
}

#[test]
fn the_tool_cquery_carries_the_shared_settings_and_ends_with_the_label() {
let recorder = Recorder(RefCell::new(Vec::new()));
let bazel = QueryBazel {
query_runner: &recorder,
workspace: Path::new("."),
startup: &[],
};
bazel.cquery_files("//t:tool", &[]).expect("cquery");
let calls = recorder.0.borrow();
let argv = &calls[0];
for setting in dx_plan::plan::shared_settings("cquery") {
assert!(argv.contains(&setting), "{setting}");
}
assert_eq!(argv.last().map(String::as_str), Some("//t:tool"));
}

#[test]
fn a_pinned_date_makes_the_audit_date_reproducible() {
assert_eq!(today_utc_from(Some("2020-01-02".to_owned())), "2020-01-02");
assert_eq!(AUDIT_TODAY_ENV, "DX_AUDIT_TODAY");
}

#[test]
fn an_unusable_pin_falls_back_to_today() {
for bad in [
"",
"2020-1-2",
"2020-01-02T00:00",
"not-a-date",
"2020/01/02x",
] {
let today = today_utc_from(None);
assert_eq!(
today_utc_from(Some(bad.to_owned())),
today,
"{bad:?} is not a pinned date"
);
}
}
Loading
Loading