From 0b65fa0fd980d3fd814f4faeb07df39b47d96a23 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 25 Jun 2021 19:52:58 +0200 Subject: Add tests for DAG-building with conditional dependency Signed-off-by: Matthias Beyer --- src/commands/tree_of.rs | 1 - src/package/dag.rs | 133 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 131 insertions(+), 3 deletions(-) diff --git a/src/commands/tree_of.rs b/src/commands/tree_of.rs index 09fcabf..811b2b7 100644 --- a/src/commands/tree_of.rs +++ b/src/commands/tree_of.rs @@ -24,7 +24,6 @@ use crate::package::condition::ConditionData; use crate::repository::Repository; use crate::util::EnvironmentVariableName; use crate::util::docker::ImageName; -use crate::util::progress::ProgressBars; /// Implementation of the "tree_of" subcommand pub async fn tree_of( diff --git a/src/package/dag.rs b/src/package/dag.rs index e5dc840..5956c60 100644 --- a/src/package/dag.rs +++ b/src/package/dag.rs @@ -161,11 +161,14 @@ mod tests { use std::collections::BTreeMap; + use crate::package::Dependencies; + use crate::package::Dependency; + use crate::package::condition::Condition; + use crate::package::condition::OneOrMore; use crate::package::tests::package; use crate::package::tests::pname; use crate::package::tests::pversion; - use crate::package::Dependencies; - use crate::package::Dependency; + use crate::util::docker::ImageName; use indicatif::ProgressBar; @@ -588,5 +591,131 @@ mod tests { assert!(ps.iter().any(|p| *p.name() == pname("p3"))); assert!(ps.iter().any(|p| *p.name() == pname("p4"))); } + + + /// Build a repository with two packages and a condition for their dependency + fn repo_with_ab_packages_with_condition(cond: Condition) -> (Package, Repository) { + let mut btree = BTreeMap::new(); + + let mut p1 = { + let name = "a"; + let vers = "1"; + let pack = package(name, vers, "https://rust-lang.org", "123"); + btree.insert((pname(name), pversion(vers)), pack.clone()); + pack + }; + + { + let name = "b"; + let vers = "2"; + let pack = package(name, vers, "https://rust-lang.org", "124"); + btree.insert((pname(name), pversion(vers)), pack); + } + + { + let d = Dependency::new_conditional(String::from("b =2"), cond); + let ds = Dependencies::with_runtime_dependency(d); + p1.set_dependencies(ds); + } + + (p1, Repository::from(btree)) + } + + // Test whether the dependency DAG is correctly build if there is NO conditional data passed + // + // Because the dependency is conditional with "fooimage" required as build-image, the + // dependency DAG should NOT contain package "b" + #[test] + fn test_add_two_dependent_packages_with_image_conditional() { + let condition = { + let in_image = Some(OneOrMore::::One(String::from("fooimage"))); + Condition::new(None, None, in_image) + }; + let (p1, repo) = repo_with_ab_packages_with_condition(condition); + + let condition_data = ConditionData { + image_name: None, + env: &[], + }; + + let progress = ProgressBar::hidden(); + + let dag = Dag::for_root_package(p1, &repo, Some(&progress), &condition_data); + assert!(dag.is_ok()); + let dag = dag.unwrap(); + let ps = dag.all_packages(); + + assert!(ps.iter().any(|p| *p.name() == pname("a"))); + assert!(ps.iter().any(|p| *p.version() == pversion("1"))); + + // Not in the tree: + assert!(!ps.iter().any(|p| *p.name() == pname("b"))); + assert!(!ps.iter().any(|p| *p.version() == pversion("2"))); + } + + // Test whether the dependency DAG is correctly build if a image is used, but not the one + // required + // + // Because the dependency is conditional with "fooimage" required as build-image, but + // "barimage" is used, the dependency DAG should NOT contain package "b" + #[test] + fn test_add_two_dependent_packages_with_image_conditional_but_other_image_provided() { + let condition = { + let in_image = Some(OneOrMore::::One(String::from("fooimage"))); + Condition::new(None, None, in_image) + }; + let (p1, repo) = repo_with_ab_packages_with_condition(condition); + + let img_name = ImageName::from("barimage"); + let condition_data = ConditionData { + image_name: Some(&img_name), + env: &[], + }; + + let progress = ProgressBar::hidden(); + + let dag = Dag::for_root_package(p1, &repo, Some(&progress), &condition_data); + assert!(dag.is_ok()); + let dag = dag.unwrap(); + let ps = dag.all_packages(); + + assert!(ps.iter().any(|p| *p.name() == pname("a"))); + assert!(ps.iter().any(|p| *p.version() == pversion("1"))); + + // Not in the tree: + assert!(!ps.iter().any(|p| *p.name() == pname("b"))); + assert!(!ps.iter().any(|p| *p.version() == pversion("2"))); + } + + // Test whether the dependency DAG is correctly build if the right image name is passed + #[test] + fn test_add_two_dependent_packages_with_image_conditional_and_image_provided() { + let condition = { + let in_image = Some(OneOrMore::::One(String::from("fooimage"))); + Condition::new(None, None, in_image) + }; + let (p1, repo) = repo_with_ab_packages_with_condition(condition); + + let img_name = ImageName::from("fooimage"); + let condition_data = ConditionData { + image_name: Some(&img_name), + env: &[], + }; + + let progress = ProgressBar::hidden(); + + let dag = Dag::for_root_package(p1, &repo, Some(&progress), &condition_data); + assert!(dag.is_ok()); + let dag = dag.unwrap(); + let ps = dag.all_packages(); + + assert!(ps.iter().any(|p| *p.name() == pname("a"))); + assert!(ps.iter().any(|p| *p.version() == pversion("1"))); + + // IN the tree: + assert!(ps.iter().any(|p| *p.name() == pname("b"))); + assert!(ps.iter().any(|p| *p.version() == pversion("2"))); + } + } -- cgit v1.2.3