Skip to content

Commit

Permalink
Make output more deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
Erin van der Veen committed Feb 19, 2024
1 parent c0f676b commit 666a362
Show file tree
Hide file tree
Showing 10 changed files with 28,871 additions and 28,849 deletions.
17 changes: 14 additions & 3 deletions genealogos/src/model/v1_4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,25 @@ impl TryFrom<Model> for cyclonedx::CycloneDx {
type Error = Error;

fn try_from(model: Model) -> Result<Self> {
let components: Vec<cyclonedx::Component> = model
let mut components: Vec<cyclonedx::Component> = model
.components
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<_>>>()?;

let dependencies: Vec<cyclonedx::Dependency> = model
.dependencies
// Sort components by bom_ref for deterministic output if testing or GENEALOGOS_DETERMINISTIC is set
if cfg!(test) || std::env::var("GENEALOGOS_DETERMINISTIC").is_ok() {
components.sort_by(|a, b| a.bom_ref.cmp(&b.bom_ref));
}

// Sort model dependencies by ref for deterministic output if testing or GENEALOGOS_DETERMINISTIC is set
// We need to sort the dependencies before we convert them to cyclonedx::Dependency
let mut dependencies: Vec<ModelDependency> = model.dependencies;
if cfg!(test) || std::env::var("GENEALOGOS_DETERMINISTIC").is_ok() {
dependencies.sort_by(|a, b| a.r#ref.cmp(&b.r#ref));
}

let dependencies: Vec<cyclonedx::Dependency> = dependencies
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<_>>>()?;
Expand Down
17 changes: 14 additions & 3 deletions genealogos/src/model/v1_5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,25 @@ impl TryFrom<Model> for cyclonedx::CycloneDx {
type Error = Error;

fn try_from(model: Model) -> Result<Self> {
let components: Vec<cyclonedx::Component> = model
let mut components: Vec<cyclonedx::Component> = model
.components
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<_>>>()?;

let dependencies: Vec<cyclonedx::Dependency> = model
.dependencies
// Sort components by bom_ref for deterministic output if testing or GENEALOGOS_DETERMINISTIC is set
if cfg!(test) || std::env::var("GENEALOGOS_DETERMINISTIC").is_ok() {
components.sort_by(|a, b| a.bom_ref.cmp(&b.bom_ref));
}

// Sort model dependencies by ref for deterministic output if testing or GENEALOGOS_DETERMINISTIC is set
// We need to sort the dependencies before we convert them to cyclonedx::Dependency
let mut dependencies: Vec<ModelDependency> = model.dependencies;
if cfg!(test) || std::env::var("GENEALOGOS_DETERMINISTIC").is_ok() {
dependencies.sort_by(|a, b| a.r#ref.cmp(&b.r#ref));
}

let dependencies: Vec<cyclonedx::Dependency> = dependencies
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<_>>>()?;
Expand Down
Loading

0 comments on commit 666a362

Please sign in to comment.