Skip to content

Commit 8add174

Browse files
committed
Add more tests
1 parent 18a06b6 commit 8add174

21 files changed

+6276
-22
lines changed

.claude/settings.local.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@
3838
"Bash(timeout 10 cargo test test_prune_branches_run_function --test test_prune_branches)",
3939
"Bash(echo $?)",
4040
"Bash(git branch:*)",
41-
"Bash(./target/debug/git-x stash-branch --help)"
41+
"Bash(./target/debug/git-x stash-branch --help)",
42+
"Bash(timeout 15 cargo test --test test_interactive)",
43+
"Bash(timeout 30 cargo test --test test_interactive -- --test-threads=1)",
44+
"Bash(timeout 20 cargo test --test test_interactive -- --test-threads=1)"
4245
],
4346
"deny": []
4447
}

SAFETY_TESTS.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Safety Tests Documentation
2+
3+
## Overview
4+
5+
The `tests/test_safety.rs` module contains tests for git-x's safety and backup functionality. Some of these tests perform **DESTRUCTIVE git operations** that can modify your working directory.
6+
7+
## Destructive Operations
8+
9+
The following operations are considered destructive and are conditionally run:
10+
11+
- **Creating backup branches**: Adds new git branches to your repository
12+
- **Creating checkpoints**: Creates git stashes that modify stash state
13+
- **Restoring checkpoints**: Pops git stashes, potentially changing working directory
14+
- **Cleaning up branches**: Can delete backup branches (though limited to very old ones)
15+
16+
## Test Execution Modes
17+
18+
### Local Development (Default - Safe Mode)
19+
```bash
20+
cargo test test_safety
21+
```
22+
- Destructive tests are **SKIPPED** automatically
23+
- Only safe tests run (method signatures, builders, validation)
24+
- Your git repository remains unchanged
25+
26+
### CI Environment (Automatic)
27+
```bash
28+
# In GitHub Actions or other CI
29+
cargo test test_safety
30+
```
31+
- All tests run automatically (detected via `CI` or `GITHUB_ACTIONS` env vars)
32+
- Safe for CI because repositories are disposable
33+
34+
### Manual Override (Use with Caution)
35+
```bash
36+
ENABLE_DESTRUCTIVE_TESTS=1 cargo test test_safety
37+
```
38+
- Forces ALL tests to run locally
39+
- **WARNING**: Will modify your git repository
40+
- Only use in test repositories or when you understand the risks
41+
42+
## Detection Logic
43+
44+
Tests are skipped locally unless one of these conditions is met:
45+
- `CI` environment variable is set
46+
- `GITHUB_ACTIONS` environment variable is set
47+
- `ENABLE_DESTRUCTIVE_TESTS` environment variable is set
48+
49+
## Safety Measures
50+
51+
Even when destructive tests run:
52+
- Backup creation uses timestamp-based names to avoid conflicts
53+
- Cleanup operations target only very old backups (365+ days)
54+
- All operations use non-interactive mode to prevent hanging
55+
- Tests are designed to be as safe as possible while still testing functionality
56+
57+
## Development Guidelines
58+
59+
When adding new safety tests:
60+
1. Wrap potentially destructive operations with `if !should_run_destructive_tests() { return; }`
61+
2. Use descriptive test names that indicate destructive nature
62+
3. Test the minimal necessary functionality to verify behavior
63+
4. Prefer testing business logic over actual git operations when possible

src/commands/repository.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,8 @@ impl Command for NewBranchCommand {
588588
// Create the new branch
589589
GitOperations::run_status(&["branch", &self.branch_name, &base_branch])?;
590590

591-
// Switch to the new branch
592-
GitOperations::run_status(&["switch", &self.branch_name])?;
591+
// Switch to the new branch (use checkout for better compatibility)
592+
GitOperations::run_status(&["checkout", &self.branch_name])?;
593593

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

src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ pub mod commands;
1313
// CLI interface
1414
pub mod cli;
1515

16-
// Test utilities for direct command testing (improves test coverage)
17-
pub mod test_utils;
18-
1916
// Examples showing architecture migration
2017
#[cfg(test)]
2118
pub mod examples;

tests/common/mod.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ pub fn repo_with_merged_branch(feature_branch: &str, main_branch: &str) -> TestR
206206
let path = temp.path().to_path_buf();
207207

208208
StdCommand::new("git")
209-
.arg("init")
209+
.args(["init", &format!("--initial-branch={main_branch}")])
210210
.current_dir(&path)
211211
.assert()
212212
.success();
@@ -217,13 +217,6 @@ pub fn repo_with_merged_branch(feature_branch: &str, main_branch: &str) -> TestR
217217
};
218218
repo.configure_git_identity();
219219

220-
// Create the requested main branch
221-
StdCommand::new("git")
222-
.args(["checkout", "-b", main_branch])
223-
.current_dir(&path)
224-
.assert()
225-
.success();
226-
227220
// Initial commit
228221
fs::write(path.join("README.md"), "# test").unwrap();
229222
StdCommand::new("git")

0 commit comments

Comments
 (0)