From f04aa4da976e16fceb47f74242a4e97387235ed9 Mon Sep 17 00:00:00 2001 From: Mukundan Sundararajan Date: Fri, 22 Dec 2017 03:38:58 -0800 Subject: [PATCH] Changed to google style docstrings. Added image_name validation test. --- repo2docker/app.py | 14 +++++++++++--- tests/argumentvalidation.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 tests/argumentvalidation.py diff --git a/repo2docker/app.py b/repo2docker/app.py index b1a6db72..16b62969 100644 --- a/repo2docker/app.py +++ b/repo2docker/app.py @@ -124,10 +124,18 @@ class Repo2Docker(Application): def validate_image_name(self, image_name): """ - Validate that the image_name read by argparse contains only lowercase characters - :param image_name: string argument read by argparse - :return: test and return image_name if it only contains lowercase characters else raise error + Validate image_name read by argparse contains only lowercase characters + + Args: + image_name: string argument read by the argument parser + + Returns: + unmodified image_name + + Raises: + ArgumentTypeError: if image_name contains characters that are not lowercase """ + if not image_name.islower(): msg = "%r is not a valid docker image name. Image name can contain only lowercase characters." % image_name raise argparse.ArgumentTypeError(msg) diff --git a/tests/argumentvalidation.py b/tests/argumentvalidation.py new file mode 100644 index 00000000..a0b78815 --- /dev/null +++ b/tests/argumentvalidation.py @@ -0,0 +1,35 @@ +""" +Tests that runs validity checks on arguments passed in from shell +""" + +import os +import subprocess + +def does_validate_image_name(builddir, image_name): + try: + output = subprocess.check_output( + [ + 'repo2docker', + '--no-run', + '--image-name', + str(image_name), + builddir + ], + stderr=subprocess.STDOUT, + ).decode() + print(output) + return True + except subprocess.CalledProcessError as e: + output = e.output.decode() + print(output) + if "error: argument --image-name: %r is not a valid docker image name. " \ + "Image name can contain only lowercase characters." % image_name in output: + return False + else: + raise + +def test_image_name_fail(): + builddir = os.path.dirname(__file__) + + assert not does_validate_image_name(builddir, 'Test/Invalid_name:1.0.0') +