3
3
import os
4
4
import shutil
5
5
import sys
6
+ import time
6
7
import unittest
7
8
8
9
from mock import patch
@@ -19,10 +20,6 @@ class ScriptExit(Exception):
19
20
def __init__ (self , exit_code ):
20
21
self .exit_code = exit_code
21
22
22
- @classmethod
23
- def mock (cls , code ):
24
- raise cls (code )
25
-
26
23
27
24
class WatcherTest (unittest .TestCase ):
28
25
@@ -35,31 +32,57 @@ def test_watch_folder(self):
35
32
os .makedirs (OUTPUT_DIR )
36
33
37
34
# 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 " )
39
36
self ._write_file (INPUT_DIR + os .sep + 'error.haml' , "%div{" )
40
37
41
38
# 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 )
43
44
44
45
# 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 ):
49
67
with open (path , 'r' ) as f :
50
- return f .read ()
68
+ self . assertEqual ( f .read (), contents )
51
69
52
70
def _write_file (self , path , text ):
53
71
with open (path , 'w' ) as f :
54
72
f .write (text )
55
73
56
74
def _run_script (self , script_args , expected_exit_code ):
75
+ def raise_exception_with_code (code ):
76
+ raise ScriptExit (code )
77
+
57
78
# 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 :
59
85
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 ()
64
87
65
- assert raises .exception .exit_code == expected_exit_code
88
+ assert raises .exception .exit_code == expected_exit_code
0 commit comments