From 407f00822f16e52cd8bbc8f9ccde3d8dab0c7979 Mon Sep 17 00:00:00 2001 From: Albert Tugushev Date: Sat, 1 Jul 2023 12:47:56 +0200 Subject: [PATCH] Add `--no-config` option (#1896) --- piptools/scripts/compile.py | 8 ++++++++ piptools/scripts/sync.py | 8 ++++++++ piptools/utils.py | 4 ++++ tests/test_cli_compile.py | 16 ++++++++++++++++ tests/test_cli_sync.py | 13 +++++++++++++ 5 files changed, 49 insertions(+) diff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py index 35476ec4b..f444271af 100755 --- a/piptools/scripts/compile.py +++ b/piptools/scripts/compile.py @@ -319,6 +319,13 @@ def _determine_linesep( is_eager=True, callback=override_defaults_from_config_file, ) +@click.option( + "--no-config", + is_flag=True, + default=False, + help="Do not read any config file.", + is_eager=True, +) def cli( ctx: click.Context, verbose: int, @@ -358,6 +365,7 @@ def cli( emit_options: bool, unsafe_package: tuple[str, ...], config: Path | None, + no_config: bool, ) -> None: """ Compiles requirements.txt from requirements.in, pyproject.toml, setup.cfg, diff --git a/piptools/scripts/sync.py b/piptools/scripts/sync.py index f20a635ae..e162feac6 100755 --- a/piptools/scripts/sync.py +++ b/piptools/scripts/sync.py @@ -103,6 +103,13 @@ is_eager=True, callback=override_defaults_from_config_file, ) +@click.option( + "--no-config", + is_flag=True, + default=False, + help="Do not read any config file.", + is_eager=True, +) def cli( ask: bool, dry_run: bool, @@ -121,6 +128,7 @@ def cli( src_files: tuple[str, ...], pip_args: str | None, config: Path | None, + no_config: bool, ) -> None: """Synchronize virtual environment with requirements.txt.""" log.verbosity = verbose - quiet diff --git a/piptools/utils.py b/piptools/utils.py index d80fdf70c..3fe0ec9b9 100644 --- a/piptools/utils.py +++ b/piptools/utils.py @@ -54,6 +54,7 @@ "--verbose", "--cache-dir", "--no-reuse-hashes", + "--no-config", } @@ -548,6 +549,9 @@ def override_defaults_from_config_file( file. Those files are searched for in the same directory as the requirements input file, or the current working directory if requirements come via stdin. """ + if ctx.params.get("no_config"): + return None + if value is None: config_file = select_config_file(ctx.params.get("src_files", ())) if config_file is None: diff --git a/tests/test_cli_compile.py b/tests/test_cli_compile.py index 84345886c..edb4d2087 100644 --- a/tests/test_cli_compile.py +++ b/tests/test_cli_compile.py @@ -2973,3 +2973,19 @@ def test_config_option(pip_conf, runner, tmp_path, make_config_file): assert out.exit_code == 0 assert "Dry-run, so nothing updated" in out.stderr + + +def test_no_config_option_overrides_config_with_defaults( + pip_conf, runner, tmp_path, make_config_file +): + config_file = make_config_file("dry-run", True) + + req_in = tmp_path / "requirements.in" + req_in.touch() + + out = runner.invoke( + cli, [req_in.as_posix(), "--no-config", "--config", config_file.as_posix()] + ) + + assert out.exit_code == 0 + assert "Dry-run, so nothing updated" not in out.stderr diff --git a/tests/test_cli_sync.py b/tests/test_cli_sync.py index e7ec3c43f..0ebcddf8d 100644 --- a/tests/test_cli_sync.py +++ b/tests/test_cli_sync.py @@ -385,3 +385,16 @@ def test_config_option(run, runner, make_config_file): assert out.exit_code == 1 assert "Would install:" in out.stdout + + +@mock.patch("piptools.sync.run") +def test_no_config_option_overrides_config_with_defaults(run, runner, make_config_file): + config_file = make_config_file("dry-run", True) + + with open(sync.DEFAULT_REQUIREMENTS_FILE, "w") as reqs_txt: + reqs_txt.write("six==1.10.0") + + out = runner.invoke(cli, ["--no-config", "--config", config_file.as_posix()]) + + assert out.exit_code == 0 + assert "Would install:" not in out.stdout