diff --git a/tests/test_main.py b/tests/test_main.py index dce9bad..61f78b7 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -31,6 +31,27 @@ def test_should_compile_files_if_available(self, mock_compile_files): main() mock_compile_files.assert_called_once_with([self.filename_c], False) + @patch('universal.main.loop_and_compile') + def test_should_loop_and_compile_files_if_loop_short_flag_set(self, mock_loop_and_compile): + seconds = 123 + mock_loop_and_compile.return_value = 0 + sys.argv = ['dummy', self.filename_c, '-l', str(seconds)] + + main() + + mock_loop_and_compile.assert_called_once_with(seconds, [self.filename_c], False) + + @patch('universal.main.loop_and_compile') + def test_should_loop_and_compile_files_if_loop_long_flag_set(self, mock_loop_and_compile): + seconds = 123 + mock_loop_and_compile.return_value = 0 + sys.argv = ['dummy', self.filename_c, '--loop', str(seconds)] + + main() + + mock_loop_and_compile.assert_called_once_with(seconds, [self.filename_c], False) + + @patch('universal.main.ArgumentParser.print_usage') def test_should_show_usage_if_no_correct_argument(self, mock_argparser): sys.argv = ['dummy'] @@ -61,6 +82,41 @@ def test_should_call_problem_with_long_flag(self, mock_problem): main() mock_problem.assert_called_once_with() +from universal.main import loop_and_compile +class TestLoopAndCompile(unittest.TestCase): + def setUp(self): + self.filename_c = 'foobar.c' + self.otherthings = [self.filename_c] + self.memory = False + + @patch('universal.main.compile_files') + def test_should_not_run_if_time_is_zero_seconds(self, mock_compile_files): + seconds = 0 + loop_and_compile(seconds, self.otherthings, self.memory) + self.assertFalse(mock_compile_files.called) + + @patch('universal.main.compile_files') + def test_should_not_run_if_time_is_negative(self, mock_compile_files): + seconds = -12 + loop_and_compile(seconds, self.otherthings, self.memory) + self.assertFalse(mock_compile_files.called) + + @patch('time.sleep') + @patch('universal.main.compile_files') + def test_should_run_in_loop_if_wait_time_valid(self, + mock_compile_files, mock_sleep): + seconds = 2 + mock_compile_files.return_value = 0 + mock_sleep.side_effect = [0, 0, IOError('Boom!')] + + with self.assertRaises(IOError): + loop_and_compile(seconds, self.otherthings, self.memory) + + mock_compile_files.assert_called_with(self.otherthings, + self.memory) + mock_sleep.assert_called_with(seconds) + + class AnyStringContaining(str): def __eq__(self, other): return self in other diff --git a/universal/main.py b/universal/main.py index 44b87a9..7fd067b 100644 --- a/universal/main.py +++ b/universal/main.py @@ -21,7 +21,9 @@ # along with Universal. If not, see . # +from __future__ import print_function import sys +import time from argparse import ArgumentParser from universal.builder import compile_files @@ -33,6 +35,8 @@ def parse_known_args(): """ Parse command line arguments """ parser = ArgumentParser() + parser.add_argument("-l", "--loop", type=int, + help="Loop every X seconds") parser.add_argument("-u", "--update", action='store_true', dest="update", help="Update the software from online repo") parser.add_argument("-p", "--problem", action='store_true', dest="problem", @@ -43,11 +47,25 @@ def parse_known_args(): return args, otherthings, parser +def loop_and_compile(wait_duration_in_sec, otherthings, memory): + if wait_duration_in_sec < 1: + print('Invalid Argument: Loop wait time should be greater than 1 second') + return + print('Looping every %d seconds.'%wait_duration_in_sec) + print('Use Ctrl-C to stop.') + while True: + compile_files(otherthings, memory) + time.sleep(wait_duration_in_sec) + + def main(): args, otherthings, parser = parse_known_args() if len(otherthings) > 0: - compile_files(otherthings, args.memory) + if args.loop is not None: + loop_and_compile(args.loop, otherthings, args.memory) + else: + compile_files(otherthings, args.memory) elif args.update: return update() elif args.problem: