1111from datetime import datetime
1212import argparse # argument parsing
1313from 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
1820class 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
7176if __name__ == '__main__' :
72- unittest .main ()
77+ unittest .main ()
0 commit comments