-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Adds new backup_attributes
context manager for tests
#1948
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
import sys | ||
import pytest | ||
import mock | ||
from util import backup_attributes | ||
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. I'm not sure if the name you give to the file can cause troubles in some import situations...since we already have a file with the same name in 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. Yes it also bugged me. Actually I usually call it 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.
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. What about 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. @Jonast, I would avoid Plus the mentioned file is not a test...it's a set of utilities to help us testing...I think that it's confusing... 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.
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. That looks better 😉 |
||
from pythonforandroid.recipe import Recipe | ||
from pythonforandroid.toolchain import ToolchainCL | ||
from pythonforandroid.util import BuildInterruptingException | ||
|
@@ -121,7 +122,10 @@ def test_recipes(self): | |
Checks the `recipes` command prints out recipes information without crashing. | ||
""" | ||
argv = ['toolchain.py', 'recipes'] | ||
with patch_sys_argv(argv), patch_sys_stdout() as m_stdout: | ||
with ( | ||
patch_sys_argv(argv)), ( | ||
patch_sys_stdout()) as m_stdout, ( | ||
backup_attributes(Recipe, {'recipes'})): | ||
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. in Python3 that could also be used as a decorator, e.g.
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. I'm wondering, will this work when used alongside with 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. Depends of what you patch and if you patch before the 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. Ok, we'll see then, thanks! |
||
ToolchainCL() | ||
# check if we have common patterns in the output | ||
expected_strings = ( | ||
|
@@ -134,5 +138,3 @@ def test_recipes(self): | |
) | ||
for expected_string in expected_strings: | ||
assert expected_string in m_stdout.getvalue() | ||
# deletes static attribute to not mess with other tests | ||
del Recipe.recipes |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from copy import deepcopy | ||
from contextlib import contextmanager | ||
|
||
|
||
@contextmanager | ||
def backup_attributes(obj, attributes): | ||
""" | ||
Makes a backup of the object attributes that gets restored later. | ||
""" | ||
obj_dict = obj.__dict__ | ||
# creates a subset dictionary of the attributes we're interested in | ||
attributes_backup = dict( | ||
(k, obj_dict[k]) for k in attributes if k in obj_dict) | ||
attributes_backup = deepcopy(attributes_backup) | ||
try: | ||
yield | ||
finally: | ||
for attribute in attributes: | ||
setattr(obj, attribute, attributes_backup[attribute]) |
Uh oh!
There was an error while loading. Please reload this page.