diff --git a/src/bumps.rs b/src/bumps.rs index 26cc76a6..593dee40 100644 --- a/src/bumps.rs +++ b/src/bumps.rs @@ -269,6 +269,13 @@ pub fn get_bumps(options: BumpOptions) -> Vec { for mut package in packages { let package_version = &package.version.to_string(); + let package_name = &package.name.to_string(); + + let already_bumped = bumps.iter().any(|b| b.conventional.package_info.name.eq(package_name)); + + if already_bumped { + continue; + } let semversion = match release_as { Bump::Major => Bump::bump_major(package_version.to_string()), @@ -310,10 +317,7 @@ pub fn get_bumps(options: BumpOptions) -> Vec { if options.sync_deps.unwrap_or(false) { let sync_packages = sync_bumps(&bump, Some(root.to_string())); - let already = bumps - .iter() - .any(|b| b.conventional.package_info.name.to_string() == package.name.to_string()); - dbg!(&package.name, &already); + if sync_packages.len() > 0 { let sync_bumps = get_bumps(BumpOptions { packages: sync_packages, @@ -331,12 +335,6 @@ pub fn get_bumps(options: BumpOptions) -> Vec { } } - let original_bumps = bumps.to_owned(); - bumps.retain(|bp| { - original_bumps - .iter() - .any(|b| b.conventional.package_info.name != bp.conventional.package_info.name) - }); bumps } @@ -436,7 +434,7 @@ mod tests { use std::process::Command; use std::process::Stdio; - fn create_package_change( + fn create_packages_change( monorepo_dir: &PathBuf, touch_no_dependent: bool, ) -> Result<(), Box> { @@ -490,12 +488,57 @@ mod tests { Ok(()) } + fn create_package_change( + monorepo_dir: &PathBuf + ) -> Result<(), Box> { + let js_path = monorepo_dir.join("packages/package-a/index.js"); + + let branch = Command::new("git") + .current_dir(&monorepo_dir) + .arg("checkout") + .arg("-b") + .arg("feat/message") + .stdout(Stdio::piped()) + .spawn() + .expect("Git branch problem"); + + branch.wait_with_output()?; + + let mut js_file = File::create(&js_path)?; + js_file + .write_all(r#"export const message = "hello";"#.as_bytes()) + .unwrap(); + + let add = Command::new("git") + .current_dir(&monorepo_dir) + .arg("add") + .arg(".") + .stdout(Stdio::piped()) + .spawn() + .expect("Git add problem"); + + add.wait_with_output()?; + + let commit = Command::new("git") + .current_dir(&monorepo_dir) + .arg("commit") + .arg("-m") + .arg("feat: message to the world") + .stdout(Stdio::piped()) + .spawn() + .expect("Git commit problem"); + + commit.wait_with_output()?; + + Ok(()) + } + #[test] - fn test_get_bumps() -> Result<(), Box> { + fn test_get_bumps_with_dependency() -> Result<(), Box> { let ref monorepo_dir = create_test_monorepo(&PackageManager::Npm)?; let project_root = get_project_root_path(Some(monorepo_dir.to_path_buf())); - create_package_change(monorepo_dir, true)?; + create_packages_change(monorepo_dir, true)?; let ref root = project_root.unwrap().to_string(); @@ -515,15 +558,67 @@ mod tests { cwd: Some(root.to_string()), }); - let finale = bumps + assert_eq!(bumps.len(), 2); + remove_dir_all(&monorepo_dir)?; + Ok(()) + } + + #[test] + fn test_get_bumps_without_dependency() -> Result<(), Box> { + let ref monorepo_dir = create_test_monorepo(&PackageManager::Npm)?; + let project_root = get_project_root_path(Some(monorepo_dir.to_path_buf())); + + create_packages_change(monorepo_dir, false)?; + + let ref root = project_root.unwrap().to_string(); + + let packages = get_changed_packages(Some(String::from("main")), Some(root.to_string())) + .iter() + .map(|package| package.name.to_string()) + .collect::>(); + + let bumps = get_bumps(BumpOptions { + packages, + since: Some(String::from("main")), + release_as: Bump::Minor, + fetch_all: None, + fetch_tags: None, + sync_deps: Some(true), + push: Some(false), + cwd: Some(root.to_string()), + }); + + assert_eq!(bumps.len(), 2); + remove_dir_all(&monorepo_dir)?; + Ok(()) + } + + #[test] + fn test_get_single_bump() -> Result<(), Box> { + let ref monorepo_dir = create_test_monorepo(&PackageManager::Npm)?; + let project_root = get_project_root_path(Some(monorepo_dir.to_path_buf())); + + create_package_change(monorepo_dir)?; + + let ref root = project_root.unwrap().to_string(); + + let packages = get_changed_packages(Some(String::from("main")), Some(root.to_string())) .iter() - .map(|b| b.conventional.package_info.name.to_string()) + .map(|package| package.name.to_string()) .collect::>(); - dbg!(&finale); + let bumps = get_bumps(BumpOptions { + packages, + since: Some(String::from("main")), + release_as: Bump::Minor, + fetch_all: None, + fetch_tags: None, + sync_deps: Some(true), + push: Some(false), + cwd: Some(root.to_string()), + }); - //assert_eq!(bumps.len(), 2); - assert_eq!(2, 2); + assert_eq!(bumps.len(), 1); remove_dir_all(&monorepo_dir)?; Ok(()) } @@ -533,7 +628,7 @@ mod tests { let ref monorepo_dir = create_test_monorepo(&PackageManager::Npm)?; let project_root = get_project_root_path(Some(monorepo_dir.to_path_buf())); - create_package_change(monorepo_dir, false)?; + create_packages_change(monorepo_dir, false)?; let ref root = project_root.unwrap().to_string(); @@ -585,7 +680,7 @@ mod tests { let ref monorepo_dir = create_test_monorepo(&PackageManager::Npm)?; let project_root = get_project_root_path(Some(monorepo_dir.to_path_buf())); - create_package_change(monorepo_dir, false)?; + create_packages_change(monorepo_dir, false)?; let ref root = project_root.unwrap().to_string();