Skip to content

Commit

Permalink
fix: bump loop skip if package already bumped
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelramos committed Sep 12, 2024
1 parent e7f7a7d commit f93bb37
Showing 1 changed file with 115 additions and 20 deletions.
135 changes: 115 additions & 20 deletions src/bumps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,13 @@ pub fn get_bumps(options: BumpOptions) -> Vec<BumpPackage> {

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()),
Expand Down Expand Up @@ -310,10 +317,7 @@ pub fn get_bumps(options: BumpOptions) -> Vec<BumpPackage> {

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,
Expand All @@ -331,12 +335,6 @@ pub fn get_bumps(options: BumpOptions) -> Vec<BumpPackage> {
}
}

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
}

Expand Down Expand Up @@ -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<dyn std::error::Error>> {
Expand Down Expand Up @@ -490,12 +488,57 @@ mod tests {
Ok(())
}

fn create_package_change(
monorepo_dir: &PathBuf
) -> Result<(), Box<dyn std::error::Error>> {
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<dyn std::error::Error>> {
fn test_get_bumps_with_dependency() -> Result<(), Box<dyn std::error::Error>> {
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();

Expand All @@ -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<dyn std::error::Error>> {
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::<Vec<String>>();

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<dyn std::error::Error>> {
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::<Vec<String>>();

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(())
}
Expand All @@ -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();

Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit f93bb37

Please sign in to comment.