Skip to content

Commit 7b78ee7

Browse files
committed
fixed test failures
1 parent 64d913e commit 7b78ee7

File tree

2 files changed

+22
-45
lines changed

2 files changed

+22
-45
lines changed

jupyterlab_git/git.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ def branch_heads(self, current_path):
383383
"""
384384
Execute 'git for-each-ref' command on refs/heads & return the result.
385385
"""
386+
# Format reference: https://git-scm.com/docs/git-for-each-ref#_field_names
386387
formats = ['refname:short', 'objectname', 'upstream:short', 'HEAD']
387388
cmd = ["git", "for-each-ref", "--format=" + "%09".join("%({})".format(f) for f in formats), "refs/heads/"]
388389
p = subprocess.Popen(
@@ -397,7 +398,6 @@ def branch_heads(self, current_path):
397398
results = []
398399
try:
399400
for name,commit_sha,upstream_name,is_current_branch in (line.split('\t') for line in output.decode("utf-8").splitlines()):
400-
# Format reference : https://git-scm.com/docs/git-for-each-ref#_field_names
401401
is_current_branch = bool(is_current_branch.strip())
402402

403403
branch = {
@@ -446,6 +446,7 @@ def branch_remotes(self, current_path):
446446
"""
447447
Execute 'git for-each-ref' command on refs/heads & return the result.
448448
"""
449+
# Format reference: https://git-scm.com/docs/git-for-each-ref#_field_names
449450
formats = ['refname:short', 'objectname']
450451
cmd = ["git", "for-each-ref", "--format=" + "%09".join("%({})".format(f) for f in formats), "refs/remotes/"]
451452
p = subprocess.Popen(
@@ -459,7 +460,6 @@ def branch_remotes(self, current_path):
459460
results = []
460461
try:
461462
for name,commit_sha in (line.split('\t') for line in output.decode("utf-8").splitlines()):
462-
# Format reference : https://git-scm.com/docs/git-for-each-ref#_field_names
463463
results.append({
464464
"is_current_branch": False,
465465
"is_remote_branch": True,

jupyterlab_git/tests/test_branch.py

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,6 @@
77
from 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-
3810
def 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

Comments
 (0)