Repository: peterbe/hashin

Issue:
New --index-url option is broken
Shame on me for landing https://github.com/peterbe/hashin/pull/107 without properly testing it manually on the command line. 

```
▶ python hashin.py -r /tmp/reqs.txt DJANGO -v
Traceback (most recent call last):
  File "hashin.py", line 812, in <module>
    sys.exit(main())
  File "hashin.py", line 804, in main
    index_url=args.index_url,
  File "hashin.py", line 134, in run
    return run_packages(specs, requirements_file, *args, **kwargs)
  File "hashin.py", line 196, in run_packages
    index_url=index_url,
  File "hashin.py", line 655, in get_package_hashes
    data = get_package_data(package, index_url, verbose)
  File "hashin.py", line 583, in get_package_data
    content = json.loads(_download(url))
  File "hashin.py", line 88, in _download
    r = urlopen(url)
  File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 511, in open
    req = Request(fullurl, data)
  File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 329, in __init__
    self.full_url = url
  File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 355, in full_url
    self._parse()
  File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 384, in _parse
    raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: '/pypi/DJANGO/json'
```

The reason this happens is that `url` [on this line](https://github.com/peterbe/hashin/blob/c06eba0c55d55b82c72522363717108458d3f912/hashin.py#L580) becomes `/pypi/DJANGO/json`.

The reason is because `index_url` is `None` and that's how `urljoin` works. 

Discussion:
peterbe: This is what happened:

```python
def foo(index_url="SOME_DEFAULT"):
    print(repr(index_url))


foo(index_url=None)
```

The output of that becomes:
```
None
```
peterbe: @nmacinnis I'm confident this PR fixes the problem and so I'm going to go ahead and land it. Again, sorry I didn't test this manually enough. 

Patch:
diff --git a/README.rst b/README.rst
index 7f63630..3aefe28 100644
--- a/README.rst
+++ b/README.rst
@@ -270,8 +270,12 @@ put it directly into ``pip``.
 Version History
 ===============
 
+0.14.4
+  * Bugfix for new ``--index-url`` option feature in version 0.14.3.
+    See https://github.com/peterbe/hashin/issues/108
+
 0.14.3
-  * New parameterr ``--index-url`` which allows to override the default which
+  * New parameter ``--index-url`` which allows to override the default which
     is ``https://pypi.org``. Thanks @nmacinnis
     See https://github.com/peterbe/hashin/pull/107
 
diff --git a/hashin.py b/hashin.py
index a296359..cbc998c 100755
--- a/hashin.py
+++ b/hashin.py
@@ -47,6 +47,7 @@ else:
 DEFAULT_ALGORITHM = "sha256"
 
 DEFAULT_INDEX_URL = os.environ.get("INDEX_URL", "https://pypi.org/")
+assert DEFAULT_INDEX_URL
 
 MAX_WORKERS = None
 
@@ -159,6 +160,7 @@ def run_packages(
     synchronous=False,
     index_url=DEFAULT_INDEX_URL,
 ):
+    assert index_url
     assert isinstance(specs, list), type(specs)
     all_new_lines = []
     first_interactive = True
@@ -754,7 +756,7 @@ def get_parser():
     parser.add_argument(
         "--index-url",
         help="alternate package index url (default {0})".format(DEFAULT_INDEX_URL),
-        default=None,
+        default=DEFAULT_INDEX_URL,
     )
     return parser
 
diff --git a/setup.py b/setup.py
index 417cf50..e42b8ab 100644
--- a/setup.py
+++ b/setup.py
@@ -14,7 +14,7 @@ except ImportError:
 
 setup(
     name="hashin",
-    version="0.14.3",
+    version="0.14.4",
     description="Edits your requirements.txt by hashing them in",
     long_description=open(path.join(_here, "README.rst")).read(),
     author="Peter Bengtsson",


Test patch:
diff --git a/tests/test_arg_parse.py b/tests/test_arg_parse.py
index 23288db..ae52330 100644
--- a/tests/test_arg_parse.py
+++ b/tests/test_arg_parse.py
@@ -1,6 +1,6 @@
 import argparse
 
-from hashin import get_parser
+from hashin import get_parser, DEFAULT_INDEX_URL
 
 
 def test_everything():
@@ -85,6 +85,6 @@ def test_minimal():
         update_all=False,
         interactive=False,
         synchronous=False,
-        index_url=None,
+        index_url=DEFAULT_INDEX_URL,
     )
     assert args == (expected, [])