Skip to content

Commit

Permalink
Add tests for select_config_file
Browse files Browse the repository at this point in the history
  • Loading branch information
atugushev committed Jul 10, 2023
1 parent 64445d0 commit 783a6e0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
11 changes: 6 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,14 @@ def make_config_file(tmpdir_cwd):
def _maker(
pyproject_param: str, new_default: Any, config_file_name: str = CONFIG_FILE_NAME
) -> Path:
# Make a config file with this one config default override
config_path = Path(tmpdir_cwd) / pyproject_param
config_file = config_path / config_file_name
config_path.mkdir(exist_ok=True)
# Create a nested directory structure if config_file_name includes directories
config_dir = Path(tmpdir_cwd / config_file_name).parent
config_dir.mkdir(exist_ok=True, parents=True)

# Make a config file with this one config default override
config_file = Path(tmpdir_cwd / config_file_name)
config_to_dump = {"tool": {"pip-tools": {pyproject_param: new_default}}}
config_file.write_text(tomli_w.dumps(config_to_dump))
config_file.write_text(tomli_w.dumps(config_to_dump), encoding="utf-8")
return config_file.relative_to(tmpdir_cwd)

return _maker
51 changes: 51 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import shlex
import sys
from pathlib import Path
from textwrap import dedent

import pip
import pytest
Expand All @@ -31,6 +32,7 @@
lookup_table,
lookup_table_from_tuples,
override_defaults_from_config_file,
select_config_file,
)


Expand Down Expand Up @@ -708,3 +710,52 @@ def test_callback_config_file_defaults_unreadable_toml(make_config_file):
"config",
"/dev/null/path/does/not/exist/my-config.toml",
)


def test_select_config_file_no_files(tmpdir_cwd):
assert select_config_file(()) is None


@pytest.mark.parametrize("filename", ("pyproject.toml", ".pip-tools.toml"))
def test_select_config_file_returns_config_in_cwd(make_config_file, filename):
config_file = make_config_file("dry-run", True, filename)
assert select_config_file(()) == config_file


def test_select_config_file_returns_empty_config_file_in_cwd(tmpdir_cwd):
config_file = Path(".pip-tools.toml")
config_file.touch()

assert select_config_file(()) == config_file


def test_select_config_file_cannot_find_config_in_cwd(tmpdir_cwd, make_config_file):
make_config_file("dry-run", True, "subdir/pyproject.toml")
assert select_config_file(()) is None


def test_select_config_file_with_config_file_in_subdir(tmpdir_cwd, make_config_file):
config_file = make_config_file("dry-run", True, "subdir/.pip-tools.toml")

requirement_file = Path("subdir/requirements.in")
requirement_file.touch()

assert select_config_file((requirement_file.as_posix(),)) == config_file


def test_select_config_file_prefers_pip_tools_toml_over_pyproject_toml(tmpdir_cwd):
pip_tools_file = Path(".pip-tools.toml")
pip_tools_file.touch()

pyproject_file = Path("pyproject.toml")
pyproject_file.write_text(
dedent(
"""\
[build-system]
requires = ["setuptools>=63", "setuptools_scm[toml]>=7"]
build-backend = "setuptools.build_meta"
"""
)
)

assert select_config_file(()) == pip_tools_file

0 comments on commit 783a6e0

Please sign in to comment.