diff --git a/crates/gh-workflow/src/generate.rs b/crates/gh-workflow/src/generate.rs index 615a55f..e1204f6 100644 --- a/crates/gh-workflow/src/generate.rs +++ b/crates/gh-workflow/src/generate.rs @@ -140,3 +140,47 @@ fn find_value<'a, K, V: PartialEq>(job: &V, map: &'a IndexMap) -> Option<& map.iter() .find_map(|(k, v)| if v == job { Some(k) } else { None }) } + +#[cfg(test)] +mod tests { + use insta::assert_snapshot; + + use super::*; + + #[test] + fn add_needs_job() { + let base_job = Job::new("Base job"); + + let job1 = + Job::new("The first job that has dependency for base_job").add_needs(base_job.clone()); + let job2 = + Job::new("The second job that has dependency for base_job").add_needs(base_job.clone()); + + let workflow = Workflow::new("All jobs were added to workflow") + .add_job("base_job", base_job) + .add_job("with-dependency-1", job1) + .add_job("with-dependency-2", job2); + + let workflow = Generate::new(workflow).workflow; + + assert_snapshot!(workflow.to_string().unwrap()); + } + + #[test] + fn missing_add_job() { + let base_job = Job::new("Base job"); + + let job1 = + Job::new("The first job that has dependency for base_job").add_needs(base_job.clone()); + let job2 = + Job::new("The second job that has dependency for base_job").add_needs(base_job.clone()); + + let workflow = Workflow::new("base_job was not added to workflow jobs") + .add_job("with-dependency-1", job1) + .add_job("with-dependency-2", job2); + + let workflow = Generate::new(workflow).workflow; + + assert_snapshot!(workflow.to_string().unwrap()); + } +} diff --git a/crates/gh-workflow/src/snapshots/gh_workflow__generate__tests__add_needs_job.snap b/crates/gh-workflow/src/snapshots/gh_workflow__generate__tests__add_needs_job.snap new file mode 100644 index 0000000..b08d9f6 --- /dev/null +++ b/crates/gh-workflow/src/snapshots/gh_workflow__generate__tests__add_needs_job.snap @@ -0,0 +1,20 @@ +--- +source: crates/gh-workflow/src/generate.rs +expression: workflow.to_string().unwrap() +snapshot_kind: text +--- +name: All jobs were added to workflow +jobs: + base_job: + name: Base job + runs-on: ubuntu-latest + with-dependency-1: + needs: + - base_job + name: The first job that has dependency for base_job + runs-on: ubuntu-latest + with-dependency-2: + needs: + - base_job + name: The second job that has dependency for base_job + runs-on: ubuntu-latest diff --git a/crates/gh-workflow/src/snapshots/gh_workflow__generate__tests__missing_add_job.snap b/crates/gh-workflow/src/snapshots/gh_workflow__generate__tests__missing_add_job.snap new file mode 100644 index 0000000..7cf13f0 --- /dev/null +++ b/crates/gh-workflow/src/snapshots/gh_workflow__generate__tests__missing_add_job.snap @@ -0,0 +1,20 @@ +--- +source: crates/gh-workflow/src/generate.rs +expression: workflow.to_string().unwrap() +snapshot_kind: text +--- +name: base_job was not added to workflow jobs +jobs: + job-0: + name: Base job + runs-on: ubuntu-latest + with-dependency-1: + needs: + - job-0 + name: The first job that has dependency for base_job + runs-on: ubuntu-latest + with-dependency-2: + needs: + - job-0 + name: The second job that has dependency for base_job + runs-on: ubuntu-latest