Skip to content

Commit 66e4622

Browse files
committed
util: add test folders and workflow
1 parent 9d93546 commit 66e4622

File tree

3 files changed

+75
-32
lines changed

3 files changed

+75
-32
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Test Util Scripts
2+
on:
3+
schedule:
4+
- cron: "0 8 * * SUN"
5+
push:
6+
paths:
7+
- 'flow/util/'
8+
- 'flow/util/test'
9+
pull_request:
10+
paths:
11+
- 'flow/util/'
12+
- 'flow/util/test'
13+
# Allows you to run this workflow manually from the Actions tab
14+
workflow_dispatch:
15+
16+
jobs:
17+
testUtilScripts:
18+
strategy:
19+
fail-fast: false
20+
runs-on: ubuntu-latest
21+
steps:
22+
- name: Check out repository code
23+
uses: actions/checkout@v3
24+
with:
25+
fetch-depth: 1
26+
submodules: recursive
27+
- name: Install Dependencies
28+
run: |
29+
sudo apt update
30+
sudo apt install -y python3-pip
31+
- name: Run Tests
32+
run: |
33+
cd flow/util/test
34+
for file in *.py; do
35+
python "$file"
36+
done

flow/util/genElapsedTime.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,30 @@
2626
# Loop on all log files in the directory
2727
for f in sorted(pathlib.Path(args.logDir).glob('**/[0-9]_*.log')):
2828
# Extract Elapsed Time line from log file
29-
for line in open(str(f)):
30-
elapsedTime = 0
31-
32-
if 'Elapsed time' in line:
33-
# Extract the portion that has the time
34-
timePor = line.strip().replace('Elapsed time: ', '')
35-
# Remove the units from the time portion
36-
timePor = timePor.split('[h:]', 1)[0]
37-
# Ensure that hours, min and seconds are separated by ':' not '.'
38-
timePor = timePor.replace('.',':')
39-
# Calculate elapsed time that has this format 'h:m:s'
40-
timeList = timePor.split(':')
41-
if len(timeList) == 2:
42-
# Only minutes and seconds are present
43-
elapsedTime = int(timeList[0])*60 + int(timeList[1])
44-
elif len(timeList) == 3:
45-
# Hours, minutes, and seconds are present
46-
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)
29+
with open(str(f)) as logfile:
30+
for line in logfile:
31+
elapsedTime = 0
32+
33+
if 'Elapsed time' in line:
34+
# Extract the portion that has the time
35+
timePor = line.strip().replace('Elapsed time: ', '')
36+
# Remove the units from the time portion
37+
timePor = timePor.split('[h:]', 1)[0]
38+
# Ensure that hours, min and seconds are separated by ':' not '.'
39+
timePor = timePor.replace('.',':')
40+
# Calculate elapsed time that has this format 'h:m:s'
41+
timeList = timePor.split(':')
42+
if len(timeList) == 2:
43+
# Only minutes and seconds are present
44+
elapsedTime = int(timeList[0])*60 + int(timeList[1])
45+
elif len(timeList) == 3:
46+
# Hours, minutes, and seconds are present
47+
elapsedTime = int(timeList[0])*3600 + int(timeList[1])*60 + int(timeList[2])
48+
if elapsedTime == 0:
49+
print('No elapsed time found in', str(f), file=sys.stderr)
4950

5051
# Print the name of the step and the corresponding elapsed time
51-
print('%-25s %10s' % (os.path.splitext(os.path.basename(str(f)))[0], elapsedTime))
52+
if elapsedTime != 0:
53+
print('%-25s %10s' % (os.path.splitext(os.path.basename(str(f)))[0], elapsedTime))
5254

5355

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@
1111
from datetime import datetime
1212
import argparse # argument parsing
1313
from pathlib import Path
14+
import importlib # module reloading
1415

1516
# make sure the working dir is flow/
16-
os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)) , '..'))
17+
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
18+
os.chdir(os.path.dirname(os.path.abspath(__file__)))
1719

1820
class TestElapsedTime(unittest.TestCase):
1921

2022
def setUp(self):
2123
self.tmp_dir = tempfile.TemporaryDirectory()
2224
self.log_file = os.path.join(self.tmp_dir.name, "1_test.log")
25+
self.module_name = "genElapsedTime"
2326

2427
@patch("sys.stdout", new_callable=StringIO)
2528
def test_elapsed_time(self, mock_stdout):
@@ -28,23 +31,24 @@ def test_elapsed_time(self, mock_stdout):
2831
f.write("Some log entry\n")
2932
f.write("Elapsed time: 01:30:00[h:]min:sec.\n")
3033
# call the script with the test log file
31-
sys.argv = ["util/genElapsedTime.py", "--logDir", str(self.tmp_dir.name)]
34+
sys.argv = ["./genElapsedTime.py", "--logDir", str(self.tmp_dir.name)]
3235
with patch.object(sys, 'argv', sys.argv):
33-
import genElapsedTime
36+
module = importlib.import_module(self.module_name)
3437
# check if output is correct
3538
expected_output = "1_test 5400\n"
3639
self.assertEqual(mock_stdout.getvalue(), expected_output)
3740

3841
@patch("sys.stdout", new_callable=StringIO)
39-
def test_longer_elapsed_time(self, mock_stdout):
42+
def test_elapsed_time_longer_duration(self, mock_stdout):
4043
# create a log file with elapsed time
4144
with open(self.log_file, "w") as f:
4245
f.write("Some log entry\n")
4346
f.write("Elapsed time: 12:24.14[h:]min:sec. CPU time: user 5081.82 sys 170.18 (705%). Peak memory: 9667132KB.\n")
4447
# call the script with the test log file
4548
sys.argv = ["util/genElapsedTime.py", "--logDir", str(self.tmp_dir.name)]
4649
with patch.object(sys, 'argv', sys.argv):
47-
import genElapsedTime
50+
module = importlib.import_module(self.module_name)
51+
importlib.reload(module)
4852
# check if output is correct
4953
expected_output = "1_test 44654\n"
5054
self.assertEqual(mock_stdout.getvalue(), expected_output)
@@ -53,20 +57,21 @@ def test_missing_arg(self):
5357
with self.assertRaises(SystemExit):
5458
with patch('sys.stderr', new=StringIO()) as fake_err_output:
5559
with patch('sys.argv', ["util/genElapsedTime.py"]):
56-
import genElapsedTime
60+
module = importlib.import_module(self.module_name)
61+
importlib.reload(module)
5762
self.assertIn('--logDir', fake_err_output.getvalue())
5863

59-
6064
def test_no_elapsed_time(self):
6165
with open(self.log_file, "w") as f:
6266
f.write('Other log message')
6367
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())
68+
with patch('sys.stderr', new=StringIO()) as fake_err_output:
69+
module = importlib.import_module(self.module_name)
70+
importlib.reload(module)
71+
self.assertIn('No elapsed time found in', fake_err_output.getvalue())
6772

6873
def tearDown(self):
6974
self.tmp_dir.cleanup()
7075

7176
if __name__ == '__main__':
72-
unittest.main()
77+
unittest.main()

0 commit comments

Comments
 (0)