Skip to content

Commit f4d0302

Browse files
committed
Update project
1 parent 451a378 commit f4d0302

4 files changed

Lines changed: 105 additions & 9 deletions

File tree

jenkins/Jenkinsfile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
pipeline {
2+
agent none
3+
stages {
4+
stage('Build') {
5+
agent {
6+
docker {
7+
image 'python:3.4-slim'
8+
}
9+
}
10+
steps {
11+
sh 'python -m py_compile sources/add2vals.py'
12+
}
13+
}
14+
stage('Test') {
15+
agent {
16+
docker {
17+
image 'python:3.4-slim'
18+
}
19+
}
20+
steps {
21+
sh 'python sources/test_calc.py'
22+
}
23+
}
24+
stage('Deliver') {
25+
agent {
26+
docker {
27+
image 'cdrx/pyinstaller-linux'
28+
}
29+
}
30+
steps {
31+
sh 'pyinstaller --onefile sources/add2vals.py'
32+
}
33+
}
34+
}
35+
}

sources/add2vals.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'''
2+
A simple command line tool that takes 2 values and adds them together using
3+
the calc.py library's 'add2' function.
4+
'''
5+
6+
import sys
7+
import calc
8+
9+
argnumbers = len(sys.argv) - 1
10+
11+
if argnumbers == 2 :
12+
print("")
13+
print("The result is " + str(calc.add2(str(sys.argv[1]), str(sys.argv[2]))))
14+
print("")
15+
sys.exit(0)
16+
17+
if argnumbers != 2 :
18+
print("")
19+
print("Usage: add X Y, where X and Y are individual values.")
20+
print(" If uncompiled, usage is 'python add2vals.py X Y'.")
21+
print(" (You entered " + str(argnumbers) + " value/s.)")
22+
print("")
23+
sys.exit(1)

sources/calc.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
'''
2-
A simple addition function.
2+
The 'calc' library contains the 'add2' function that takes 2 values and adds
3+
them together. If either value is a string (or both of them are) 'add2' ensures
4+
they are both strings, thereby resulting in a concatenated result.
5+
NOTE: If a value submitted to the 'add2' function is a float, it must be done so
6+
in quotes (i.e. as a string).
37
'''
4-
def add(a, b):
5-
return a + b
8+
9+
# If 'value' is not an integer, convert it to a float and failing that, a string.
10+
def conv(value):
11+
try:
12+
return int(value)
13+
except ValueError:
14+
try:
15+
return float(value)
16+
except ValueError:
17+
return str(value)
18+
19+
# The 'add2' function itself
20+
def add2(arg1, arg2):
21+
# Convert 'arg1' and 'arg2' to their appropriate types
22+
arg1conv = conv(arg1)
23+
arg2conv = conv(arg2)
24+
# If either 'arg1' or 'arg2' is a string, ensure they're both strings.
25+
if isinstance(arg1conv, str) or isinstance(arg2conv, str):
26+
arg1conv = str(arg1conv)
27+
arg2conv = str(arg2conv)
28+
return arg1conv + arg2conv

sources/test_calc.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import calc
21
import unittest
2+
import calc
33

4-
class TestAdd(unittest.TestCase):
4+
class TestCalc(unittest.TestCase):
55
"""
66
Test the add function from the calc library
77
"""
@@ -10,24 +10,39 @@ def test_add_integers(self):
1010
"""
1111
Test that the addition of two integers returns the correct total
1212
"""
13-
result = calc.add(1, 2)
13+
result = calc.add2(1, 2)
1414
self.assertEqual(result, 3)
1515

1616
def test_add_floats(self):
1717
"""
1818
Test that the addition of two floats returns the correct result
1919
"""
20-
result = calc.add(10.5, 2)
20+
result = calc.add2('10.5', 2)
2121
self.assertEqual(result, 12.5)
2222

2323
def test_add_strings(self):
2424
"""
25-
Test the addition of two strings returns the two string as one
25+
Test the addition of two strings returns the two strings as one
2626
concatenated string
2727
"""
28-
result = calc.add('abc', 'def')
28+
result = calc.add2('abc', 'def')
2929
self.assertEqual(result, 'abcdef')
3030

31+
def test_add_string_and_integer(self):
32+
"""
33+
Test the addition of a string and an integer returns them as one
34+
concatenated string (in which the integer is converted to a string)
35+
"""
36+
result = calc.add2('abc', 3)
37+
self.assertEqual(result, 'abc3')
38+
39+
def test_add_string_and_number(self):
40+
"""
41+
Test the addition of a string and a float returns them as one
42+
concatenated string (in which the float is converted to a string)
43+
"""
44+
result = calc.add2('abc', '5.5')
45+
self.assertEqual(result, 'abc5.5')
3146

3247
if __name__ == '__main__':
3348
unittest.main()

0 commit comments

Comments
 (0)