def test_callback_not_called_if_same_mtime(self): """Test that we ignore files with same mtime.""" cb = mock.Mock() self.os.stat = lambda x: FakeStat(101) self.mock_util.calc_md5_with_blocking_retries = lambda x: "1" ro = event_based_file_watcher.EventBasedFileWatcher( "/this/is/my/file.py", cb) fo = event_based_file_watcher._MultiFileWatcher.get_singleton() fo._observer.schedule.assert_called_once() folder_handler = fo._observer.schedule.call_args[0][0] cb.assert_not_called() # self.os.stat = lambda x: FakeStat(102) # Same mtime! self.mock_util.calc_md5_with_blocking_retries = lambda x: "2" ev = events.FileSystemEvent("/this/is/my/file.py") ev.event_type = events.EVENT_TYPE_MODIFIED folder_handler.on_modified(ev) # This is the test: cb.assert_not_called() ro.close()
def test_file_watch_and_callback(self): """Test that when a file is modified, the callback is called.""" cb = mock.Mock() self.os.stat = lambda x: FakeStat(101) self.mock_util.calc_md5_with_blocking_retries = lambda x: "1" ro = EventBasedFileWatcher.EventBasedFileWatcher("/this/is/my/file.py", cb) fo = EventBasedFileWatcher._MultiFileWatcher.get_singleton() fo._observer.schedule.assert_called_once() folder_handler = fo._observer.schedule.call_args[0][0] cb.assert_not_called() self.os.stat = lambda x: FakeStat(102) self.mock_util.calc_md5_with_blocking_retries = lambda x: "2" ev = events.FileSystemEvent("/this/is/my/file.py") ev.event_type = events.EVENT_TYPE_MODIFIED folder_handler.on_modified(ev) cb.assert_called_once() ro.close()
def test_works_with_directories(self): """Test that when a directory is modified, the callback is called.""" cb = mock.Mock() self.mock_util.path_modification_time = lambda *args: 101.0 self.mock_util.calc_md5_with_blocking_retries = lambda _, **kwargs: "1" ro = event_based_path_watcher.EventBasedPathWatcher( "/this/is/my/dir", cb) fo = event_based_path_watcher._MultiPathWatcher.get_singleton() fo._observer.schedule.assert_called_once() folder_handler = fo._observer.schedule.call_args[0][0] cb.assert_not_called() self.mock_util.path_modification_time = lambda *args: 102.0 self.mock_util.calc_md5_with_blocking_retries = lambda _, **kwargs: "2" ev = events.FileSystemEvent("/this/is/my/dir") ev.event_type = events.EVENT_TYPE_MODIFIED ev.is_directory = True folder_handler.on_modified(ev) cb.assert_called_once() ro.close()
def test_callback_not_called_if_wrong_event_type(self): """Test that we ignore created files.""" cb = mock.Mock() self.mock_util.path_modification_time = lambda *args: 101.0 self.mock_util.calc_md5_with_blocking_retries = lambda _, **kwargs: "1" ro = event_based_path_watcher.EventBasedPathWatcher( "/this/is/my/file.py", cb) fo = event_based_path_watcher._MultiPathWatcher.get_singleton() fo._observer.schedule.assert_called_once() folder_handler = fo._observer.schedule.call_args[0][0] cb.assert_not_called() self.mock_util.path_modification_time = lambda *args: 102.0 self.mock_util.calc_md5_with_blocking_retries = lambda _, **kwargs: "2" ev = events.FileSystemEvent("/this/is/my/file.py") ev.event_type = events.EVENT_TYPE_DELETED # Wrong type folder_handler.on_modified(ev) # This is the test: cb.assert_not_called() ro.close()
def test_callback_not_called_if_wrong_event_type(self): """Test that we ignore created files.""" cb = mock.Mock() self.os.stat = lambda x: FakeStat(101) self.mock_util.calc_md5_with_blocking_retries = lambda x: '1' ro = EventBasedFileWatcher.EventBasedFileWatcher( '/this/is/my/file.py', cb) fo = EventBasedFileWatcher._MultiFileWatcher.get_singleton() fo._observer.schedule.assert_called_once() folder_handler = fo._observer.schedule.call_args[0][0] cb.assert_not_called() self.os.stat = lambda x: FakeStat(102) self.mock_util.calc_md5_with_blocking_retries = lambda x: '2' ev = events.FileSystemEvent('/this/is/my/file.py') ev.event_type = events.EVENT_TYPE_DELETED # Wrong type folder_handler.on_modified(ev) # This is the test: cb.assert_not_called() ro.close()
def modify_mock_file(): self.os.stat = lambda x: FakeStat(mod_count[0]) self.mock_util.calc_md5_with_blocking_retries = ( lambda x: "%d" % mod_count[0]) ev = events.FileSystemEvent(filename) ev.event_type = events.EVENT_TYPE_MODIFIED folder_handler.on_modified(ev) mod_count[0] += 1
def modify_mock_file(): self.mock_util.path_modification_time = lambda *args: mod_count[0] self.mock_util.calc_md5_with_blocking_retries = ( lambda _, **kwargs: "%d" % mod_count[0]) ev = events.FileSystemEvent(filename) ev.event_type = events.EVENT_TYPE_MODIFIED folder_handler.on_modified(ev) mod_count[0] += 1.0
def test_kwargs_plumbed_to_calc_md5(self): """Test that we pass the glob_pattern and allow_nonexistent kwargs to calc_md5_with_blocking_retries. `EventBasedPathWatcher`s can be created with optional kwargs allowing the caller to specify what types of files to watch (when watching a directory) and whether to allow watchers on paths with no files/dirs. This test ensures that these optional parameters make it to our hash calculation helpers across different on_changed events. """ cb = mock.Mock() self.mock_util.path_modification_time = lambda *args: 101.0 self.mock_util.calc_md5_with_blocking_retries = mock.Mock( return_value="1") ro = event_based_path_watcher.EventBasedPathWatcher( "/this/is/my/dir", cb, glob_pattern="*.py", allow_nonexistent=True, ) fo = event_based_path_watcher._MultiPathWatcher.get_singleton() fo._observer.schedule.assert_called_once() folder_handler = fo._observer.schedule.call_args[0][0] _, kwargs = self.mock_util.calc_md5_with_blocking_retries.call_args assert kwargs == {"glob_pattern": "*.py", "allow_nonexistent": True} cb.assert_not_called() self.mock_util.path_modification_time = lambda *args: 102.0 self.mock_util.calc_md5_with_blocking_retries = mock.Mock( return_value="3") ev = events.FileSystemEvent("/this/is/my/dir") ev.event_type = events.EVENT_TYPE_MODIFIED ev.is_directory = True folder_handler.on_modified(ev) _, kwargs = self.mock_util.calc_md5_with_blocking_retries.call_args assert kwargs == {"glob_pattern": "*.py", "allow_nonexistent": True} cb.assert_called_once() ro.close()