Skip to content

Commit

Permalink
Add loop_and_compile argument -l/--loop
Browse files Browse the repository at this point in the history
Issue #12
  • Loading branch information
ylogx committed May 17, 2015
1 parent be8efaf commit 4adc1ed
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
56 changes: 56 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion universal/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
# along with Universal. If not, see <http://www.gnu.org/licenses/>.
#

from __future__ import print_function
import sys
import time
from argparse import ArgumentParser

from universal.builder import compile_files
Expand All @@ -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",
Expand All @@ -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:
Expand Down

0 comments on commit 4adc1ed

Please sign in to comment.