Skip to content

Commit 434734e

Browse files
committed
Fix test
1 parent 47f8aa1 commit 434734e

File tree

3 files changed

+92
-11
lines changed

3 files changed

+92
-11
lines changed

CLAUDE.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
1414
- `make test` - Run unit and integration tests
1515
- `cargo test` - Alternative test command
1616
- `cargo test <test_name>` - Run specific test
17+
- `make coverage` - Generate test coverage using tarpaulin
1718

1819
### Code Quality
1920
- `make fmt` - Format code with rustfmt
@@ -41,11 +42,16 @@ Each Git workflow command is implemented as a separate module:
4142
- Error handling uses `expect()` with descriptive messages
4243

4344
### Key Dependencies
44-
- `clap` - Command-line parsing with derive macros
45-
- `clap_complete` - Shell completion generation
46-
- `console` - Terminal output formatting
45+
- `clap` - Command-line parsing with derive macros and subcommands
46+
- `console` - Terminal output formatting with colors and emojis
4747
- `chrono` - Date/time handling for summary command
4848

49+
### Dev Dependencies
50+
- `assert_cmd` - CLI testing framework for integration tests
51+
- `tempfile` - Temporary Git repositories for testing
52+
- `predicates` - Assertion helpers for test conditions
53+
- `criterion` - Benchmarking framework
54+
4955
### Testing
5056
- Integration tests in `tests/` directory
5157
- Each command has corresponding `test_<command>.rs` file
@@ -54,5 +60,10 @@ Each Git workflow command is implemented as a separate module:
5460
### Git Plugin Integration
5561
The binary name `git-x` enables Git's plugin system - any executable named `git-<name>` can be invoked as `git <name>`. Commands like `git x info` work automatically once installed.
5662

57-
### Completion System
58-
Shell completions are generated via `--generate-completions <shell>` flag using `clap_complete`. The README provides setup instructions for bash, zsh, fish, PowerShell, and Elvish.
63+
### Error Handling and Types
64+
- Common error type `GitXError` defined in `src/lib.rs` with variants for Git commands, IO, and parsing
65+
- Most commands use `expect()` with descriptive messages for quick failure feedback
66+
- Type alias `Result<T>` available for consistent error handling
67+
68+
### Shell Completion Generation
69+
Shell completions can be generated via `--generate-completions <shell>` flag using `clap_complete`. The README provides setup instructions for bash, zsh, fish, PowerShell, and Elvish.

tests/test_sync.rs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,33 @@ fn test_get_current_branch_success() {
212212
fn test_get_current_branch_not_git_repo() {
213213
let temp_dir = TempDir::new().expect("Failed to create temp directory");
214214

215-
std::env::set_current_dir(temp_dir.path()).expect("Failed to change directory");
215+
// Create a completely isolated directory that definitely isn't a git repo
216+
let isolated_dir = temp_dir.path().join("isolated");
217+
std::fs::create_dir(&isolated_dir).expect("Failed to create isolated directory");
218+
219+
// Unset GIT_DIR and GIT_WORK_TREE to ensure git doesn't find parent repos
220+
let original_git_dir = std::env::var("GIT_DIR").ok();
221+
let original_git_work_tree = std::env::var("GIT_WORK_TREE").ok();
222+
unsafe {
223+
std::env::remove_var("GIT_DIR");
224+
std::env::remove_var("GIT_WORK_TREE");
225+
}
226+
227+
let original_dir = std::env::current_dir().expect("Failed to get current directory");
228+
std::env::set_current_dir(&isolated_dir).expect("Failed to change directory");
216229

217230
let result = get_current_branch();
218-
std::env::set_current_dir("/").expect("Failed to reset directory");
231+
232+
// Restore original directory and environment
233+
std::env::set_current_dir(&original_dir).expect("Failed to reset directory");
234+
unsafe {
235+
if let Some(git_dir) = original_git_dir {
236+
std::env::set_var("GIT_DIR", git_dir);
237+
}
238+
if let Some(git_work_tree) = original_git_work_tree {
239+
std::env::set_var("GIT_WORK_TREE", git_work_tree);
240+
}
241+
}
219242

220243
assert!(result.is_err());
221244
assert_eq!(result.unwrap_err(), "Not in a git repository");
@@ -545,9 +568,33 @@ fn test_get_current_branch_comprehensive() {
545568

546569
// Test error case
547570
let temp_dir = TempDir::new().expect("Failed to create temp directory");
548-
std::env::set_current_dir(temp_dir.path()).expect("Failed to change directory");
571+
572+
// Create a completely isolated directory that definitely isn't a git repo
573+
let isolated_dir = temp_dir.path().join("isolated");
574+
std::fs::create_dir(&isolated_dir).expect("Failed to create isolated directory");
575+
576+
// Unset GIT_DIR and GIT_WORK_TREE to ensure git doesn't find parent repos
577+
let original_git_dir = std::env::var("GIT_DIR").ok();
578+
let original_git_work_tree = std::env::var("GIT_WORK_TREE").ok();
579+
unsafe {
580+
std::env::remove_var("GIT_DIR");
581+
std::env::remove_var("GIT_WORK_TREE");
582+
}
583+
584+
let original_dir = std::env::current_dir().expect("Failed to get current directory");
585+
std::env::set_current_dir(&isolated_dir).expect("Failed to change directory");
549586
let result_error = get_current_branch();
550-
std::env::set_current_dir("/").expect("Failed to reset directory");
587+
588+
// Restore original directory and environment
589+
std::env::set_current_dir(&original_dir).expect("Failed to reset directory");
590+
unsafe {
591+
if let Some(git_dir) = original_git_dir {
592+
std::env::set_var("GIT_DIR", git_dir);
593+
}
594+
if let Some(git_work_tree) = original_git_work_tree {
595+
std::env::set_var("GIT_WORK_TREE", git_work_tree);
596+
}
597+
}
551598

552599
assert!(result_error.is_err());
553600
assert_eq!(result_error.unwrap_err(), "Not in a git repository");

tests/test_upstream.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,10 +593,33 @@ fn test_get_current_branch_success() {
593593
fn test_get_current_branch_not_git_repo() {
594594
let temp_dir = TempDir::new().expect("Failed to create temp directory");
595595

596-
std::env::set_current_dir(temp_dir.path()).expect("Failed to change directory");
596+
// Create a completely isolated directory that definitely isn't a git repo
597+
let isolated_dir = temp_dir.path().join("isolated");
598+
std::fs::create_dir(&isolated_dir).expect("Failed to create isolated directory");
599+
600+
// Unset GIT_DIR and GIT_WORK_TREE to ensure git doesn't find parent repos
601+
let original_git_dir = std::env::var("GIT_DIR").ok();
602+
let original_git_work_tree = std::env::var("GIT_WORK_TREE").ok();
603+
unsafe {
604+
std::env::remove_var("GIT_DIR");
605+
std::env::remove_var("GIT_WORK_TREE");
606+
}
607+
608+
let original_dir = std::env::current_dir().expect("Failed to get current directory");
609+
std::env::set_current_dir(&isolated_dir).expect("Failed to change directory");
597610

598611
let result = get_current_branch();
599-
std::env::set_current_dir("/").expect("Failed to reset directory");
612+
613+
// Restore original directory and environment
614+
std::env::set_current_dir(&original_dir).expect("Failed to reset directory");
615+
unsafe {
616+
if let Some(git_dir) = original_git_dir {
617+
std::env::set_var("GIT_DIR", git_dir);
618+
}
619+
if let Some(git_work_tree) = original_git_work_tree {
620+
std::env::set_var("GIT_WORK_TREE", git_work_tree);
621+
}
622+
}
600623

601624
assert!(result.is_err());
602625
}

0 commit comments

Comments
 (0)