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

Add pyproject.toml as default input file format #1780

Merged
merged 18 commits into from
Dec 24, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
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
20 changes: 12 additions & 8 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@
)
from ..writer import OutputWriter

DEFAULT_REQUIREMENTS_FILE = "requirements.in"
DEFAULT_REQUIREMENTS_FILES = (
"requirements.in",
"setup.py",
"pyproject.toml",
"setup.cfg",
)
DEFAULT_REQUIREMENTS_OUTPUT_FILE = "requirements.txt"
METADATA_FILENAMES = frozenset({"setup.py", "setup.cfg", "pyproject.toml"})

Expand Down Expand Up @@ -342,16 +347,15 @@ def cli(
log.verbosity = verbose - quiet

if len(src_files) == 0:
if os.path.exists(DEFAULT_REQUIREMENTS_FILE):
src_files = (DEFAULT_REQUIREMENTS_FILE,)
elif os.path.exists("setup.py"):
src_files = ("setup.py",)
for file_path in DEFAULT_REQUIREMENTS_FILES:
if os.path.exists(file_path):
src_files = (file_path,)
break
else:
raise click.BadParameter(
(
"If you do not specify an input file, "
"the default is {} or setup.py"
).format(DEFAULT_REQUIREMENTS_FILE)
"If you do not specify an input file, the default is one of: {}"
).format(", ".join(DEFAULT_REQUIREMENTS_FILES))
)

if not output_file:
Expand Down
45 changes: 35 additions & 10 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1393,16 +1393,6 @@ def test_stdin_without_output_file(runner):
assert "--output-file is required if input is from stdin" in out.stderr


def test_not_specified_input_file(runner):
berislavlopac marked this conversation as resolved.
Show resolved Hide resolved
"""
It should raise an error if there are no input files or default input files
such as "setup.py" or "requirements.in".
"""
out = runner.invoke(cli)
assert "If you do not specify an input file" in out.stderr
assert out.exit_code == 2


def test_stdin(pip_conf, runner):
"""
Test compile requirements from STDIN.
Expand Down Expand Up @@ -2457,6 +2447,41 @@ def test_triple_equal_pinned_dependency_is_used(
)


@pytest.mark.parametrize(("fname", "content"), METADATA_TEST_CASES)
def test_not_specified_input_file(
runner, make_module, fname, content, tmpdir, monkeypatch
):
"""
Test that a default-named file is parsed if present.
"""
make_module(fname=fname, content=content)

monkeypatch.chdir(tmpdir)
out = runner.invoke(cli)
berislavlopac marked this conversation as resolved.
Show resolved Hide resolved
monkeypatch.undo()
berislavlopac marked this conversation as resolved.
Show resolved Hide resolved

assert "small-fake-a==0.1" in out.stderr
atugushev marked this conversation as resolved.
Show resolved Hide resolved
assert "small-fake-b" not in out.stderr
assert "small-fake-c" not in out.stderr
assert "extra ==" not in out.stderr
assert out.exit_code == 1


def test_not_specified_input_file_without_allowed_files(runner):
"""
It should raise an error if there are no input files or default input files
such as "setup.py" or "requirements.in".
"""
out = runner.invoke(cli)
assert "If you do not specify an input file" in out.stderr
berislavlopac marked this conversation as resolved.
Show resolved Hide resolved
assert out.exit_code == 2
expected_error = (
"Error: Invalid value: If you do not specify an input file, the default "
"is one of: requirements.in, setup.py, pyproject.toml, setup.cfg"
)
assert expected_error in out.stderr.splitlines()


@pytest.mark.network
@pytest.mark.parametrize(("fname", "content"), METADATA_TEST_CASES)
def test_input_formats(fake_dists, runner, make_module, fname, content):
Expand Down