Skip to content

Commit

Permalink
Cache only these results that have no warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
BlankSpruce committed Aug 2, 2024
1 parent ec30f7a commit 1225dc6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
14 changes: 9 additions & 5 deletions gersemi/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def run_task(
return task(formatted_file)


def consume_task_result(task_result: TaskResult, quiet: bool) -> Tuple[Path, int]:
def consume_task_result(task_result: TaskResult, quiet: bool) -> Tuple[Path, int, bool]:
if task_result.to_stdout != "":
print_to_stdout(task_result.to_stdout)

Expand All @@ -142,7 +142,7 @@ def consume_task_result(task_result: TaskResult, quiet: bool) -> Tuple[Path, int
if task_result.to_stderr != "":
print_to_stderr(task_result.to_stderr)

return task_result.path, task_result.return_code
return task_result.path, task_result.return_code, (len(task_result.warnings) > 0)


def create_pool(is_stdin_in_sources, num_workers):
Expand Down Expand Up @@ -195,7 +195,7 @@ def handle_already_formatted_files(
consume_task_result(result, quiet)
for result in map(execute, already_formatted_files)
]
return compute_error_code(code for _, code in results)
return compute_error_code(code for _, code, _ in results)


def handle_files_to_format(
Expand Down Expand Up @@ -227,9 +227,13 @@ def handle_files_to_format(
mode,
cache,
configuration_summary,
(path for path, code in results if code == SUCCESS and path != Path("-")),
(
path
for path, code, has_warnings in results
if code == SUCCESS and path != Path("-") and (not has_warnings)
),
)
return compute_error_code(code for _, code in results)
return compute_error_code(code for _, code, _ in results)


def run(mode: Mode, configuration: Configuration, sources: Iterable[Path]):
Expand Down
6 changes: 3 additions & 3 deletions gersemi/warnings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Iterable, Tuple, Union
from typing import Sequence, Tuple, Union


Position = Tuple[int, int]
Expand All @@ -8,7 +8,7 @@
@dataclass
class UnknownCommandWarning:
command_name: str
positions: Iterable[Position]
positions: Sequence[Position]

def get_message(self, filepath: str) -> str:
return "\n".join(
Expand All @@ -26,4 +26,4 @@ def get_message(self, filepath: str) -> str:


FormatterWarning = Union[UnknownCommandWarning, WrongFormattingWarning]
FormatterWarnings = Iterable[FormatterWarning]
FormatterWarnings = Sequence[FormatterWarning]
42 changes: 31 additions & 11 deletions tests/test_executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,20 +901,25 @@ def check_warnings(result, stderr):


@pytest.mark.parametrize(
["args"],
["args", "check_cache"],
[
((),),
(("--check",),),
(("--in-place",),),
(("--diff",),),
((), False),
(("--check",), True),
(("--in-place",), True),
(("--diff",), False),
],
)
def test_warn_about_unknown_commands(tmpdir, args):
with temporary_dir_copy(TESTS / "warn_about_unknown_commands") as copy:
cmakelists = Path(copy) / "CMakeLists.txt"
custom_command_definition = Path(copy) / "watch_movies.cmake"
def test_warn_about_unknown_commands(tmpdir, args, check_cache):
original = TESTS / "warn_about_unknown_commands"

with_definition = gersemi(
with cache_tests(original) as (target, gersemi_, inspector):
if check_cache:
inspector.assert_that_has_no_tables()

cmakelists = Path(target) / "CMakeLists.txt"
custom_command_definition = Path(target) / "watch_movies.cmake"

with_definition = gersemi_(
*args,
cmakelists,
"--definitions",
Expand All @@ -927,7 +932,17 @@ def test_warn_about_unknown_commands(tmpdir, args):
)
assert with_definition.stderr == "", with_definition.stderr

without_definition = gersemi(*args, cmakelists, cwd=tmpdir)
if check_cache:
inspector.assert_that_has_initialized_tables()
assert len(inspector.get_files()) > 0
assert len(inspector.get_formatted()) > 0

with cache_tests(original) as (target, gersemi_, inspector):
if check_cache:
inspector.assert_that_has_no_tables()

cmakelists = Path(target) / "CMakeLists.txt"
without_definition = gersemi_(*args, cmakelists, cwd=tmpdir)
assert without_definition.returncode == 0, (
without_definition.returncode,
without_definition.stderr,
Expand All @@ -945,6 +960,11 @@ def test_warn_about_unknown_commands(tmpdir, args):
"""
), without_definition.stderr

if check_cache:
inspector.assert_that_has_initialized_tables()
assert len(inspector.get_files()) == 0
assert len(inspector.get_formatted()) == 0


@pytest.mark.parametrize(
["args"],
Expand Down

0 comments on commit 1225dc6

Please sign in to comment.