Skip to content

Commit 90b377b

Browse files
committed
Fix test
1 parent b8770ce commit 90b377b

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

src/commands/repository.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -934,11 +934,7 @@ impl NewBranchCommand {
934934
impl Command for NewBranchCommand {
935935
fn execute(&self) -> Result<String> {
936936
// Validate branch name format and safety
937-
if self.branch_name.is_empty() {
938-
return Err(GitXError::GitCommand(
939-
"Branch name cannot be empty".to_string(),
940-
));
941-
}
937+
crate::commands::stash::utils::validate_branch_name(&self.branch_name)?;
942938

943939
// Check if branch already exists
944940
if self.branch_exists(&self.branch_name) {
@@ -968,11 +964,8 @@ impl Command for NewBranchCommand {
968964
Format::bold(&base_branch)
969965
));
970966

971-
// Create the new branch
972-
GitOperations::run_status(&["branch", &self.branch_name, &base_branch])?;
973-
974-
// Switch to the new branch (use checkout for better compatibility)
975-
GitOperations::run_status(&["checkout", &self.branch_name])?;
967+
// Create and switch to the new branch in one atomic operation
968+
GitOperations::run_status(&["checkout", "-b", &self.branch_name, &base_branch])?;
976969

977970
output.push(format!(
978971
"✅ Successfully created and switched to branch '{}'",

tests/test_new_branch.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ fn test_new_branch_invalid_name_dash() {
164164
.current_dir(&repo_path)
165165
.assert()
166166
.success() // The command succeeds but shows validation error
167-
.stderr(predicate::str::contains("unknown switch"));
167+
.stderr(predicate::str::contains(
168+
"Branch name cannot start with a dash",
169+
));
168170
}
169171

170172
#[test]
@@ -176,7 +178,7 @@ fn test_new_branch_invalid_name_double_dot() {
176178
.current_dir(&repo_path)
177179
.assert()
178180
.success() // The command succeeds but shows validation error
179-
.stderr(predicate::str::contains("not a valid branch name"));
181+
.stderr(predicate::str::contains("Branch name cannot contain '..'"));
180182
}
181183

182184
#[test]
@@ -188,7 +190,9 @@ fn test_new_branch_invalid_name_spaces() {
188190
.current_dir(&repo_path)
189191
.assert()
190192
.success() // The command succeeds but shows validation error
191-
.stderr(predicate::str::contains("not a valid branch name"));
193+
.stderr(predicate::str::contains(
194+
"Branch name cannot contain spaces",
195+
));
192196
}
193197

194198
#[test]

0 commit comments

Comments
 (0)