Beispiel #1
0
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",
    )
Beispiel #2
0
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",
    )
Beispiel #3
0
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_regex_match(
        from_line=expected,
        to_line=ret,
        from_name="Expected output",
        to_name="Actual output",
    )
Beispiel #4
0
def test_partial_failure(
    tmpdir: Any,
) -> None:
    cmd = [
        sys.executable,
        "-Werror",
        "tests/test_apps/app_can_fail/my_app.py",
        "--multirun",
        "+divisor=1,0",
        "hydra.run.dir=" + str(tmpdir),
        "hydra.hydra_logging.formatters.simple.format='[HYDRA] %(message)s'",
    ]
    out, err = run_process(cmd=cmd, print_error=False, raise_exception=False)

    expected_out = dedent(
        """\
        [HYDRA] Launching 2 jobs locally
        [HYDRA] \t#0 : +divisor=1
        val=1.0
        [HYDRA] \t#1 : +divisor=0"""
    )

    assert_regex_match(
        from_line=expected_out,
        to_line=out,
        from_name="Expected output",
        to_name="Actual output",
    )

    expected_err = dedent(
        """
        Error executing job with overrides: ['+divisor=0']
        Traceback (most recent call last):
          File ".*my_app.py", line 9, in my_app
            val = 1 / cfg.divisor
        ZeroDivisionError: division by zero

        Set the environment variable HYDRA_FULL_ERROR=1 for a complete stack trace.
        """
    )

    assert_regex_match(
        from_line=expected_err,
        to_line=err,
        from_name="Expected error",
        to_name="Actual error",
    )
Beispiel #5
0
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
Beispiel #6
0
def test_hydra_main_without_config_path(tmpdir: Path) -> None:
    cmd = [
        "tests/test_apps/hydra_main_without_config_path/my_app.py",
        f"hydra.run.dir={tmpdir}",
    ]
    _, err = run_python_script(cmd, allow_warnings=True)

    expected = dedent("""
        .*my_app.py:7: UserWarning:
        config_path is not specified in @hydra.main().
        See https://hydra.cc/docs/next/upgrades/1.0_to_1.1/changes_to_hydra_main_config_path for more information.
          @hydra.main()
        """)
    assert_regex_match(
        from_line=expected,
        to_line=err,
        from_name="Expected error",
        to_name="Actual error",
    )
Beispiel #7
0
def test_job_chdir_not_specified(tmpdir: Path) -> None:
    cmd = [
        "tests/test_apps/app_with_no_chdir_override/my_app.py",
        f"hydra.run.dir={tmpdir}",
    ]
    _, err = run_python_script(cmd, allow_warnings=True)

    expected = dedent(
        """
        .*UserWarning: Hydra 1.3 will no longer change working directory at job runtime by default.
        See https://hydra.cc/docs/upgrades/1.1_to_1.2/changes_to_job_working_dir for more information..*
        .*
        """
    )
    assert_regex_match(
        from_line=expected,
        to_line=err,
        from_name="Expected error",
        to_name="Actual error",
    )
Beispiel #8
0
def test_app_with_callbacks(
    tmpdir: Path,
    app_path: str,
    args: List[str],
    expected: str,
) -> None:

    cmd = [
        app_path,
        "hydra.run.dir=" + str(tmpdir),
        "hydra.hydra_logging.formatters.simple.format='[HYDRA] %(message)s'",
        "hydra.job_logging.formatters.simple.format='[JOB] %(message)s'",
    ]
    cmd.extend(args)
    result, _err = run_python_script(cmd)

    assert_regex_match(
        from_line=expected,
        to_line=result,
        from_name="Expected output",
        to_name="Actual output",
    )
Beispiel #9
0
def test_basic_sweep_example(
    tmpdir: Path,
    args: List[str],
    expected: str,
) -> None:
    app_path = "examples/tutorials/basic/running_your_hydra_app/5_basic_sweep/my_app.py"

    cmd = [
        app_path,
        "--multirun",
        "hydra.run.dir=" + str(tmpdir),
        "hydra.job.chdir=True",
        "hydra.hydra_logging.formatters.simple.format='[HYDRA] %(message)s'",
        "hydra.job_logging.formatters.simple.format='[JOB] %(message)s'",
    ]
    cmd.extend(args)
    result, _err = run_python_script(cmd)

    assert_regex_match(
        from_line=expected,
        to_line=result,
        from_name="Expected output",
        to_name="Actual output",
    )