Skip to content

Commit

Permalink
Merge pull request #71 from kmosher/master
Browse files Browse the repository at this point in the history
Fix crash when tracking mtime of 0 files
  • Loading branch information
dnephin committed Feb 12, 2015
2 parents 2f98067 + 6237465 commit f94a077
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions staticconf/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ def __init__(self, filenames):
self.last_max_mtime = self.get_most_recent_changed()

def get_most_recent_changed(self):
if not self.filenames:
return -1
return max(os.path.getmtime(name) for name in self.filenames)

def has_changed(self):
Expand Down
34 changes: 34 additions & 0 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,10 @@ def test_reload_custom(self):

class TestInodeComparator(object):

def test_get_inodes_empty(self):
comparator = config.InodeComparator([])
assert comparator.get_inodes() == []

@mock.patch('staticconf.config.os.stat', autospec=True)
def test_get_inodes(self, mock_stat):
comparator = config.InodeComparator(['./one.file'])
Expand All @@ -396,6 +400,32 @@ def test_get_inodes(self, mock_stat):
assert_equal(inodes, expected)


class TestMTimeComparator(object):

def test_get_most_recent_empty(self):
comparator = config.MTimeComparator([])
assert comparator.get_most_recent_changed() == -1

@mock.patch('staticconf.config.os.path.getmtime', autospec=True, side_effect=[0,0,1,2,3])
def test_get_most_recent(self, mock_mtime):
comparator = config.MTimeComparator(['./one.file', './two.file'])
assert comparator.get_most_recent_changed() == 2
assert mock_mtime.call_count == 4

@mock.patch('staticconf.config.os.path.getmtime', autospec=True, return_value=1)
def test_no_change(self, mock_mtime):
comparator = config.MTimeComparator(['./one.file'])
assert not comparator.has_changed()
assert not comparator.has_changed()

@mock.patch('staticconf.config.os.path.getmtime', autospec=True, side_effect=[0,1,1,2])
def test_changes(self, mock_mtime):
comparator = config.MTimeComparator(['./one.file'])
assert comparator.has_changed()
assert not comparator.has_changed()
assert comparator.has_changed()


class TestMD5Comparator(object):

@pytest.yield_fixture()
Expand All @@ -410,6 +440,10 @@ def write_contents(self, contents):
self.file.write(contents)
self.file.flush()

def test_get_hashes_empty(self):
comparator = config.MD5Comparator([])
assert comparator.get_hashes() == []

def test_has_changed_no_changes(self, comparator):
assert not comparator.has_changed()
self.write_contents(self.original_contents)
Expand Down

0 comments on commit f94a077

Please sign in to comment.