Skip to content

Commit 9d93546

Browse files
committed
util: unit tests
1 parent e6094fa commit 9d93546

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

flow/util/genElapsedTime.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pathlib
88
import os
99
import argparse # argument parsing
10+
import sys
1011

1112
# Parse and validate arguments
1213
# ==============================================================================
@@ -18,7 +19,7 @@
1819

1920
if not args.logDir:
2021
print('[ERROR] Please add a logDir'
21-
'-d/--logDir.')
22+
'-d/--logDir.', file=sys.stderr)
2223
parser.print_help()
2324
sys.exit(1)
2425

@@ -43,6 +44,8 @@
4344
elif len(timeList) == 3:
4445
# Hours, minutes, and seconds are present
4546
elapsedTime = int(timeList[0])*3600 + int(timeList[1])*60 + int(timeList[2])
47+
if elapsedTime == 0:
48+
print('No elapsed time found in', str(f), file=sys.stderr)
4649

4750
# Print the name of the step and the corresponding elapsed time
4851
print('%-25s %10s' % (os.path.splitext(os.path.basename(str(f)))[0], elapsedTime))

flow/util/test_genElapsedTime.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env python3
2+
3+
import unittest
4+
from unittest.mock import patch
5+
from unittest.mock import MagicMock
6+
from io import StringIO
7+
import io
8+
import sys
9+
import os
10+
import tempfile
11+
from datetime import datetime
12+
import argparse # argument parsing
13+
from pathlib import Path
14+
15+
# make sure the working dir is flow/
16+
os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)) , '..'))
17+
18+
class TestElapsedTime(unittest.TestCase):
19+
20+
def setUp(self):
21+
self.tmp_dir = tempfile.TemporaryDirectory()
22+
self.log_file = os.path.join(self.tmp_dir.name, "1_test.log")
23+
24+
@patch("sys.stdout", new_callable=StringIO)
25+
def test_elapsed_time(self, mock_stdout):
26+
# create a log file with elapsed time
27+
with open(self.log_file, "w") as f:
28+
f.write("Some log entry\n")
29+
f.write("Elapsed time: 01:30:00[h:]min:sec.\n")
30+
# call the script with the test log file
31+
sys.argv = ["util/genElapsedTime.py", "--logDir", str(self.tmp_dir.name)]
32+
with patch.object(sys, 'argv', sys.argv):
33+
import genElapsedTime
34+
# check if output is correct
35+
expected_output = "1_test 5400\n"
36+
self.assertEqual(mock_stdout.getvalue(), expected_output)
37+
38+
@patch("sys.stdout", new_callable=StringIO)
39+
def test_longer_elapsed_time(self, mock_stdout):
40+
# create a log file with elapsed time
41+
with open(self.log_file, "w") as f:
42+
f.write("Some log entry\n")
43+
f.write("Elapsed time: 12:24.14[h:]min:sec. CPU time: user 5081.82 sys 170.18 (705%). Peak memory: 9667132KB.\n")
44+
# call the script with the test log file
45+
sys.argv = ["util/genElapsedTime.py", "--logDir", str(self.tmp_dir.name)]
46+
with patch.object(sys, 'argv', sys.argv):
47+
import genElapsedTime
48+
# check if output is correct
49+
expected_output = "1_test 44654\n"
50+
self.assertEqual(mock_stdout.getvalue(), expected_output)
51+
52+
def test_missing_arg(self):
53+
with self.assertRaises(SystemExit):
54+
with patch('sys.stderr', new=StringIO()) as fake_err_output:
55+
with patch('sys.argv', ["util/genElapsedTime.py"]):
56+
import genElapsedTime
57+
self.assertIn('--logDir', fake_err_output.getvalue())
58+
59+
60+
def test_no_elapsed_time(self):
61+
with open(self.log_file, "w") as f:
62+
f.write('Other log message')
63+
with patch('sys.argv', ["util/genElapsedTime.py", "--logDir", str(self.tmp_dir.name)]):
64+
with patch('sys.stderr', new=StringIO()) as fake_err_output:
65+
import genElapsedTime
66+
self.assertIn('No elapsed time found in', fake_err_output.getvalue())
67+
68+
def tearDown(self):
69+
self.tmp_dir.cleanup()
70+
71+
if __name__ == '__main__':
72+
unittest.main()

0 commit comments

Comments
 (0)