Skip to content

Commit

Permalink
improved test data; refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cduriau committed Feb 4, 2024
1 parent 018540a commit 2c03ba7
Showing 1 changed file with 36 additions and 65 deletions.
101 changes: 36 additions & 65 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,50 +40,41 @@ def mock_os_makedirs(p: str):
assert exc.value.code == 0


def test_list_available_software(capsys, monkeypatch: pytest.MonkeyPatch):
def mock_configutils_load() -> dict:
return {"a": "foo", "b": "bar"}

monkeypatch.setattr(configutils, "load", mock_configutils_load)

def test_list_available_software(
mock_settings_file_config: None,
capsys,
monkeypatch: pytest.MonkeyPatch,
):
with pytest.raises(SystemExit) as exc:
cli.run(["avail"])

assert exc.value.code == 0
out, _err = capsys.readouterr()
assert out == "\n".join(["a", "b"]) + "\n"
assert out == "foo" + "\n"


@pytest.mark.parametrize(
["software", "url", "configured", "exit_code", "error"],
["software", "url", "exit_code", "error"],
[
(" ", "", False, 1, "no software provided"),
("foo", " ", False, 1, "no url provided"),
("foo", "bar", True, 1, "software already configured"),
("foo", "bar", False, 1, r"no {version} format string found in url"),
("foo", r"https://example.com/{version}/foo.zip", False, 0, ""),
(" ", "", 1, "no software provided"),
("foo", " ", 1, "no url provided"),
("foo", "bar", 1, "software already configured"),
("bar", "bar", 1, r"no {version} format string found in url"),
("bar", r"https://example.com/{version}/bar.zip", 0, ""),
],
)
def test_add_software(
software: str,
url: str,
exit_code: int,
configured: bool,
error: str,
capsys,
mock_settings_file_config: None,
monkeypatch: pytest.MonkeyPatch,
):
mock_config: dict = {}
if configured:
mock_config[software] = url

def mock_configutils_load() -> dict:
return mock_config

def mock_configutils_dump(config: dict):
return

monkeypatch.setattr(configutils, "load", mock_configutils_load)
monkeypatch.setattr(configutils, "dump", mock_configutils_dump)

with pytest.raises(SystemExit) as exc:
Expand All @@ -92,85 +83,65 @@ def mock_configutils_dump(config: dict):
assert exc.value.code == exit_code
out, _err = capsys.readouterr()

if exit_code == 0:
assert mock_config[software] == url
else:
if exit_code != 0:
assert out == error + "\n"


@pytest.mark.parametrize(
["software", "configured", "exit_code", "error"],
["software", "exit_code", "error"],
[
(" ", False, 1, "no software provided"),
("foo", False, 1, "software not found"),
("foo", True, 0, ""),
(" ", 1, "no software provided"),
("bar", 1, "software not found"),
("foo", 0, ""),
],
)
def test_remove_software(
software: str,
configured: bool,
exit_code: bool,
error: str,
capsys,
mock_settings_file_config: None,
monkeypatch: pytest.MonkeyPatch,
):
mock_config: dict = {}
if configured:
mock_config[software] = "bar"

def mock_configutils_load() -> dict:
return mock_config
def mock_dump(config: dict) -> None:
return

monkeypatch.setattr(configutils, "load", mock_configutils_load)
monkeypatch.setattr(configutils, "dump", mock_dump)

with pytest.raises(SystemExit) as exc:
cli.run(["remove", "-s", software])

assert exc.value.code == exit_code
out, _err = capsys.readouterr()

if exit_code == 0:
assert software not in mock_config
else:
if exit_code != 0:
assert out == error + "\n"


@pytest.mark.parametrize(
["software", "version", "configured", "found", "exit_code", "error"],
["software", "version", "exit_code", "error"],
[
(" ", "0.1.0", False, False, 1, "no software provided"),
("foo", "0.1.0", False, False, 1, "software not found"),
("foo", "0.1.0", True, True, 0, ""),
("foo", "0.1.0", True, False, 0, ""),
(" ", "0.1.0", 1, "no software provided"),
("bar", "0.1.0", 1, "software not found"),
("foo", "0.1.0", 0, ""),
("foo", "0.3.0", 0, ""),
],
)
def test_download_software(
software: str,
version: str,
configured: bool,
found: bool,
exit_code: bool,
exit_code: int,
error: str,
capsys,
fix_dir_downloaded: str,
mock_settings_file_config: None,
mock_settings_dir_download: None,
monkeypatch: pytest.MonkeyPatch,
):
mock_config: dict = {}
if configured:
mock_config[software] = "/tmp/foo.zip"

def mock_configutils_load() -> dict:
return mock_config

def mock_downloadutils_find_archive(software: str, version: str) -> str | None:
if found:
return "/tmp/foo.zip"
return None

def mock_downloadutils_download(software: str, version: str, config: dict) -> str:
return "/tmp/foo.zip"
def mock_downloadutils_download(software: str, version: str, config: dict | None = None) -> str:
path = os.path.join(fix_dir_downloaded, f"{software}-{version}.zip")
return path

monkeypatch.setattr(configutils, "load", mock_configutils_load)
monkeypatch.setattr(downloadutils, "find_archive", mock_downloadutils_find_archive)
monkeypatch.setattr(downloadutils, "download", mock_downloadutils_download)

with pytest.raises(SystemExit) as exc:
Expand All @@ -180,6 +151,6 @@ def mock_downloadutils_download(software: str, version: str, config: dict) -> st
out, _err = capsys.readouterr()

if exit_code == 0:
assert out == "/tmp/foo.zip" + "\n"
assert out == mock_downloadutils_download(software, version) + "\n"
else:
assert out == error + "\n"

0 comments on commit 2c03ba7

Please sign in to comment.