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_watchdog(fixtures):
    """Checks that the file system observer triggers imports on new/changed files"""
    tmpdir = fixtures["root"]

    acq_dir = tmpdir.join("12345678T000000Z_inst_zab")

    node = st.StorageNode.get(st.StorageNode.name == "x")

    import watchdog.events as ev

    watchdog_handler = auto_import.RegisterFile(node)

    # new acquisition file
    f = acq_dir.join("new_file.log")
    f.write("")
    assert file_copy_count("new_file.log") == 0
    watchdog_handler.on_created(ev.FileCreatedEvent(str(f)))
    assert file_copy_count("new_file.log") == 1

    # this file is outside any acqs and should be ignored
    g = tmpdir.join("some_file.log")
    g.write("Where is my acq?!")
    assert file_copy_count("some_file.log") == 0
    watchdog_handler.on_created(ev.FileCreatedEvent(str(g)))
    assert file_copy_count("some_file.log") == 0

    # now delete the lock and try reimport, which should succeed
    lock = acq_dir.join(".foo.zxc.lock")
    lock.remove()
    assert file_copy_count("foo.zxc") == 0
    watchdog_handler.on_deleted(ev.FileDeletedEvent(str(lock)))
    assert file_copy_count("foo.zxc") == 1
Exemple #3
0
    def test_add_remove(self):
        e = events.FileCreatedEvent('added.jpg')
        self.playlist._wrap_asset_iter.on_created(e)
        self.assertEqual(self.playlist.length(), 3)

        e = events.FileDeletedEvent('added.jpg')
        self.playlist._wrap_asset_iter.on_deleted(e)
        self.assertEqual(self.playlist.length(), 2)
Exemple #4
0
def test_deleted(repo_path, fake_handler):
    fake_handler.dispatch(events.FileDeletedEvent(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 == 1
    assert fake_handler.on_modified.call_count == 0
    assert fake_handler.on_moved.call_count == 0
Exemple #5
0
    def test_remove(self):
        asset_name_lst = [
            'test/media/home/20190601_12440.png',
            'test/media/home/IMG_6849.png'
        ]
        e = events.FileDeletedEvent('test/media/home/20190601_12440.png')
        self.playlist._wrap_asset_iter.on_deleted(e)
        self.assertEqual(self.playlist.length(), 1)
        self.assertEqual(
            self.playlist.get_next(False).filename, asset_name_lst[1])
        self.assertEqual(
            self.playlist.get_next(False).filename, asset_name_lst[1])
        self.assertEqual(
            self.playlist.get_next(True).filename, asset_name_lst[1])

        e = events.FileDeletedEvent('test/media/home/IMG_6849.png')
        self.playlist._wrap_asset_iter.on_deleted(e)
        self.assertEqual(self.playlist.length(), 0)
        self.assertIsNone(self.playlist.get_next(False))
        self.assertIsNone(self.playlist.get_next(True))
    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
Exemple #7
0
 def test_file_on_delete(self):
     ev = events.FileDeletedEvent(osp.join(self.ws, "foo.txt"))
     return self.assertFailure(
         self.h._add_hash_to_inode(ev, {}),
         RuntimeError,
     )
Exemple #8
0
    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))