77import sys
88import os
99import tempfile
10- import importlib # module reloading
1110
1211# make sure the working dir is flow/
13- sys .path .append (os .path .join (os .path .dirname (os .path .abspath (__file__ )), '..' , 'util' ))
12+ sys .path .append (os .path .join (os .path .dirname (os .path .abspath (__file__ )),
13+ '..' , 'util' ))
1414os .chdir (os .path .join (os .path .dirname (os .path .abspath (__file__ ))))
1515
16+ import genElapsedTime
17+
1618class TestElapsedTime (unittest .TestCase ):
1719 def setUp (self ):
1820 self .tmp_dir = tempfile .TemporaryDirectory ()
@@ -24,29 +26,25 @@ def test_elapsed_time(self, mock_stdout):
2426 # create a log file with elapsed time
2527 with open (self .log_file , "w" ) as f :
2628 f .write ("Some log entry\n " )
27- f .write ("Elapsed time: 01:30:00[h:]min:sec.\n " )
29+ f .write ("Elapsed time: 01:30:00[h:]min:sec. Peak memory: 9667132KB. \n " )
2830 # call the script with the test log file
29- sys .argv = ["./genElapsedTime.py" , "--logDir" , str (self .tmp_dir .name ),
30- "--noHeader" ]
31- with patch .object (sys , 'argv' , sys .argv ):
32- module = importlib .import_module (self .module_name )
31+ genElapsedTime .scan_logs (["--logDir" , str (self .tmp_dir .name ),
32+ "--noHeader" ])
3333 # check if output is correct
34- expected_output = self .tmp_dir .name + "\n 1_test 5400\n Total 5400\n "
34+ expected_output = self .tmp_dir .name + "\n 1_test 5400 9667132 \n Total 5400 9667132 \n "
3535 self .assertEqual (mock_stdout .getvalue (), expected_output )
3636
3737 @patch ("sys.stdout" , new_callable = StringIO )
3838 def test_zero_time (self , mock_stdout ):
3939 # create a log file with elapsed time
4040 with open (self .log_file , "w" ) as f :
4141 f .write ("Some log entry\n " )
42- f .write ("Elapsed time: 00:00:74[h:]min:sec.\n " )
42+ f .write ("Elapsed time: 00:00:74[h:]min:sec. Peak memory: 9667132KB. \n " )
4343 # call the script with the test log file
44- sys .argv = ["./genElapsedTime.py" , "--logDir" , str (self .tmp_dir .name ),
45- "--noHeader" ]
46- with patch .object (sys , 'argv' , sys .argv ):
47- module = importlib .import_module (self .module_name )
48- # check if output is correct
49- self .assertEqual (mock_stdout .getvalue (), "" )
44+ genElapsedTime .scan_logs (["--logDir" , str (self .tmp_dir .name ),
45+ "--noHeader" ])
46+ expected_output = self .tmp_dir .name + "\n 1_test 74 9667132\n Total 74 9667132\n "
47+ self .assertEqual (mock_stdout .getvalue (), expected_output )
5048
5149 @patch ("sys.stdout" , new_callable = StringIO )
5250 def test_elapsed_time_longer_duration (self , mock_stdout ):
@@ -55,36 +53,30 @@ def test_elapsed_time_longer_duration(self, mock_stdout):
5553 f .write ("Some log entry\n " )
5654 f .write ("Elapsed time: 12:24.14[h:]min:sec. CPU time: user 5081.82 sys 170.18 (705%). Peak memory: 9667132KB.\n " )
5755 # call the script with the test log file
58- sys .argv = ["util/genElapsedTime.py" , "--logDir" , str (self .tmp_dir .name ),
59- "--noHeader" ]
60- with patch .object (sys , 'argv' , sys .argv ):
61- module = importlib .import_module (self .module_name )
62- importlib .reload (module )
56+ genElapsedTime .scan_logs (["--logDir" , str (self .tmp_dir .name ),
57+ "--noHeader" ])
6358 # check if output is correct
64- expected_output = self .tmp_dir .name + "\n 1_test 744\n Total 744\n "
59+ expected_output = (self .tmp_dir .name +
60+ "\n 1_test 744 9667132\n Total 744 9667132\n " )
6561 self .assertEqual (mock_stdout .getvalue (), expected_output )
6662
6763 def test_missing_arg (self ):
6864 with self .assertRaises (SystemExit ):
6965 with patch ('sys.stderr' , new = StringIO ()) as fake_err_output :
70- with patch ('sys.argv' , ["util/genElapsedTime.py" ]):
71- module = importlib .import_module (self .module_name )
72- importlib .reload (module )
66+ genElapsedTime .scan_logs (["util/genElapsedTime.py" ])
7367 self .assertIn ('--logDir' , fake_err_output .getvalue ())
7468
75- def test_no_elapsed_time (self ):
69+ @patch ("sys.stderr" , new_callable = StringIO )
70+ def test_no_elapsed_time (self , fake_err_output ):
7671 with open (self .log_file , "w" ) as f :
7772 f .write ('Other log message' )
78- with patch ('sys.argv' , ["util/genElapsedTime.py" ,
79- "--logDir" , str (self .tmp_dir .name ),
80- "--noHeader" ]):
81- with patch ('sys.stderr' , new = StringIO ()) as fake_err_output :
82- module = importlib .import_module (self .module_name )
83- importlib .reload (module )
84- self .assertIn ('No elapsed time found in' , fake_err_output .getvalue ())
73+ genElapsedTime .scan_logs (["--logDir" , str (self .tmp_dir .name ),
74+ "--noHeader" ])
75+ self .assertIn ('No elapsed time found in' , fake_err_output .getvalue ())
8576
8677 def tearDown (self ):
87- self .tmp_dir .cleanup ()
78+ self .tmp_dir .cleanup ()
79+
8880
8981if __name__ == '__main__' :
9082 unittest .main ()
0 commit comments