From 845029a551601086bc5316126198f8fc5e246a54 Mon Sep 17 00:00:00 2001 From: Albert Tugushev Date: Tue, 11 Jul 2023 01:39:57 +0200 Subject: [PATCH] Add tests for select_config_file --- tests/conftest.py | 9 ++++---- tests/test_utils.py | 51 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 024efa480..4cfafd74e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -454,11 +454,12 @@ 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)) return config_file.relative_to(tmpdir_cwd) diff --git a/tests/test_utils.py b/tests/test_utils.py index 003894111..0d7b250f0 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -6,6 +6,7 @@ import shlex import sys from pathlib import Path +from textwrap import dedent import pip import pytest @@ -31,6 +32,7 @@ lookup_table, lookup_table_from_tuples, override_defaults_from_config_file, + select_config_file, ) @@ -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