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-2022 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 copy | import copy | ||||
import datetime | import datetime | ||||
import os | import os | ||||
import shutil | import shutil | ||||
▲ Show 20 Lines • Show All 267 Lines • ▼ Show 20 Lines | def test_commit_to_revision_with_extra_headers(self): | ||||
offset=120, | offset=120, | ||||
), | ), | ||||
parents=(bytes.fromhex("c730509025c6e81947102b2d77bc4dc1cade9489"),), | parents=(bytes.fromhex("c730509025c6e81947102b2d77bc4dc1cade9489"),), | ||||
synthetic=False, | synthetic=False, | ||||
) | ) | ||||
assert revision == expected_revision | assert revision == expected_revision | ||||
@pytest.mark.parametrize( | @pytest.mark.parametrize("attribute", ["message", "encoding", "author", "gpgsig"]) | ||||
"attribute", ["_message", "_encoding", "_author", "_gpgsig"] | |||||
) | |||||
def test_corrupt_commit(self, attribute): | def test_corrupt_commit(self, attribute): | ||||
# has a signature | sha = hash_to_bytes("c40d5a78d0d499296c101fd6e9fe161e2a9af43b") | ||||
sha1 = b"322f5bc915e50fc25e85226b5a182bded0e98e4b" | target = b"641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce" | ||||
commit = copy.deepcopy(self.repo[sha1]) | message = b"some commit message" | ||||
commit = dulwich.objects.Commit() | |||||
commit.tree = target | |||||
commit.message = message | |||||
commit.gpgsig = GPGSIG | |||||
commit.author = commit.committer = b"Foo <foo@example.org>" | |||||
commit.author_time = commit.commit_time = 1641980946 | |||||
commit.author_timezone = commit.commit_timezone = 60 | |||||
converters.dulwich_commit_to_revision(commit) | converters.dulwich_commit_to_revision(commit) | ||||
assert commit.sha().digest() == sha | |||||
original_sha = commit.sha() | |||||
setattr(commit, attribute, b"abcde") | setattr(commit, attribute, b"abcde") | ||||
commit.sha() # reset tag._needs_serialization | |||||
commit._sha = original_sha # force the wrong hash | |||||
with pytest.raises(converters.HashMismatch): | with pytest.raises(converters.HashMismatch): | ||||
converters.dulwich_commit_to_revision(commit) | converters.dulwich_commit_to_revision(commit) | ||||
if attribute == "_gpgsig": | if attribute == "_gpgsig": | ||||
setattr(commit, attribute, None) | setattr(commit, attribute, None) | ||||
commit.sha() # reset tag._needs_serialization | |||||
commit._sha = original_sha # force the wrong hash | |||||
with pytest.raises(converters.HashMismatch): | with pytest.raises(converters.HashMismatch): | ||||
converters.dulwich_commit_to_revision(commit) | converters.dulwich_commit_to_revision(commit) | ||||
def test_commit_to_revision_with_extra_headers_mergetag(self): | def test_commit_to_revision_with_extra_headers_mergetag(self): | ||||
sha1 = b"3ab3da4bf0f81407be16969df09cd1c8af9ac703" | sha1 = b"3ab3da4bf0f81407be16969df09cd1c8af9ac703" | ||||
revision = converters.dulwich_commit_to_revision(self.repo[sha1]) | revision = converters.dulwich_commit_to_revision(self.repo[sha1]) | ||||
expected_revision = Revision( | expected_revision = Revision( | ||||
Show All 27 Lines | def test_commit_to_revision_with_extra_headers_mergetag(self): | ||||
bytes.fromhex("322f5bc915e50fc25e85226b5a182bded0e98e4b"), | bytes.fromhex("322f5bc915e50fc25e85226b5a182bded0e98e4b"), | ||||
bytes.fromhex("9768d0b576dbaaecd80abedad6dfd0d72f1476da"), | bytes.fromhex("9768d0b576dbaaecd80abedad6dfd0d72f1476da"), | ||||
), | ), | ||||
synthetic=False, | synthetic=False, | ||||
) | ) | ||||
assert revision == expected_revision | assert revision == expected_revision | ||||
def test_weird_commit(self): | |||||
"""Checks raw_manifest is set when the commit cannot fit the data model""" | |||||
# Well-formed manifest | |||||
raw_manifest = ( | |||||
b"tree 641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce\n" | |||||
b"author Foo <foo@example.org> 1640191028 +0200\n" | |||||
b"committer Foo <foo@example.org> 1640191028 +0200\n\n" | |||||
b"some commit message" | |||||
) | |||||
commit = dulwich.objects.Commit.from_raw_string(b"commit", raw_manifest) | |||||
date = TimestampWithTimezone( | |||||
timestamp=Timestamp(seconds=1640191028, microseconds=0), | |||||
offset=120, | |||||
negative_utc=False, | |||||
offset_bytes=b"+0200", | |||||
) | |||||
assert converters.dulwich_commit_to_revision(commit) == Revision( | |||||
message=b"some commit message", | |||||
directory=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | |||||
synthetic=False, | |||||
author=Person.from_fullname(b"Foo <foo@example.org>",), | |||||
committer=Person.from_fullname(b"Foo <foo@example.org>",), | |||||
date=date, | |||||
committer_date=date, | |||||
type=RevisionType.GIT, | |||||
raw_manifest=None, | |||||
) | |||||
# Mess with the offset | |||||
raw_manifest2 = raw_manifest.replace(b"+0200", b"+200") | |||||
commit = dulwich.objects.Commit.from_raw_string(b"commit", raw_manifest2) | |||||
date = TimestampWithTimezone( | |||||
timestamp=Timestamp(seconds=1640191028, microseconds=0), | |||||
offset=120, | |||||
negative_utc=False, | |||||
offset_bytes=b"+200", | |||||
) | |||||
assert converters.dulwich_commit_to_revision(commit) == Revision( | |||||
message=b"some commit message", | |||||
directory=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | |||||
synthetic=False, | |||||
author=Person.from_fullname(b"Foo <foo@example.org>",), | |||||
committer=Person.from_fullname(b"Foo <foo@example.org>",), | |||||
date=date, | |||||
committer_date=date, | |||||
type=RevisionType.GIT, | |||||
raw_manifest=None, | |||||
) | |||||
# Mess with the rest of the manifest | |||||
raw_manifest2 = raw_manifest.replace( | |||||
b"641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce", | |||||
b"641FB6E08DDB2E4FD096DCF18E80B894BF7E25CE", | |||||
) | |||||
commit = dulwich.objects.Commit.from_raw_string(b"commit", raw_manifest2) | |||||
date = TimestampWithTimezone( | |||||
timestamp=Timestamp(seconds=1640191028, microseconds=0), | |||||
offset=120, | |||||
negative_utc=False, | |||||
offset_bytes=b"+0200", | |||||
) | |||||
assert converters.dulwich_commit_to_revision(commit) == Revision( | |||||
message=b"some commit message", | |||||
directory=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | |||||
synthetic=False, | |||||
author=Person.from_fullname(b"Foo <foo@example.org>",), | |||||
committer=Person.from_fullname(b"Foo <foo@example.org>",), | |||||
date=date, | |||||
committer_date=date, | |||||
type=RevisionType.GIT, | |||||
raw_manifest=b"commit 161\x00" + raw_manifest2, | |||||
) | |||||
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 pytest.raises(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>", | ||||
▲ Show 20 Lines • Show All 206 Lines • ▼ Show 20 Lines | def test_dulwich_tag_to_release_signature(self): | ||||
target=hash_to_bytes(target.decode()), | target=hash_to_bytes(target.decode()), | ||||
target_type=ObjectType.REVISION, | target_type=ObjectType.REVISION, | ||||
) | ) | ||||
assert actual_release == expected_release | assert actual_release == expected_release | ||||
@pytest.mark.parametrize("attribute", ["name", "message", "signature"]) | @pytest.mark.parametrize("attribute", ["name", "message", "signature"]) | ||||
def test_corrupt_tag(self, attribute): | def test_corrupt_tag(self, attribute): | ||||
# has a signature | |||||
sha = hash_to_bytes("46fff489610ed733d2cc904e363070dadee05c71") | sha = hash_to_bytes("46fff489610ed733d2cc904e363070dadee05c71") | ||||
target = b"641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce" | target = b"641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce" | ||||
message = b"some release message" | message = b"some release message" | ||||
tag = dulwich.objects.Tag() | tag = dulwich.objects.Tag() | ||||
tag.name = b"blah" | tag.name = b"blah" | ||||
tag.object = (dulwich.objects.Commit, target) | tag.object = (dulwich.objects.Commit, target) | ||||
tag.message = message | tag.message = message | ||||
tag.signature = GPGSIG | tag.signature = GPGSIG | ||||
Show All 12 Lines | def test_corrupt_tag(self, attribute): | ||||
converters.dulwich_tag_to_release(tag) | converters.dulwich_tag_to_release(tag) | ||||
if attribute == "signature": | if attribute == "signature": | ||||
setattr(tag, attribute, None) | setattr(tag, attribute, None) | ||||
tag.sha() # reset tag._needs_serialization | tag.sha() # reset tag._needs_serialization | ||||
tag._sha = original_sha # force the wrong hash | tag._sha = original_sha # force the wrong hash | ||||
with pytest.raises(converters.HashMismatch): | with pytest.raises(converters.HashMismatch): | ||||
converters.dulwich_tag_to_release(tag) | converters.dulwich_tag_to_release(tag) | ||||
def test_weird_tag(self): | |||||
"""Checks raw_manifest is set when the tag cannot fit the data model""" | |||||
# Well-formed manifest | |||||
raw_manifest = ( | |||||
b"object 641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce\n" | |||||
b"type commit\n" | |||||
b"tag blah\n" | |||||
b"tagger Foo <foo@example.org> 1640191027 +0200\n\n" | |||||
b"some release message" | |||||
) | |||||
tag = dulwich.objects.Tag.from_raw_string(b"tag", raw_manifest) | |||||
assert converters.dulwich_tag_to_release(tag) == Release( | |||||
name=b"blah", | |||||
message=b"some release message", | |||||
target=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | |||||
target_type=ObjectType.REVISION, | |||||
synthetic=False, | |||||
author=Person.from_fullname(b"Foo <foo@example.org>",), | |||||
date=TimestampWithTimezone( | |||||
timestamp=Timestamp(seconds=1640191027, microseconds=0), | |||||
offset=120, | |||||
negative_utc=False, | |||||
offset_bytes=b"+0200", | |||||
), | |||||
raw_manifest=None, | |||||
) | |||||
# Mess with the offset | |||||
raw_manifest2 = raw_manifest.replace(b"+0200", b"+200") | |||||
tag = dulwich.objects.Tag.from_raw_string(b"tag", raw_manifest2) | |||||
assert converters.dulwich_tag_to_release(tag) == Release( | |||||
name=b"blah", | |||||
message=b"some release message", | |||||
target=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | |||||
target_type=ObjectType.REVISION, | |||||
synthetic=False, | |||||
author=Person.from_fullname(b"Foo <foo@example.org>",), | |||||
date=TimestampWithTimezone( | |||||
timestamp=Timestamp(seconds=1640191027, microseconds=0), | |||||
offset=120, | |||||
negative_utc=False, | |||||
offset_bytes=b"+200", | |||||
), | |||||
) | |||||
# Mess with the rest of the manifest | |||||
raw_manifest2 = raw_manifest.replace( | |||||
b"641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce", | |||||
b"641FB6E08DDB2E4FD096DCF18E80B894BF7E25CE", | |||||
) | |||||
tag = dulwich.objects.Tag.from_raw_string(b"tag", raw_manifest2) | |||||
assert converters.dulwich_tag_to_release(tag) == Release( | |||||
name=b"blah", | |||||
message=b"some release message", | |||||
target=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | |||||
target_type=ObjectType.REVISION, | |||||
synthetic=False, | |||||
author=Person.from_fullname(b"Foo <foo@example.org>",), | |||||
date=TimestampWithTimezone( | |||||
timestamp=Timestamp(seconds=1640191027, microseconds=0), | |||||
offset=120, | |||||
negative_utc=False, | |||||
), | |||||
raw_manifest=b"tag 136\x00" + raw_manifest2, | |||||
) |