Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/git/tests/test_converters.py
Show First 20 Lines • Show All 183 Lines • ▼ Show 20 Lines | def test_corrupt_tree(self): | ||||
assert tree.sha().hexdigest() == sha1.decode() | assert tree.sha().hexdigest() == sha1.decode() | ||||
with pytest.raises(converters.HashMismatch): | with pytest.raises(converters.HashMismatch): | ||||
converters.dulwich_tree_to_directory(tree) | converters.dulwich_tree_to_directory(tree) | ||||
def test_weird_tree(self): | def test_weird_tree(self): | ||||
"""Tests a tree with entries the wrong order""" | """Tests a tree with entries the wrong order""" | ||||
raw_manifest = ( | raw_string = ( | ||||
b"0644 file2\x00" | b"0644 file2\x00" | ||||
b"d\x1f\xb6\xe0\x8d\xdb.O\xd0\x96\xdc\xf1\x8e\x80\xb8\x94\xbf~%\xce" | b"d\x1f\xb6\xe0\x8d\xdb.O\xd0\x96\xdc\xf1\x8e\x80\xb8\x94\xbf~%\xce" | ||||
b"0644 file1\x00" | b"0644 file1\x00" | ||||
b"d\x1f\xb6\xe0\x8d\xdb.O\xd0\x96\xdc\xf1\x8e\x80\xb8\x94\xbf~%\xce" | b"d\x1f\xb6\xe0\x8d\xdb.O\xd0\x96\xdc\xf1\x8e\x80\xb8\x94\xbf~%\xce" | ||||
) | ) | ||||
tree = dulwich.objects.Tree.from_raw_string(b"tree", raw_manifest) | tree = dulwich.objects.Tree.from_raw_string(b"tree", raw_string) | ||||
assert converters.dulwich_tree_to_directory(tree) == Directory( | assert converters.dulwich_tree_to_directory(tree) == Directory( | ||||
entries=( | entries=( | ||||
# in alphabetical order, as it should be | # in alphabetical order, as it should be | ||||
DirectoryEntry( | DirectoryEntry( | ||||
name=b"file1", | name=b"file1", | ||||
type="file", | type="file", | ||||
target=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | target=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | ||||
perms=0o644, | perms=0o644, | ||||
), | ), | ||||
DirectoryEntry( | DirectoryEntry( | ||||
name=b"file2", | name=b"file2", | ||||
type="file", | type="file", | ||||
target=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | target=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | ||||
perms=0o644, | perms=0o644, | ||||
), | ), | ||||
), | ), | ||||
raw_manifest=b"tree 62\x00" + raw_manifest, | raw_manifest=b"tree 62\x00" + raw_string, | ||||
) | ) | ||||
def test_tree_perms(self): | def test_tree_perms(self): | ||||
entries = [ | entries = [ | ||||
(b"blob_100644", 0o100644, "file"), | (b"blob_100644", 0o100644, "file"), | ||||
(b"blob_100664", 0o100664, "file"), | (b"blob_100664", 0o100664, "file"), | ||||
(b"blob_100666", 0o100666, "file"), | (b"blob_100666", 0o100666, "file"), | ||||
(b"blob_120000", 0o120000, "file"), | (b"blob_120000", 0o120000, "file"), | ||||
▲ Show 20 Lines • Show All 207 Lines • ▼ Show 20 Lines | def test_commit_to_revision_with_extra_headers_mergetag(self): | ||||
) | ) | ||||
assert revision == expected_revision | assert revision == expected_revision | ||||
def test_weird_commit(self): | def test_weird_commit(self): | ||||
"""Checks raw_manifest is set when the commit cannot fit the data model""" | """Checks raw_manifest is set when the commit cannot fit the data model""" | ||||
# Well-formed manifest | # Well-formed manifest | ||||
raw_manifest = ( | raw_string = ( | ||||
b"tree 641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce\n" | b"tree 641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce\n" | ||||
b"author Foo <foo@example.org> 1640191028 +0200\n" | b"author Foo <foo@example.org> 1640191028 +0200\n" | ||||
b"committer Foo <foo@example.org> 1640191028 +0200\n\n" | b"committer Foo <foo@example.org> 1640191028 +0200\n\n" | ||||
b"some commit message" | b"some commit message" | ||||
) | ) | ||||
commit = dulwich.objects.Commit.from_raw_string(b"commit", raw_manifest) | commit = dulwich.objects.Commit.from_raw_string(b"commit", raw_string) | ||||
date = TimestampWithTimezone( | date = TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=1640191028, microseconds=0), | timestamp=Timestamp(seconds=1640191028, microseconds=0), | ||||
offset_bytes=b"+0200", | offset_bytes=b"+0200", | ||||
) | ) | ||||
assert converters.dulwich_commit_to_revision(commit) == Revision( | assert converters.dulwich_commit_to_revision(commit) == Revision( | ||||
message=b"some commit message", | message=b"some commit message", | ||||
directory=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | directory=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | ||||
synthetic=False, | synthetic=False, | ||||
author=Person.from_fullname( | author=Person.from_fullname( | ||||
b"Foo <foo@example.org>", | b"Foo <foo@example.org>", | ||||
), | ), | ||||
committer=Person.from_fullname( | committer=Person.from_fullname( | ||||
b"Foo <foo@example.org>", | b"Foo <foo@example.org>", | ||||
), | ), | ||||
date=date, | date=date, | ||||
committer_date=date, | committer_date=date, | ||||
type=RevisionType.GIT, | type=RevisionType.GIT, | ||||
raw_manifest=None, | raw_manifest=None, | ||||
) | ) | ||||
# Mess with the offset | # Mess with the offset | ||||
raw_manifest2 = raw_manifest.replace(b"+0200", b"+200") | raw_string2 = raw_string.replace(b"+0200", b"+200") | ||||
commit = dulwich.objects.Commit.from_raw_string(b"commit", raw_manifest2) | commit = dulwich.objects.Commit.from_raw_string(b"commit", raw_string2) | ||||
date = TimestampWithTimezone( | date = TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=1640191028, microseconds=0), | timestamp=Timestamp(seconds=1640191028, microseconds=0), | ||||
offset_bytes=b"+200", | offset_bytes=b"+200", | ||||
) | ) | ||||
assert converters.dulwich_commit_to_revision(commit) == Revision( | assert converters.dulwich_commit_to_revision(commit) == Revision( | ||||
message=b"some commit message", | message=b"some commit message", | ||||
directory=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | directory=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | ||||
synthetic=False, | synthetic=False, | ||||
author=Person.from_fullname( | author=Person.from_fullname( | ||||
b"Foo <foo@example.org>", | b"Foo <foo@example.org>", | ||||
), | ), | ||||
committer=Person.from_fullname( | committer=Person.from_fullname( | ||||
b"Foo <foo@example.org>", | b"Foo <foo@example.org>", | ||||
), | ), | ||||
date=date, | date=date, | ||||
committer_date=date, | committer_date=date, | ||||
type=RevisionType.GIT, | type=RevisionType.GIT, | ||||
raw_manifest=None, | raw_manifest=None, | ||||
) | ) | ||||
# Mess with the rest of the manifest | # Mess with the rest of the manifest | ||||
raw_manifest2 = raw_manifest.replace( | raw_string2 = raw_string.replace( | ||||
b"641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce", | b"641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce", | ||||
b"641FB6E08DDB2E4FD096DCF18E80B894BF7E25CE", | b"641FB6E08DDB2E4FD096DCF18E80B894BF7E25CE", | ||||
) | ) | ||||
commit = dulwich.objects.Commit.from_raw_string(b"commit", raw_manifest2) | commit = dulwich.objects.Commit.from_raw_string(b"commit", raw_string2) | ||||
date = TimestampWithTimezone( | date = TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=1640191028, microseconds=0), | timestamp=Timestamp(seconds=1640191028, microseconds=0), | ||||
offset_bytes=b"+0200", | offset_bytes=b"+0200", | ||||
) | ) | ||||
assert converters.dulwich_commit_to_revision(commit) == Revision( | assert converters.dulwich_commit_to_revision(commit) == Revision( | ||||
message=b"some commit message", | message=b"some commit message", | ||||
directory=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | directory=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | ||||
synthetic=False, | synthetic=False, | ||||
author=Person.from_fullname( | author=Person.from_fullname( | ||||
b"Foo <foo@example.org>", | b"Foo <foo@example.org>", | ||||
), | ), | ||||
committer=Person.from_fullname( | committer=Person.from_fullname( | ||||
b"Foo <foo@example.org>", | b"Foo <foo@example.org>", | ||||
), | ), | ||||
date=date, | date=date, | ||||
committer_date=date, | committer_date=date, | ||||
type=RevisionType.GIT, | type=RevisionType.GIT, | ||||
raw_manifest=b"commit 161\x00" + raw_manifest2, | raw_manifest=b"commit 161\x00" + raw_string2, | ||||
) | ) | ||||
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 = { | ||||
▲ Show 20 Lines • Show All 259 Lines • ▼ Show 20 Lines | def test_corrupt_tag(self, attribute): | ||||
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): | def test_weird_tag(self): | ||||
"""Checks raw_manifest is set when the tag cannot fit the data model""" | """Checks raw_manifest is set when the tag cannot fit the data model""" | ||||
# Well-formed manifest | # Well-formed manifest | ||||
raw_manifest = ( | raw_string = ( | ||||
b"object 641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce\n" | b"object 641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce\n" | ||||
b"type commit\n" | b"type commit\n" | ||||
b"tag blah\n" | b"tag blah\n" | ||||
b"tagger Foo <foo@example.org> 1640191027 +0200\n\n" | b"tagger Foo <foo@example.org> 1640191027 +0200\n\n" | ||||
b"some release message" | b"some release message" | ||||
) | ) | ||||
tag = dulwich.objects.Tag.from_raw_string(b"tag", raw_manifest) | tag = dulwich.objects.Tag.from_raw_string(b"tag", raw_string) | ||||
assert converters.dulwich_tag_to_release(tag) == Release( | assert converters.dulwich_tag_to_release(tag) == Release( | ||||
name=b"blah", | name=b"blah", | ||||
message=b"some release message", | message=b"some release message", | ||||
target=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | target=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | ||||
target_type=ObjectType.REVISION, | target_type=ObjectType.REVISION, | ||||
synthetic=False, | synthetic=False, | ||||
author=Person.from_fullname( | author=Person.from_fullname( | ||||
b"Foo <foo@example.org>", | b"Foo <foo@example.org>", | ||||
), | ), | ||||
date=TimestampWithTimezone( | date=TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=1640191027, microseconds=0), | timestamp=Timestamp(seconds=1640191027, microseconds=0), | ||||
offset_bytes=b"+0200", | offset_bytes=b"+0200", | ||||
), | ), | ||||
raw_manifest=None, | raw_manifest=None, | ||||
) | ) | ||||
# Mess with the offset (negative UTC) | # Mess with the offset (negative UTC) | ||||
raw_manifest2 = raw_manifest.replace(b"+0200", b"-0000") | raw_string2 = raw_string.replace(b"+0200", b"-0000") | ||||
tag = dulwich.objects.Tag.from_raw_string(b"tag", raw_manifest2) | tag = dulwich.objects.Tag.from_raw_string(b"tag", raw_string2) | ||||
assert converters.dulwich_tag_to_release(tag) == Release( | assert converters.dulwich_tag_to_release(tag) == Release( | ||||
name=b"blah", | name=b"blah", | ||||
message=b"some release message", | message=b"some release message", | ||||
target=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | target=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | ||||
target_type=ObjectType.REVISION, | target_type=ObjectType.REVISION, | ||||
synthetic=False, | synthetic=False, | ||||
author=Person.from_fullname( | author=Person.from_fullname( | ||||
b"Foo <foo@example.org>", | b"Foo <foo@example.org>", | ||||
), | ), | ||||
date=TimestampWithTimezone( | date=TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=1640191027, microseconds=0), | timestamp=Timestamp(seconds=1640191027, microseconds=0), | ||||
offset_bytes=b"-0000", | offset_bytes=b"-0000", | ||||
), | ), | ||||
) | ) | ||||
# Mess with the offset (other) | # Mess with the offset (other) | ||||
raw_manifest2 = raw_manifest.replace(b"+0200", b"+200") | raw_string2 = raw_string.replace(b"+0200", b"+200") | ||||
tag = dulwich.objects.Tag.from_raw_string(b"tag", raw_manifest2) | tag = dulwich.objects.Tag.from_raw_string(b"tag", raw_string2) | ||||
assert converters.dulwich_tag_to_release(tag) == Release( | assert converters.dulwich_tag_to_release(tag) == Release( | ||||
name=b"blah", | name=b"blah", | ||||
message=b"some release message", | message=b"some release message", | ||||
target=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | target=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | ||||
target_type=ObjectType.REVISION, | target_type=ObjectType.REVISION, | ||||
synthetic=False, | synthetic=False, | ||||
author=Person.from_fullname( | author=Person.from_fullname( | ||||
b"Foo <foo@example.org>", | b"Foo <foo@example.org>", | ||||
), | ), | ||||
date=TimestampWithTimezone( | date=TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=1640191027, microseconds=0), | timestamp=Timestamp(seconds=1640191027, microseconds=0), | ||||
offset_bytes=b"+200", | offset_bytes=b"+200", | ||||
), | ), | ||||
) | ) | ||||
# Mess with the rest of the manifest | # Mess with the rest of the manifest | ||||
raw_manifest2 = raw_manifest.replace( | raw_string2 = raw_string.replace( | ||||
b"641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce", | b"641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce", | ||||
b"641FB6E08DDB2E4FD096DCF18E80B894BF7E25CE", | b"641FB6E08DDB2E4FD096DCF18E80B894BF7E25CE", | ||||
) | ) | ||||
tag = dulwich.objects.Tag.from_raw_string(b"tag", raw_manifest2) | tag = dulwich.objects.Tag.from_raw_string(b"tag", raw_string2) | ||||
assert converters.dulwich_tag_to_release(tag) == Release( | assert converters.dulwich_tag_to_release(tag) == Release( | ||||
name=b"blah", | name=b"blah", | ||||
message=b"some release message", | message=b"some release message", | ||||
target=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | target=hash_to_bytes("641fb6e08ddb2e4fd096dcf18e80b894bf7e25ce"), | ||||
target_type=ObjectType.REVISION, | target_type=ObjectType.REVISION, | ||||
synthetic=False, | synthetic=False, | ||||
author=Person.from_fullname( | author=Person.from_fullname( | ||||
b"Foo <foo@example.org>", | b"Foo <foo@example.org>", | ||||
), | ), | ||||
date=TimestampWithTimezone( | date=TimestampWithTimezone( | ||||
timestamp=Timestamp(seconds=1640191027, microseconds=0), | timestamp=Timestamp(seconds=1640191027, microseconds=0), | ||||
offset_bytes=b"+0200", | offset_bytes=b"+0200", | ||||
), | ), | ||||
raw_manifest=b"tag 136\x00" + raw_manifest2, | raw_manifest=b"tag 136\x00" + raw_string2, | ||||
) | ) |