|
6 | 6 | import shlex |
7 | 7 | import sys |
8 | 8 | from pathlib import Path |
| 9 | +from textwrap import dedent |
9 | 10 |
|
10 | 11 | import pip |
11 | 12 | import pytest |
|
31 | 32 | lookup_table, |
32 | 33 | lookup_table_from_tuples, |
33 | 34 | override_defaults_from_config_file, |
| 35 | + select_config_file, |
34 | 36 | ) |
35 | 37 |
|
36 | 38 |
|
@@ -415,6 +417,36 @@ def test_get_compile_command_with_config(tmpdir_cwd, config_file, expected_comma |
415 | 417 | assert get_compile_command(ctx) == expected_command |
416 | 418 |
|
417 | 419 |
|
| 420 | +@pytest.mark.parametrize("config_file", ("pyproject.toml", ".pip-tools.toml")) |
| 421 | +@pytest.mark.parametrize( |
| 422 | + "config_file_content", |
| 423 | + ( |
| 424 | + pytest.param("", id="empty config file"), |
| 425 | + pytest.param("[tool.pip-tools]", id="empty config section"), |
| 426 | + pytest.param("[tool.pip-tools]\ndry-run = true", id="non-empty config section"), |
| 427 | + ), |
| 428 | +) |
| 429 | +def test_get_compile_command_does_not_include_default_config_if_reqs_file_in_subdir( |
| 430 | + tmpdir_cwd, config_file, config_file_content |
| 431 | +): |
| 432 | + """ |
| 433 | + Test that ``get_compile_command`` does not include default config file |
| 434 | + if requirements file is in a subdirectory. |
| 435 | + Regression test for issue GH-1903. |
| 436 | + """ |
| 437 | + default_config_file = Path(config_file) |
| 438 | + default_config_file.write_text(config_file_content) |
| 439 | + |
| 440 | + (tmpdir_cwd / "subdir").mkdir() |
| 441 | + req_file = Path("subdir/requirements.in") |
| 442 | + req_file.touch() |
| 443 | + req_file.write_bytes(b"") |
| 444 | + |
| 445 | + # Make sure that the default config file is not included |
| 446 | + with compile_cli.make_context("pip-compile", [req_file.as_posix()]) as ctx: |
| 447 | + assert get_compile_command(ctx) == f"pip-compile {req_file.as_posix()}" |
| 448 | + |
| 449 | + |
418 | 450 | def test_get_compile_command_escaped_filenames(tmpdir_cwd): |
419 | 451 | """ |
420 | 452 | Test that get_compile_command output (re-)escapes ' -- '-escaped filenames. |
@@ -683,3 +715,52 @@ def test_callback_config_file_defaults_unreadable_toml(make_config_file): |
683 | 715 | "config", |
684 | 716 | "/dev/null/path/does/not/exist/my-config.toml", |
685 | 717 | ) |
| 718 | + |
| 719 | + |
| 720 | +def test_select_config_file_no_files(tmpdir_cwd): |
| 721 | + assert select_config_file(()) is None |
| 722 | + |
| 723 | + |
| 724 | +@pytest.mark.parametrize("filename", ("pyproject.toml", ".pip-tools.toml")) |
| 725 | +def test_select_config_file_returns_config_in_cwd(make_config_file, filename): |
| 726 | + config_file = make_config_file("dry-run", True, filename) |
| 727 | + assert select_config_file(()) == config_file |
| 728 | + |
| 729 | + |
| 730 | +def test_select_config_file_returns_empty_config_file_in_cwd(tmpdir_cwd): |
| 731 | + config_file = Path(".pip-tools.toml") |
| 732 | + config_file.touch() |
| 733 | + |
| 734 | + assert select_config_file(()) == config_file |
| 735 | + |
| 736 | + |
| 737 | +def test_select_config_file_cannot_find_config_in_cwd(tmpdir_cwd, make_config_file): |
| 738 | + make_config_file("dry-run", True, "subdir/pyproject.toml") |
| 739 | + assert select_config_file(()) is None |
| 740 | + |
| 741 | + |
| 742 | +def test_select_config_file_with_config_file_in_subdir(tmpdir_cwd, make_config_file): |
| 743 | + config_file = make_config_file("dry-run", True, "subdir/.pip-tools.toml") |
| 744 | + |
| 745 | + requirement_file = Path("subdir/requirements.in") |
| 746 | + requirement_file.touch() |
| 747 | + |
| 748 | + assert select_config_file((requirement_file.as_posix(),)) == config_file |
| 749 | + |
| 750 | + |
| 751 | +def test_select_config_file_prefers_pip_tools_toml_over_pyproject_toml(tmpdir_cwd): |
| 752 | + pip_tools_file = Path(".pip-tools.toml") |
| 753 | + pip_tools_file.touch() |
| 754 | + |
| 755 | + pyproject_file = Path("pyproject.toml") |
| 756 | + pyproject_file.write_text( |
| 757 | + dedent( |
| 758 | + """\ |
| 759 | + [build-system] |
| 760 | + requires = ["setuptools>=63", "setuptools_scm[toml]>=7"] |
| 761 | + build-backend = "setuptools.build_meta" |
| 762 | + """ |
| 763 | + ) |
| 764 | + ) |
| 765 | + |
| 766 | + assert select_config_file(()) == pip_tools_file |
0 commit comments