Skip to content

Commit

Permalink
Test config settings options
Browse files Browse the repository at this point in the history
  • Loading branch information
analog-cbarber committed May 4, 2024
1 parent f7da3c9 commit bb3a5f8
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/whl2conda/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down Expand Up @@ -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))

Expand Down
135 changes: 134 additions & 1 deletion test/cli/test_config.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
4 changes: 3 additions & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit bb3a5f8

Please sign in to comment.