From bb3a5f8f11d217709d5266e6d9417915f4932729 Mon Sep 17 00:00:00 2001 From: Christopher Barber Date: Sat, 4 May 2024 18:29:34 -0400 Subject: [PATCH] Test config settings options --- src/whl2conda/cli/config.py | 4 +- test/cli/test_config.py | 135 +++++++++++++++++++++++++++++++++++- test/conftest.py | 4 +- 3 files changed, 139 insertions(+), 4 deletions(-) diff --git a/src/whl2conda/cli/config.py b/src/whl2conda/cli/config.py index 25352c5..85094f1 100644 --- a/src/whl2conda/cli/config.py +++ b/src/whl2conda/cli/config.py @@ -30,7 +30,7 @@ from .common import add_markdown_help, dedent from ..impl.pyproject import add_pyproject_defaults from ..api.stdrename import user_stdrenames_path -from ..settings import settings +from ..settings import settings, _fromidentifier __all__ = ["config_main"] @@ -220,7 +220,7 @@ def show_user_settings( print(f"==> {settings.settings_file} <==") if keys: for key in keys: - print(f"{key}: {json.dumps(settings.get(key))}") + print(f"{_fromidentifier(key)}: {json.dumps(settings.get(key))}") else: print(json.dumps(settings.to_dict(), indent=2)) diff --git a/test/cli/test_config.py b/test/cli/test_config.py index 14b00ee..c676bb1 100644 --- a/test/cli/test_config.py +++ b/test/cli/test_config.py @@ -1,4 +1,4 @@ -# Copyright 2023 Christopher Barber +# Copyright 2023-2024 Christopher Barber # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -18,15 +18,22 @@ from __future__ import annotations +# standard +import json from pathlib import Path from typing import Optional from urllib.error import URLError +# third party +from platformdirs import user_config_path import pytest +# this project from whl2conda.cli import main from whl2conda.cli.config import update_std_renames from whl2conda.api.stdrename import user_stdrenames_path +from whl2conda.impl.pyproject import CondaPackageFormat +from whl2conda.settings import Whl2CondaSettings, settings # pylint: disable=too-many-statements @@ -182,3 +189,129 @@ def test_generate_pyproject( main(["config", "--generate-pyproject", "foo.txt"]) out, err = capsys.readouterr() assert "Cannot write to non .toml file" in err + + +@pytest.fixture +def tmp_settings_file( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, +) -> Path: + # override home directory + home = tmp_path / "home" + monkeypatch.setenv("HOME", str(home.resolve())) + monkeypatch.setenv("USERPROFILE", str(home.resolve())) + + # point settings at location in fake home dir + config_path = user_config_path("whl2conda") + assert config_path.is_relative_to(home) + settings_file = config_path / settings.SETTINGS_FILENAME + settings.load(settings_file) + assert settings.settings_file == settings_file + assert not settings_file.exists() + + return settings_file + + +def test_config_show( + tmp_settings_file: Path, + capsys: pytest.CaptureFixture, +) -> None: + """ + Test config --show and --show-sources + """ + + main(["config", "--show"]) + out, err = capsys.readouterr() + assert not err + assert json.loads(out) == settings.to_dict() + + main(["config", "--show-sources"]) + out, err = capsys.readouterr() + sourceheader, out = out.split("\n", maxsplit=1) + assert not err + assert str(settings.settings_file) in sourceheader + assert json.loads(out) == settings.to_dict() + + main(["config", "--show", "conda-format"]) + out, err = capsys.readouterr() + assert not err + assert out.strip() == 'conda-format: ".conda"' + + main(["config", "--show", "auto-update-std-renames", "conda_format"]) + out, err = capsys.readouterr() + assert not err + line1, line2 = out.strip().split("\n", maxsplit=1) + assert line1 == 'auto-update-std-renames: false' + assert line2 == 'conda-format: ".conda"' + + +def test_config_set( + tmp_settings_file: Path, +) -> None: + """ + Test config --set + """ + + # dry-run: settings changed in memory but not saved + main(["config", "--set", "conda-format", "V1", "--dry-run"]) + assert settings.conda_format is CondaPackageFormat.V1 + assert not tmp_settings_file.exists() + + main(["config", "--set", "conda-format", "V2", "--dry-run"]) + assert settings.conda_format is CondaPackageFormat.V2 + assert not tmp_settings_file.exists() + + main(["config", "--set", "conda-format", "V1"]) + assert settings.conda_format is CondaPackageFormat.V1 + assert tmp_settings_file.exists() + + settings2 = Whl2CondaSettings.from_file(tmp_settings_file) + assert settings2.conda_format is CondaPackageFormat.V1 + + +def test_config_remove( + tmp_settings_file: Path, +) -> None: + """ + Test config --remove + """ + assert not tmp_settings_file.exists() + main(["config", "--set", "conda-format", "V1"]) + main(["config", "--set", "pypi-indexes.foo", "https://foo.com/pypi"]) + main(["config", "--set", "pypi-indexes.bar", "https://bar.com/pypi"]) + assert tmp_settings_file.exists() + assert settings.conda_format is CondaPackageFormat.V1 + assert settings.pypi_indexes["foo"] == "https://foo.com/pypi" + assert settings.pypi_indexes["bar"] == "https://bar.com/pypi" + + main(["config", "--remove", "conda-format", "--dry-run"]) + assert settings.conda_format is CondaPackageFormat.V2 # prev default + settings2 = Whl2CondaSettings.from_file(tmp_settings_file) + assert settings2.conda_format is CondaPackageFormat.V1 + + main(["config", "--remove", "conda-format"]) + assert settings.conda_format is CondaPackageFormat.V2 # prev default + settings2 = Whl2CondaSettings.from_file(tmp_settings_file) + assert settings2.conda_format is CondaPackageFormat.V2 + + main(["config", "--remove", "pypi-indexes.foo"]) + settings2 = Whl2CondaSettings.from_file(tmp_settings_file) + assert settings.pypi_indexes == dict(bar="https://bar.com/pypi") + + +def test_override_settings( + tmp_path: Path, + capsys: pytest.CaptureFixture, +) -> None: + """ + Test whl2conda --settings option + """ + + new_path = tmp_path / "settings.json" + main(["--settings", str(new_path), "config", "--show-sources"]) + out, err = capsys.readouterr() + + assert not err + line1, rest = out.split("\n", maxsplit=1) + assert str(new_path) in line1 + assert settings.settings_file == new_path diff --git a/test/conftest.py b/test/conftest.py index 85baae1..d818e64 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -29,9 +29,11 @@ @pytest.fixture(autouse=True) def clear_settings(): """ - Fixture clears user settings before each test + Fixture clears user settings and resets location of settings file + prior to each test. """ settings.unset_all() + settings._settings_file = settings.DEFAULT_SETTINGS_FILE def pytest_addoption(parser):