Skip to content

Commit

Permalink
make python 2 3 compatible & coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-vk committed Dec 1, 2022
1 parent 7238ac5 commit 11ecfdd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dockerfile_parse/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def parent_images(self):
in_stage = True
image, _ = image_from(instr['value'])
if image is not None:
image = WordSplitter(image.to_str(), args=top_args).dequote()
image = WordSplitter(b2u(image.to_str()), args=top_args).dequote()
parents.append(image)
return parents

Expand Down
6 changes: 3 additions & 3 deletions dockerfile_parse/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def __init__(self, registry=None, namespace=None, repo=None, tag=None):
def parse(cls, image_name):
result = cls()

if not image_name or image_name.isspace():
if not image_name or str(image_name).isspace():
return ImageName()

if isinstance(image_name, cls):
Expand Down Expand Up @@ -434,9 +434,9 @@ def __repr__(self):
).format(s=self)

def __eq__(self, other):
if type(other) == str:
if isinstance(other, str):
return self.__str__() == other
elif type(other) == type(self):
elif isinstance(other, ImageName):
return self.__dict__ == other.__dict__
else:
return NotImplemented
Expand Down
15 changes: 11 additions & 4 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@
)
])
class TestImageName(object):
def test_util_ImageName_parse(self, image_string, dictionary):
def test_util_image_name_parse(self, image_string, dictionary):
image = ImageName.parse(image_string)
assert image.namespace == dictionary["namespace"]
assert image.registry == dictionary["registry"]
assert image.tag == dictionary["tag"]
assert image.repo == dictionary["repo"]

def test_util_ImageName_get_repo(self, image_string, dictionary):
def test_util_image_name_get_repo(self, image_string, dictionary):
image = ImageName.parse(image_string)
repo = "/".join(filter(None, (dictionary["namespace"], dictionary["repo"])))
assert image.get_repo() == (repo if repo != "" else None)
assert image.get_repo(explicit_namespace=True) == "{0}/{1}".format(
dictionary["namespace"] if dictionary["namespace"] else "library", dictionary["repo"])

def test_util_ImageName_to_str(self, image_string, dictionary):
def test_util_image_name_to_str(self, image_string, dictionary):
image = ImageName.parse(image_string)
if dictionary["repo"] is None:
with pytest.raises(RuntimeError):
Expand All @@ -70,16 +70,23 @@ def test_util_ImageName_to_str(self, image_string, dictionary):
assert image.to_str() == image_string.lstrip('/')

def test_image_name_comparison(self, image_string, dictionary):
# make sure that both "==" and "!=" are implemented right on both Python major releases
# make sure that "==" is implemented correctly on both Python major releases
i1 = ImageName.parse(image_string)
i2 = ImageName(registry=dictionary["registry"], namespace=dictionary["namespace"],
repo=dictionary["repo"],
tag=dictionary["tag"])
assert i1 == i2

i2 = ImageName(registry='foo.com', namespace='spam', repo='bar', tag='2')
# pylint: disable=unneeded-not
assert not i1 == i2

i1 = ImageName.parse(i2)
assert i1 == i2

i1 = i2
assert i1 == i2


@pytest.mark.parametrize(('repo', 'organization', 'enclosed_repo'), (
('fedora', 'spam', 'spam/fedora'),
Expand Down

0 comments on commit 11ecfdd

Please sign in to comment.