Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/git/tests/test_converters.py
# Copyright (C) 2015-2018 The Software Heritage developers | # Copyright (C) 2015-2018 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import os | import os | ||||
import shutil | import shutil | ||||
import subprocess | import subprocess | ||||
import tempfile | import tempfile | ||||
import unittest | |||||
import dulwich.repo | import dulwich.repo | ||||
import pytest | import pytest | ||||
import swh.loader.git.converters as converters | import swh.loader.git.converters as converters | ||||
from swh.model.hashutil import bytehex_to_hash, hash_to_bytes | from swh.model.hashutil import bytehex_to_hash, hash_to_bytes | ||||
from swh.model.model import ( | from swh.model.model import ( | ||||
Content, | Content, | ||||
▲ Show 20 Lines • Show All 92 Lines • ▼ Show 20 Lines | ): | ||||
self._tag_timezone_neg_utc = False | self._tag_timezone_neg_utc = False | ||||
def sha(self): | def sha(self): | ||||
from hashlib import sha1 | from hashlib import sha1 | ||||
return sha1() | return sha1() | ||||
@pytest.mark.fs | @pytest.mark.fs | ||||
olasd: I don't think this mark is useful anymore (it used to mark tests that would depend on resources… | |||||
class TestConverters(unittest.TestCase): | class TestConverters: | ||||
@classmethod | @classmethod | ||||
def setUpClass(cls): | def setup_class(cls): | ||||
super().setUpClass() | |||||
cls.repo_path = tempfile.mkdtemp() | cls.repo_path = tempfile.mkdtemp() | ||||
bundle = os.path.join(TEST_DATA, "git-repos", "example-submodule.bundle") | bundle = os.path.join(TEST_DATA, "git-repos", "example-submodule.bundle") | ||||
git = subprocess.Popen( | git = subprocess.Popen( | ||||
["git", "clone", "--quiet", "--bare", "--mirror", bundle, cls.repo_path], | ["git", "clone", "--quiet", "--bare", "--mirror", bundle, cls.repo_path], | ||||
cwd=TEST_DATA, | cwd=TEST_DATA, | ||||
) | ) | ||||
Show All 24 Lines | def test_blob_to_content(self): | ||||
b'[submodule "example-dependency"]\n' | b'[submodule "example-dependency"]\n' | ||||
b"\tpath = example-dependency\n" | b"\tpath = example-dependency\n" | ||||
b"\turl = https://github.com/githubtraining/" | b"\turl = https://github.com/githubtraining/" | ||||
b"example-dependency.git\n" | b"example-dependency.git\n" | ||||
), | ), | ||||
length=124, | length=124, | ||||
status="visible", | status="visible", | ||||
) | ) | ||||
self.assertEqual(content, expected_content) | assert content == expected_content | ||||
def test_convertion_wrong_input(self): | def test_convertion_wrong_input(self): | ||||
class Something: | class Something: | ||||
type_name = b"something-not-the-right-type" | type_name = b"something-not-the-right-type" | ||||
m = { | m = { | ||||
"blob": converters.dulwich_blob_to_content, | "blob": converters.dulwich_blob_to_content, | ||||
"blob2": converters.dulwich_blob_to_content_id, | "blob2": converters.dulwich_blob_to_content_id, | ||||
"tree": converters.dulwich_tree_to_directory, | "tree": converters.dulwich_tree_to_directory, | ||||
"commit": converters.dulwich_tree_to_directory, | "commit": converters.dulwich_tree_to_directory, | ||||
"tag": converters.dulwich_tag_to_release, | "tag": converters.dulwich_tag_to_release, | ||||
} | } | ||||
for _callable in m.values(): | for _callable in m.values(): | ||||
with self.assertRaises(ValueError): | with pytest.raises(ValueError): | ||||
_callable(Something()) | _callable(Something()) | ||||
def test_commit_to_revision(self): | def test_commit_to_revision(self): | ||||
sha1 = b"9768d0b576dbaaecd80abedad6dfd0d72f1476da" | sha1 = b"9768d0b576dbaaecd80abedad6dfd0d72f1476da" | ||||
revision = converters.dulwich_commit_to_revision(self.repo[sha1]) | revision = converters.dulwich_commit_to_revision(self.repo[sha1]) | ||||
expected_revision = Revision( | expected_revision = Revision( | ||||
id=hash_to_bytes("9768d0b576dbaaecd80abedad6dfd0d72f1476da"), | id=hash_to_bytes("9768d0b576dbaaecd80abedad6dfd0d72f1476da"), | ||||
Show All 21 Lines | def test_commit_to_revision(self): | ||||
timestamp=Timestamp(seconds=1443083765, microseconds=0,), | timestamp=Timestamp(seconds=1443083765, microseconds=0,), | ||||
negative_utc=False, | negative_utc=False, | ||||
offset=120, | offset=120, | ||||
), | ), | ||||
parents=(b"\xc3\xc5\x88q23`\x9f[\xbb\xb2\xd9\xe7\xf3\xfbJf\x0f?r",), | parents=(b"\xc3\xc5\x88q23`\x9f[\xbb\xb2\xd9\xe7\xf3\xfbJf\x0f?r",), | ||||
synthetic=False, | synthetic=False, | ||||
) | ) | ||||
self.assertEqual(revision, expected_revision) | assert revision == expected_revision | ||||
def test_commit_to_revision_with_extra_headers(self): | def test_commit_to_revision_with_extra_headers(self): | ||||
sha1 = b"322f5bc915e50fc25e85226b5a182bded0e98e4b" | sha1 = b"322f5bc915e50fc25e85226b5a182bded0e98e4b" | ||||
revision = converters.dulwich_commit_to_revision(self.repo[sha1]) | revision = converters.dulwich_commit_to_revision(self.repo[sha1]) | ||||
expected_revision = Revision( | expected_revision = Revision( | ||||
id=hash_to_bytes(sha1.decode()), | id=hash_to_bytes(sha1.decode()), | ||||
directory=bytes.fromhex("f8ec06e4ed7b9fff4918a0241a48023143f30000"), | directory=bytes.fromhex("f8ec06e4ed7b9fff4918a0241a48023143f30000"), | ||||
▲ Show 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | def test_commit_to_revision_with_extra_headers_mergetag(self): | ||||
), | ), | ||||
synthetic=False, | synthetic=False, | ||||
) | ) | ||||
assert revision == expected_revision | assert revision == expected_revision | ||||
def test_author_line_to_author(self): | def test_author_line_to_author(self): | ||||
# edge case out of the way | # edge case out of the way | ||||
with self.assertRaises(TypeError): | with pytest.raises(TypeError): | ||||
converters.parse_author(None) | converters.parse_author(None) | ||||
tests = { | tests = { | ||||
b"a <b@c.com>": Person( | b"a <b@c.com>": Person( | ||||
name=b"a", email=b"b@c.com", fullname=b"a <b@c.com>", | name=b"a", email=b"b@c.com", fullname=b"a <b@c.com>", | ||||
), | ), | ||||
b"<foo@bar.com>": Person( | b"<foo@bar.com>": Person( | ||||
name=None, email=b"foo@bar.com", fullname=b"<foo@bar.com>", | name=None, email=b"foo@bar.com", fullname=b"<foo@bar.com>", | ||||
), | ), | ||||
b"malformed <email": Person( | b"malformed <email": Person( | ||||
name=b"malformed", email=b"email", fullname=b"malformed <email" | name=b"malformed", email=b"email", fullname=b"malformed <email" | ||||
), | ), | ||||
b"trailing <sp@c.e> ": Person( | b"trailing <sp@c.e> ": Person( | ||||
name=b"trailing", email=b"sp@c.e", fullname=b"trailing <sp@c.e> ", | name=b"trailing", email=b"sp@c.e", fullname=b"trailing <sp@c.e> ", | ||||
), | ), | ||||
b"no<sp@c.e>": Person(name=b"no", email=b"sp@c.e", fullname=b"no<sp@c.e>",), | b"no<sp@c.e>": Person(name=b"no", email=b"sp@c.e", fullname=b"no<sp@c.e>",), | ||||
b" <>": Person(name=None, email=None, fullname=b" <>",), | b" <>": Person(name=None, email=None, fullname=b" <>",), | ||||
b"something": Person(name=b"something", email=None, fullname=b"something"), | b"something": Person(name=b"something", email=None, fullname=b"something"), | ||||
} | } | ||||
for author in sorted(tests): | for author in sorted(tests): | ||||
parsed_author = tests[author] | parsed_author = tests[author] | ||||
self.assertEqual(parsed_author, converters.parse_author(author)) | assert parsed_author == converters.parse_author(author) | ||||
def test_dulwich_tag_to_release_no_author_no_date(self): | def test_dulwich_tag_to_release_no_author_no_date(self): | ||||
target = b"641fb6e08ddb2e4fd096dcf18e80b894bf" | target = b"641fb6e08ddb2e4fd096dcf18e80b894bf" | ||||
message = b"some release message" | message = b"some release message" | ||||
tag = SWHTag( | tag = SWHTag( | ||||
name=b"blah", | name=b"blah", | ||||
type_name=b"tag", | type_name=b"tag", | ||||
target=target, | target=target, | ||||
Show All 16 Lines | def test_dulwich_tag_to_release_no_author_no_date(self): | ||||
message=message, | message=message, | ||||
metadata=None, | metadata=None, | ||||
name=b"blah", | name=b"blah", | ||||
synthetic=False, | synthetic=False, | ||||
target=hash_to_bytes(target.decode()), | target=hash_to_bytes(target.decode()), | ||||
target_type=ObjectType.REVISION, | target_type=ObjectType.REVISION, | ||||
) | ) | ||||
self.assertEqual(actual_release, expected_release) | assert actual_release == expected_release | ||||
def test_dulwich_tag_to_release_author_and_date(self): | def test_dulwich_tag_to_release_author_and_date(self): | ||||
tagger = b"hey dude <hello@mail.org>" | tagger = b"hey dude <hello@mail.org>" | ||||
target = b"641fb6e08ddb2e4fd096dcf18e80b894bf" | target = b"641fb6e08ddb2e4fd096dcf18e80b894bf" | ||||
message = b"some release message" | message = b"some release message" | ||||
import datetime | import datetime | ||||
Show All 30 Lines | def test_dulwich_tag_to_release_author_and_date(self): | ||||
message=message, | message=message, | ||||
metadata=None, | metadata=None, | ||||
name=b"blah", | name=b"blah", | ||||
synthetic=False, | synthetic=False, | ||||
target=hash_to_bytes(target.decode()), | target=hash_to_bytes(target.decode()), | ||||
target_type=ObjectType.REVISION, | target_type=ObjectType.REVISION, | ||||
) | ) | ||||
self.assertEqual(actual_release, expected_release) | assert actual_release == expected_release | ||||
def test_dulwich_tag_to_release_author_no_date(self): | def test_dulwich_tag_to_release_author_no_date(self): | ||||
# to reproduce bug T815 (fixed) | # to reproduce bug T815 (fixed) | ||||
tagger = b"hey dude <hello@mail.org>" | tagger = b"hey dude <hello@mail.org>" | ||||
target = b"641fb6e08ddb2e4fd096dcf18e80b894bf" | target = b"641fb6e08ddb2e4fd096dcf18e80b894bf" | ||||
message = b"some release message" | message = b"some release message" | ||||
tag = SWHTag( | tag = SWHTag( | ||||
name=b"blah", | name=b"blah", | ||||
Show All 22 Lines | def test_dulwich_tag_to_release_author_no_date(self): | ||||
message=message, | message=message, | ||||
metadata=None, | metadata=None, | ||||
name=b"blah", | name=b"blah", | ||||
synthetic=False, | synthetic=False, | ||||
target=hash_to_bytes(target.decode()), | target=hash_to_bytes(target.decode()), | ||||
target_type=ObjectType.REVISION, | target_type=ObjectType.REVISION, | ||||
) | ) | ||||
self.assertEqual(actual_release, expected_release) | assert actual_release == expected_release | ||||
def test_dulwich_tag_to_release_signature(self): | def test_dulwich_tag_to_release_signature(self): | ||||
target = b"641fb6e08ddb2e4fd096dcf18e80b894bf" | target = b"641fb6e08ddb2e4fd096dcf18e80b894bf" | ||||
message = b"some release message" | message = b"some release message" | ||||
tag = SWHTag( | tag = SWHTag( | ||||
name=b"blah", | name=b"blah", | ||||
type_name=b"tag", | type_name=b"tag", | ||||
target=target, | target=target, | ||||
Show All 16 Lines | def test_dulwich_tag_to_release_signature(self): | ||||
message=message + GPGSIG, | message=message + GPGSIG, | ||||
metadata=None, | metadata=None, | ||||
name=b"blah", | name=b"blah", | ||||
synthetic=False, | synthetic=False, | ||||
target=hash_to_bytes(target.decode()), | target=hash_to_bytes(target.decode()), | ||||
target_type=ObjectType.REVISION, | target_type=ObjectType.REVISION, | ||||
) | ) | ||||
self.assertEqual(actual_release, expected_release) | assert actual_release == expected_release |
I don't think this mark is useful anymore (it used to mark tests that would depend on resources external to the current package)