@@ -13,7 +13,9 @@ use anyhow::Result;
1313use git_workers:: commands:: { find_config_file_path, get_worktree_icon, validate_custom_path} ;
1414use git_workers:: constants;
1515use git_workers:: infrastructure:: git:: { GitWorktreeManager , WorktreeInfo } ;
16+ use serial_test:: serial;
1617use std:: fs;
18+ use std:: path:: { Path , PathBuf } ;
1719use tempfile:: TempDir ;
1820
1921// ============================================================================
@@ -45,6 +47,15 @@ fn setup_non_bare_repo() -> Result<(TempDir, GitWorktreeManager)> {
4547 . current_dir ( temp_dir. path ( ) )
4648 . output ( ) ?;
4749
50+ std:: process:: Command :: new ( "git" )
51+ . args ( [ "config" , "user.email" , "test@example.com" ] )
52+ . current_dir ( temp_dir. path ( ) )
53+ . output ( ) ?;
54+ std:: process:: Command :: new ( "git" )
55+ . args ( [ "config" , "user.name" , "Test User" ] )
56+ . current_dir ( temp_dir. path ( ) )
57+ . output ( ) ?;
58+
4859 // Create initial commit
4960 fs:: write ( temp_dir. path ( ) . join ( "README.md" ) , "# Test" ) ?;
5061 std:: process:: Command :: new ( "git" )
@@ -63,6 +74,24 @@ fn setup_non_bare_repo() -> Result<(TempDir, GitWorktreeManager)> {
6374 Ok ( ( temp_dir, manager) )
6475}
6576
77+ struct CurrentDirGuard {
78+ original : PathBuf ,
79+ }
80+
81+ impl CurrentDirGuard {
82+ fn change_to ( path : & Path ) -> Result < Self > {
83+ let original = std:: env:: current_dir ( ) ?;
84+ std:: env:: set_current_dir ( path) ?;
85+ Ok ( Self { original } )
86+ }
87+ }
88+
89+ impl Drop for CurrentDirGuard {
90+ fn drop ( & mut self ) {
91+ let _ = std:: env:: set_current_dir ( & self . original ) ;
92+ }
93+ }
94+
6695// ============================================================================
6796// Icon and Display Tests
6897// ============================================================================
@@ -149,41 +178,34 @@ fn test_get_worktree_icon_locked() -> Result<()> {
149178// ============================================================================
150179
151180#[ test]
152- #[ ignore = "Test requires isolated environment to avoid finding project config" ]
153- fn test_find_config_file_path_in_bare_repo ( ) -> Result < ( ) > {
181+ #[ serial ]
182+ fn test_find_config_file_path_defaults_to_current_bare_repo_directory ( ) -> Result < ( ) > {
154183 let ( temp_dir, manager) = setup_test_repo ( ) ?;
155-
156- // Create main worktree
157- let main_path = temp_dir. path ( ) . join ( "main" ) ;
158- fs:: create_dir_all ( & main_path) ?;
159-
160- // Create config file in main worktree
161- let config_path = main_path. join ( ".git-workers.toml" ) ;
162- fs:: write ( & config_path, "[worktree]\n pattern = \" subdirectory\" " ) ?;
163-
164- // Update git config to point to main worktree
165- std:: process:: Command :: new ( "git" )
166- . args ( [ "config" , "core.worktree" , main_path. to_str ( ) . unwrap ( ) ] )
167- . current_dir ( temp_dir. path ( ) )
168- . output ( ) ?;
184+ let _guard = CurrentDirGuard :: change_to ( temp_dir. path ( ) ) ?;
169185
170186 let found_path = find_config_file_path ( & manager) ?;
171- assert_eq ! ( found_path, config_path) ;
187+ assert_eq ! (
188+ found_path,
189+ std:: env:: current_dir( ) ?. join( ".git-workers.toml" )
190+ ) ;
172191
173192 Ok ( ( ) )
174193}
175194
176195#[ test]
177- #[ ignore = "Test requires isolated environment to avoid finding project config" ]
178- fn test_find_config_file_path_in_worktree ( ) -> Result < ( ) > {
196+ #[ serial ]
197+ fn test_find_config_file_path_prefers_current_worktree_directory ( ) -> Result < ( ) > {
179198 let ( temp_dir, _manager) = setup_non_bare_repo ( ) ?;
180199
181- // Create config file in repository root
182- let config_path = temp_dir. path ( ) . join ( ".git-workers.toml" ) ;
183- fs:: write ( & config_path, "[worktree]\n pattern = \" same-level\" " ) ?;
200+ fs:: write (
201+ temp_dir. path ( ) . join ( ".git-workers.toml" ) ,
202+ "[worktree]\n pattern = \" same-level\" " ,
203+ ) ?;
184204
185- // Create a worktree
186- let worktree_path = temp_dir. path ( ) . join ( "../feature" ) ;
205+ let worktree_path = temp_dir. path ( ) . parent ( ) . unwrap ( ) . join ( format ! (
206+ "{}-feature" ,
207+ temp_dir. path( ) . file_name( ) . unwrap( ) . to_string_lossy( )
208+ ) ) ;
187209 std:: process:: Command :: new ( "git" )
188210 . args ( [
189211 "worktree" ,
@@ -195,9 +217,11 @@ fn test_find_config_file_path_in_worktree() -> Result<()> {
195217 . current_dir ( temp_dir. path ( ) )
196218 . output ( ) ?;
197219
220+ let worktree_path = worktree_path. canonicalize ( ) ?;
221+ let _guard = CurrentDirGuard :: change_to ( & worktree_path) ?;
198222 let manager = GitWorktreeManager :: new_from_path ( & worktree_path) ?;
199223 let found_path = find_config_file_path ( & manager) ?;
200- assert_eq ! ( found_path, config_path ) ;
224+ assert_eq ! ( found_path, worktree_path . join ( ".git-workers.toml" ) ) ;
201225
202226 Ok ( ( ) )
203227}
0 commit comments