From 49dfd175602fde233eadde2df627bd96dc246bf1 Mon Sep 17 00:00:00 2001 From: meskill <8974488+meskill@users.noreply.github.com> Date: Wed, 25 Dec 2024 09:20:10 +0000 Subject: [PATCH 1/2] test: add test for duplicated jobs --- crates/gh-workflow/src/generate.rs | 43 +++++++++++++++++++ ...kflow__generate__tests__add_needs_job.snap | 20 +++++++++ ...low__generate__tests__missing_add_job.snap | 20 +++++++++ 3 files changed, 83 insertions(+) create mode 100644 crates/gh-workflow/src/snapshots/gh_workflow__generate__tests__add_needs_job.snap create mode 100644 crates/gh-workflow/src/snapshots/gh_workflow__generate__tests__missing_add_job.snap diff --git a/crates/gh-workflow/src/generate.rs b/crates/gh-workflow/src/generate.rs index 615a55f..9a97aea 100644 --- a/crates/gh-workflow/src/generate.rs +++ b/crates/gh-workflow/src/generate.rs @@ -140,3 +140,46 @@ 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 super::*; + use insta::assert_snapshot; + + #[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 From 88bbf3662f0406359b174a486e4c61c3da0e6333 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Wed, 25 Dec 2024 10:30:59 +0000 Subject: [PATCH 2/2] [autofix.ci] apply automated fixes --- crates/gh-workflow/src/generate.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/gh-workflow/src/generate.rs b/crates/gh-workflow/src/generate.rs index 9a97aea..e1204f6 100644 --- a/crates/gh-workflow/src/generate.rs +++ b/crates/gh-workflow/src/generate.rs @@ -143,9 +143,10 @@ fn find_value<'a, K, V: PartialEq>(job: &V, map: &'a IndexMap) -> Option<& #[cfg(test)] mod tests { - use super::*; use insta::assert_snapshot; + use super::*; + #[test] fn add_needs_job() { let base_job = Job::new("Base job");