Skip to content

Commit e84d743

Browse files
committed
Make pip-sync honor src_files from the config file
pip-compile reads src_files out of the click context default_map when none are given on the command line, but pip-sync never did, so [tool.pip-tools.sync] src_files was silently ignored and pip-sync always fell back to requirements.txt (or exited 2 when it was absent). Apply the same default_map lookup in pip-sync. CLI arguments still win. Fixes #2187
1 parent bccf97a commit e84d743

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

changelog.d/2187.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Made `pip-sync` respect `src_files` set in the config file, matching `pip-compile` -- by {user}`Sanjays2402`.

piptools/scripts/sync.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949

5050
@click.command(name="pip-sync")
51+
@click.pass_context
5152
@options.help_option(epilog=SYNC_EPILOG)
5253
@options.version
5354
@options.ask
@@ -69,6 +70,7 @@
6970
@options.config
7071
@options.no_config
7172
def cli(
73+
ctx: click.Context,
7274
ask: bool,
7375
dry_run: bool,
7476
force: bool,
@@ -91,6 +93,12 @@ def cli(
9193
"""Synchronize virtual environment with requirements.txt."""
9294
log.verbosity = verbose - quiet
9395

96+
# If ``src_files`` was not provided as an input, but rather as config,
97+
# it will be part of the click context ``ctx``.
98+
# However, if ``src_files`` is specified, then we want to use that.
99+
if not src_files and ctx.default_map and "src_files" in ctx.default_map:
100+
src_files = tuple(ctx.default_map["src_files"])
101+
94102
if not src_files:
95103
if os.path.exists(DEFAULT_REQUIREMENTS_FILE):
96104
src_files = (DEFAULT_REQUIREMENTS_FILE,)

tests/test_cli_sync.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,3 +488,43 @@ def test_tool_specific_config_option(run, runner, make_config_file):
488488

489489
assert out.exit_code == 1
490490
assert "Would install:" in out.stdout
491+
492+
493+
@mock.patch("piptools.sync.run")
494+
def test_use_src_files_from_config_if_option_is_not_specified_from_cli(
495+
run, runner, make_config_file
496+
):
497+
config_file = make_config_file(
498+
"src-files", ["requirements_lock.txt"], section="pip-tools", subsection="sync"
499+
)
500+
501+
with open("requirements_lock.txt", "w") as reqs_txt:
502+
reqs_txt.write("six==1.10.0")
503+
504+
out = runner.invoke(cli, ["--dry-run", "--config", config_file.as_posix()])
505+
506+
assert out.exit_code == 1
507+
assert "Would install:" in out.stdout
508+
assert "six==1.10.0" in out.stdout
509+
510+
511+
@mock.patch("piptools.sync.run")
512+
def test_use_src_files_from_cli_if_option_is_specified_in_both_config_and_cli(
513+
run, runner, make_config_file
514+
):
515+
config_file = make_config_file(
516+
"src-files", ["requirements_lock.txt"], section="pip-tools", subsection="sync"
517+
)
518+
519+
with open("requirements_lock.txt", "w") as reqs_txt:
520+
reqs_txt.write("six==1.10.0")
521+
with open("other.txt", "w") as reqs_txt:
522+
reqs_txt.write("small-fake-a==0.1")
523+
524+
out = runner.invoke(
525+
cli, ["other.txt", "--dry-run", "--config", config_file.as_posix()]
526+
)
527+
528+
assert out.exit_code == 1
529+
assert "small-fake-a==0.1" in out.stdout
530+
assert "six==1.10.0" not in out.stdout

0 commit comments

Comments
 (0)