|
| 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