77from jupyterlab_git .git import Git
88
99
10- def test_is_branch ():
11- test_cases = [
12- ('refs/heads/feature-foo' , True ),
13- ('refs/heads/master' , True ),
14- ('refs/remotes/origin/feature-foo' , True ),
15- ('refs/remotes/origin/HEAD' , True ),
16- ('refs/stash' , False ),
17- ('refs/tags/v0.1.0' , False ),
18- ('refs/tags/blah@0.2.0' , False )
19- ]
20- for test_case in test_cases :
21- actual_response = Git (root_dir = '/bin' )._is_branch (test_case [0 ])
22- assert test_case [1 ] == actual_response
23-
24-
25- def test_is_current_branch ():
26- current_branch = 'feature-foo'
27- test_cases = [
28- ('feature-foo' , True ),
29- ('master' , False ),
30- ('origin/feature-foo' , False ),
31- ('origin/HEAD' , False )
32- ]
33- for test_case in test_cases :
34- actual_response = Git (root_dir = '/bin' )._is_current_branch (test_case [0 ], current_branch )
35- assert test_case [1 ] == actual_response
36-
37-
3810def test_is_remote_branch ():
3911 test_cases = [
4012 ('refs/heads/feature-foo' , False ),
@@ -89,7 +61,7 @@ def test_get_current_branch_success(mock_subproc_popen):
8961 # Then
9062 mock_subproc_popen .assert_has_calls ([
9163 call (
92- ['git' , 'rev-parse' , '--abbrev -ref' , 'HEAD' ],
64+ ['git' , 'symbolic -ref' , 'HEAD' ],
9365 stdout = PIPE ,
9466 stderr = PIPE ,
9567 cwd = '/bin/test_curr_path'
@@ -366,20 +338,20 @@ def test_get_current_branch_failure(mock_subproc_popen):
366338 # Then
367339 mock_subproc_popen .assert_has_calls ([
368340 call (
369- ['git' , 'rev-parse' , '--abbrev -ref' , 'HEAD' ],
341+ ['git' , 'symbolic -ref' , 'HEAD' ],
370342 stdout = PIPE ,
371343 stderr = PIPE ,
372344 cwd = '/bin/test_curr_path'
373345 ),
374346 call ().communicate ()
375347 ])
376348 assert 'Error [fatal: Not a git repository (or any of the parent directories): .git] ' \
377- 'occurred while executing [git rev-parse --abbrev -ref HEAD] command to get current branch.' == str (
349+ 'occurred while executing [git symbolic -ref HEAD] command to get current branch.' == str (
378350 error .value )
379351
380352
381353@patch ('subprocess.Popen' )
382- def test_get_detached_head_name_success (mock_subproc_popen ):
354+ def test_get_current_branch_detached_success (mock_subproc_popen ):
383355 # Given
384356 process_output = [
385357 '* (HEAD detached at origin/feature-foo)' ,
@@ -396,7 +368,7 @@ def test_get_detached_head_name_success(mock_subproc_popen):
396368 mock_subproc_popen .return_value = process_mock
397369
398370 # When
399- actual_response = Git (root_dir = '/bin' )._get_detached_head_name (
371+ actual_response = Git (root_dir = '/bin' )._get_current_branch_detached (
400372 current_path = 'test_curr_path' )
401373
402374 # Then
@@ -413,7 +385,7 @@ def test_get_detached_head_name_success(mock_subproc_popen):
413385
414386
415387@patch ('subprocess.Popen' )
416- def test_get_detached_head_name_failure (mock_subproc_popen ):
388+ def test_get_current_branch_detached_failure (mock_subproc_popen ):
417389 # Given
418390 process_mock = Mock ()
419391 attrs = {
@@ -426,7 +398,7 @@ def test_get_detached_head_name_failure(mock_subproc_popen):
426398
427399 # When
428400 with pytest .raises (Exception ) as error :
429- Git (root_dir = '/bin' )._get_detached_head_name (current_path = 'test_curr_path' )
401+ Git (root_dir = '/bin' )._get_current_branch_detached (current_path = 'test_curr_path' )
430402
431403 # Then
432404 mock_subproc_popen .assert_has_calls ([
@@ -798,19 +770,23 @@ def test_branch_success_detached_head(mock_subproc_popen):
798770 ' master' ,
799771 ' remotes/origin/feature-foo'
800772 ]
801- process_mock = Mock (returncode = 0 )
802- process_mock .communicate .side_effect = [
773+
774+ process_mock = Mock ()
775+ com_returncodes = [0 , 128 , 0 , 0 ]
776+ com_returns = [
803777 # Response for get all refs/heads
804778 ('\n ' .join (process_output_heads ).encode ('utf-8' ), '' .encode ('utf-8' )),
805-
806779 # Response for get current branch
807- ('HEAD ' .encode ('utf-8' ), '' .encode ('utf-8' )),
808- # Responses for detached head name
780+ ('' .encode ('utf-8' ), 'fatal: ref HEAD is not a symbolic ref ' .encode ('utf-8' )),
781+ # Response for get current branch detached
809782 ('\n ' .join (detached_head_output ).encode ('utf-8' ), '' .encode ('utf-8' )),
810-
811783 # Response for get all refs/remotes
812784 ('\n ' .join (process_output_remotes ).encode ('utf-8' ), '' .encode ('utf-8' )),
813785 ]
786+ def com_mock_side_effect ():
787+ process_mock .returncode = com_returncodes .pop (0 )
788+ return com_returns .pop (0 )
789+ process_mock .communicate .side_effect = com_mock_side_effect
814790 mock_subproc_popen .return_value = process_mock
815791
816792 expected_response = {
@@ -868,13 +844,14 @@ def test_branch_success_detached_head(mock_subproc_popen):
868844
869845 # call to get current branch
870846 call (
871- ['git' , 'rev-parse' , '--abbrev -ref' , 'HEAD' ],
847+ ['git' , 'symbolic -ref' , 'HEAD' ],
872848 stdout = PIPE ,
873849 stderr = PIPE ,
874850 cwd = '/bin/test_curr_path'
875851 ),
876852 call ().communicate (),
877- # call to get detached head name
853+
854+ # call to get current branch name given a detached head
878855 call (
879856 ['git' , 'branch' , '-a' ],
880857 stdout = PIPE ,
0 commit comments