-
-
Notifications
You must be signed in to change notification settings - Fork 764
Use python imports to identify fixtures (part 1/6) #5699
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
Conversation
st2common.content.utils.get_relative_path_to_pack_file assumes that a relative path is already relative to the pack dir. So, make sure we are passing an absolute path to tests.
07fbf46
to
cba8437
Compare
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.
There is a lot of boilerplate in this PR - moving fixture path calculation from tests into the fixtures themselves - but it should be fairly easy to review as that boilerplate is very repetitive.
def get_fixture_name_and_path(fixture_file): | ||
pack_name = os.path.basename(os.path.dirname(fixture_file)) | ||
pack_path = os.path.join(get_fixtures_packs_base_path(), pack_name) | ||
return pack_name, pack_path |
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 is the primary fixture loader util - start by reviewing this and the other utils in this file.
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.
Most fixtures will do something like this to use this util:
from st2tests import fixturesloader
PACK_NAME, PACK_PATH = fixturesloader.get_fixture_name_and_path(__file__)
But the interface could be simplified to avoid passing __file__
if we change the get_fixture_name_and_path()
util by using inspect
like this:
import inspect
from types import FrameType, ModuleType
def get_fixture_name_and_path():
currentframe = inspect.currentframe()
assert isinstance(currentframe, FrameType)
caller_frame = currentframe.f_back
caller_module = inspect.getmodule(caller_frame)
assert isinstance(caller_module, ModuleType)
pack_name = caller_module.__package__.split(".")[-1]
pack_path = os.path.abspath(os.path.dirname(caller_module.__file__))
return pack_name, pack_path
Thoughts? Do we want to pass __file__
as an arg or discover it with inspect
?
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 think passing file makes it more easier to understand what this piece of code is doing. inspect
method here seems rather complicated.
from st2tests import fixturesloader | ||
|
||
PACK_NAME, PACK_PATH = fixturesloader.get_fixture_name_and_path(__file__) |
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 is an example of the boilerplate added to each fixture pack.
@@ -23,6 +23,9 @@ | |||
from st2common.util.shell import run_command | |||
from st2tests import config as test_config | |||
from st2tests.fixturesloader import get_fixtures_packs_base_path | |||
from st2tests.fixtures.packs.dummy_pack_1.fixture import PACK_PATH as DUMMY_PACK_1_PATH |
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.
Here is an import that would show pants that this test file uses the dummy_pack_1
fixture.
@@ -43,7 +46,7 @@ def setUp(self): | |||
test_config.parse_args() | |||
|
|||
def test_register_from_pack_success(self): | |||
pack_dir = os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1") |
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.
In most tests, we actually calculate the fixture path every time it's used. By adding the boilerplate inside each fixture, that calculation only needs to be done in one place (DRY). Instead, we just need to use the imported DUMMY_PACK_1_PATH
constant here.
This line gets simplified to be:
pack_dir = DUMMY_PACK_1_PATH
@@ -30,11 +30,10 @@ | |||
from st2common.services import policies as policy_service | |||
|
|||
import st2tests | |||
from st2tests.fixtures.generic.fixture import PACK_NAME as PACK |
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.
Many tests already used a constant to define the PACK name, so all I had to do was import the constant from the fixture instead of hard-coding it in the test files.
In order for pants to detect which changes need to be re-run after changing fixtures, pants needs to be able to infer the relevant dependencies. We could use a super-level target to depend on everything, but then we lose the fine-grained invalidation. The only way to tell pants that a test file is using a particular set of fixtures is via python imports. So, this adds some boilerplate python in every pack dir: __init__.py fixture.py We cannot use pack.yaml as a source root marker any more or importing these files fails. We'll have to implement something else for the main packs under contrib/.
e9aba2f
to
a136ed7
Compare
a136ed7
to
3b9717e
Compare
def get_fixture_name_and_path(fixture_file): | ||
pack_name = os.path.basename(os.path.dirname(fixture_file)) | ||
pack_path = os.path.join(get_fixtures_packs_base_path(), pack_name) | ||
return pack_name, pack_path |
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 think passing file makes it more easier to understand what this piece of code is doing. inspect
method here seems rather complicated.
Background
I'm working towards introducing
pants
. Eventually I would like to use pants to run tests to take advantage of the fine-grained per-file caching of results that accounts for dependencies by python files (pants infers the deps by reading the python files).In order to use the fine-grained caching, Pants needs to know which tests rely on which fixtures. We could add extra metadata to tie the tests to the fixtures, but that would be an additional maintenance burden that will become out-of-date. We can also include all fixtures for all tests, but then we don't benefit from the fine-grained per-file caching. However, pants can already infer dependencies based on python imports, so that is what this PR (and several follow-up PRs) takes advantage of.
Overview
This PR does the following:
st2tests/st2tests/fixturesloader.py
so that we can use those utilities to identify every fixture.__init__.py
.fixture.py
file in each fixture that uses the fixturesloader utils (where helpful) to identify itself withPATH
andNAME
vars.PATH
and/orNAME
vars from that fixture.This PR focuses on only 2 fixture packs:
dummy_pack_1
anddummy_pack_10
.Follow-up PRs will address other fixture packs and other sets of fixtures. I will submit those PRs after this one is merged.
Follow-up PRS: #5702 #5703 #5704 #5705 #5706
I'm leaving a comment on one of the
fixturesloader.py
utils, as we could simplify the interface by usinginspect
. I'd like feedback on preferences about how much magic we're okay with here.Commits