@@ -48,13 +48,16 @@ fn create_test_repo() -> (TempDir, PathBuf) {
4848}
4949
5050// Helper function to create a remote repository
51- fn create_remote_repo ( name : & str , repo_path : & PathBuf ) -> PathBuf {
52- let remote_dir = repo_path. parent ( ) . unwrap ( ) . join ( format ! ( "{name}.git" ) ) ;
53-
51+ fn create_remote_repo ( name : & str , repo_path : & PathBuf ) -> ( PathBuf , String ) {
52+ use std:: time:: { SystemTime , UNIX_EPOCH } ;
53+ let timestamp = SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . unwrap ( ) . as_nanos ( ) ;
54+ let unique_name = format ! ( "{name}_{timestamp}" ) ;
55+ let remote_dir = repo_path. parent ( ) . unwrap ( ) . join ( format ! ( "{unique_name}.git" ) ) ;
56+
5457 // Initialize bare remote repo
5558 Command :: new ( "git" )
5659 . args ( [ "init" , "--bare" ] )
57- . current_dir ( & remote_dir. parent ( ) . unwrap ( ) )
60+ . current_dir ( remote_dir. parent ( ) . unwrap ( ) )
5861 . arg ( & remote_dir)
5962 . assert ( )
6063 . success ( ) ;
@@ -66,19 +69,30 @@ fn create_remote_repo(name: &str, repo_path: &PathBuf) -> PathBuf {
6669 . assert ( )
6770 . success ( ) ;
6871
69- // Push initial branch to remote
72+ // Get current branch name
73+ let branch_output = Command :: new ( "git" )
74+ . args ( [ "branch" , "--show-current" ] )
75+ . current_dir ( repo_path)
76+ . output ( )
77+ . expect ( "Failed to get current branch" ) ;
78+ let current_branch = String :: from_utf8_lossy ( & branch_output. stdout ) . trim ( ) . to_string ( ) ;
79+
80+ // Push initial branch to remote with set-upstream and force
7081 Command :: new ( "git" )
71- . args ( [ "push" , name, "master "] )
82+ . args ( [ "push" , "-u" , name, & current_branch , "--force "] )
7283 . current_dir ( repo_path)
7384 . assert ( )
7485 . success ( ) ;
7586
76- remote_dir
87+ ( remote_dir, current_branch )
7788}
7889
7990#[ test]
8091fn test_get_git_branch_set_upstream_args ( ) {
81- assert_eq ! ( get_git_branch_set_upstream_args( ) , [ "branch" , "--set-upstream-to" ] ) ;
92+ assert_eq ! (
93+ get_git_branch_set_upstream_args( ) ,
94+ [ "branch" , "--set-upstream-to" ]
95+ ) ;
8296}
8397
8498#[ test]
@@ -195,7 +209,9 @@ fn test_upstream_status_command_help() {
195209 cmd. args ( [ "upstream" , "status" , "--help" ] )
196210 . assert ( )
197211 . success ( )
198- . stdout ( predicate:: str:: contains ( "Show upstream status for all branches" ) ) ;
212+ . stdout ( predicate:: str:: contains (
213+ "Show upstream status for all branches" ,
214+ ) ) ;
199215}
200216
201217#[ test]
@@ -204,7 +220,9 @@ fn test_upstream_sync_all_command_help() {
204220 cmd. args ( [ "upstream" , "sync-all" , "--help" ] )
205221 . assert ( )
206222 . success ( )
207- . stdout ( predicate:: str:: contains ( "Sync all branches with their upstreams" ) ) ;
223+ . stdout ( predicate:: str:: contains (
224+ "Sync all local branches with their upstreams" ,
225+ ) ) ;
208226}
209227
210228#[ test]
@@ -225,7 +243,9 @@ fn test_upstream_set_invalid_format() {
225243 . current_dir ( & repo_path)
226244 . assert ( )
227245 . success ( )
228- . stderr ( predicate:: str:: contains ( "must be in format 'remote/branch'" ) ) ;
246+ . stderr ( predicate:: str:: contains (
247+ "must be in format 'remote/branch'" ,
248+ ) ) ;
229249
230250 // Test upstream with empty parts
231251 let mut cmd = Command :: cargo_bin ( "git-x" ) . expect ( "Failed to find binary" ) ;
@@ -258,15 +278,19 @@ fn test_upstream_set_nonexistent_upstream() {
258278#[ test]
259279fn test_upstream_set_success ( ) {
260280 let ( _temp_dir, repo_path) = create_test_repo ( ) ;
261- let _remote_dir = create_remote_repo ( "origin" , & repo_path) ;
281+ let ( _remote_dir, branch_name ) = create_remote_repo ( "origin" , & repo_path) ;
262282
263283 let mut cmd = Command :: cargo_bin ( "git-x" ) . expect ( "Failed to find binary" ) ;
264- cmd. args ( [ "upstream" , "set" , "origin/master" ] )
284+ cmd. args ( [ "upstream" , "set" , & format ! ( "origin/{}" , branch_name ) ] )
265285 . current_dir ( & repo_path)
266286 . assert ( )
267287 . success ( )
268- . stdout ( predicate:: str:: contains ( "Setting upstream for 'master' to 'origin/master'" ) )
269- . stdout ( predicate:: str:: contains ( "Upstream for 'master' set to 'origin/master'" ) ) ;
288+ . stdout ( predicate:: str:: contains (
289+ & format ! ( "Setting upstream for '{}' to 'origin/{}'" , branch_name, branch_name) ,
290+ ) )
291+ . stdout ( predicate:: str:: contains (
292+ & format ! ( "Upstream for '{}' set to 'origin/{}'" , branch_name, branch_name) ,
293+ ) ) ;
270294}
271295
272296#[ test]
@@ -304,7 +328,7 @@ fn test_upstream_status_no_branches() {
304328#[ test]
305329fn test_upstream_status_with_branches ( ) {
306330 let ( _temp_dir, repo_path) = create_test_repo ( ) ;
307- let _remote_dir = create_remote_repo ( "origin" , & repo_path) ;
331+ let ( _remote_dir, branch_name ) = create_remote_repo ( "origin" , & repo_path) ;
308332
309333 // Create a feature branch
310334 Command :: new ( "git" )
@@ -313,15 +337,15 @@ fn test_upstream_status_with_branches() {
313337 . assert ( )
314338 . success ( ) ;
315339
316- // Set upstream for master
340+ // Set upstream for the main branch
317341 Command :: new ( "git" )
318- . args ( [ "checkout" , "master" ] )
342+ . args ( [ "checkout" , & branch_name ] )
319343 . current_dir ( & repo_path)
320344 . assert ( )
321345 . success ( ) ;
322-
346+
323347 Command :: new ( "git" )
324- . args ( [ "branch" , "--set-upstream-to" , "origin/master" ] )
348+ . args ( [ "branch" , "--set-upstream-to" , & format ! ( "origin/{}" , branch_name ) ] )
325349 . current_dir ( & repo_path)
326350 . assert ( )
327351 . success ( ) ;
@@ -332,7 +356,7 @@ fn test_upstream_status_with_branches() {
332356 . assert ( )
333357 . success ( )
334358 . stdout ( predicate:: str:: contains ( "Upstream status for all branches" ) )
335- . stdout ( predicate:: str:: contains ( "master -> origin/master" ) )
359+ . stdout ( predicate:: str:: contains ( & format ! ( "{} -> origin/{}" , branch_name , branch_name ) ) )
336360 . stdout ( predicate:: str:: contains ( "feature -> (no upstream)" ) ) ;
337361}
338362
@@ -345,17 +369,19 @@ fn test_upstream_sync_all_no_upstreams() {
345369 . current_dir ( & repo_path)
346370 . assert ( )
347371 . success ( )
348- . stdout ( predicate:: str:: contains ( "No branches with upstream configuration found" ) ) ;
372+ . stdout ( predicate:: str:: contains (
373+ "No branches with upstream configuration found" ,
374+ ) ) ;
349375}
350376
351377#[ test]
352378fn test_upstream_sync_all_dry_run ( ) {
353379 let ( _temp_dir, repo_path) = create_test_repo ( ) ;
354- let _remote_dir = create_remote_repo ( "origin" , & repo_path) ;
380+ let ( _remote_dir, branch_name ) = create_remote_repo ( "origin" , & repo_path) ;
355381
356382 // Set upstream for master
357383 Command :: new ( "git" )
358- . args ( [ "branch" , "--set-upstream-to" , "origin/master" ] )
384+ . args ( [ "branch" , "--set-upstream-to" , & format ! ( "origin/{}" , branch_name ) ] )
359385 . current_dir ( & repo_path)
360386 . assert ( )
361387 . success ( ) ;
@@ -372,11 +398,11 @@ fn test_upstream_sync_all_dry_run() {
372398#[ test]
373399fn test_upstream_sync_all_with_merge ( ) {
374400 let ( _temp_dir, repo_path) = create_test_repo ( ) ;
375- let _remote_dir = create_remote_repo ( "origin" , & repo_path) ;
401+ let ( _remote_dir, branch_name ) = create_remote_repo ( "origin" , & repo_path) ;
376402
377403 // Set upstream for master
378404 Command :: new ( "git" )
379- . args ( [ "branch" , "--set-upstream-to" , "origin/master" ] )
405+ . args ( [ "branch" , "--set-upstream-to" , & format ! ( "origin/{}" , branch_name ) ] )
380406 . current_dir ( & repo_path)
381407 . assert ( )
382408 . success ( ) ;
@@ -398,7 +424,7 @@ fn test_upstream_command_outside_git_repo() {
398424 . current_dir ( temp_dir. path ( ) )
399425 . assert ( )
400426 . success ( )
401- . stderr ( predicate:: str:: contains ( "Failed" ) ) ;
427+ . stderr ( predicate:: str:: contains ( "Failed to list local branches " ) ) ;
402428}
403429
404430#[ test]
@@ -410,7 +436,7 @@ fn test_upstream_set_outside_git_repo() {
410436 . current_dir ( temp_dir. path ( ) )
411437 . assert ( )
412438 . success ( )
413- . stderr ( predicate:: str:: contains ( "Failed " ) ) ;
439+ . stderr ( predicate:: str:: contains ( "Upstream branch does not exist " ) ) ;
414440}
415441
416442#[ test]
@@ -419,5 +445,7 @@ fn test_upstream_main_command_help() {
419445 cmd. args ( [ "upstream" , "--help" ] )
420446 . assert ( )
421447 . success ( )
422- . stdout ( predicate:: str:: contains ( "Manage upstream branch relationships" ) ) ;
423- }
448+ . stdout ( predicate:: str:: contains (
449+ "Manage upstream branch relationships" ,
450+ ) ) ;
451+ }
0 commit comments