44from unittest .mock import patch
55from unittest .mock import MagicMock
66from io import StringIO
7- import io
87import sys
98import os
109import tempfile
11- from datetime import datetime
12- import argparse # argument parsing
13- from pathlib import Path
10+ import importlib # module reloading
1411
1512# make sure the working dir is flow/
16- os .chdir (os .path .join (os .path .dirname (os .path .abspath (__file__ )) , '..' ))
13+ sys .path .append (os .path .join (os .path .dirname (os .path .abspath (__file__ )), '..' , 'util' ))
14+ os .chdir (os .path .join (os .path .dirname (os .path .abspath (__file__ ))))
1715
1816class TestElapsedTime (unittest .TestCase ):
19-
2017 def setUp (self ):
2118 self .tmp_dir = tempfile .TemporaryDirectory ()
2219 self .log_file = os .path .join (self .tmp_dir .name , "1_test.log" )
20+ self .module_name = "genElapsedTime"
2321
2422 @patch ("sys.stdout" , new_callable = StringIO )
2523 def test_elapsed_time (self , mock_stdout ):
@@ -28,23 +26,24 @@ def test_elapsed_time(self, mock_stdout):
2826 f .write ("Some log entry\n " )
2927 f .write ("Elapsed time: 01:30:00[h:]min:sec.\n " )
3028 # call the script with the test log file
31- sys .argv = ["util /genElapsedTime.py" , "--logDir" , str (self .tmp_dir .name )]
29+ sys .argv = [". /genElapsedTime.py" , "--logDir" , str (self .tmp_dir .name )]
3230 with patch .object (sys , 'argv' , sys .argv ):
33- import genElapsedTime
31+ module = importlib . import_module ( self . module_name )
3432 # check if output is correct
3533 expected_output = "1_test 5400\n "
3634 self .assertEqual (mock_stdout .getvalue (), expected_output )
3735
3836 @patch ("sys.stdout" , new_callable = StringIO )
39- def test_longer_elapsed_time (self , mock_stdout ):
37+ def test_elapsed_time_longer_duration (self , mock_stdout ):
4038 # create a log file with elapsed time
4139 with open (self .log_file , "w" ) as f :
4240 f .write ("Some log entry\n " )
4341 f .write ("Elapsed time: 12:24.14[h:]min:sec. CPU time: user 5081.82 sys 170.18 (705%). Peak memory: 9667132KB.\n " )
4442 # call the script with the test log file
4543 sys .argv = ["util/genElapsedTime.py" , "--logDir" , str (self .tmp_dir .name )]
4644 with patch .object (sys , 'argv' , sys .argv ):
47- import genElapsedTime
45+ module = importlib .import_module (self .module_name )
46+ importlib .reload (module )
4847 # check if output is correct
4948 expected_output = "1_test 44654\n "
5049 self .assertEqual (mock_stdout .getvalue (), expected_output )
@@ -53,20 +52,21 @@ def test_missing_arg(self):
5352 with self .assertRaises (SystemExit ):
5453 with patch ('sys.stderr' , new = StringIO ()) as fake_err_output :
5554 with patch ('sys.argv' , ["util/genElapsedTime.py" ]):
56- import genElapsedTime
55+ module = importlib .import_module (self .module_name )
56+ importlib .reload (module )
5757 self .assertIn ('--logDir' , fake_err_output .getvalue ())
5858
59-
6059 def test_no_elapsed_time (self ):
6160 with open (self .log_file , "w" ) as f :
6261 f .write ('Other log message' )
6362 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 ())
63+ with patch ('sys.stderr' , new = StringIO ()) as fake_err_output :
64+ module = importlib .import_module (self .module_name )
65+ importlib .reload (module )
66+ self .assertIn ('No elapsed time found in' , fake_err_output .getvalue ())
6767
6868 def tearDown (self ):
6969 self .tmp_dir .cleanup ()
7070
7171if __name__ == '__main__' :
72- unittest .main ()
72+ unittest .main ()
0 commit comments