-
Notifications
You must be signed in to change notification settings - Fork 579
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
backport py3 testing enhancements #239
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d7f1c23
tests: show found number of features when unexpected
williballenthin c032d55
tests: freeze: make py3 compatible
williballenthin 1873d0b
*: py3 compat
williballenthin 0af6386
tests: fixtures: add ctxmgr for catching xfail
williballenthin 295d3fe
tests: limit tests to py2/py3
williballenthin 6cd2931
ci: test on both py2 and py3
williballenthin 89edaf4
tests: xfail things that won't work on py3
williballenthin b084f7c
pep8
williballenthin 385c956
fixtures: fix doc
williballenthin 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
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 |
---|---|---|
|
@@ -7,7 +7,9 @@ | |
# See the License for the specific language governing permissions and limitations under the License. | ||
|
||
import os | ||
import sys | ||
import os.path | ||
import contextlib | ||
import collections | ||
|
||
import pytest | ||
|
@@ -27,6 +29,44 @@ | |
CD = os.path.dirname(__file__) | ||
|
||
|
||
@contextlib.contextmanager | ||
def xfail(condition, reason=None): | ||
""" | ||
context manager that wraps a block that is expected to fail in some cases. | ||
when it does fail (and is expected), then mark this as pytest.xfail. | ||
if its unexpected, raise an exception, so the test fails. | ||
|
||
example:: | ||
|
||
# this test: | ||
# - passes on py3 if foo() works | ||
# - fails on py3 if foo() fails | ||
# - xfails on py2 if foo() fails | ||
# - fails on py2 if foo() works | ||
with xfail(sys.version_info < (3, 0), reason="py3 doesn't foo"): | ||
foo() | ||
""" | ||
try: | ||
# do the block | ||
yield | ||
except: | ||
if condition: | ||
# we expected the test to fail, so raise and register this via pytest | ||
pytest.xfail(reason) | ||
else: | ||
# we don't expect an exception, so the test should fail | ||
raise | ||
else: | ||
if not condition: | ||
# here we expect the block to run successfully, | ||
# and we've received no exception, | ||
# so this is good | ||
pass | ||
else: | ||
# we expected an exception, but didn't find one. that's an error. | ||
raise RuntimeError("expected to fail, but didn't") | ||
|
||
|
||
@lru_cache() | ||
def get_viv_extractor(path): | ||
import capa.features.extractors.viv | ||
|
@@ -376,14 +416,20 @@ def do_test_feature_presence(get_extractor, sample, scope, feature, expected): | |
def do_test_feature_count(get_extractor, sample, scope, feature, expected): | ||
extractor = get_extractor(sample) | ||
features = scope(extractor) | ||
msg = "%s should be found %d times in %s" % (str(feature), expected, scope.__name__) | ||
msg = "%s should be found %d times in %s, found: %d" % ( | ||
str(feature), | ||
expected, | ||
scope.__name__, | ||
len(features[feature]), | ||
) | ||
assert len(features[feature]) == expected, msg | ||
|
||
|
||
def get_extractor(path): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we might want to merge this routine with |
||
# decide here which extractor to load for tests. | ||
# maybe check which python version we've loaded or if we're in IDA. | ||
extractor = get_viv_extractor(path) | ||
if sys.version_info >= (3, 0): | ||
raise RuntimeError("no supported py3 backends yet") | ||
else: | ||
extractor = get_viv_extractor(path) | ||
|
||
# overload the extractor so that the fixture exposes `extractor.path` | ||
setattr(extractor, "path", path) | ||
|
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
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.
This could be done with a matrix for the python version instead of duplicating the code. But as we want to get rid of Python 2 in the near future, I guess this won't have a long life anyway... 😄
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.
oh, good point. though, then the linting would happen twice?
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.
@williballenthin
no, I meant changing
tests
to the following (instead of duplicatingtests
intotests27
andtest38
:I can send a PR as this can be useful if we want to test several python versions.