Skip to content

Commit

Permalink
add excluded scope tests
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Theil <theil.markus@gmail.com>
  • Loading branch information
thillux committed Jul 15, 2024
1 parent 401638a commit eda8764
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 10 deletions.
9 changes: 5 additions & 4 deletions cargo-cyclonedx/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ pub enum ArgsError {
FilenameOverrideError(#[from] FilenameOverrideError),
}

#[cfg(test)]
pub fn parse_to_config(args: &[&str]) -> SbomConfig {
Args::parse_from(args.iter()).as_config().unwrap()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -228,10 +233,6 @@ mod tests {
assert!(!contains_feature(&config, ""));
}

fn parse_to_config(args: &[&str]) -> SbomConfig {
Args::parse_from(args.iter()).as_config().unwrap()
}

fn contains_feature(config: &SbomConfig, feature: &str) -> bool {
config
.features
Expand Down
1 change: 1 addition & 0 deletions cargo-cyclonedx/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@ fn filtered_dependencies<'a>(
/// * `package_name` - Package from which this SBOM was generated
/// * `sbom_config` - Configuration options used during generation
/// * `target_kinds` - Detailed information on the kinds of targets in `sbom`
#[derive(Debug)]
pub struct GeneratedSbom {
pub bom: Bom,
pub manifest_path: PathBuf,
Expand Down
83 changes: 77 additions & 6 deletions cargo-cyclonedx/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use cargo_cyclonedx::{
config::{SbomConfig, Target},
generator::SbomGenerator,
GeneratedSbom,
};

use std::{
Expand All @@ -65,22 +66,28 @@ use log::LevelFilter;
mod cli;
use cli::{Args, Opts};

fn main() -> anyhow::Result<()> {
let Opts::Bom(args) = Opts::parse();
setup_logging(&args)?;

fn generate_sboms(args: &Args) -> Result<Vec<GeneratedSbom>> {
let cli_config = args.as_config()?;
let manifest_path = locate_manifest(&args)?;
let manifest_path = locate_manifest(args)?;
log::debug!("Found the Cargo.toml file at {}", manifest_path.display());

log::trace!("Running `cargo metadata` started");
let metadata = get_metadata(&args, &manifest_path, &cli_config)?;
let metadata = get_metadata(args, &manifest_path, &cli_config)?;
log::trace!("Running `cargo metadata` finished");

log::trace!("SBOM generation started");
let boms = SbomGenerator::create_sboms(metadata, &cli_config)?;
log::trace!("SBOM generation finished");

Ok(boms)
}

fn main() -> anyhow::Result<()> {
let Opts::Bom(args) = Opts::parse();
setup_logging(&args)?;

let boms = generate_sboms(&args)?;

log::trace!("SBOM output started");
for bom in boms {
bom.write_to_files()?;
Expand Down Expand Up @@ -163,3 +170,67 @@ fn get_metadata(

Ok(cmd.exec()?)
}

#[cfg(test)]
mod tests {
use cyclonedx_bom::prelude::NormalizedString;

#[test]
fn parse_toml_only_normal() {
use crate::cli;
use crate::generate_sboms;
use clap::Parser;
use cyclonedx_bom::models::component::Scope;
use std::path::PathBuf;

let mut test_cargo_toml = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_cargo_toml.push("tests/fixtures/test_crate/Cargo.toml");

let path_arg = &format!("--manifest-path={}", test_cargo_toml.display());
let args = ["cyclonedx", path_arg, "--only-normal-deps"];
let args_parsed = cli::Args::parse_from(args.iter());

let sboms = generate_sboms(&args_parsed).unwrap();

let components = sboms[0].bom.components.as_ref().unwrap();
assert!(components
.0
.iter()
.all(|f| f.scope == Some(Scope::Required)));
}

#[test]
fn parse_toml_with_excluded() {
use crate::cli;
use crate::generate_sboms;
use clap::Parser;
use cyclonedx_bom::models::component::Scope;
use std::path::PathBuf;

let mut test_cargo_toml = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_cargo_toml.push("tests/fixtures/test_crate/Cargo.toml");

let path_arg = &format!("--manifest-path={}", test_cargo_toml.display());
let args = ["cyclonedx", path_arg];
let args_parsed = cli::Args::parse_from(args.iter());

let sboms = generate_sboms(&args_parsed).unwrap();

// zstd is a build dependency -> excluded
// zstd-sys is a dependency of a build dependency -> excluded
// cfg-if is also a normal dependency -> required
let components = sboms[0].bom.components.as_ref().unwrap();
assert!(components
.0
.iter()
.all(|c| c.name != NormalizedString::new("zstd")
|| c.scope == Some(Scope::Excluded)));
assert!(components.0.iter().all(
|c| c.name != NormalizedString::new("zstd-sys") || c.scope == Some(Scope::Excluded)
));
assert!(components
.0
.iter()
.all(|c| c.name != NormalizedString::new("cfg-if") || c.scope == Some(Scope::Required)));
}
}
76 changes: 76 additions & 0 deletions cargo-cyclonedx/tests/fixtures/test_crate/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions cargo-cyclonedx/tests/fixtures/test_crate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "test_crate"
version = "0.1.0"
edition = "2021"

[build-dependencies]
zstd = "0.13"

[dependencies]
cfg-if = "0.1"

[dev-dependencies]
cfg-if = "0.1"

[workspace]
3 changes: 3 additions & 0 deletions cargo-cyclonedx/tests/fixtures/test_crate/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

0 comments on commit eda8764

Please sign in to comment.