From 04614daad07c73b7ca4695d60fe839e2ab63d845 Mon Sep 17 00:00:00 2001 From: Miguel Ramos Date: Mon, 9 Sep 2024 11:06:27 +0100 Subject: [PATCH] fix: duplicate changes entries and check if exist --- src/changes.rs | 86 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 80 insertions(+), 6 deletions(-) diff --git a/src/changes.rs b/src/changes.rs index 1bf6620b..1c5a874d 100644 --- a/src/changes.rs +++ b/src/changes.rs @@ -193,11 +193,18 @@ pub fn add_change(change: &Change, cwd: Option) -> bool { if changes.changes.contains_key(&branch) { let branch_changes = changes.changes.get_mut(&branch).unwrap(); - branch_changes.push(Change { - package: change.package.to_string(), - release_as: change.release_as, - deploy: change.deploy.to_vec(), - }); + + let pkg_already_added = branch_changes + .iter() + .any(|branch_change| branch_change.package.as_str() == change.package.as_str()); + + if !pkg_already_added { + branch_changes.push(Change { + package: change.package.to_string(), + release_as: change.release_as, + deploy: change.deploy.to_vec(), + }); + } } else { changes.changes.insert( branch, @@ -325,7 +332,7 @@ pub fn change_exist(branch: String, packages_name: Vec, cwd: Option Result<(), Box> { + let ref monorepo_dir = create_test_monorepo(&PackageManager::Npm)?; + let project_root = get_project_root_path(Some(monorepo_dir.to_path_buf())); + + let ref root = project_root.unwrap().to_string(); + + let change = Change { + package: String::from("test-package"), + release_as: Bump::Major, + deploy: vec![String::from("production")], + }; + + init_changes(Some(root.to_string()), &None); + + let ref changes_path = monorepo_dir.join(String::from(".changes.json")); + add_change(&change, Some(root.to_string())); + add_change(&change, Some(root.to_string())); + + let changes = get_changes(Some(root.to_string())); + let length = changes.changes["main"].len(); + + assert_eq!(length, 1); + assert_eq!(changes_path.is_file(), true); + remove_dir_all(&monorepo_dir)?; + Ok(()) + } + #[test] fn test_remove_change() -> Result<(), Box> { let ref monorepo_dir = create_test_monorepo(&PackageManager::Npm)?; @@ -504,6 +539,45 @@ mod tests { Ok(()) } + #[test] + fn test_multiple_change_exist() { + let ref monorepo_dir = create_test_monorepo(&PackageManager::Npm).unwrap(); + let project_root = get_project_root_path(Some(monorepo_dir.to_path_buf())).unwrap(); + + let ref root = project_root.to_string(); + + let change_package_a = Change { + package: String::from("@scope/package-a"), + release_as: Bump::Major, + deploy: vec![String::from("production")], + }; + + let change_package_b = Change { + package: String::from("@scope/package-b"), + release_as: Bump::Major, + deploy: vec![String::from("production")], + }; + + init_changes(Some(root.to_string()), &None); + + let ref changes_path = monorepo_dir.join(String::from(".changes.json")); + add_change(&change_package_a, Some(root.to_string())); + add_change(&change_package_b, Some(root.to_string())); + + let result = change_exist( + String::from("main"), + vec![ + "@scope/package-a".to_string(), + "@scope/package-b".to_string(), + ], + Some(root.to_string()), + ); + + assert_eq!(result, true); + assert_eq!(changes_path.is_file(), true); + remove_dir_all(&monorepo_dir).unwrap(); + } + #[test] fn test_changes_file_exist() -> Result<(), Box> { let ref monorepo_dir = create_test_monorepo(&PackageManager::Npm)?;