Skip to content

Commit 5a5205c

Browse files
committed
Fix unit tests
1 parent 8ea5e4b commit 5a5205c

File tree

7 files changed

+59
-32
lines changed

7 files changed

+59
-32
lines changed

jupyterlab_git/git.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ async def status(self, current_path):
271271
are_binary = dict()
272272
if text_code == 0:
273273
for line in filter(lambda l: len(l) > 0, strip_and_split(text_output)):
274-
diff, name = line.rsplit(maxsplit=1)
274+
diff, name = line.rsplit("\t", maxsplit=1)
275275
are_binary[name] = diff.startswith("-\t-")
276276

277277
result = []
@@ -345,23 +345,12 @@ async def detailed_log(self, selected_hash, current_path):
345345
if code != 0:
346346
return {"code": code, "command": " ".join(cmd), "message": my_error}
347347

348-
# Test for binary file
349-
command = ["git", "diff", "--numstat", "-z", "4b825dc642cb6eb9a060e54bf8d69288fbee4904", selected_hash]
350-
text_code, text_output, _ = await execute(
351-
command,
352-
cwd=os.path.join(self.root_dir, current_path),
353-
)
354-
are_binary = dict()
355-
if text_code == 0:
356-
for line in filter(lambda l: len(l) > 0, strip_and_split(text_output)):
357-
diff, name = line.rsplit(maxsplit=1)
358-
are_binary[name] = diff.startswith("-\t-")
359-
360348
total_insertions = 0
361349
total_deletions = 0
362350
result = []
363351
line_iterable = iter(strip_and_split(my_output)[1:])
364352
for line in line_iterable:
353+
is_binary = line.startswith("-\t-\t")
365354
insertions, deletions, file = line.split('\t')
366355
insertions = insertions.replace('-', '0')
367356
deletions = deletions.replace('-', '0')
@@ -372,11 +361,9 @@ async def detailed_log(self, selected_hash, current_path):
372361
to_path = next(line_iterable)
373362
modified_file_name = from_path + " => " + to_path
374363
modified_file_path = to_path
375-
is_binary = are_binary.get(to_path, None)
376364
else:
377365
modified_file_name = file.split("/")[-1]
378366
modified_file_path = file
379-
is_binary = are_binary.get(file, None)
380367

381368
result.append({
382369
"modified_file_path": modified_file_path,
@@ -1057,9 +1044,7 @@ async def _is_binary(self, filename, ref, top_repo_path):
10571044
else:
10581045
command = ["git", "diff", "--numstat", "4b825dc642cb6eb9a060e54bf8d69288fbee4904", ref, "--", filename] # where 4b825... is a magic SHA which represents the empty tree
10591046
# code, output, error = await execute(command, cwd=top_repo_path)
1060-
r = await execute(command, cwd=top_repo_path)
1061-
print(r)
1062-
code, output, error = r
1047+
code, output, error = await execute(command, cwd=top_repo_path)
10631048

10641049
if code != 0:
10651050
err_msg = "fatal: Path '{}' does not exist (neither on disk nor in the index)".format(filename).lower()

jupyterlab_git/tests/test_detailed_log.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async def test_detailed_log():
2929
]
3030

3131

32-
mock_execute._mock_return_value = maybe_future(
32+
mock_execute.return_value = maybe_future(
3333
(0, "\x00".join(process_output)+"\x00", "")
3434
)
3535

@@ -45,42 +45,49 @@ async def test_detailed_log():
4545
"modified_file_name": "notebook_without_spaces.ipynb",
4646
"insertion": "10",
4747
"deletion": "3",
48+
"is_binary": False,
4849
},
4950
{
5051
"modified_file_path": "Notebook with spaces.ipynb",
5152
"modified_file_name": "Notebook with spaces.ipynb",
5253
"insertion": "11",
5354
"deletion": "4",
55+
"is_binary": False,
5456
},
5557
{
5658
"modified_file_path": "path/notebook_without_spaces.ipynb",
5759
"modified_file_name": "notebook_without_spaces.ipynb",
5860
"insertion": "12",
5961
"deletion": "5",
62+
"is_binary": False,
6063
},
6164
{
6265
"modified_file_path": "path/Notebook with spaces.ipynb",
6366
"modified_file_name": "Notebook with spaces.ipynb",
6467
"insertion": "13",
6568
"deletion": "6",
69+
"is_binary": False,
6670
},
6771
{
6872
"modified_file_path": "path/Notebook with λ.ipynb",
6973
"modified_file_name": "Notebook with λ.ipynb",
7074
"insertion": "14",
7175
"deletion": "1",
76+
"is_binary": False,
7277
},
7378
{
7479
"modified_file_path": "folder2/file with spaces.py",
7580
"modified_file_name": "folder1/file with spaces and λ.py => folder2/file with spaces.py",
7681
"insertion": "0",
7782
"deletion": "0",
83+
"is_binary": False,
7884
},
7985
{
8086
"modified_file_path": "binary_file.png",
8187
"modified_file_name": "binary_file.png",
8288
"insertion": "0",
8389
"deletion": "0",
90+
"is_binary": True,
8491
},
8592
],
8693
}

jupyterlab_git/tests/test_status.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,64 +13,97 @@
1313

1414
@pytest.mark.asyncio
1515
@pytest.mark.parametrize(
16-
"output,expected",
16+
"output,diff_output,expected",
1717
[
1818
(
1919
(
2020
"A notebook with spaces.ipynb",
2121
"M notebook with λ.ipynb",
22+
"M binary file.gif",
2223
"R renamed_to_θ.py",
2324
"originally_named_π.py",
2425
"?? untracked.ipynb",
2526
),
27+
(
28+
"0\t0\tnotebook with spaces.ipynb",
29+
"-\t-\tbinary file.gif",
30+
"0\t0\trenamed_to_θ.py",
31+
),
2632
[
2733
{
2834
"x": "A",
2935
"y": " ",
3036
"to": "notebook with spaces.ipynb",
3137
"from": "notebook with spaces.ipynb",
38+
"is_binary": False,
3239
},
3340
{
3441
"x": "M",
3542
"y": " ",
3643
"to": "notebook with λ.ipynb",
3744
"from": "notebook with λ.ipynb",
45+
"is_binary": None,
46+
},
47+
{
48+
"x": "M",
49+
"y": " ",
50+
"to": "binary file.gif",
51+
"from": "binary file.gif",
52+
"is_binary": True,
3853
},
3954
{
4055
"x": "R",
4156
"y": " ",
4257
"to": "renamed_to_θ.py",
4358
"from": "originally_named_π.py",
59+
"is_binary": False,
4460
},
4561
{
4662
"x": "?",
4763
"y": "?",
4864
"to": "untracked.ipynb",
4965
"from": "untracked.ipynb",
66+
"is_binary": None,
5067
},
5168
],
5269
),
53-
((""), ([])), # Empty answer
70+
((""), (""), ([])), # Empty answer
5471
],
5572
)
56-
async def test_status(output, expected):
73+
async def test_status(output, diff_output, expected):
5774
with patch("jupyterlab_git.git.execute") as mock_execute:
5875
# Given
5976
root = "/bin"
6077
repository = "test_curr_path"
61-
mock_execute.return_value = maybe_future(
62-
(0, "\x00".join(output)+"\x00", "")
63-
)
78+
mock_execute.side_effect = [
79+
maybe_future((0, "\x00".join(output) + "\x00", "")),
80+
maybe_future((0, "\x00".join(diff_output) + "\x00", "")),
81+
]
6482

6583
# When
6684
actual_response = await Git(FakeContentManager(root)).status(
6785
current_path=repository
6886
)
6987

7088
# Then
71-
mock_execute.assert_called_once_with(
72-
["git", "status", "--porcelain", "-u", "-z"],
73-
cwd=os.path.join(root, repository),
89+
mock_execute.assert_has_calls(
90+
[
91+
call(
92+
["git", "status", "--porcelain", "-u", "-z"],
93+
cwd=os.path.join(root, repository),
94+
),
95+
call(
96+
[
97+
"git",
98+
"diff",
99+
"--numstat",
100+
"-z",
101+
"--cached",
102+
"4b825dc642cb6eb9a060e54bf8d69288fbee4904",
103+
],
104+
cwd=os.path.join(root, repository),
105+
),
106+
]
74107
)
75108

76109
assert {"code": 0, "files": expected} == actual_response

src/components/diff/PlainTextDiff.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ export class PlainTextDiff extends React.Component<
122122
* @param currContent the raw value of the current content
123123
*/
124124
private _addDiffViewer(prevContent: string, currContent: string) {
125-
const mode = Mode.findBest(this.props.path);
125+
const mode =
126+
Mode.findByFileName(this.props.path) || Mode.findBest(this.props.path);
126127

127128
mergeView(this._mergeViewRef.current, {
128129
value: currContent,

tests/GitExtension.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ describe('IGitExtension', () => {
190190

191191
model.pathRepository = '/path/to/server/repo';
192192
await model.ready;
193-
status = [{ x: '', y: '', from: '', to: '' }];
193+
status = [{ x: '', y: '', from: '', to: '', is_binary: null }];
194194
await model.refreshStatus();
195195
expect(model.status).toHaveLength(1);
196196

@@ -220,7 +220,7 @@ describe('IGitExtension', () => {
220220

221221
model.pathRepository = '/path/to/server/repo';
222222
await model.ready;
223-
status = [{ x: '', y: '', from: '', to: '' }];
223+
status = [{ x: '', y: '', from: '', to: '', is_binary: null }];
224224
await model.refreshStatus();
225225
await testSignal;
226226
});

tests/test-components/Diff.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ describe('Diff', () => {
4040
);
4141

4242
it('should support diffing all files', function() {
43-
expect(isDiffSupported('/path/to/script.unk')).toBeTruthy();
43+
expect(isDiffSupported('/path/to/script.unk')).toBeFalsy();
4444
});
4545
});

tests/test-components/FileItem.spec.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe('FileItem', () => {
1111
y: 'M',
1212
to: 'some/file/path/file-name',
1313
from: '',
14+
is_binary: null,
1415
status: null
1516
},
1617
model: null,

0 commit comments

Comments
 (0)