Skip to content

Commit 2a9006c

Browse files
committed
Fix status error on empty string
1 parent 9a85fa7 commit 2a9006c

File tree

2 files changed

+60
-25
lines changed

2 files changed

+60
-25
lines changed

jupyterlab_git/git.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,17 @@ def call_subprocess(
101101

102102
return code, output, error
103103

104-
def strip_and_split(s):
105-
"""strip trailing \x00 and split on \x00
104+
def strip_and_split(s: "str") -> "List[str]":
105+
"""Strip trailing \x00 and split on \x00 a string.
106106
107107
Useful for parsing output of git commands with -z flag.
108108
"""
109-
return s.strip("\x00").split("\x00")
109+
s = s.strip("\x00")
110+
if len(s) > 0:
111+
return s.split("\x00")
112+
else:
113+
return list()
114+
110115

111116
class Git:
112117
"""
@@ -263,7 +268,7 @@ async def status(self, current_path):
263268
"to": line[3:],
264269
# if file was renamed, next line contains original path
265270
"from": next(line_iterable) if line[0]=='R' else line[3:]
266-
})
271+
})
267272
return {"code": code, "files": result}
268273

269274
async def log(self, current_path, history_count=10):

jupyterlab_git/tests/test_status.py

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,67 @@
1010

1111
from .testutils import FakeContentManager
1212

13+
1314
@pytest.mark.asyncio
14-
async def test_status():
15+
@pytest.mark.parametrize(
16+
"output,expected",
17+
[
18+
(
19+
(
20+
"A notebook with spaces.ipynb",
21+
"M notebook with λ.ipynb",
22+
"R renamed_to_θ.py",
23+
"originally_named_π.py",
24+
"?? untracked.ipynb",
25+
),
26+
[
27+
{
28+
"x": "A",
29+
"y": " ",
30+
"to": "notebook with spaces.ipynb",
31+
"from": "notebook with spaces.ipynb",
32+
},
33+
{
34+
"x": "M",
35+
"y": " ",
36+
"to": "notebook with λ.ipynb",
37+
"from": "notebook with λ.ipynb",
38+
},
39+
{
40+
"x": "R",
41+
"y": " ",
42+
"to": "renamed_to_θ.py",
43+
"from": "originally_named_π.py",
44+
},
45+
{
46+
"x": "?",
47+
"y": "?",
48+
"to": "untracked.ipynb",
49+
"from": "untracked.ipynb",
50+
},
51+
],
52+
),
53+
((""), ([])), # Empty answer
54+
],
55+
)
56+
async def test_status(output, expected):
1557
with patch("jupyterlab_git.git.execute") as mock_execute:
1658
# Given
17-
process_output = (
18-
"A notebook with spaces.ipynb",
19-
"M notebook with λ.ipynb",
20-
"R renamed_to_θ.py",
21-
"originally_named_π.py",
22-
"?? untracked.ipynb",
23-
)
24-
25-
expected_resonse = [
26-
{"x": "A", "y": " ", "to": "notebook with spaces.ipynb", "from": "notebook with spaces.ipynb"},
27-
{"x": "M", "y": " ", "to": "notebook with λ.ipynb", "from": "notebook with λ.ipynb"},
28-
{"x": "R", "y": " ", "to": "renamed_to_θ.py", "from": "originally_named_π.py"},
29-
{"x": "?", "y": "?", "to": "untracked.ipynb", "from": "untracked.ipynb"},
30-
]
59+
root = "/bin"
60+
repository = "test_curr_path"
3161
mock_execute.return_value = tornado.gen.maybe_future(
32-
(0, "\x00".join(process_output), "")
33-
62+
(0, "\x00".join(output), "")
3463
)
3564

3665
# When
37-
actual_response = await Git(FakeContentManager("/bin")).status(
38-
current_path="test_curr_path"
66+
actual_response = await Git(FakeContentManager(root)).status(
67+
current_path=repository
3968
)
4069

4170
# Then
4271
mock_execute.assert_called_once_with(
43-
["git", "status", "--porcelain" , "-u", "-z"], cwd="/bin/test_curr_path"
72+
["git", "status", "--porcelain", "-u", "-z"],
73+
cwd=os.path.join(root, repository),
4474
)
4575

46-
assert {"code": 0, "files": expected_resonse} == actual_response
76+
assert {"code": 0, "files": expected} == actual_response

0 commit comments

Comments
 (0)