From f4d03025e538ec91b8dc1f02179b0fdac7b832fc Mon Sep 17 00:00:00 2001 From: Giles Gaskell Date: Mon, 4 Dec 2017 16:11:24 +1100 Subject: [PATCH] Update project --- jenkins/Jenkinsfile | 35 +++++++++++++++++++++++++++++++++++ sources/add2vals.py | 23 +++++++++++++++++++++++ sources/calc.py | 29 ++++++++++++++++++++++++++--- sources/test_calc.py | 27 +++++++++++++++++++++------ 4 files changed, 105 insertions(+), 9 deletions(-) create mode 100644 jenkins/Jenkinsfile create mode 100644 sources/add2vals.py diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile new file mode 100644 index 0000000..a924e3f --- /dev/null +++ b/jenkins/Jenkinsfile @@ -0,0 +1,35 @@ +pipeline { + agent none + stages { + stage('Build') { + agent { + docker { + image 'python:3.4-slim' + } + } + steps { + sh 'python -m py_compile sources/add2vals.py' + } + } + stage('Test') { + agent { + docker { + image 'python:3.4-slim' + } + } + steps { + sh 'python sources/test_calc.py' + } + } + stage('Deliver') { + agent { + docker { + image 'cdrx/pyinstaller-linux' + } + } + steps { + sh 'pyinstaller --onefile sources/add2vals.py' + } + } + } +} diff --git a/sources/add2vals.py b/sources/add2vals.py new file mode 100644 index 0000000..c6bae7f --- /dev/null +++ b/sources/add2vals.py @@ -0,0 +1,23 @@ +''' +A simple command line tool that takes 2 values and adds them together using +the calc.py library's 'add2' function. +''' + +import sys +import calc + +argnumbers = len(sys.argv) - 1 + +if argnumbers == 2 : + print("") + print("The result is " + str(calc.add2(str(sys.argv[1]), str(sys.argv[2])))) + print("") + sys.exit(0) + +if argnumbers != 2 : + print("") + print("Usage: add X Y, where X and Y are individual values.") + print(" If uncompiled, usage is 'python add2vals.py X Y'.") + print(" (You entered " + str(argnumbers) + " value/s.)") + print("") + sys.exit(1) diff --git a/sources/calc.py b/sources/calc.py index d1cbd6c..368c55b 100644 --- a/sources/calc.py +++ b/sources/calc.py @@ -1,5 +1,28 @@ ''' -A simple addition function. +The 'calc' library contains the 'add2' function that takes 2 values and adds +them together. If either value is a string (or both of them are) 'add2' ensures +they are both strings, thereby resulting in a concatenated result. +NOTE: If a value submitted to the 'add2' function is a float, it must be done so +in quotes (i.e. as a string). ''' -def add(a, b): - return a + b + +# If 'value' is not an integer, convert it to a float and failing that, a string. +def conv(value): + try: + return int(value) + except ValueError: + try: + return float(value) + except ValueError: + return str(value) + +# The 'add2' function itself +def add2(arg1, arg2): + # Convert 'arg1' and 'arg2' to their appropriate types + arg1conv = conv(arg1) + arg2conv = conv(arg2) + # If either 'arg1' or 'arg2' is a string, ensure they're both strings. + if isinstance(arg1conv, str) or isinstance(arg2conv, str): + arg1conv = str(arg1conv) + arg2conv = str(arg2conv) + return arg1conv + arg2conv diff --git a/sources/test_calc.py b/sources/test_calc.py index 02f41f8..130f9f7 100644 --- a/sources/test_calc.py +++ b/sources/test_calc.py @@ -1,7 +1,7 @@ -import calc import unittest +import calc -class TestAdd(unittest.TestCase): +class TestCalc(unittest.TestCase): """ Test the add function from the calc library """ @@ -10,24 +10,39 @@ class TestAdd(unittest.TestCase): """ Test that the addition of two integers returns the correct total """ - result = calc.add(1, 2) + result = calc.add2(1, 2) self.assertEqual(result, 3) def test_add_floats(self): """ Test that the addition of two floats returns the correct result """ - result = calc.add(10.5, 2) + result = calc.add2('10.5', 2) self.assertEqual(result, 12.5) def test_add_strings(self): """ - Test the addition of two strings returns the two string as one + Test the addition of two strings returns the two strings as one concatenated string """ - result = calc.add('abc', 'def') + result = calc.add2('abc', 'def') self.assertEqual(result, 'abcdef') + def test_add_string_and_integer(self): + """ + Test the addition of a string and an integer returns them as one + concatenated string (in which the integer is converted to a string) + """ + result = calc.add2('abc', 3) + self.assertEqual(result, 'abc3') + + def test_add_string_and_number(self): + """ + Test the addition of a string and a float returns them as one + concatenated string (in which the float is converted to a string) + """ + result = calc.add2('abc', '5.5') + self.assertEqual(result, 'abc5.5') if __name__ == '__main__': unittest.main()