test_svn.py
195 lines
| 6.1 KiB
| text/x-python
|
PythonLexer
r5088 | # Copyright (C) 2010-2023 RhodeCode GmbH | |||
r1 | # | |||
# This program is free software: you can redistribute it and/or modify | ||||
# it under the terms of the GNU Affero General Public License, version 3 | ||||
# (only), as published by the Free Software Foundation. | ||||
# | ||||
# This program is distributed in the hope that it will be useful, | ||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
# | ||||
# You should have received a copy of the GNU Affero General Public License | ||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
# | ||||
# This program is dual-licensed. If you wish to learn more about the | ||||
# RhodeCode Enterprise Edition, including its added features, Support services, | ||||
# and proprietary license terms, please see https://rhodecode.com/licenses/ | ||||
import os | ||||
import mock | ||||
import pytest | ||||
r5087 | from rhodecode.lib.str_utils import safe_bytes | |||
r1 | from rhodecode.tests import SVN_REPO, TEST_DIR, TESTS_TMP_PATH | |||
from rhodecode.lib.vcs.backends.svn.repository import SubversionRepository | ||||
from rhodecode.lib.vcs.conf import settings | ||||
from rhodecode.lib.vcs.exceptions import VCSError | ||||
pytestmark = [ | ||||
pytest.mark.backends("svn"), | ||||
r2351 | pytest.mark.usefixtures("baseapp"), | |||
r1 | ] | |||
r3946 | @pytest.fixture() | |||
r2351 | def repo(baseapp): | |||
r1 | repo = SubversionRepository(os.path.join(TESTS_TMP_PATH, SVN_REPO)) | |||
return repo | ||||
r3946 | @pytest.fixture() | |||
r1 | def head(repo): | |||
return repo.get_commit() | ||||
def test_init_fails_if_path_does_not_exist(): | ||||
r5607 | path = os.path.join(TEST_DIR, "i-do-not-exist") | |||
r1 | with pytest.raises(VCSError): | |||
SubversionRepository(path) | ||||
def test_init_fails_if_path_is_not_a_valid_repository(tmpdir): | ||||
r5607 | path = str(tmpdir.mkdir("unicode ä")) | |||
r1 | with pytest.raises(VCSError): | |||
SubversionRepository(path) | ||||
def test_repo_clone(vcsbackend, reposerver): | ||||
source = vcsbackend.create_repo(number_of_commits=3) | ||||
reposerver.serve(source) | ||||
r5607 | repo = SubversionRepository(vcsbackend.new_repo_path(), create=True, src_url=reposerver.url) | |||
r1 | ||||
assert source.commit_ids == repo.commit_ids | ||||
assert source[0].message == repo[0].message | ||||
def test_latest_commit(head): | ||||
r5607 | assert head.raw_id == "393" | |||
r1 | ||||
def test_commit_description(head): | ||||
assert head.message == """Added a symlink""" | ||||
def test_commit_author(head): | ||||
r5607 | assert head.author == "marcin" | |||
r1 | ||||
r5607 | @pytest.mark.parametrize( | |||
"filename, content, mime_type", | ||||
[ | ||||
(b"test.txt", b"Text content\n", None), | ||||
(b"test.bin", b"\0 binary \0", "application/octet-stream"), | ||||
], | ||||
ids=["text", "binary"], | ||||
) | ||||
r1 | def test_sets_mime_type_correctly(vcsbackend, filename, content, mime_type): | |||
repo = vcsbackend.create_repo() | ||||
vcsbackend.ensure_file(filename, content) | ||||
file_properties = repo._remote.node_properties(filename, 1) | ||||
r5607 | assert file_properties.get("svn:mime-type") == mime_type | |||
r1 | ||||
def test_slice_access(repo): | ||||
page_size = 5 | ||||
page = 0 | ||||
start = page * page_size | ||||
end = start + page_size - 1 | ||||
commits = list(repo[start:end]) | ||||
r5607 | assert [commit.raw_id for commit in commits] == ["1", "2", "3", "4"] | |||
r1 | ||||
def test_walk_changelog_page(repo): | ||||
page_size = 5 | ||||
page = 0 | ||||
start = page * page_size | ||||
end = start + page_size - 1 | ||||
commits = list(repo[start:end]) | ||||
r5607 | changelog = ["r%s, %s, %s" % (c.raw_id, c.author, c.message) for c in commits] | |||
r1 | ||||
expexted_messages = [ | ||||
r5607 | "r1, marcin, initial import", | |||
"r2, marcin, hg ignore", | ||||
"r3, marcin, Pip standards refactor", | ||||
"r4, marcin, Base repository few new functions added", | ||||
] | ||||
r1 | assert changelog == expexted_messages | |||
def test_read_full_file_tree(head): | ||||
for topnode, dirs, files in head.walk(): | ||||
for f in files: | ||||
len(f.content) | ||||
def test_topnode_files_attribute(head): | ||||
r5607 | topnode = head.get_node("") | |||
r1 | topnode.files | |||
r5607 | @pytest.mark.parametrize( | |||
"filename, content, branch, mime_type", | ||||
[ | ||||
("branches/plain/test.txt", b"Text content\n", "plain", None), | ||||
("branches/uniçö∂e/test.bin", b"\0 binary \0", "uniçö∂e", "application/octet-stream"), | ||||
], | ||||
ids=["text", "binary"], | ||||
) | ||||
r1 | def test_unicode_refs(vcsbackend, filename, content, branch, mime_type): | |||
r5087 | filename = safe_bytes(filename) | |||
r1 | repo = vcsbackend.create_repo() | |||
vcsbackend.ensure_file(filename, content) | ||||
r5607 | with mock.patch( | |||
("rhodecode.lib.vcs.backends.svn.repository" ".SubversionRepository._patterns_from_section"), | ||||
return_value=["branches/*"], | ||||
): | ||||
assert f"branches/{branch}" in repo.branches | ||||
r1 | ||||
def test_compatible_version(monkeypatch, vcsbackend): | ||||
r5607 | monkeypatch.setattr(settings, "SVN_COMPATIBLE_VERSION", "pre-1.8-compatible") | |||
r1 | path = vcsbackend.new_repo_path() | |||
SubversionRepository(path, create=True) | ||||
r5607 | with open(f"{path}/db/format") as f: | |||
r1 | first_line = f.readline().strip() | |||
r5607 | assert first_line == "4" | |||
r1 | ||||
def test_invalid_compatible_version(monkeypatch, vcsbackend): | ||||
r5607 | monkeypatch.setattr(settings, "SVN_COMPATIBLE_VERSION", "i-am-an-invalid-setting") | |||
r1 | path = vcsbackend.new_repo_path() | |||
with pytest.raises(Exception): | ||||
SubversionRepository(path, create=True) | ||||
class TestSVNCommit(object): | ||||
@pytest.fixture(autouse=True) | ||||
def prepare(self, repo): | ||||
self.repo = repo | ||||
def test_file_history_from_commits(self): | ||||
r5607 | node = self.repo[10].get_node("setup.py") | |||
r1 | commit_ids = [commit.raw_id for commit in node.history] | |||
r5607 | assert ["8"] == commit_ids | |||
r1 | ||||
r5607 | node = self.repo[20].get_node("setup.py") | |||
r1 | node_ids = [commit.raw_id for commit in node.history] | |||
r5607 | assert ["18", "8"] == node_ids | |||
r1 | ||||
# special case we check history from commit that has this particular | ||||
# file changed this means we check if it's included as well | ||||
r5607 | node = self.repo.get_commit("18").get_node("setup.py") | |||
r1 | node_ids = [commit.raw_id for commit in node.history] | |||
r5607 | assert ["18", "8"] == node_ids | |||
r5087 | ||||
def test_repo_files_content_type(self): | ||||
test_commit = self.repo.get_commit(commit_idx=100) | ||||
r5607 | for node in test_commit.get_node("/"): | |||
r5087 | if node.is_file(): | |||
assert type(node.content) == bytes | ||||
assert type(node.str_content) == str | ||||