Skip to content
This repository was archived by the owner on Aug 17, 2023. It is now read-only.

Commit ad0feee

Browse files
committed
Add more test coverage for hamlpy_watcher
1 parent 9fa2ad2 commit ad0feee

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

hamlpy/hamlpy_watcher.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,5 +182,5 @@ def compile_file(fullpath, outfile_name, compiler_args):
182182
return False
183183

184184

185-
if __name__ == '__main__':
185+
if __name__ == '__main__': # pragma: no cover
186186
watch_folder()

hamlpy/test/test_watcher.py

+40-17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import shutil
55
import sys
6+
import time
67
import unittest
78

89
from mock import patch
@@ -19,10 +20,6 @@ class ScriptExit(Exception):
1920
def __init__(self, exit_code):
2021
self.exit_code = exit_code
2122

22-
@classmethod
23-
def mock(cls, code):
24-
raise cls(code)
25-
2623

2724
class WatcherTest(unittest.TestCase):
2825

@@ -35,31 +32,57 @@ def test_watch_folder(self):
3532
os.makedirs(OUTPUT_DIR)
3633

3734
# create some haml files for testing
38-
self._write_file(INPUT_DIR + os.sep + 'test.haml', "%span{'class': 'test'}\n")
35+
self._write_file(INPUT_DIR + os.sep + 'test.haml', "%span{'class': 'test'}\n- macro\n")
3936
self._write_file(INPUT_DIR + os.sep + 'error.haml', "%div{")
4037

4138
# run as once off pass - should return 1 for number of failed conversions
42-
self._run_script(['hamlpy_watcher.py', INPUT_DIR, OUTPUT_DIR, '--once', '--input-extension=.haml'], 1)
39+
self._run_script([
40+
'hamlpy_watcher.py',
41+
INPUT_DIR, OUTPUT_DIR,
42+
'--once', '--input-extension=.haml', '--verbose', '--tag=macro:endmacro'
43+
], 1)
4344

4445
# check file without errors was converted
45-
output = self._read_file(OUTPUT_DIR + os.sep + 'test.html')
46-
self.assertEqual(output, "<span class='test'></span>\n")
47-
48-
def _read_file(self, path):
46+
self.assertFileContents(OUTPUT_DIR + os.sep + 'test.html',
47+
"<span class='test'></span>\n{% macro %}\n{% endmacro %}\n")
48+
49+
# run without output directory which should make it default to re-using the input directory
50+
self._run_script([
51+
'hamlpy_watcher.py',
52+
INPUT_DIR,
53+
'--once', '--input-extension=.haml', '--tag=macro:endmacro'
54+
], 1)
55+
56+
self.assertFileContents(INPUT_DIR + os.sep + 'test.html',
57+
"<span class='test'></span>\n{% macro %}\n{% endmacro %}\n")
58+
59+
# run in watch mode with 1 second refresh
60+
self._run_script([
61+
'hamlpy_watcher.py',
62+
INPUT_DIR,
63+
'--refresh=1', '--input-extension=.haml', '--tag=macro:endmacro'
64+
], 1)
65+
66+
def assertFileContents(self, path, contents):
4967
with open(path, 'r') as f:
50-
return f.read()
68+
self.assertEqual(f.read(), contents)
5169

5270
def _write_file(self, path, text):
5371
with open(path, 'w') as f:
5472
f.write(text)
5573

5674
def _run_script(self, script_args, expected_exit_code):
75+
def raise_exception_with_code(code):
76+
raise ScriptExit(code)
77+
5778
# patch sys.exit so it throws an exception so we can return execution to this test
58-
with patch.object(sys, 'exit', side_effect=ScriptExit.mock):
79+
# patch sys.argv to pass our arguments to the script
80+
# patch time.sleep to be interrupted
81+
with patch.object(sys, 'exit', side_effect=raise_exception_with_code), \
82+
patch.object(sys, 'argv', script_args), \
83+
patch.object(time, 'sleep', side_effect=KeyboardInterrupt), \
84+
self.assertRaises(ScriptExit) as raises:
5985

60-
# patch sys.argv to pass our arguments to the script
61-
with patch.object(sys, 'argv', script_args):
62-
with self.assertRaises(ScriptExit) as raises:
63-
watch_folder()
86+
watch_folder()
6487

65-
assert raises.exception.exit_code == expected_exit_code
88+
assert raises.exception.exit_code == expected_exit_code

0 commit comments

Comments
 (0)