Skip to content

Commit

Permalink
chore: merge pull request #44 from websublime/fix/change-exist-diff
Browse files Browse the repository at this point in the history
fix: changes diff exists for empty and new changed package
  • Loading branch information
miguelramos authored Sep 11, 2024
2 parents ea2a273 + a578494 commit e56bb2b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "workspace-node-tools"
version = "1.0.17"
version = "1.0.18"
edition = "2021"
description = "Node workspace version tools"
repository = "https://github.com/websublime/workspace-node-tools"
Expand Down
78 changes: 73 additions & 5 deletions src/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,26 @@ pub fn change_exist(branch: String, packages_name: Vec<String>, cwd: Option<Stri
if changes.changes.contains_key(&branch) {
let branch_changes = changes.changes.get(&branch).unwrap();

return branch_changes.iter().all(|change| {
packages_name
.iter()
.any(|package_name| change.package.as_str() == package_name.as_str())
});
let existing_packages_changes = branch_changes
.iter()
.map(|change| change.package.to_string())
.collect::<Vec<String>>();

let package_names_diff = packages_name
.iter()
.filter_map(|p| {
if existing_packages_changes.contains(&p) {
None
} else {
Some(p.to_string())
}
})
.collect::<Vec<String>>();

match package_names_diff.len() {
0 => return true,
_ => return false,
};
}
}

Expand Down Expand Up @@ -578,6 +593,59 @@ mod tests {
remove_dir_all(&monorepo_dir).unwrap();
}

#[test]
fn test_change_exist_with_new_package() -> 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()));

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()));

let result = change_exist(
String::from("main"),
vec!["test-package".to_string(), "@scope/package-a".to_string()],
Some(root.to_string()),
);

assert_eq!(result, false);
assert_eq!(changes_path.is_file(), true);
remove_dir_all(&monorepo_dir)?;
Ok(())
}

#[test]
fn test_change_exist_with_empty_packages() -> 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()));

let ref root = project_root.unwrap().to_string();

init_changes(Some(root.to_string()), &None);

let ref changes_path = monorepo_dir.join(String::from(".changes.json"));

let result = change_exist(
String::from("main"),
vec!["test-package".to_string(), "@scope/package-a".to_string()],
Some(root.to_string()),
);

assert_eq!(result, false);
assert_eq!(changes_path.is_file(), true);
remove_dir_all(&monorepo_dir)?;
Ok(())
}

#[test]
fn test_changes_file_exist() -> Result<(), Box<dyn std::error::Error>> {
let ref monorepo_dir = create_test_monorepo(&PackageManager::Npm)?;
Expand Down

0 comments on commit e56bb2b

Please sign in to comment.