# Copyright 2022-2026 The Ramble Authors
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.
import sys
import pytest
import ramble.caches
import ramble.main
clean = ramble.main.RambleCommand("clean")
pytestmark = pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
[docs]
@pytest.fixture()
def mock_calls_for_clean(monkeypatch):
counts = {}
class Counter:
def __init__(self, name):
self.name = name
counts[name] = 0
def __call__(self, *args, **kwargs):
counts[self.name] += 1
monkeypatch.setattr(ramble.caches.fetch_cache, "destroy", Counter("downloads"), raising=False)
monkeypatch.setattr(ramble.caches.misc_cache, "destroy", Counter("caches"))
monkeypatch.setattr(ramble.cmd.clean, "remove_python_caches", Counter("python_caches"))
monkeypatch.setattr(ramble.cmd.clean, "remove_reports_files", Counter("reports"))
yield counts
# All possible effect categories monitored by the test harness
all_monitored_effects = ["downloads", "caches", "python_caches", "reports"]
# '-a' / '--all' is defined as AllClean (equivalent to -dmp) and excludes 'reports'
all_flag_effects = ["downloads", "caches", "python_caches"]
[docs]
@pytest.mark.usefixtures("config")
@pytest.mark.parametrize(
"args,effects",
[
([], ["downloads"]),
(["-d"], ["downloads"]),
(["--downloads"], ["downloads"]),
(["-m"], ["caches"]),
(["--misc-cache"], ["caches"]),
(["-p"], ["python_caches"]),
(["--python-cache"], ["python_caches"]),
(["-r"], ["reports"]),
(["--reports"], ["reports"]),
(["-a"], all_flag_effects),
(["--all"], all_flag_effects),
],
)
def test_function_calls(args, effects, mock_calls_for_clean):
# Call the command with the supplied command line
clean(*args)
# Assert that we called the expected functions the correct
# number of times
for name in all_monitored_effects:
assert mock_calls_for_clean[name] == (1 if name in effects else 0)