From 6237465a344fd97d561f0175e770a4c59dd745a6 Mon Sep 17 00:00:00 2001 From: Keith Mosher Date: Thu, 12 Feb 2015 00:42:27 -0800 Subject: [PATCH] Fix crash when tracking mtime of 0 files --- staticconf/config.py | 2 ++ tests/config_test.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/staticconf/config.py b/staticconf/config.py index eeb304a..a3545ca 100644 --- a/staticconf/config.py +++ b/staticconf/config.py @@ -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): diff --git a/tests/config_test.py b/tests/config_test.py index 2b123f7..d27797d 100644 --- a/tests/config_test.py +++ b/tests/config_test.py @@ -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']) @@ -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() @@ -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)