7
7
from jupyterlab_git .git import Git
8
8
9
9
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/[email protected] ' ,
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
-
38
10
def test_is_remote_branch ():
39
11
test_cases = [
40
12
('refs/heads/feature-foo' , False ),
@@ -89,7 +61,7 @@ def test_get_current_branch_success(mock_subproc_popen):
89
61
# Then
90
62
mock_subproc_popen .assert_has_calls ([
91
63
call (
92
- ['git' , 'rev-parse' , '--abbrev -ref' , 'HEAD' ],
64
+ ['git' , 'symbolic -ref' , 'HEAD' ],
93
65
stdout = PIPE ,
94
66
stderr = PIPE ,
95
67
cwd = '/bin/test_curr_path'
@@ -366,20 +338,20 @@ def test_get_current_branch_failure(mock_subproc_popen):
366
338
# Then
367
339
mock_subproc_popen .assert_has_calls ([
368
340
call (
369
- ['git' , 'rev-parse' , '--abbrev -ref' , 'HEAD' ],
341
+ ['git' , 'symbolic -ref' , 'HEAD' ],
370
342
stdout = PIPE ,
371
343
stderr = PIPE ,
372
344
cwd = '/bin/test_curr_path'
373
345
),
374
346
call ().communicate ()
375
347
])
376
348
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 (
378
350
error .value )
379
351
380
352
381
353
@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 ):
383
355
# Given
384
356
process_output = [
385
357
'* (HEAD detached at origin/feature-foo)' ,
@@ -396,7 +368,7 @@ def test_get_detached_head_name_success(mock_subproc_popen):
396
368
mock_subproc_popen .return_value = process_mock
397
369
398
370
# When
399
- actual_response = Git (root_dir = '/bin' )._get_detached_head_name (
371
+ actual_response = Git (root_dir = '/bin' )._get_current_branch_detached (
400
372
current_path = 'test_curr_path' )
401
373
402
374
# Then
@@ -413,7 +385,7 @@ def test_get_detached_head_name_success(mock_subproc_popen):
413
385
414
386
415
387
@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 ):
417
389
# Given
418
390
process_mock = Mock ()
419
391
attrs = {
@@ -426,7 +398,7 @@ def test_get_detached_head_name_failure(mock_subproc_popen):
426
398
427
399
# When
428
400
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' )
430
402
431
403
# Then
432
404
mock_subproc_popen .assert_has_calls ([
@@ -798,19 +770,23 @@ def test_branch_success_detached_head(mock_subproc_popen):
798
770
' master' ,
799
771
' remotes/origin/feature-foo'
800
772
]
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 = [
803
777
# Response for get all refs/heads
804
778
('\n ' .join (process_output_heads ).encode ('utf-8' ), '' .encode ('utf-8' )),
805
-
806
779
# 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
809
782
('\n ' .join (detached_head_output ).encode ('utf-8' ), '' .encode ('utf-8' )),
810
-
811
783
# Response for get all refs/remotes
812
784
('\n ' .join (process_output_remotes ).encode ('utf-8' ), '' .encode ('utf-8' )),
813
785
]
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
814
790
mock_subproc_popen .return_value = process_mock
815
791
816
792
expected_response = {
@@ -868,13 +844,14 @@ def test_branch_success_detached_head(mock_subproc_popen):
868
844
869
845
# call to get current branch
870
846
call (
871
- ['git' , 'rev-parse' , '--abbrev -ref' , 'HEAD' ],
847
+ ['git' , 'symbolic -ref' , 'HEAD' ],
872
848
stdout = PIPE ,
873
849
stderr = PIPE ,
874
850
cwd = '/bin/test_curr_path'
875
851
),
876
852
call ().communicate (),
877
- # call to get detached head name
853
+
854
+ # call to get current branch name given a detached head
878
855
call (
879
856
['git' , 'branch' , '-a' ],
880
857
stdout = PIPE ,
0 commit comments