Skip to content

Commit

Permalink
Merge pull request #21 from graingert/fix-long-form-args
Browse files Browse the repository at this point in the history
make args testable, and test them with tox
  • Loading branch information
Peter Bengtsson authored Sep 7, 2016
2 parents 3b513d1 + 6ad7e00 commit 6549e7a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 29 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
.eggs/
hashin.egg-info/
.tox/

### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
57 changes: 28 additions & 29 deletions hashin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

from __future__ import print_function
import argparse
import cgi
import tempfile
import os
Expand All @@ -30,6 +31,33 @@
"Consider upgrading your version of Python."
)

parser = argparse.ArgumentParser()
parser.add_argument(
'packages',
help="One or more package specifiers (e.g. some-package or some-package==1.2.3)",
nargs='+'
)
parser.add_argument(
'-r', '--requirements-file',
help="requirements file to write to (default requirements.txt)",
default='requirements.txt'
)
parser.add_argument(
'-a', '--algorithm',
help="The hash algorithm to use: one of sha256, sha384, sha512",
default='sha256'
)
parser.add_argument(
'-v', '--verbose',
help="Verbose output",
action="store_true",
)
parser.add_argument(
'-p', '--python-version',
help='Python version to add wheels for. May be used multiple times.',
action='append',
default=[],
)

major_pip_version = int(pip.__version__.split('.')[0])
if major_pip_version < 8:
Expand Down Expand Up @@ -292,35 +320,6 @@ def add_hashes(releases, algorithm, verbose=False):


def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
'packages',
help="One or more package specifiers (e.g. some-package or some-package==1.2.3)",
nargs='+'
)
parser.add_argument(
'-r', '--requirements-file',
help="requirements file to write to (default requirements.txt)",
default='requirements.txt'
)
parser.add_argument(
'-a', '--algorithm',
help="The hash algorithm to use: one of sha256, sha384, sha512",
default='sha256'
)
parser.add_argument(
'-v', '--verbose',
help="Verbose output",
action="store_true",
)
parser.add_argument(
'-p', '--python-version',
help='Python version to add wheels for. May be used multiple times.',
action='append',
default=[],
)

args = parser.parse_args()

return run(
Expand Down
53 changes: 53 additions & 0 deletions tests/test_arg_parse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import argparse

from nose.tools import eq_

from hashin import parser


def test_everything():
args = parser.parse_known_args([
'example', 'another-example',
'-r', 'reqs.txt',
'-a', 'sha512',
'-p', '3.5',
'-v',
])
expected = argparse.Namespace(
algorithm='sha512',
packages=['example', 'another-example'],
python_version=['3.5'],
requirements_file='reqs.txt',
verbose=True,
)
eq_(args, (expected, []))


def test_everything_long():
args = parser.parse_known_args([
'example', 'another-example',
'--requirements-file', 'reqs.txt',
'--algorithm', 'sha512',
'--python-version', '3.5',
'--verbose',
])
expected = argparse.Namespace(
algorithm='sha512',
packages=['example', 'another-example'],
python_version=['3.5'],
requirements_file='reqs.txt',
verbose=True,
)
eq_(args, (expected, []))


def test_minimal():
args = parser.parse_known_args(['example'])
expected = argparse.Namespace(
algorithm='sha256',
packages=['example'],
python_version=[],
requirements_file='requirements.txt',
verbose=False,
)
eq_(args, (expected, []))

0 comments on commit 6549e7a

Please sign in to comment.