Skip to content

Commit

Permalink
Be kinder if the package is a requirements file
Browse files Browse the repository at this point in the history
Fixes #104
  • Loading branch information
peterbe committed Mar 6, 2019
1 parent 2384037 commit b535440
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
17 changes: 17 additions & 0 deletions hashin.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,23 @@ def main():
parser = get_parser()
args = parser.parse_args()

if (
args.update_all
and args.packages
and len(args.packages) == 1
and os.path.isfile(args.packages[0])
and args.packages[0].endswith(".txt")
):
# It's totally common to make the mistake of using the `--update-all` flag
# and specifying the requirements file as the first argument. E.g.
#
# $ hashin --update-all --interactive myproject/reqs.txt
#
# The user intention is clear any non-keyed flags get interpreted as a
# list of "packages". Let's fix that for the user.
args.requirements_file = args.packages[0]
args.packages = []

if args.update_all:
if args.packages:
print(
Expand Down
30 changes: 30 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,36 @@ def mock_parse_args(*a, **k):
)


def test_packages_and_update_all_with_requirements_file(
capsys, mock_get_parser, tmpfile
):
with tmpfile() as filename:
with open(filename, "w") as f:
f.write("")

def mock_parse_args(*a, **k):
return argparse.Namespace(
packages=[filename], # Note!
algorithm="sha256",
python_version="3.8",
verbose=False,
include_prereleases=False,
dry_run=False,
update_all=True,
interactive=False,
synchronous=False,
index_url="anything",
)

mock_get_parser().parse_args.side_effect = mock_parse_args

error = hashin.main()
assert error == 0
# Because the requirements file is empty, the update-all command
# won't find anything to query the internet about so we don't
# need to mock murlopen in this test.


def test_no_packages_and_not_update_all(capsys, mock_get_parser):
def mock_parse_args(*a, **k):
return argparse.Namespace(
Expand Down

0 comments on commit b535440

Please # to comment.