From 11ecfdd9f3bab091915b382ac1676545c9f89549 Mon Sep 17 00:00:00 2001 From: Tim van Katwijk Date: Thu, 1 Dec 2022 19:42:33 +0100 Subject: [PATCH] make python 2 3 compatible & coverage --- dockerfile_parse/parser.py | 2 +- dockerfile_parse/util.py | 6 +++--- tests/test_parser.py | 15 +++++++++++---- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/dockerfile_parse/parser.py b/dockerfile_parse/parser.py index a6d0c5d..157e06c 100644 --- a/dockerfile_parse/parser.py +++ b/dockerfile_parse/parser.py @@ -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 diff --git a/dockerfile_parse/util.py b/dockerfile_parse/util.py index 820c65c..4dd3143 100644 --- a/dockerfile_parse/util.py +++ b/dockerfile_parse/util.py @@ -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): @@ -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 diff --git a/tests/test_parser.py b/tests/test_parser.py index c2445a1..dfd2d34 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -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): @@ -70,7 +70,7 @@ 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"], @@ -78,8 +78,15 @@ def test_image_name_comparison(self, image_string, dictionary): 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'),