Skip to content

Commit 3c55116

Browse files
committed
update test_branch to deal with slashes
1 parent 3d01df5 commit 3c55116

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

jupyterlab_git/git.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ async def get_upstream_branch(self, current_path, branch_name):
940940
command, cwd=os.path.join(self.root_dir, current_path)
941941
)
942942
if code != 0:
943-
return {"code": code, "command": " ".join(cmd), "message": error}
943+
return {"code": code, "command": " ".join(command), "message": error}
944944
rev_parse_output = output.strip()
945945

946946
command = ["git", "config", "--local", f"branch.{branch_name}.remote"]

jupyterlab_git/tests/test_branch.py

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ async def test_get_current_branch_success():
4444

4545
# Then
4646
mock_execute.assert_called_once_with(
47-
["git", "symbolic-ref", "HEAD"], cwd=os.path.join("/bin", "test_curr_path")
47+
["git", "symbolic-ref", "--short", "HEAD"], cwd=os.path.join("/bin", "test_curr_path")
4848
)
4949
assert "feature-foo" == actual_response
5050

@@ -337,11 +337,11 @@ async def test_get_current_branch_failure():
337337

338338
# Then
339339
mock_execute.assert_called_once_with(
340-
["git", "symbolic-ref", "HEAD"], cwd=os.path.join("/bin", "test_curr_path")
340+
["git", "symbolic-ref", "--short", "HEAD"], cwd=os.path.join("/bin", "test_curr_path")
341341
)
342342
assert (
343343
"Error [fatal: Not a git repository (or any of the parent directories): .git] "
344-
"occurred while executing [git symbolic-ref HEAD] command to get current branch."
344+
"occurred while executing [git symbolic-ref --short HEAD] command to get current branch."
345345
== str(error.value)
346346
)
347347

@@ -403,29 +403,42 @@ async def test_get_current_branch_detached_failure():
403403

404404
@pytest.mark.asyncio
405405
@pytest.mark.parametrize(
406-
"branch,upstream",
406+
"branch,upstream,remotename",
407407
[
408-
("feature-foo", "origin/master"),
409-
("master", "origin/master"),
410-
("feature-bar", "feature-foo"),
408+
("feature-foo", "master", "origin/withslash"),
409+
("master", "master", "origin"),
410+
("feature/bar", "feature-foo", ""),
411411
],
412412
)
413-
async def test_get_upstream_branch_success(branch, upstream):
413+
async def test_get_upstream_branch_success(branch, upstream, remotename):
414414
with patch("jupyterlab_git.git.execute") as mock_execute:
415415
# Given
416-
mock_execute.return_value = maybe_future((0, upstream, ""))
416+
mock_execute.side_effect = [
417+
maybe_future((0, remotename + '/' + upstream, '')),
418+
maybe_future((0, remotename, ''))
419+
]
417420

418421
# When
419422
actual_response = await Git(FakeContentManager("/bin")).get_upstream_branch(
420423
current_path="test_curr_path", branch_name=branch
421424
)
422425

423426
# Then
424-
mock_execute.assert_called_once_with(
425-
["git", "rev-parse", "--abbrev-ref", "{}@{{upstream}}".format(branch)],
426-
cwd=os.path.join("/bin", "test_curr_path"),
427+
mock_execute.assert_has_calls(
428+
[
429+
call(
430+
["git", "rev-parse", "--abbrev-ref", "{}@{{upstream}}".format(branch)],
431+
cwd=os.path.join("/bin", "test_curr_path"),
432+
),
433+
call(
434+
['git', 'config', '--local', f'branch.{branch}.remote'],
435+
cwd='/bin/test_curr_path',
436+
),
437+
438+
],
439+
any_order=False,
427440
)
428-
assert upstream == actual_response
441+
assert {'code': 0, 'remote_branch': upstream, 'remote_short_name': remotename} == actual_response
429442

430443

431444
@pytest.mark.asyncio
@@ -454,17 +467,12 @@ async def test_get_upstream_branch_failure(outputs, message):
454467
mock_execute.return_value = maybe_future(outputs)
455468

456469
# When
457-
if message:
458-
with pytest.raises(Exception) as error:
459-
await Git(FakeContentManager("/bin")).get_upstream_branch(
460-
current_path="test_curr_path", branch_name="blah"
461-
)
462-
assert message == str(error.value)
463-
else:
464-
response = await Git(FakeContentManager("/bin")).get_upstream_branch(
465-
current_path="test_curr_path", branch_name="blah"
466-
)
467-
assert response is None
470+
response = await Git(FakeContentManager("/bin")).get_upstream_branch(
471+
current_path="test_curr_path", branch_name="blah"
472+
)
473+
expected = {'code': 128, 'command': 'git rev-parse --abbrev-ref blah@{upstream}', 'message': outputs[2]}
474+
475+
assert response == expected
468476

469477
# Then
470478
mock_execute.assert_has_calls(
@@ -807,7 +815,7 @@ async def test_branch_success_detached_head():
807815
),
808816
# call to get current branch
809817
call(
810-
["git", "symbolic-ref", "HEAD"],
818+
["git", "symbolic-ref", "--short", "HEAD"],
811819
cwd=os.path.join("/bin", "test_curr_path"),
812820
),
813821
# call to get current branch name given a detached head

0 commit comments

Comments
 (0)