This repository has been archived by the owner on Aug 17, 2023. It is now read-only.
forked from jessemiller/HamlPy
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from Psycojoker/watcher_testing
Fix PEP issues in hamlpy_watcher and add tests
- Loading branch information
Showing
3 changed files
with
122 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
.DS_Store | ||
.idea/ | ||
.tox/ | ||
.watcher_test/ | ||
build/ | ||
htmlcov/ | ||
env/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from __future__ import print_function, unicode_literals | ||
|
||
import os | ||
import shutil | ||
import sys | ||
import time | ||
import unittest | ||
|
||
from mock import patch | ||
|
||
from hamlpy.hamlpy_watcher import watch_folder | ||
|
||
|
||
WORKING_DIR = '.watcher_test' | ||
INPUT_DIR = WORKING_DIR + os.sep + 'input' | ||
OUTPUT_DIR = WORKING_DIR + os.sep + 'output' | ||
|
||
|
||
class ScriptExit(Exception): | ||
def __init__(self, exit_code): | ||
self.exit_code = exit_code | ||
|
||
|
||
class WatcherTest(unittest.TestCase): | ||
|
||
def test_watch_folder(self): | ||
# remove working directory if it exists and re-create it | ||
if os.path.exists(WORKING_DIR): | ||
shutil.rmtree(WORKING_DIR) | ||
|
||
os.makedirs(INPUT_DIR) | ||
os.makedirs(OUTPUT_DIR) | ||
|
||
# create some haml files for testing | ||
self._write_file(INPUT_DIR + os.sep + 'test.haml', "%span{'class': 'test'}\n- macro\n") | ||
self._write_file(INPUT_DIR + os.sep + 'error.haml', "%div{") | ||
|
||
# run as once off pass - should return 1 for number of failed conversions | ||
self._run_script([ | ||
'hamlpy_watcher.py', | ||
INPUT_DIR, OUTPUT_DIR, | ||
'--once', '--input-extension=.haml', '--verbose', '--tag=macro:endmacro' | ||
], 1) | ||
|
||
# check file without errors was converted | ||
self.assertFileContents(OUTPUT_DIR + os.sep + 'test.html', | ||
"<span class='test'></span>\n{% macro %}\n{% endmacro %}\n") | ||
|
||
# run without output directory which should make it default to re-using the input directory | ||
self._run_script([ | ||
'hamlpy_watcher.py', | ||
INPUT_DIR, | ||
'--once', '--input-extension=.haml', '--tag=macro:endmacro' | ||
], 1) | ||
|
||
self.assertFileContents(INPUT_DIR + os.sep + 'test.html', | ||
"<span class='test'></span>\n{% macro %}\n{% endmacro %}\n") | ||
|
||
# run in watch mode with 1 second refresh | ||
self._run_script([ | ||
'hamlpy_watcher.py', | ||
INPUT_DIR, | ||
'--refresh=1', '--input-extension=.haml', '--tag=macro:endmacro' | ||
], 1) | ||
|
||
def assertFileContents(self, path, contents): | ||
with open(path, 'r') as f: | ||
self.assertEqual(f.read(), contents) | ||
|
||
def _write_file(self, path, text): | ||
with open(path, 'w') as f: | ||
f.write(text) | ||
|
||
def _run_script(self, script_args, expected_exit_code): | ||
def raise_exception_with_code(code): | ||
raise ScriptExit(code) | ||
|
||
# patch sys.exit so it throws an exception so we can return execution to this test | ||
# patch sys.argv to pass our arguments to the script | ||
# patch time.sleep to be interrupted | ||
with patch.object(sys, 'exit', side_effect=raise_exception_with_code), \ | ||
patch.object(sys, 'argv', script_args), \ | ||
patch.object(time, 'sleep', side_effect=KeyboardInterrupt), \ | ||
self.assertRaises(ScriptExit) as raises: | ||
|
||
watch_folder() | ||
|
||
assert raises.exception.exit_code == expected_exit_code |