Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude default config value from the pip-compile header #1893

Merged
merged 4 commits into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions piptools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import collections
import copy
import functools
import itertools
import json
import os
Expand Down Expand Up @@ -366,6 +365,12 @@ def get_compile_command(click_ctx: click.Context) -> str:
if option_long_name in COMPILE_EXCLUDE_OPTIONS:
continue

# Exclude config option if it's the default one
if option_long_name == "--config":
default_config = select_config_file(click_ctx.params.get("src_files", ()))
if value == default_config:
continue

# Skip options without a value
if option.default is None and not value:
continue
Expand Down Expand Up @@ -594,7 +599,7 @@ def select_config_file(src_files: tuple[str, ...]) -> Path | None:
),
None,
)
return config_file_path
return config_file_path.relative_to(working_directory) if config_file_path else None


# Some of the defined click options have different `dest` values than the defaults
Expand Down Expand Up @@ -628,7 +633,6 @@ def get_click_dest_for_option(option_name: str) -> str:
]


@functools.lru_cache()
webknjaz marked this conversation as resolved.
Show resolved Hide resolved
def parse_config_file(config_file: Path) -> dict[str, Any]:
try:
config = tomllib.loads(config_file.read_text(encoding="utf-8"))
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,6 @@ def _maker(

config_to_dump = {"tool": {"pip-tools": {pyproject_param: new_default}}}
config_file.write_text(tomli_w.dumps(config_to_dump))
return config_file
return config_file.relative_to(tmpdir_cwd)

return _maker
24 changes: 24 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,30 @@ def test_get_compile_command(tmpdir_cwd, cli_args, expected_command):
assert get_compile_command(ctx) == expected_command


@pytest.mark.parametrize(
("config_file", "expected_command"),
(
pytest.param(
"pyproject.toml", "pip-compile", id="exclude default pyproject.toml"
webknjaz marked this conversation as resolved.
Show resolved Hide resolved
),
pytest.param(
".pip-tools.toml", "pip-compile", id="exclude default .pip-tools.toml"
),
pytest.param(
"my-config.toml",
"pip-compile --config=my-config.toml",
id="include non-default my-config.toml",
),
),
)
def test_get_compile_command_with_config(tmpdir_cwd, config_file, expected_command):
"""Test that get_compile_command excludes or includes config file."""
with open(config_file, "w"):
pass
with compile_cli.make_context("pip-compile", ["--config", config_file]) as ctx:
assert get_compile_command(ctx) == expected_command


def test_get_compile_command_escaped_filenames(tmpdir_cwd):
"""
Test that get_compile_command output (re-)escapes ' -- '-escaped filenames.
Expand Down