def test_app_with_error_exception_sanitized(tmpdir: Any, monkeypatch: Any) -> None: monkeypatch.chdir("tests/test_apps/app_with_runtime_config_error") cmd = [ "my_app.py", "hydra.sweep.dir=" + str(tmpdir), ] expected = dedent("""\ Traceback (most recent call last): File ".*my_app.py", line 13, in my_app foo(cfg) File ".*my_app.py", line 8, in foo cfg.foo = "bar" # does not exist in the config omegaconf.errors.ConfigAttributeError: Key 'foo' is not in struct full_key: foo object_type=dict Set the environment variable HYDRA_FULL_ERROR=1 for a complete stack trace.""" ) ret = run_with_error(cmd) assert_regex_match( from_line=expected, to_line=ret, from_name="Expected output", to_name="Actual output", )
def test_hydra_exception(monkeypatch: Any, tmpdir: Any, expected: str) -> None: monkeypatch.chdir("tests/test_apps/app_exception") ret = run_with_error(["my_app.py", f"hydra.run.dir={tmpdir}"]) assert_text_same( from_line=expected, to_line=ret, from_name="Expected output", to_name="Actual output", )
def test_job_exception_full_error(tmpdir: Any) -> None: ret = run_with_error( ["tests/test_apps/app_exception/my_app.py", f"hydra.run.dir={tmpdir}"], env={ **os.environ, "HYDRA_FULL_ERROR": "1" }, ) assert "ZeroDivisionError: division by zero" in ret assert "Set the environment variable HYDRA_FULL_ERROR=1" not in ret
def test_write_protect_config_node(tmpdir: Any) -> None: cmd = [ "examples/patterns/write_protect_config_node/frozen.py", "hydra.run.dir=" + str(tmpdir), "data_bits=10", ] err = run_with_error(cmd) assert re.search(re.escape("Error merging override data_bits=10"), err) is not None
def test_1_basic_run_with_override_error(tmpdir: Path) -> None: expected = """Key 'pork' not in 'MySQLConfig' \tfull_key: pork \treference_type=Optional[MySQLConfig] \tobject_type=MySQLConfig""" err = run_with_error([ "examples/tutorials/structured_configs/1_minimal/my_app_type_error.py", "hydra.run.dir=" + str(tmpdir), ]) assert re.search(re.escape(expected), err) is not None
def test_resolve_flag_without_cfg_flag(tmpdir: Path) -> None: cmd = [ "examples/tutorials/basic/your_first_hydra_app/3_using_config/my_app.py", "hydra.run.dir=" + str(tmpdir), "--resolve", ] err = run_with_error(cmd) assert normalize_newlines(err).endswith( "ValueError: The --resolve flag can only be used in conjunction with --cfg" )
def test_1_basic_run_with_override_error(tmpdir: Path) -> None: expected = dedent("""\ Key 'pork' not in 'MySQLConfig' full_key: pork object_type=MySQLConfig""") err = run_with_error([ "examples/tutorials/structured_configs/1_minimal/my_app_type_error.py", "hydra.run.dir=" + str(tmpdir), "hydra.job.chdir=True", ]) assert re.search(re.escape(expected), err) is not None
def test_job_exception( tmpdir: Any, expected: str, ) -> None: ret = run_with_error( ["tests/test_apps/app_exception/my_app.py", f"hydra.run.dir={tmpdir}"]) assert_regex_match( from_line=expected, to_line=ret, from_name="Expected output", to_name="Actual output", )
def test_resolve_flag_errmsg(tmpdir: Path, other_flag: Optional[str]) -> None: cmd = [ "examples/tutorials/basic/your_first_hydra_app/3_using_config/my_app.py", "hydra.run.dir=" + str(tmpdir), "--resolve", ] if other_flag is not None: cmd.append(other_flag) err = run_with_error(cmd) assert normalize_newlines(err).endswith( "ValueError: The --resolve flag can only be used in conjunction with --cfg or --help" )
def test_config_search_path(args: List[str], expected: str, tmpdir: Path, error: Optional[str]) -> None: cmd = [ "examples/advanced/config_search_path/my_app.py", "hydra.run.dir=" + str(tmpdir), ] cmd.extend(args) if error is not None: ret = run_with_error(cmd) assert re.search(re.escape(error), ret) is not None else: result, _err = run_python_script(cmd) assert OmegaConf.create(result) == expected
def test_multirun_structured_conflict(tmpdir: Any, overrides: List[str], error: bool, expected: Any) -> None: cmd = [ "tests/test_apps/multirun_structured_conflict/my_app.py", "hydra.sweep.dir=" + str(tmpdir), ] cmd.extend(overrides) if error: expected = normalize_newlines(expected) ret = normalize_newlines(run_with_error(cmd)) assert re.search(re.escape(expected), ret) is not None else: ret, _err = get_run_output(cmd) assert ret == expected
def test_1_basic_override_type_error(tmpdir: Path) -> None: cmd = [ "examples/tutorials/structured_configs/1_minimal/my_app.py", "hydra.run.dir=" + str(tmpdir), "port=foo", ] expected = """Value 'foo' could not be converted to Integer \tfull_key: port \treference_type=Optional[MySQLConfig] \tobject_type=MySQLConfig""" err = run_with_error(cmd) assert re.search(re.escape(expected), err) is not None
def test_module_run(tmpdir: Any, directory: str, file: str, module: str, error: Optional[str]) -> None: cmd = [ directory + "/" + file, "hydra.run.dir=" + str(tmpdir), ] modified_env = os.environ.copy() modified_env["PYTHONPATH"] = directory modified_env["HYDRA_MAIN_MODULE"] = module if error is not None: ret = run_with_error(cmd, modified_env) assert re.search(re.escape(error), ret) is not None else: result, _err = run_python_script(cmd, env=modified_env) assert OmegaConf.create(result) == {"x": 10}
def test_1_basic_override_type_error(tmpdir: Path) -> None: cmd = [ "examples/tutorials/structured_configs/1_minimal/my_app.py", "hydra.run.dir=" + str(tmpdir), "hydra.job.chdir=True", "port=foo", ] expected = dedent("""\ Value 'foo'( of type 'str')? could not be converted to Integer full_key: port object_type=MySQLConfig""") err = run_with_error(cmd) assert re.search(expected, err) is not None
def test_cli_error(tmpdir: Any, monkeypatch: Any, override: Any, expected: str) -> None: monkeypatch.chdir("tests/test_apps/app_without_config/") if isinstance(override, str): override = [override] cmd = ["my_app.py", "hydra.sweep.dir=" + str(tmpdir)] + override ret = normalize_newlines(run_with_error(cmd)) assert (re.search("^" + re.escape(normalize_newlines(expected.strip())), ret) is not None), (f"Result:" f"\n---" f"\n{ret}" f"\n---" f"\nDid not match expected:" f"\n---" f"\n{expected}" f"\n---")
def test_run_with_missing_default(self, cmd_base: List[str], tmpdir: Any, sweep: bool) -> None: cmd = cmd_base + [ "hydra.sweep.dir=" + str(tmpdir), "--config-name=unspecified_mandatory_default", "--config-path=../../../hydra/test_utils/configs", ] if sweep: cmd.append("-m") expected = """You must specify 'group1', e.g, group1=<OPTION> Available options: \tabc.cde \tfile1 \tfile2""" ret = run_with_error(cmd) assert re.search(re.escape(expected), ret) is not None
def test_write_protect_config_node(tmpdir: Any) -> None: cmd = [ "examples/patterns/write_protect_config_node/frozen.py", "hydra.run.dir=" + str(tmpdir), "data_bits=10", ] expected = dedent("""\ Error merging override data_bits=10 Cannot change read-only config container full_key: data_bits object_type=SerialPort Set the environment variable HYDRA_FULL_ERROR=1 for a complete stack trace. """) err = run_with_error(cmd) assert_text_same(from_line=expected, to_line=err)
def test_multirun_structured_conflict(tmpdir: Any, overrides: List[str], error: bool, expected: Any) -> None: cmd = [ "tests/test_apps/multirun_structured_conflict/my_app.py", "hydra.sweep.dir=" + str(tmpdir), ] cmd.extend(overrides) if error: expected = normalize_newlines(expected) ret = run_with_error(cmd) assert_regex_match( from_line=expected, to_line=ret, from_name="Expected output", to_name="Actual output", ) else: ret, _err = run_python_script(cmd) assert ret == expected
def test_app_with_error_exception_sanitized(tmpdir: Any, monkeypatch: Any) -> None: monkeypatch.chdir("tests/test_apps/app_with_runtime_config_error") cmd = [ "my_app.py", "hydra.sweep.dir=" + str(tmpdir), ] expected = """Traceback (most recent call last): File "my_app.py", line 13, in my_app foo(cfg) File "my_app.py", line 8, in foo cfg.foo = "bar" # does not exist in the config omegaconf.errors.ConfigAttributeError: Key 'foo' is not in struct \tfull_key: foo \treference_type=Optional[Dict[Union[str, Enum], Any]] \tobject_type=dict Set the environment variable HYDRA_FULL_ERROR=1 for a complete stack trace.""" ret = run_with_error(cmd) assert normalize_newlines(expected) == normalize_newlines(ret)