Skip to content

Commit f838270

Browse files
authored
Merge pull request #490 from fcollonval/patch-less-test-more
Use ServerTest for handler testing
2 parents 813c7b3 + 253e69f commit f838270

File tree

4 files changed

+378
-402
lines changed

4 files changed

+378
-402
lines changed

jupyterlab_git/git.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Git:
6767

6868
def __init__(self, contents_manager):
6969
self.contents_manager = contents_manager
70-
self.root_dir = os.path.realpath(os.path.expanduser(contents_manager.root_dir))
70+
self.root_dir = os.path.expanduser(contents_manager.root_dir)
7171

7272
def config(self, top_repo_path, **kwargs):
7373
"""Get or set Git options.

jupyterlab_git/handlers.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -503,31 +503,31 @@ def setup_handlers(web_app):
503503
"""
504504

505505
git_handlers = [
506-
("/git/show_top_level", GitShowTopLevelHandler),
507-
("/git/show_prefix", GitShowPrefixHandler),
508506
("/git/add", GitAddHandler),
509507
("/git/add_all_unstaged", GitAddAllUnstagedHandler),
510508
("/git/add_all_untracked", GitAddAllUntrackedHandler),
511-
("/git/status", GitStatusHandler),
509+
("/git/all_history", GitAllHistoryHandler),
512510
("/git/branch", GitBranchHandler),
513-
("/git/reset", GitResetHandler),
514-
("/git/delete_commit", GitDeleteCommitHandler),
515-
("/git/reset_to_commit", GitResetToCommitHandler),
511+
("/git/changed_files", GitChangedFilesHandler),
516512
("/git/checkout", GitCheckoutHandler),
513+
("/git/clone", GitCloneHandler),
517514
("/git/commit", GitCommitHandler),
518-
("/git/pull", GitPullHandler),
519-
("/git/push", GitPushHandler),
520-
("/git/diff", GitDiffHandler),
521-
("/git/log", GitLogHandler),
515+
("/git/config", GitConfigHandler),
516+
("/git/delete_commit", GitDeleteCommitHandler),
522517
("/git/detailed_log", GitDetailedLogHandler),
518+
("/git/diff", GitDiffHandler),
519+
("/git/diffcontent", GitDiffContentHandler),
523520
("/git/init", GitInitHandler),
524-
("/git/all_history", GitAllHistoryHandler),
525-
("/git/clone", GitCloneHandler),
526-
("/git/upstream", GitUpstreamHandler),
527-
("/git/config", GitConfigHandler),
528-
("/git/changed_files", GitChangedFilesHandler),
521+
("/git/log", GitLogHandler),
522+
("/git/pull", GitPullHandler),
523+
("/git/push", GitPushHandler),
524+
("/git/reset", GitResetHandler),
525+
("/git/reset_to_commit", GitResetToCommitHandler),
529526
("/git/server_root", GitServerRootHandler),
530-
("/git/diffcontent", GitDiffContentHandler)
527+
("/git/show_prefix", GitShowPrefixHandler),
528+
("/git/show_top_level", GitShowTopLevelHandler),
529+
("/git/status", GitStatusHandler),
530+
("/git/upstream", GitUpstreamHandler),
531531
]
532532

533533
# add the baseurl to our paths

jupyterlab_git/tests/test_config.py

Lines changed: 69 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -7,103 +7,89 @@
77
from jupyterlab_git.git import Git
88
from jupyterlab_git.handlers import GitConfigHandler
99

10-
from .testutils import FakeContentManager
10+
from .testutils import FakeContentManager, ServerTest
1111

1212

13-
@patch("jupyterlab_git.handlers.GitConfigHandler.__init__", Mock(return_value=None))
14-
@patch(
15-
"jupyterlab_git.handlers.GitConfigHandler.get_json_body",
16-
Mock(return_value={"path": "test_path"}),
17-
)
18-
@patch("jupyterlab_git.handlers.GitConfigHandler.git", Git(FakeContentManager("/bin")))
19-
@patch("jupyterlab_git.handlers.GitConfigHandler.finish")
20-
@patch("subprocess.Popen")
21-
def test_git_get_config_success(popen, finish):
22-
# Given
23-
process_mock = Mock()
24-
attrs = {
25-
"communicate": Mock(
26-
return_value=(
27-
b"user.name=John Snow\n[email protected]",
28-
b"",
29-
)
30-
),
31-
"returncode": 0,
32-
}
33-
process_mock.configure_mock(**attrs)
34-
popen.return_value = process_mock
35-
36-
# When
37-
GitConfigHandler().post()
13+
class TestConfig(ServerTest):
14+
@patch("subprocess.Popen")
15+
def test_git_get_config_success(self, popen):
16+
# Given
17+
process_mock = Mock()
18+
attrs = {
19+
"communicate": Mock(
20+
return_value=(
21+
b"user.name=John Snow\n[email protected]",
22+
b"",
23+
)
24+
),
25+
"returncode": 0,
26+
}
27+
process_mock.configure_mock(**attrs)
28+
popen.return_value = process_mock
3829

39-
# Then
40-
popen.assert_called_once_with(
41-
["git", "config", "--list"],
42-
stdout=subprocess.PIPE,
43-
stderr=subprocess.PIPE,
44-
cwd="test_path",
45-
)
46-
process_mock.communicate.assert_called_once_with()
30+
# When
31+
body = {"path": "test_path"}
32+
response = self.tester.post(["config"], body=body)
4733

48-
finish.assert_called_once_with(
49-
json.dumps(
50-
{
51-
"code": 0,
52-
"options": {
53-
"user.name": "John Snow",
54-
"user.email": "[email protected]",
55-
},
56-
}
34+
# Then
35+
popen.assert_called_once_with(
36+
["git", "config", "--list"],
37+
stdout=subprocess.PIPE,
38+
stderr=subprocess.PIPE,
39+
cwd="test_path",
5740
)
58-
)
41+
process_mock.communicate.assert_called_once_with()
5942

43+
assert response.status_code == 201
44+
payload = response.json()
45+
assert payload == {
46+
"code": 0,
47+
"options": {
48+
"user.name": "John Snow",
49+
"user.email": "[email protected]",
50+
},
51+
}
6052

61-
@patch("jupyterlab_git.handlers.GitConfigHandler.__init__", Mock(return_value=None))
62-
@patch(
63-
"jupyterlab_git.handlers.GitConfigHandler.get_json_body",
64-
Mock(
65-
return_value={
53+
@patch("subprocess.Popen")
54+
def test_git_set_config_success(self, popen):
55+
# Given
56+
process_mock = Mock()
57+
attrs = {"communicate": Mock(return_value=(b"", b"")), "returncode": 0}
58+
process_mock.configure_mock(**attrs)
59+
popen.return_value = process_mock
60+
61+
# When
62+
body = {
6663
"path": "test_path",
6764
"options": {
6865
"user.name": "John Snow",
6966
"user.email": "[email protected]",
7067
},
7168
}
72-
),
73-
)
74-
@patch("jupyterlab_git.handlers.GitConfigHandler.git", Git(FakeContentManager('/bin')))
75-
@patch("jupyterlab_git.handlers.GitConfigHandler.finish")
76-
@patch("subprocess.Popen")
77-
def test_git_set_config_success(popen, finish):
78-
# Given
79-
process_mock = Mock()
80-
attrs = {"communicate": Mock(return_value=(b"", b"")), "returncode": 0}
81-
process_mock.configure_mock(**attrs)
82-
popen.return_value = process_mock
83-
84-
# When
85-
GitConfigHandler().post()
69+
response = self.tester.post(["config"], body=body)
8670

87-
# Then
88-
assert popen.call_count == 2
89-
assert (
90-
call(
91-
["git", "config", "--add", "user.email", "[email protected]"],
92-
stdout=subprocess.PIPE,
93-
stderr=subprocess.PIPE,
94-
cwd="test_path",
71+
# Then
72+
assert popen.call_count == 2
73+
assert (
74+
call(
75+
["git", "config", "--add", "user.email", "[email protected]"],
76+
stdout=subprocess.PIPE,
77+
stderr=subprocess.PIPE,
78+
cwd="test_path",
79+
)
80+
in popen.call_args_list
9581
)
96-
in popen.call_args_list
97-
)
98-
assert (
99-
call(
100-
["git", "config", "--add", "user.name", "John Snow"],
101-
stdout=subprocess.PIPE,
102-
stderr=subprocess.PIPE,
103-
cwd="test_path",
82+
assert (
83+
call(
84+
["git", "config", "--add", "user.name", "John Snow"],
85+
stdout=subprocess.PIPE,
86+
stderr=subprocess.PIPE,
87+
cwd="test_path",
88+
)
89+
in popen.call_args_list
10490
)
105-
in popen.call_args_list
106-
)
107-
assert process_mock.communicate.call_count == 2
91+
assert process_mock.communicate.call_count == 2
10892

109-
finish.assert_called_once_with(json.dumps({"code": 0, "message": ""}))
93+
assert response.status_code == 201
94+
payload = response.json()
95+
assert payload == {"code": 0, "message": ""}

0 commit comments

Comments
 (0)