-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Pick up hashes from contraints files and intersect #8839
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
New resolver: Pick up hash declarations in constraints files and use them to | ||
filter available distributions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
New resolver: If a package appears multiple times in user specification with | ||
different ``--hash`` options, only hashes that present in all specifications | ||
should be allowed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import collections | ||
import hashlib | ||
|
||
import pytest | ||
|
||
from pip._internal.utils.urls import path_to_url | ||
from tests.lib import ( | ||
create_basic_sdist_for_package, | ||
create_basic_wheel_for_package, | ||
) | ||
|
||
_FindLinks = collections.namedtuple( | ||
"_FindLinks", "index_html sdist_hash wheel_hash", | ||
) | ||
|
||
|
||
def _create_find_links(script): | ||
sdist_path = create_basic_sdist_for_package(script, "base", "0.1.0") | ||
wheel_path = create_basic_wheel_for_package(script, "base", "0.1.0") | ||
|
||
sdist_hash = hashlib.sha256(sdist_path.read_bytes()).hexdigest() | ||
wheel_hash = hashlib.sha256(wheel_path.read_bytes()).hexdigest() | ||
|
||
index_html = script.scratch_path / "index.html" | ||
index_html.write_text( | ||
""" | ||
<a href="{sdist_url}#sha256={sdist_hash}">{sdist_path.stem}</a> | ||
<a href="{wheel_url}#sha256={wheel_hash}">{wheel_path.stem}</a> | ||
""".format( | ||
sdist_url=path_to_url(sdist_path), | ||
sdist_hash=sdist_hash, | ||
sdist_path=sdist_path, | ||
wheel_url=path_to_url(wheel_path), | ||
wheel_hash=wheel_hash, | ||
wheel_path=wheel_path, | ||
) | ||
) | ||
|
||
return _FindLinks(index_html, sdist_hash, wheel_hash) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"requirements_template, message", | ||
[ | ||
( | ||
""" | ||
base==0.1.0 --hash=sha256:{sdist_hash} --hash=sha256:{wheel_hash} | ||
base==0.1.0 --hash=sha256:{sdist_hash} --hash=sha256:{wheel_hash} | ||
""", | ||
"Checked 2 links for project {name!r} against 2 hashes " | ||
"(2 matches, 0 no digest): discarding no candidates", | ||
), | ||
( | ||
# Different hash lists are intersected. | ||
""" | ||
base==0.1.0 --hash=sha256:{sdist_hash} --hash=sha256:{wheel_hash} | ||
base==0.1.0 --hash=sha256:{sdist_hash} | ||
""", | ||
"Checked 2 links for project {name!r} against 1 hashes " | ||
"(1 matches, 0 no digest): discarding 1 non-matches", | ||
), | ||
], | ||
ids=["identical", "intersect"], | ||
) | ||
def test_new_resolver_hash_intersect(script, requirements_template, message): | ||
find_links = _create_find_links(script) | ||
|
||
requirements_txt = script.scratch_path / "requirements.txt" | ||
requirements_txt.write_text( | ||
requirements_template.format( | ||
sdist_hash=find_links.sdist_hash, | ||
wheel_hash=find_links.wheel_hash, | ||
), | ||
) | ||
|
||
result = script.pip( | ||
"install", | ||
"--use-feature=2020-resolver", | ||
"--no-cache-dir", | ||
"--no-deps", | ||
"--no-index", | ||
"--find-links", find_links.index_html, | ||
"--verbose", | ||
"--requirement", requirements_txt, | ||
) | ||
|
||
assert message.format(name=u"base") in result.stdout, str(result) | ||
|
||
|
||
def test_new_resolver_hash_intersect_from_constraint(script): | ||
find_links = _create_find_links(script) | ||
|
||
constraints_txt = script.scratch_path / "constraints.txt" | ||
constraints_txt.write_text( | ||
"base==0.1.0 --hash=sha256:{sdist_hash}".format( | ||
sdist_hash=find_links.sdist_hash, | ||
), | ||
) | ||
requirements_txt = script.scratch_path / "requirements.txt" | ||
requirements_txt.write_text( | ||
""" | ||
base==0.1.0 --hash=sha256:{sdist_hash} --hash=sha256:{wheel_hash} | ||
""".format( | ||
sdist_hash=find_links.sdist_hash, | ||
wheel_hash=find_links.wheel_hash, | ||
), | ||
) | ||
|
||
result = script.pip( | ||
"install", | ||
"--use-feature=2020-resolver", | ||
"--no-cache-dir", | ||
"--no-deps", | ||
"--no-index", | ||
"--find-links", find_links.index_html, | ||
"--verbose", | ||
"--constraint", constraints_txt, | ||
"--requirement", requirements_txt, | ||
) | ||
|
||
message = ( | ||
"Checked 2 links for project {name!r} against 1 hashes " | ||
"(1 matches, 0 no digest): discarding 1 non-matches" | ||
).format(name=u"base") | ||
assert message in result.stdout, str(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a case of the intersection being empty, to ensure we test the error message as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean a test for something like this?
The error you get here is unrelated to the intersection logic. This input is rejected straight on parsing since the requirements parser checks every requirement line in the file contains at least one hash. This behaviour definitely needs to be tested (I’m not sure if it is currently), but not here IMO. It is a part of the requirements file parser, not the resolver.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant something like
The intersection of these hashes would be empty, which is what I'm suggesting it might be worthwhile to test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, that makes sense. Will update soon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, so
pip install
fails as expected, but the error message seems suboptimal. Since the two lines are two separate requirements, the error users see is(The hashes in Expected and Got flip if you flip the requirement lines.)
But improving the error message seems out of scope of this fix (and I’m not even sure how to improve it). I will just test the errors as-is for now and open a new issue.