Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 1 addition & 28 deletions hydra/test_utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def experiment(cfg):
f"Unexpected number of output lines from {task_file}, output lines:\n\n{file_str}"
)
for idx in range(len(output)):
assert_regex_match(expected_outputs[idx], output[idx])
assert_multiline_regex_search(expected_outputs[idx], output[idx])
# some tests are parsing the file output for more specialized testing.
return file_str
finally:
Expand Down Expand Up @@ -429,33 +429,6 @@ def assert_text_same(
assert False, "Mismatch between expected and actual text"


def assert_regex_match(
from_line: str, to_line: str, from_name: str = "Expected", to_name: str = "Actual"
) -> None:
"""Check that the lines of `from_line` (which can be a regex expression)
matches the corresponding lines of `to_line` string.

In case the regex match fails, we display the diff as if `from_line` was a regular string.
"""
normalized_from_line = [x for x in normalize_newlines(from_line).split("\n") if x]
normalized_to_line = [x for x in normalize_newlines(to_line).split("\n") if x]
if len(normalized_from_line) != len(normalized_to_line):
assert_text_same(
from_line=from_line,
to_line=to_line,
from_name=from_name,
to_name=to_name,
)
for line1, line2 in zip(normalized_from_line, normalized_to_line):
if line1 != line2 and re.match(line1, line2) is None:
assert_text_same(
from_line=from_line,
to_line=to_line,
from_name=from_name,
to_name=to_name,
)


def assert_multiline_regex_search(
pattern: str, string: str, from_name: str = "Expected", to_name: str = "Actual"
) -> None:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from hydra.core.utils import JobReturn, JobStatus
from hydra.test_utils.test_utils import (
assert_regex_match,
assert_multiline_regex_search,
chdir_hydra_root,
run_process,
run_python_script,
Expand Down Expand Up @@ -101,9 +101,9 @@
cmd.extend(args)
result, _err = run_python_script(cmd)

assert_regex_match(
assert_multiline_regex_search(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve literal matching for expected log output

When any of these callback cases produces the expected output, this assertion still fails because assert_multiline_regex_search always interprets the entire string as a regex. Literal prefixes such as [HYDRA] and [JOB] become character classes, whereas the removed helper first accepted lines that were exactly equal; for example, searching for [HYDRA] Init custom_callback does not match the identical literal text. The same unescaped prefixes occur in the changed sweep and Hydra-mode tests, so either retain the literal-equality behavior or escape those portions before switching helpers.

Useful? React with 👍 / 👎.

from_line=expected,

Check failure on line 105 in tests/test_callbacks.py

View workflow job for this annotation

GitHub Actions / lint

Pyrefly unexpected-keyword

Unexpected keyword argument `from_line` in function `hydra.test_utils.test_utils.assert_multiline_regex_search`
to_line=result,

Check failure on line 106 in tests/test_callbacks.py

View workflow job for this annotation

GitHub Actions / lint

Pyrefly unexpected-keyword

Unexpected keyword argument `to_line` in function `hydra.test_utils.test_utils.assert_multiline_regex_search`
from_name="Expected output",
to_name="Actual output",
)

Check failure on line 109 in tests/test_callbacks.py

View workflow job for this annotation

GitHub Actions / lint

Pyrefly missing-argument

Missing argument `string` in function `hydra.test_utils.test_utils.assert_multiline_regex_search`

Check failure on line 109 in tests/test_callbacks.py

View workflow job for this annotation

GitHub Actions / lint

Pyrefly missing-argument

Missing argument `pattern` in function `hydra.test_utils.test_utils.assert_multiline_regex_search`
Expand Down
4 changes: 2 additions & 2 deletions tests/test_examples/test_basic_sweep.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pytest import mark

from hydra.test_utils.test_utils import (
assert_regex_match,
assert_multiline_regex_search,
chdir_hydra_root,
run_python_script,
)
Expand Down Expand Up @@ -72,9 +72,9 @@
cmd.extend(args)
result, _err = run_python_script(cmd)

assert_regex_match(
assert_multiline_regex_search(
from_line=expected,

Check failure on line 76 in tests/test_examples/test_basic_sweep.py

View workflow job for this annotation

GitHub Actions / lint

Pyrefly unexpected-keyword

Unexpected keyword argument `from_line` in function `hydra.test_utils.test_utils.assert_multiline_regex_search`
to_line=result,

Check failure on line 77 in tests/test_examples/test_basic_sweep.py

View workflow job for this annotation

GitHub Actions / lint

Pyrefly unexpected-keyword

Unexpected keyword argument `to_line` in function `hydra.test_utils.test_utils.assert_multiline_regex_search`
from_name="Expected output",
to_name="Actual output",
)

Check failure on line 80 in tests/test_examples/test_basic_sweep.py

View workflow job for this annotation

GitHub Actions / lint

Pyrefly missing-argument

Missing argument `string` in function `hydra.test_utils.test_utils.assert_multiline_regex_search`

Check failure on line 80 in tests/test_examples/test_basic_sweep.py

View workflow job for this annotation

GitHub Actions / lint

Pyrefly missing-argument

Missing argument `pattern` in function `hydra.test_utils.test_utils.assert_multiline_regex_search`
15 changes: 7 additions & 8 deletions tests/test_hydra.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
TSweepRunner,
TTaskRunner,
assert_multiline_regex_search,
assert_regex_match,
assert_text_same,
chdir_hydra_root,
integration_test,
Expand Down Expand Up @@ -1601,12 +1600,12 @@
See https://hydra.cc/docs/1.2/upgrades/1.0_to_1.1/changes_to_hydra_main_config_path for more information.
@hydra.main()
""")
assert_regex_match(
assert_multiline_regex_search(
from_line=expected,
to_line=err,
from_name="Expected error",
to_name="Actual error",
)

Check failure on line 1608 in tests/test_hydra.py

View workflow job for this annotation

GitHub Actions / lint

Pyrefly missing-argument

Missing argument `string` in function `hydra.test_utils.test_utils.assert_multiline_regex_search`

Check failure on line 1608 in tests/test_hydra.py

View workflow job for this annotation

GitHub Actions / lint

Pyrefly missing-argument

Missing argument `pattern` in function `hydra.test_utils.test_utils.assert_multiline_regex_search`


def test_job_chdir_not_specified(tmpdir: Path) -> None:
Expand All @@ -1621,7 +1620,7 @@
See https://hydra.cc/docs/1.2/upgrades/1.1_to_1.2/changes_to_job_working_dir/ for more information..*
.*
""")
assert_regex_match(
assert_multiline_regex_search(
from_line=expected,
to_line=err,
from_name="Expected error",
Expand Down Expand Up @@ -1929,30 +1928,30 @@
if error:
expected = normalize_newlines(expected_output)
ret = run_with_error(cmd)
assert_regex_match(
assert_multiline_regex_search(
from_line=expected,
to_line=ret,
from_name="Expected output",
to_name="Actual output",
)
elif warning:
out, err = run_python_script(cmd, allow_warnings=True)
assert_regex_match(
assert_multiline_regex_search(
from_line=expected_output,
to_line=out,
from_name="Expected output",
to_name="Actual output",
)
assert warning_msg is not None
assert_regex_match(
assert_multiline_regex_search(
from_line=warning_msg,
to_line=err,
from_name="Expected error",
to_name="Actual error",
)
else:
out, _ = run_python_script(cmd)
assert_regex_match(
assert_multiline_regex_search(
from_line=expected_output,
to_line=out,
from_name="Expected output",
Expand All @@ -1978,7 +1977,7 @@
nesterov""")

out, _ = run_python_script(cmd)
assert_regex_match(
assert_multiline_regex_search(
from_line=expected_output,
to_line=out,
from_name="Expected output",
Expand Down
5 changes: 2 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from hydra.errors import HydraDeprecationError
from hydra.test_utils.test_utils import (
assert_multiline_regex_search,
assert_regex_match,
)


Expand Down Expand Up @@ -345,7 +344,7 @@ def test_simplified_traceback_with_no_module(self) -> None:
run_and_report(demo_func)
mock_stderr.seek(0)
stderr_output = mock_stderr.read()
assert_regex_match(expected_traceback_regex, stderr_output)
assert_multiline_regex_search(expected_traceback_regex, stderr_output)

def test_simplified_traceback_failure(self) -> None:
"""
Expand Down Expand Up @@ -373,4 +372,4 @@ def throws(*args: Any, **kwargs: Any) -> NoReturn:
run_and_report(demo_func)
mock_stderr.seek(0)
stderr_output = mock_stderr.read()
assert_regex_match(expected_traceback_regex, stderr_output)
assert_multiline_regex_search(expected_traceback_regex, stderr_output)
Loading