Skip to content

Commit

Permalink
More tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
lgastako committed Mar 29, 2013
1 parent ce7ad79 commit c839b07
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
8 changes: 6 additions & 2 deletions mon.py
Expand Up @@ -199,10 +199,14 @@ def _removed_files(self, pattern, pattern_files):
self.pattern_files_cache[pattern] = pattern_files
return removed_files


def _file_changed(self, pfile):
last_ts = self._timestamps.get(pfile)
file_ts = self._get_file_timestamp(pfile)
try:
file_ts = self._get_file_timestamp(pfile)
except OSError:
# We assume this is because the file was removed before we could
# get the time, so it definitely changed.
return True
if not last_ts or file_ts > last_ts:
self._timestamps[pfile] = file_ts
return True
Expand Down
52 changes: 38 additions & 14 deletions test_mon.py
Expand Up @@ -275,6 +275,8 @@ def test_can_reference_modified_filename_list_in_action(self, capfd):

class TestPollingMonitor:

default_fn = "foo.ttt"

def setup_method(self, method):
self.thread = None
self.calls = 0
Expand All @@ -295,48 +297,70 @@ def run_in_thread(self, f):

def execute_all(self, quiet, changes):
# Mock method for our PollingMonitor
print "CHANGES: ", changes
self.calls += 1

def touch(self, tmpdir, filename):
def touch(self, tmpdir, filename=default_fn):
with tmpdir.join(filename).open("w") as f:
pass

def wait_a_sec(self):
time.sleep(0.01)
def remove(self, tmpdir, filename=default_fn):
tmpdir.join(filename).remove(rec=1)

def wait_a_sec(self, duration=0.01):
time.sleep(duration)

def start_monitor(self, monitor):
self.run_in_thread(lambda: monitor.monitor())

def test_no_call_when_no_change(self, tmpdir):
monitor = self.make_monitor(tmpdir)
try:
self.run_in_thread(lambda: monitor.monitor())
self.start_monitor(monitor)
self.wait_a_sec()
finally:
monitor.stop()
assert self.calls == 0

def test_detects_file_changed(self, tmpdir):
self.touch(tmpdir, "foo.ttt")
self.touch(tmpdir)
monitor = self.make_monitor(tmpdir)
try:
self.run_in_thread(lambda: monitor.monitor())
self.touch(tmpdir, "foo.ttt")
self.start_monitor(monitor)
self.touch(tmpdir)
self.wait_a_sec()
finally:
monitor.stop()
assert self.calls == 1
# One for start, one for change
assert self.calls == 2

def test_detects_file_added(self, tmpdir):
monitor = self.make_monitor(tmpdir)
try:
self.run_in_thread(lambda: monitor.monitor())
self.touch(tmpdir, "foo.ttt")
self.start_monitor(monitor)
self.touch(tmpdir)
self.wait_a_sec()
finally:
monitor.stop()
# None for start (no files), one for add.
assert self.calls == 1

def test_detects_file_removed(self):
raise NotImplementedError
def test_detects_file_removed(self, tmpdir):
self.touch(tmpdir)
monitor = self.make_monitor(tmpdir)
try:
self.run_in_thread(lambda: monitor.monitor())
self.remove(tmpdir)
self.wait_a_sec(0.1)
finally:
monitor.stop()
# One for start, one for remove
assert self.calls == 2

def test_stop(self):
raise NotImplementedError
def test_stop(self, tmpdir):
monitor = self.make_monitor(tmpdir)
thread = self.run_in_thread(lambda: monitor.monitor())
monitor.stop()
self.wait_a_sec()
assert not thread.is_alive()

0 comments on commit c839b07

Please sign in to comment.