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