Skip to content

Commit b31a5ef

Browse files
authored
fix(caching): Fix CLI always overriding cache=true/false in the configuration file (#1608)
1 parent f88a815 commit b31a5ef

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

src/robocop/config.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,11 +633,11 @@ def _sorted_tuple(items: list[str] | None) -> tuple[str, ...]:
633633
return int.from_bytes(hash_bytes[:8], byteorder="big", signed=True)
634634

635635

636-
@dataclass(frozen=True)
636+
@dataclass
637637
class CacheConfig:
638638
"""Configuration for file-level caching."""
639639

640-
enabled: bool = True
640+
enabled: bool | None = True
641641
cache_dir: Path | None = None
642642

643643
@classmethod
@@ -657,6 +657,14 @@ def from_toml(cls, config: dict, config_parent: Path) -> CacheConfig:
657657
cache_dir = config_parent / cache_dir
658658
return cls(enabled=enabled, cache_dir=cache_dir)
659659

660+
def overwrite_from_config(self, overwrite_config: Self) -> None:
661+
"""Overwrite configuration with optional values from CLI."""
662+
if overwrite_config.cache_dir is not None:
663+
self.cache_dir = overwrite_config.cache_dir
664+
self.enabled = True
665+
elif overwrite_config.enabled is not None:
666+
self.enabled = overwrite_config.enabled
667+
660668

661669
@dataclass
662670
class FileFiltersOptions(ConfigContainer):
@@ -828,7 +836,7 @@ def overwrite_from_config(self, overwrite_config: Config | None) -> None:
828836
setattr(self, config_field.name, value)
829837
# Handle cache config - CLI cache settings override file config
830838
if overwrite_config.cache is not None:
831-
self.cache = overwrite_config.cache
839+
self.cache.overwrite_from_config(overwrite_config.cache)
832840
if overwrite_config.linter:
833841
for config_field in fields(overwrite_config.linter):
834842
if config_field.name == "config_source":

src/robocop/run.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ def list_commands(self, ctx: typer.Context) -> list[str]: # noqa: ARG002
144144
ignore_file_config_option = Annotated[
145145
bool, typer.Option(rich_help_panel="Configuration", help="Do not load configuration files.")
146146
]
147-
no_cache_option = Annotated[
147+
cache_option = Annotated[
148148
bool,
149149
typer.Option(
150-
"--no-cache",
150+
"--cache",
151151
help="Disable file caching. All files will be processed regardless of modifications.",
152152
rich_help_panel="Caching",
153153
),
@@ -316,7 +316,7 @@ def check_files(
316316
root: project_root_option = None,
317317
verbose: verbose_option = None,
318318
silent: silent_option = None,
319-
no_cache: no_cache_option = False,
319+
cache: cache_option = None,
320320
clear_cache: clear_cache_option = False,
321321
cache_dir: cache_dir_option = None,
322322
) -> list[Diagnostic]:
@@ -342,7 +342,7 @@ def check_files(
342342
file_filters = config.FileFiltersOptions(
343343
include=include, default_include=default_include, exclude=exclude, default_exclude=default_exclude
344344
)
345-
cache_config = config.CacheConfig(enabled=not no_cache, cache_dir=cache_dir)
345+
cache_config = config.CacheConfig(enabled=cache, cache_dir=cache_dir)
346346
overwrite_config = config.Config(
347347
linter=linter_config,
348348
formatter=None,
@@ -639,7 +639,7 @@ def format_files(
639639
root: project_root_option = None,
640640
verbose: verbose_option = None,
641641
silent: silent_option = None,
642-
no_cache: no_cache_option = False,
642+
cache: cache_option = None,
643643
clear_cache: clear_cache_option = False,
644644
cache_dir: cache_dir_option = None,
645645
return_result: Annotated[
@@ -685,7 +685,7 @@ def format_files(
685685
file_filters = config.FileFiltersOptions(
686686
include=include, default_include=default_include, exclude=exclude, default_exclude=default_exclude
687687
)
688-
cache_config = config.CacheConfig(enabled=not no_cache, cache_dir=cache_dir)
688+
cache_config = config.CacheConfig(enabled=cache, cache_dir=cache_dir)
689689
overwrite_config = config.Config(
690690
formatter=formatter_config,
691691
linter=None,

tests/cache/test_cache_integration.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def test_cache_disabled_with_no_cache_flag(self, tmp_path, capsys):
144144
_out1, _ = capsys.readouterr()
145145

146146
# Act - run with --no-cache flag
147-
second_result = check_files(no_cache=True, return_result=True, verbose=True)
147+
second_result = check_files(cache=False, return_result=True, verbose=True)
148148
out2, _ = capsys.readouterr()
149149

150150
# Assert - should rescan even though file unchanged
@@ -351,8 +351,29 @@ def test_empty_file_integration(self, tmp_path, capsys):
351351
# Assert - should rescan (cache invalidated due to size change)
352352
assert "Scanning file:" in out2
353353

354+
def test_default_cli_does_not_override_config_file_disabled(self, tmp_path, capsys):
355+
"""Test CLI without any option set does not override the config file with cache=false."""
356+
# Arrange - create config file with cache disabled
357+
config_file = tmp_path / "pyproject.toml"
358+
config_file.write_text("[tool.robocop]\ncache = false\n", encoding="utf-8")
359+
test_file = tmp_path / "test.robot"
360+
test_file.write_text("*** Test Cases ***\nTest\n Log Hello\n", encoding="utf-8")
361+
362+
with working_directory(tmp_path):
363+
# Act - run with default CLI (cache disabled from config)
364+
check_files(verbose=True, return_result=True)
365+
out, _ = capsys.readouterr()
366+
367+
# Assert - should NOT use cache (rescans file)
368+
assert "Scanning file:" in out
369+
assert "Used cached results" not in out
370+
371+
# Assert - cache file should not be created
372+
cache_dir = tmp_path / ".robocop_cache"
373+
assert not cache_dir.exists()
374+
354375
def test_cli_cache_dir_overrides_config_file_disabled(self, tmp_path):
355-
"""Test CLI --cache-dir enables cache and overrides config file with cache=false."""
376+
"""Test CLI --cache-dir enables cache and overrides the config file with cache=false."""
356377
# Arrange - create config file with cache disabled
357378
config_file = tmp_path / "pyproject.toml"
358379
config_file.write_text("[tool.robocop]\ncache = false\n", encoding="utf-8")
@@ -396,9 +417,8 @@ def test_cli_no_cache_flag_overrides_config_file_enabled(self, tmp_path, capsys)
396417
# Verify cache was created
397418
assert is_file_in_cache(tmp_path, test_file)
398419

399-
# Act - run with no_cache=True via CLI (simulating --no-cache flag)
400-
# This overrides the config file's cache=true
401-
check_files(no_cache=True, return_result=True, verbose=True)
420+
# Act - Override config file's cache=true with --no-cache flag
421+
check_files(cache=False, return_result=True, verbose=True)
402422
out2, _ = capsys.readouterr()
403423

404424
# Assert - should NOT use cache (rescans file)

0 commit comments

Comments
 (0)