Пример #1
0
 def test_import_prepend_append(
     self, pytester: Pytester, monkeypatch: MonkeyPatch
 ) -> None:
     root1 = pytester.mkdir("root1")
     root2 = pytester.mkdir("root2")
     root1.joinpath("x456.py").touch()
     root2.joinpath("x456.py").touch()
     p = root2.joinpath("test_x456.py")
     monkeypatch.syspath_prepend(str(root1))
     p.write_text(
         textwrap.dedent(
             """\
             import x456
             def test():
                 assert x456.__file__.startswith({!r})
             """.format(
                 str(root2)
             )
         )
     )
     with monkeypatch.context() as mp:
         mp.chdir(root2)
         reprec = pytester.inline_run("--import-mode=append")
         reprec.assertoutcome(passed=0, failed=1)
         reprec = pytester.inline_run()
         reprec.assertoutcome(passed=1)
Пример #2
0
def test_syspath_prepend_double_undo(mp: MonkeyPatch) -> None:
    old_syspath = sys.path[:]
    try:
        mp.syspath_prepend("hello world")
        mp.undo()
        sys.path.append("more hello world")
        mp.undo()
        assert sys.path[-1] == "more hello world"
    finally:
        sys.path[:] = old_syspath
Пример #3
0
def test_syspath_prepend(mp: MonkeyPatch) -> None:
    old = list(sys.path)
    mp.syspath_prepend("world")
    mp.syspath_prepend("hello")
    assert sys.path[0] == "hello"
    assert sys.path[1] == "world"
    mp.undo()
    assert sys.path == old
    mp.undo()
    assert sys.path == old
Пример #4
0
 def test_set_app_module_custom_starlette(
     self,
     app_module_tmp_path: Path,
     mock_logger: logging.Logger,
     monkeypatch: MonkeyPatch,
 ) -> None:
     """Test `start.set_app_module` with custom module path to Starlette app."""
     monkeypatch.syspath_prepend(app_module_tmp_path)
     monkeypatch.setenv("APP_MODULE", "tmp_app.starlettebase.main:app")
     start.set_app_module(logger=mock_logger)
     mock_logger.debug.assert_called_once_with(  # type: ignore[attr-defined]
         "App module set to tmp_app.starlettebase.main:app.")
Пример #5
0
 def test_configure_logging_tmp_module(
     self,
     logging_conf_tmp_file_path: Path,
     mock_logger: logging.Logger,
     monkeypatch: MonkeyPatch,
 ) -> None:
     """Test `start.configure_logging` with temporary logging config path."""
     monkeypatch.syspath_prepend(logging_conf_tmp_file_path)
     monkeypatch.setenv("LOGGING_CONF", "tmp_log")
     assert os.getenv("LOGGING_CONF") == "tmp_log"
     start.configure_logging(logger=mock_logger, logging_conf="tmp_log")
     mock_logger.debug.assert_called_once_with(  # type: ignore[attr-defined]
         "Logging dict config loaded from tmp_log.")
Пример #6
0
def test_samefile_false_negatives(tmp_path: Path,
                                  monkeypatch: MonkeyPatch) -> None:
    """
    import_file() should not raise ImportPathMismatchError if the paths are exactly
    equal on Windows. It seems directories mounted as UNC paths make os.path.samefile
    return False, even when they are clearly equal.
    """
    module_path = tmp_path.joinpath("my_module.py")
    module_path.write_text("def foo(): return 42")
    monkeypatch.syspath_prepend(tmp_path)

    with monkeypatch.context() as mp:
        # Forcibly make os.path.samefile() return False here to ensure we are comparing
        # the paths too. Using a context to narrow the patch as much as possible given
        # this is an important system function.
        mp.setattr(os.path, "samefile", lambda x, y: False)
        module = import_path(module_path)
    assert getattr(module, "foo")() == 42
Пример #7
0
 def test_configure_logging_tmp_module_no_dict(
     self,
     logging_conf_tmp_path_no_dict: Path,
     mock_logger: logging.Logger,
     monkeypatch: MonkeyPatch,
 ) -> None:
     """Test `start.configure_logging` with temporary logging config path.
     - Correct module name
     - No `LOGGING_CONFIG` object
     """
     monkeypatch.syspath_prepend(logging_conf_tmp_path_no_dict)
     monkeypatch.setenv("LOGGING_CONF", "no_dict")
     assert os.getenv("LOGGING_CONF") == "no_dict"
     with pytest.raises(AttributeError):
         start.configure_logging(logger=mock_logger, logging_conf="no_dict")
         logger_error_msg = "Error when configuring logging:"
         attribute_error_msg = "No LOGGING_CONFIG in no_dict."
         mock_logger.debug.assert_called_once_with(  # type: ignore[attr-defined]
             f"{logger_error_msg} {attribute_error_msg}.")
Пример #8
0
 def test_configure_logging_tmp_module_incorrect_type(
     self,
     logging_conf_tmp_path_incorrect_type: Path,
     mock_logger: logging.Logger,
     monkeypatch: MonkeyPatch,
 ) -> None:
     """Test `start.configure_logging` with temporary logging config path.
     - Correct module name
     - `LOGGING_CONFIG` object with incorrect type
     """
     monkeypatch.syspath_prepend(logging_conf_tmp_path_incorrect_type)
     monkeypatch.setenv("LOGGING_CONF", "incorrect_type")
     assert os.getenv("LOGGING_CONF") == "incorrect_type"
     with pytest.raises(TypeError):
         start.configure_logging(logger=mock_logger,
                                 logging_conf="incorrect_type")
         logger_error_msg = "Error when configuring logging:"
         type_error_msg = "LOGGING_CONFIG is not a dictionary instance."
         mock_logger.debug.assert_called_once_with(  # type: ignore[attr-defined]
             f"{logger_error_msg} {type_error_msg}.")
Пример #9
0
def test_syspath_prepend_with_namespace_packages(
        testdir: Testdir, monkeypatch: MonkeyPatch) -> None:
    for dirname in "hello", "world":
        d = testdir.mkdir(dirname)
        ns = d.mkdir("ns_pkg")
        ns.join("__init__.py").write(
            "__import__('pkg_resources').declare_namespace(__name__)")
        lib = ns.mkdir(dirname)
        lib.join("__init__.py").write("def check(): return %r" % dirname)

    monkeypatch.syspath_prepend("hello")
    import ns_pkg.hello

    assert ns_pkg.hello.check() == "hello"

    with pytest.raises(ImportError):
        import ns_pkg.world

    # Prepending should call fixup_namespace_packages.
    monkeypatch.syspath_prepend("world")
    import ns_pkg.world

    assert ns_pkg.world.check() == "world"

    # Should invalidate caches via importlib.invalidate_caches.
    tmpdir = testdir.tmpdir
    modules_tmpdir = tmpdir.mkdir("modules_tmpdir")
    monkeypatch.syspath_prepend(str(modules_tmpdir))
    modules_tmpdir.join("main_app.py").write("app = True")
    from main_app import app  # noqa: F401
Пример #10
0
def test_fingerprint_changes_if_module_changes(tmp_path: Path,
                                               domain_path: Text,
                                               stories_path: Text,
                                               monkeypatch: MonkeyPatch):
    rule_policy_path = inspect.getfile(RulePolicy)
    module_name = "custom_rule_policy"
    new_class_name = "CustomRulePolicy"

    custom_module_path = Path(tmp_path, f"{module_name}.py")
    shutil.copy2(rule_policy_path, custom_module_path)

    # Rename class as the class name has to be unique
    source_code = custom_module_path.read_text()
    source_code = source_code.replace("RulePolicy", new_class_name)
    custom_module_path.write_text(source_code)

    config = textwrap.dedent(f"""
    version: "3.0"
    recipe: "default.v1"

    policies:
    - name: RulePolicy
    - name: {module_name}.{new_class_name}
    """)
    monkeypatch.syspath_prepend(tmp_path)

    new_config_path = tmp_path / "config.yml"
    rasa.shared.utils.io.write_yaml(rasa.shared.utils.io.read_yaml(config),
                                    new_config_path)

    # Train to initialize cache
    rasa.train(domain_path,
               str(new_config_path), [stories_path],
               output=str(tmp_path))

    # Make sure that the caching works as expected the code didn't change
    result = rasa.train(
        domain_path,
        str(new_config_path),
        [stories_path],
        output=str(tmp_path),
        dry_run=True,
    )

    assert result.code == 0

    # Make a change to the code so a new training is necessary
    source_code = custom_module_path.read_text()
    source_code = source_code.replace("Dict[Text, Any]", "Dict")
    custom_module_path.write_text(source_code)

    result = rasa.train(
        domain_path,
        str(new_config_path),
        [stories_path],
        output=str(tmp_path),
        dry_run=True,
    )

    assert result.code == rasa.model_training.CODE_NEEDS_TO_BE_RETRAINED
    assert not result.dry_run_results[
        f"train_{module_name}.{new_class_name}1"].is_hit
Пример #11
0
 def module_dir(self, monkeypatch: MonkeyPatch, module_cache):
     with TemporaryDirectory() as tmpdir:
         monkeypatch.chdir(tmpdir)
         monkeypatch.syspath_prepend(tmpdir)
         monkeypatch.setattr("sys.modules", module_cache)
         yield tmpdir