Beispiel #1
0
def mock_reload_env():
    """Set up the mock environment for unit-testing the reloader module."""
    # For unmodified files, just return a fake stat result with everything set
    # to 0.
    unmodified_stat_result = os.stat_result(0 for _ in range(10))

    # For modified files, set the st_mtime to 1 day in the future. It doesn't
    # matter that the modification time is in the future - just so long as it
    # is greater than the current time. The reloader only checks st_mtime,
    # which is the value at the 8th index passed to os.stat_result.
    one_day_hence = time.time() + 24 * 60 * 60
    modified_stat_result = os.stat_result(0 if i != 8 else one_day_hence
                                          for i in range(10))

    @contextmanager
    def mock_open(file, mode="r", **kwargs):
        yield io.StringIO("")

    def mock_stat(filepath):
        if filepath in mock_stat.modified_files:
            return modified_stat_result
        else:
            return unmodified_stat_result

    mock_stat.modified_files = set()

    with mock.patch("io.open", new=mock_open), mock.patch(
            "os.stat", new=mock_stat), mock.patch(
                "sys.modules", new=MOCK_SYSMODULES), mock.patch(
                    "modulefinder.ModuleFinder",
                    new=MockModuleFinder), mock.patch(
                        "importlib.reload",
                        side_effect=lambda module: module) as mock_reload:

        # Despite mocking modulefinder.ModuleFinder above, we also need to
        # swap out the real ModuleFinder with our mock one in the list of
        # bases for the GraphModuleFinder.
        old_bases = reloader._GraphModuleFinder.__bases__
        try:
            reloader._GraphModuleFinder.__bases__ = (
                MockModuleFinder,
                logger.Loggable,
            )
            reload_obj = reloader.ModuleReloader()

            yield reload_obj, mock_reload, mock_stat
        finally:
            reloader._GraphModuleFinder.__bases__ = old_bases
Beispiel #2
0
    def __init__(self, target, startup_timeout=10, http_port=0):
        super(TestRunnerIHandler,
              self).__init__(target=target,
                             startup_timeout=startup_timeout,
                             http_port=http_port)
        self._cfg.parent = self.target.cfg

        self.report = self._initial_report()
        self.report_mutex = threading.Lock()
        self._pool = None
        self._http_handler = None

        self._created_environments = {}
        self._reloader = reloader.ModuleReloader(
            extra_deps=self.cfg.extra_deps)
        self._resource_loader = resource_loader.ResourceLoader()
Beispiel #3
0
def mock_reload_env():
    """Set up the mock environment for unit-testing the reloader module."""
    # For unmodified files, just return a fake stat result with everything set
    # to 0.
    unmodified_stat_result = os.stat_result(0 for _ in range(10))

    # For modified files, set the st_mtime to 1 day in the future. It doesn't
    # matter that the modification time is in the future - just so long as it
    # is greater than the current time. The reloader only checks st_mtime,
    # which is the value at the 8th index passed to os.stat_result.
    one_day_hence = time.time() + 24 * 60 * 60
    modified_stat_result = os.stat_result(0 if i != 8 else one_day_hence
                                          for i in range(10))

    def mock_stat(filepath):
        if filepath in mock_stat.modified_files:
            return modified_stat_result
        else:
            return unmodified_stat_result

    mock_stat.modified_files = set()

    reloader_patch = "testplan.runnable.interactive.reloader.reload_module"
    with mock.patch("modulefinder.ModuleFinder",
                    new=MockModuleFinder), (mock.patch(
                        reloader_patch,
                        side_effect=lambda module: module)) as mock_reload, (
                            mock.patch("os.stat", new=mock_stat)), (mock.patch(
                                "sys.modules", new=MOCK_SYSMODULES)):

        # Despite mocking modulefinder.ModuleFinder above, we also need to
        # swap out the real ModuleFinder with our mock one in the list of
        # bases for the GraphModuleFinder.
        reloader._GraphModuleFinder.__bases__ = (
            MockModuleFinder,
            logger.Loggable,
        )
        reload_obj = reloader.ModuleReloader()

        yield reload_obj, mock_reload, mock_stat
Beispiel #4
0
    def __init__(self, target, startup_timeout=10, http_port=0):
        super(TestRunnerIHandler,
              self).__init__(target=target,
                             startup_timeout=startup_timeout,
                             http_port=http_port)
        self.cfg.parent = self.target.cfg
        self.parent = self.target
        self.report = self._initial_report()
        self.report_mutex = threading.Lock()
        self._pool = None
        self._http_handler = None
        self._created_environments = {}

        try:
            self._reloader = reloader.ModuleReloader(
                extra_deps=getattr(self.cfg, "extra_deps", None),
                scheduled_modules=getattr(self.parent, "scheduled_modules",
                                          None),
            )
        except RuntimeError:
            self._reloader = None
        self._resource_loader = resource_loader.ResourceLoader()