Skip to content

Commit

Permalink
Merge pull request #731 from atugushev/fix-output-file-for-setup-py
Browse files Browse the repository at this point in the history
Fix the output file for pip-compile with an explicit setup.py as source file
  • Loading branch information
atugushev authored Feb 28, 2019
2 parents 1545aa7 + 37ae661 commit 5af073e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
10 changes: 5 additions & 5 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from ..writer import OutputWriter

DEFAULT_REQUIREMENTS_FILE = 'requirements.in'
DEFAULT_REQUIREMENTS_OUTPUT_FILE = 'requirements.txt'


@click.command()
Expand Down Expand Up @@ -72,15 +73,14 @@ def cli(verbose, quiet, dry_run, pre, rebuild, find_links, index_url, extra_inde
src_files = (DEFAULT_REQUIREMENTS_FILE,)
elif os.path.exists('setup.py'):
src_files = ('setup.py',)
if not output_file:
output_file = 'requirements.txt'
else:
raise click.BadParameter(("If you do not specify an input file, "
"the default is {} or setup.py").format(DEFAULT_REQUIREMENTS_FILE))

if len(src_files) == 1 and src_files[0] == '-':
if not output_file:
raise click.BadParameter('--output-file is required if input is from stdin')
if src_files == ('-',) and not output_file:
raise click.BadParameter('--output-file is required if input is from stdin')
elif src_files == ('setup.py',):
output_file = DEFAULT_REQUIREMENTS_OUTPUT_FILE

if len(src_files) > 1 and not output_file:
raise click.BadParameter('--output-file is required if two or more input files are given.')
Expand Down
22 changes: 19 additions & 3 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ def test_command_line_overrides_pip_conf(pip_conf):
assert 'Using indexes:\n http://override.com' in out.output


def test_command_line_setuptools_read(pip_conf):

@pytest.mark.parametrize('cli_args', [
[],
['setup.py'],
])
def test_command_line_setuptools_read(pip_conf, cli_args):
runner = CliRunner()
with runner.isolated_filesystem():
package = open('setup.py', 'w')
Expand All @@ -87,10 +90,11 @@ def test_command_line_setuptools_read(pip_conf):
setup(install_requires=[])
"""))
package.close()
out = runner.invoke(cli)
out = runner.invoke(cli, cli_args)

# check that pip-compile generated a configuration
assert 'This file is autogenerated by pip-compile' in out.output
assert os.path.exists('requirements.txt')


def test_find_links_option(pip_conf):
Expand Down Expand Up @@ -398,3 +402,15 @@ def test_default_index_url():
' http://example.com)' + os.linesep
)
assert expected in output


def test_stdin_without_output_file():
"""
The --output-file option is required for STDIN.
"""
runner = CliRunner()
with runner.isolated_filesystem():
out = runner.invoke(cli, ['-n', '-'])

assert out.exit_code == 2
assert '--output-file is required if input is from stdin' in out.output

0 comments on commit 5af073e

Please sign in to comment.