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

Keep a consistant order for hashes for get_package_hashes. #131

Merged
merged 4 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ Version History

* Add python 3.9 and 3.10 to the test matrix.

* Preserve lexigraphical order of hashes for the output of the
``get_releases_hashes`` function.
See https://github.com/peterbe/hashin/issues/126

0.16.0
* Preserve indented comments when updating requirements files.
See https://github.com/peterbe/hashin/issues/124
Expand Down
7 changes: 4 additions & 3 deletions hashin.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def run_packages(
maybe_restriction = "" if not restriction else "; {0}".format(restriction)
new_lines = "{0}=={1}{2} \\\n".format(req, data["version"], maybe_restriction)
padding = " " * 4
for i, release in enumerate(sorted(data["hashes"], key=lambda r: r["hash"])):
for i, release in enumerate(data["hashes"]):
new_lines += "{0}--hash={1}:{2}".format(padding, algorithm, release["hash"])
if i != len(data["hashes"]) - 1:
new_lines += " \\"
Expand Down Expand Up @@ -707,8 +707,9 @@ def get_package_hashes(
else:
raise PackageError("No releases could be found for {0}".format(version))

hashes = list(
get_releases_hashes(releases=releases, algorithm=algorithm, verbose=verbose)
hashes = sorted(
get_releases_hashes(releases=releases, algorithm=algorithm, verbose=verbose),
key=lambda r: r["hash"],
)
return {"package": package, "version": version, "hashes": hashes}

Expand Down
46 changes: 44 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2370,10 +2370,10 @@ def mocked_get(url, **options):
"version": "0.10",
"hashes": [
{
"hash": "45d1c5d2237a3b4f78b4198709fb2ecf1f781c8234ce3d94356f2100a36739433952c6c13b2843952f608949e6baa9f95055a314487cd8fb3f9d76522d8edb50"
"hash": "0d63bf4c115154781846ecf573049324f06b021a1d4b92da4fae2bf491da2b83a13096b14d73e73cefad36855f4fa936bac4b2357dabf05a2b1e7329ff1e5455"
},
{
"hash": "0d63bf4c115154781846ecf573049324f06b021a1d4b92da4fae2bf491da2b83a13096b14d73e73cefad36855f4fa936bac4b2357dabf05a2b1e7329ff1e5455"
"hash": "45d1c5d2237a3b4f78b4198709fb2ecf1f781c8234ce3d94356f2100a36739433952c6c13b2843952f608949e6baa9f95055a314487cd8fb3f9d76522d8edb50"
},
{
"hash": "c32e6d9fb09dc36ab9222c4606a1f43a2dcc183a8c64bdd9199421ef779072c174fa044b155babb12860cf000e36bc4d358694fa22420c997b1dd75b623d4daa"
Expand Down Expand Up @@ -2442,6 +2442,48 @@ def mocked_get(url, **options):
hashin.get_package_hashes(package="uggamugga")


def test_get_package_hashes_consistant_order(murlopen):
def mocked_get(url, **options):
if url == "https://pypi.org/pypi/hashin/json":
return _Response(
{
"info": {"version": "0.10", "name": "hashin"},
"releases": {
"0.10": [
{
"url": "https://pypi.org/packages/3.3/p/hashin/hashin-0.10-py3-none-any.whl",
"digests": {"sha256": "bbbbb"},
},
{
"url": "https://pypi.org/packages/source/p/hashin/hashin-0.10.tar.gz",
"digests": {"sha256": "ccccc"},
},
{
"url": "https://pypi.org/packages/2.7/p/hashin/hashin-0.10-py2-none-any.whl",
"digests": {"sha256": "aaaaa"},
},
]
},
}
)

raise NotImplementedError(url)

murlopen.side_effect = mocked_get

result = hashin.get_package_hashes(
package="hashin", version="0.10", algorithm="sha256"
)

expected = {
"package": "hashin",
"version": "0.10",
"hashes": [{"hash": "aaaaa"}, {"hash": "bbbbb"}, {"hash": "ccccc"}],
}

assert result == expected


def test_with_extras_syntax(murlopen, tmpfile):
"""When you want to add the hashes of a package by using the
"extras notation". E.g `requests[security]`.
Expand Down