def test_on_modify(handler: XacroUpdateHandler, xacro_file, xacro_dir, robot_description_server, canonicalize_xml): # Invalid types handler.on_modified(wevt.FileCreatedEvent(xacro_file)) handler.on_modified(wevt.FileDeletedEvent(xacro_file)) handler.on_modified(wevt.FileMovedEvent(xacro_file, '')) handler.on_modified(wevt.DirCreatedEvent(xacro_dir)) handler.on_modified(wevt.DirMovedEvent(xacro_dir, '')) handler.on_modified(wevt.DirDeletedEvent(xacro_dir)) handler.on_modified(wevt.DirModifiedEvent(xacro_dir)) # Invalid file handler.on_modified(wevt.FileModifiedEvent('')) rclpy.spin_once(robot_description_server, timeout_sec=1) assert robot_description_server.get_parameter( 'robot_description').value == '' # Valid file handler.on_modified(wevt.FileModifiedEvent(xacro_file)) rclpy.spin_once(robot_description_server, timeout_sec=1) expected = canonicalize_xml(handler.xacro_observer.xacro_tree.xml_string()) actual = canonicalize_xml( robot_description_server.get_parameter('robot_description').value) assert expected == actual
def test_ignored(repo_path, fake_handler): fake_handler.dispatch(events.FileModifiedEvent(os.path.join( repo_path, 'a/1.txt'))) fake_handler.dispatch(events.FileModifiedEvent(os.path.join( repo_path, 'a/2.txt'))) fake_handler.dispatch(events.FileModifiedEvent(os.path.join( repo_path, '.git/eci/version'))) assert fake_handler.on_any_event.call_count == 0 assert fake_handler.on_created.call_count == 0 assert fake_handler.on_deleted.call_count == 0 assert fake_handler.on_modified.call_count == 0 assert fake_handler.on_moved.call_count == 0
def test_modified(repo_path, fake_handler): fake_handler.dispatch(events.FileModifiedEvent(os.path.join( repo_path, 'test'))) assert fake_handler.on_any_event.call_count == 1 assert fake_handler.on_created.call_count == 0 assert fake_handler.on_deleted.call_count == 0 assert fake_handler.on_modified.call_count == 1 assert fake_handler.on_moved.call_count == 0
def test_on_modified_regenerates_output(self): markdown = os.path.join(self.site.path, 'index.md') open(markdown, 'w').close() handler = SiteHandler(self.director) event = events.FileModifiedEvent(markdown) handler.on_modified(event) html = os.path.join(self.director.config.outdir, 'index.html') self.assertTrue(os.path.exists(html))
def test_file_on_modify(self): content = b"now is the winter of our discontent" checksum = md5(content).hexdigest() p = osp.join(self.ws, "foo.txt") with open(p, "wb") as f: f.write(content) ev = events.FileModifiedEvent(p) inode = dict(node_path=p) yield self.h._add_hash_to_inode(ev, inode) self.assertIn("md5", inode, "checksum was not added to inode") self.assertEquals(checksum, inode["md5"])
def _dispatch_test_helper(self, path, ignore_directories=False): """patterns: 'relative/path/*.py', 'relative/path/src/' ignore_patterns: 'relative/path/*.rst', 'relative/path/__pycache__/', 'relative/path/htmlcov/' """ from watchdog import events class EchoAutoRunTrick(AutoRunTrick): log = [] def __del__(self): del type(self).log def on_any_event(self, event): type(self).log += ['on_any_event'] def on_created(self, event): type(self).log += ['on_created'] def on_modified(self, event): type(self).log += ['on_modified'] def on_moved(self, event): type(self).log += ['on_moved'] def on_deleted(self, event): type(self).log += ['on_deleted'] included = ['relative/path/*.py', 'relative/path/src/'] excluded = ['relative/path/*.rst', 'relative/path/__pycache__/', 'relative/path/htmlcov/'] handler = EchoAutoRunTrick(patterns=included, ignore_patterns=excluded, ignore_directories=ignore_directories) created = events.FileCreatedEvent(path) modified = events.FileModifiedEvent(path) deleted = events.FileDeletedEvent(path) moved = events.FileMovedEvent(path, 'relative/path/yummy.rst') dircreated = events.DirCreatedEvent(path) dirmodified = events.DirModifiedEvent(path) dirdeleted = events.DirDeletedEvent(path) dirmoved = events.DirMovedEvent(path, 'relative/path/htmlcov/') fevents = (created, modified, moved, deleted) devents = (dircreated, dirmodified, dirmoved, dirdeleted) event_types = ['on_created', 'on_modified', 'on_moved', 'on_deleted'] return handler, fevents, devents, event_types
def reload_and_rerun_on_module_changes(module: types.ModuleType, session_id): event_handler = events.FileModifiedEvent(module.__file__) def dispatch(event): if event.src_path == module.__file__: importlib.reload(module) rerun(session_id) event_handler.dispatch = dispatch module_directory = pathlib.Path(module.__file__).parent # If a normal observer is used here sometimes there can be a burst # of observations triggered (for example when using VS Code). By # using polling here this effectively debounces the observation # signal. observer = polling.PollingObserver() observer.schedule(event_handler, module_directory, recursive=False) observer.start()
def events(self): if self._ignore: self._pool = OrderedDict([(k, v) for k, v in self._pool.items() if not is_ignored(k)]) self._final = OrderedDict([(k, v) for k, v in self._final.items() if not is_ignored(k)]) self._initial = OrderedDict([(k, v) for k, v in self._initial.items() if not is_ignored(k)]) i_pool = {v: k for k, v in self._pool.items()} i_final = {v: k for k, v in self._final.items()} i_initial = {v: k for k, v in self._initial.items()} moved = set() created = set(self._final.keys()) - set(self._initial.keys()) deleted = set(self._initial.keys()) - set(self._final.keys()) modified = set(i for i in self._pool.values() if i.modified and not i.is_folder) # Probably don't need both loops here but better safe than sorry # If an item has been created and deleted it is actuall a move for key in set(created): src = i_initial.get(self._final[key]) if src: created.remove(key) moved.add((src, key)) for key in set(deleted): dest = i_final.get(self._initial[key]) if dest: deleted.remove(key) moved.add((key, dest)) # Sort by place in the file hierarchy # Children come before parents # NOTE: Create events must be sorted THE OPPOSITE direction sorter = lambda x: x.count(os.path.sep) # Windows reports folder deletes are file deletes + modifies # If a child exists for any file assume it is a directory for delete in deleted: for other in deleted: if delete != other and other.startswith(delete): self._initial[delete].is_folder = True break evts = list( itertools.chain( (events.DirMovedEvent(src, dest) if self._final[dest].is_folder else events.FileMovedEvent(src, dest) for src, dest in sorted(moved, key=lambda x: x[0].count(os.path.sep), reverse=True)), (events.DirDeletedEvent(x) if self._initial[x].is_folder else events.FileDeletedEvent(x) for x in sorted(deleted, key=sorter, reverse=True)), (events.DirCreatedEvent(x) if self._final[x].is_folder else events.FileCreatedEvent(x) for x in sorted(created, key=sorter)), (events.FileModifiedEvent(i_pool[x]) for x in modified if x in i_final and not i_pool[x] in created), )) mapped = set([(getattr(event, 'dest_path', event.src_path), event.event_type) for event in evts if event.is_directory and not event.event_type == EVENT_TYPE_CREATED]) # Do our best to dedup all found events. # If there is a matching event type at a parent path disregard the children def check(event): segments = getattr(event, 'dest_path', event.src_path).split(os.path.sep) for i in range(len(segments) - 1): if (os.path.sep.join(segments[:i + 1]), event.event_type) in mapped: return False return True return list(filter(check, evts))