Show More
@@ -1,144 +1,130 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2010-2018 RhodeCode GmbH |
|
3 | # Copyright (C) 2010-2018 RhodeCode GmbH | |
4 | # |
|
4 | # | |
5 | # This program is free software: you can redistribute it and/or modify |
|
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |
7 | # (only), as published by the Free Software Foundation. |
|
7 | # (only), as published by the Free Software Foundation. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License |
|
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | # |
|
16 | # | |
17 | # This program is dual-licensed. If you wish to learn more about the |
|
17 | # This program is dual-licensed. If you wish to learn more about the | |
18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
20 |
|
20 | |||
21 | """ |
|
21 | """ | |
22 | Tests for main module's methods. |
|
22 | Tests for main module's methods. | |
23 | """ |
|
23 | """ | |
24 |
|
24 | import os | ||
|
25 | import tempfile | |||
|
26 | import shutil | |||
25 | import mock |
|
27 | import mock | |
26 | import os |
|
|||
27 | import shutil |
|
|||
28 | import tempfile |
|
|||
29 |
|
||||
30 | import pytest |
|
28 | import pytest | |
31 |
|
29 | |||
32 | from rhodecode.lib.vcs import VCSError, get_backend, get_vcs_instance |
|
30 | from rhodecode.lib.vcs import VCSError, get_backend, get_vcs_instance | |
33 | from rhodecode.lib.vcs.backends.hg import MercurialRepository |
|
|||
34 | from rhodecode.tests import TEST_HG_REPO, TEST_GIT_REPO |
|
|||
35 |
|
31 | |||
36 |
|
32 | |||
37 | pytestmark = pytest.mark.usefixtures("baseapp") |
|
33 | pytestmark = pytest.mark.usefixtures("baseapp") | |
38 |
|
34 | |||
39 |
|
35 | |||
40 | def test_get_backend(): |
|
36 | def test_get_backend(backend): | |
41 | hg = get_backend('hg') |
|
37 | repo_class = get_backend(backend.alias) | |
42 | assert hg == MercurialRepository |
|
38 | assert repo_class == backend.repo.scm_instance().__class__ | |
43 |
|
39 | |||
44 |
|
40 | |||
45 |
def test_alias_detect |
|
41 | def test_alias_detect(backend): | |
46 | alias = 'hg' |
|
42 | alias = backend.alias | |
47 | path = TEST_HG_REPO |
|
43 | path = backend.repo.scm_instance().path | |
48 | backend = get_backend(alias) |
|
|||
49 | repo = backend(path) |
|
|||
50 | assert 'hg' == repo.alias |
|
|||
51 |
|
44 | |||
|
45 | new_backend = get_backend(alias) | |||
|
46 | repo = new_backend(path) | |||
52 |
|
47 | |||
53 | def test_alias_detect_git(): |
|
48 | assert alias == repo.alias | |
54 | alias = 'git' |
|
|||
55 | path = TEST_GIT_REPO |
|
|||
56 | backend = get_backend(alias) |
|
|||
57 | repo = backend(path) |
|
|||
58 | assert 'git' == repo.alias |
|
|||
59 |
|
49 | |||
60 |
|
50 | |||
61 | def test_wrong_alias(): |
|
51 | def test_wrong_alias(): | |
62 | alias = 'wrong_alias' |
|
52 | alias = 'wrong_alias' | |
63 | with pytest.raises(VCSError): |
|
53 | with pytest.raises(VCSError): | |
64 | get_backend(alias) |
|
54 | get_backend(alias) | |
65 |
|
55 | |||
66 |
|
56 | |||
67 | def test_get_vcs_instance_by_path(vcs_repo): |
|
57 | def test_get_vcs_instance_by_path(vcs_repo): | |
68 | repo = get_vcs_instance(vcs_repo.path) |
|
58 | repo = get_vcs_instance(vcs_repo.path) | |
69 |
|
59 | |||
70 | assert repo.__class__ == vcs_repo.__class__ |
|
60 | assert repo.__class__ == vcs_repo.__class__ | |
71 | assert repo.path == vcs_repo.path |
|
61 | assert repo.path == vcs_repo.path | |
72 | assert repo.alias == vcs_repo.alias |
|
62 | assert repo.alias == vcs_repo.alias | |
73 | assert repo.name == vcs_repo.name |
|
63 | assert repo.name == vcs_repo.name | |
74 |
|
64 | |||
75 |
|
65 | |||
76 | @mock.patch('rhodecode.lib.vcs.backends.get_scm') |
|
66 | def test_get_vcs_instance_by_path_empty_dir(request, tmpdir): | |
77 | @mock.patch('rhodecode.lib.vcs.backends.get_backend') |
|
|||
78 | def test_get_vcs_instance_by_path_args_passed( |
|
|||
79 | get_backend_mock, get_scm_mock): |
|
|||
80 | """ |
|
|||
81 | Test that the arguments passed to ``get_vcs_instance_by_path`` are |
|
|||
82 | forewarded to the vcs backend class. |
|
|||
83 | """ |
|
|||
84 | backend = mock.MagicMock() |
|
|||
85 | get_backend_mock.return_value = backend |
|
|||
86 | args = ['these-are-test-args', 0, True, None] |
|
|||
87 | get_vcs_instance(TEST_HG_REPO, *args) |
|
|||
88 |
|
||||
89 | backend.assert_called_with(*args, repo_path=TEST_HG_REPO) |
|
|||
90 |
|
||||
91 |
|
||||
92 | @mock.patch('rhodecode.lib.vcs.backends.get_scm') |
|
|||
93 | @mock.patch('rhodecode.lib.vcs.backends.get_backend') |
|
|||
94 | def test_get_vcs_instance_by_path_kwargs_passed( |
|
|||
95 | get_backend_mock, get_scm_mock): |
|
|||
96 | """ |
|
|||
97 | Test that the keyword arguments passed to ``get_vcs_instance_by_path`` are |
|
|||
98 | forewarded to the vcs backend class. |
|
|||
99 | """ |
|
|||
100 | backend = mock.MagicMock() |
|
|||
101 | get_backend_mock.return_value = backend |
|
|||
102 | kwargs = { |
|
|||
103 | 'foo': 'these-are-test-args', |
|
|||
104 | 'bar': 0, |
|
|||
105 | 'baz': True, |
|
|||
106 | 'foobar': None |
|
|||
107 | } |
|
|||
108 | get_vcs_instance(TEST_HG_REPO, **kwargs) |
|
|||
109 |
|
||||
110 | backend.assert_called_with(repo_path=TEST_HG_REPO, **kwargs) |
|
|||
111 |
|
||||
112 |
|
||||
113 | def test_get_vcs_instance_by_path_err(request): |
|
|||
114 | """ |
|
67 | """ | |
115 | Test that ``get_vcs_instance_by_path`` returns None if a path is passed |
|
68 | Test that ``get_vcs_instance_by_path`` returns None if a path is passed | |
116 | to an empty directory. |
|
69 | to an empty directory. | |
117 | """ |
|
70 | """ | |
118 | empty_dir = tempfile.mkdtemp(prefix='pytest-empty-dir-') |
|
71 | empty_dir = str(tmpdir) | |
119 |
|
||||
120 | def fin(): |
|
|||
121 | shutil.rmtree(empty_dir) |
|
|||
122 | request.addfinalizer(fin) |
|
|||
123 |
|
||||
124 | repo = get_vcs_instance(empty_dir) |
|
72 | repo = get_vcs_instance(empty_dir) | |
125 |
|
||||
126 | assert repo is None |
|
73 | assert repo is None | |
127 |
|
74 | |||
128 |
|
75 | |||
129 | def test_get_vcs_instance_by_path_multiple_repos(request): |
|
76 | def test_get_vcs_instance_by_path_multiple_repos(request): | |
130 | """ |
|
77 | """ | |
131 | Test that ``get_vcs_instance_by_path`` returns None if a path is passed |
|
78 | Test that ``get_vcs_instance_by_path`` returns None if a path is passed | |
132 | to a directory with multiple repositories. |
|
79 | to a directory with multiple repositories. | |
133 | """ |
|
80 | """ | |
134 | empty_dir = tempfile.mkdtemp(prefix='pytest-empty-dir-') |
|
81 | empty_dir = tempfile.mkdtemp(prefix='pytest-empty-dir-') | |
135 | os.mkdir(os.path.join(empty_dir, '.git')) |
|
82 | os.mkdir(os.path.join(empty_dir, '.git')) | |
136 | os.mkdir(os.path.join(empty_dir, '.hg')) |
|
83 | os.mkdir(os.path.join(empty_dir, '.hg')) | |
137 |
|
84 | |||
138 | def fin(): |
|
85 | def fin(): | |
139 | shutil.rmtree(empty_dir) |
|
86 | shutil.rmtree(empty_dir) | |
140 | request.addfinalizer(fin) |
|
87 | request.addfinalizer(fin) | |
141 |
|
88 | |||
142 | repo = get_vcs_instance(empty_dir) |
|
89 | repo = get_vcs_instance(empty_dir) | |
143 |
|
90 | |||
144 | assert repo is None |
|
91 | assert repo is None | |
|
92 | ||||
|
93 | ||||
|
94 | @mock.patch('rhodecode.lib.vcs.backends.get_scm') | |||
|
95 | @mock.patch('rhodecode.lib.vcs.backends.get_backend') | |||
|
96 | def test_get_vcs_instance_by_path_args_passed( | |||
|
97 | get_backend_mock, get_scm_mock, tmpdir, vcs_repo): | |||
|
98 | """ | |||
|
99 | Test that the arguments passed to ``get_vcs_instance_by_path`` are | |||
|
100 | forwarded to the vcs backend class. | |||
|
101 | """ | |||
|
102 | backend = mock.MagicMock() | |||
|
103 | get_backend_mock.return_value = backend | |||
|
104 | args = ['these-are-test-args', 0, True, None] | |||
|
105 | repo = vcs_repo.path | |||
|
106 | get_vcs_instance(repo, *args) | |||
|
107 | ||||
|
108 | backend.assert_called_with(*args, repo_path=repo) | |||
|
109 | ||||
|
110 | ||||
|
111 | @mock.patch('rhodecode.lib.vcs.backends.get_scm') | |||
|
112 | @mock.patch('rhodecode.lib.vcs.backends.get_backend') | |||
|
113 | def test_get_vcs_instance_by_path_kwargs_passed( | |||
|
114 | get_backend_mock, get_scm_mock, vcs_repo): | |||
|
115 | """ | |||
|
116 | Test that the keyword arguments passed to ``get_vcs_instance_by_path`` are | |||
|
117 | forwarded to the vcs backend class. | |||
|
118 | """ | |||
|
119 | backend = mock.MagicMock() | |||
|
120 | get_backend_mock.return_value = backend | |||
|
121 | kwargs = { | |||
|
122 | 'foo': 'these-are-test-args', | |||
|
123 | 'bar': 0, | |||
|
124 | 'baz': True, | |||
|
125 | 'foobar': None | |||
|
126 | } | |||
|
127 | repo = vcs_repo.path | |||
|
128 | get_vcs_instance(repo, **kwargs) | |||
|
129 | ||||
|
130 | backend.assert_called_with(repo_path=repo, **kwargs) |
General Comments 0
You need to be logged in to leave comments.
Login now