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

Use python imports to identify fixtures (part 4/6) #5704

Merged
merged 4 commits into from
Aug 17, 2022

Conversation

cognifloyd
Copy link
Member

@cognifloyd cognifloyd commented Aug 11, 2022

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:

  • turn every fixture into an importable python module with an __init__.py.
  • add a fixture.py file in each fixture that uses the fixturesloader utils (where helpful) to identify itself with PATH and NAME vars.
  • also add a DIR_NAME var for the fixture packs that use a name that differs from the dir name
  • in every test that uses a given fixture, import PATH, NAME and/or DIR_NAME vars from that fixture.

This PR focuses on only fixture packs that needed some custom bits in fixture.py like the DIR_NAME var.
Follow-up PRs will address other fixture packs and other sets of fixtures. I will submit those PRs after this one is merged.

Statistics

Changes Summary:

  • 1 line updated in changelog
  • 5 copies of fixture.py (~16 lines, 13 of which are copyright/license header): 18+17*3+16=+85 lines
  • 5 fixture.py files adjusted (improve vars for special pack fixtures): (-1 +2)*5=(-5 +10)
  • -47 +52 lines changed in test files to use the new fixture vars.

@cognifloyd cognifloyd added this to the 3.8.0 milestone Aug 11, 2022
@cognifloyd cognifloyd self-assigned this Aug 11, 2022
@pull-request-size pull-request-size bot added the size/L PR that changes 100-499 lines. Requires some effort to review. label Aug 11, 2022
@cognifloyd cognifloyd changed the base branch from fixtures-loading-3 to master August 11, 2022 17:23
@pull-request-size pull-request-size bot added size/XXL PR that changes 1000+ lines. You should absolutely split your PR into several. and removed size/L PR that changes 100-499 lines. Requires some effort to review. labels Aug 11, 2022
@cognifloyd cognifloyd force-pushed the fixtures-loading-4 branch 3 times, most recently from 769d0c8 to e7135b0 Compare August 11, 2022 18:55
@cognifloyd cognifloyd changed the base branch from master to fixtures-loading-3 August 11, 2022 19:06
@pull-request-size pull-request-size bot added size/L PR that changes 100-499 lines. Requires some effort to review. and removed size/XXL PR that changes 1000+ lines. You should absolutely split your PR into several. labels Aug 11, 2022
@cognifloyd cognifloyd changed the base branch from fixtures-loading-3 to master August 11, 2022 19:53
@pull-request-size pull-request-size bot added size/XXL PR that changes 1000+ lines. You should absolutely split your PR into several. and removed size/L PR that changes 100-499 lines. Requires some effort to review. labels Aug 11, 2022
@cognifloyd cognifloyd changed the base branch from master to fixtures-loading-3 August 11, 2022 20:40
@pull-request-size pull-request-size bot added size/L PR that changes 100-499 lines. Requires some effort to review. and removed size/XXL PR that changes 1000+ lines. You should absolutely split your PR into several. labels Aug 11, 2022
@cognifloyd cognifloyd changed the title Use python imports to identify fixtures (part 4) Use python imports to identify fixtures (part 4/6) Aug 11, 2022
@cognifloyd cognifloyd requested a review from rush-skills August 12, 2022 15:48
Copy link
Member

@rush-skills rush-skills left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Overall, had a minor question below

self.assertEqual(len(resp.json), 1)
self.assertEqual(resp.json[0]["file_path"], "pack.yaml")
self.assertEqual(len(resp.json), 3)
self.assertIn("pack.yaml", [f["file_path"] for f in resp.json])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pack in question only had pack.yaml before this PR. I added fixture.py and __init__.py, so the file count goes up to 3.
And, since there is more than one file, we can't just assume that the resp.json[0] is the pack.yaml file. But is it [1] or [2]? It's hard to say because the order might depend on the the filesystem. I tried resp.json[2] which on GHA was not pack.yaml, so they are not sorted alphabetically. To avoid a flaky test relying on a particular sort, I made a list of the files with [f["file_path"] for f in resp.json], and just assert that pack.yaml is in it.

Base automatically changed from fixtures-loading-3 to master August 17, 2022 16:31
@cognifloyd cognifloyd marked this pull request as ready for review August 17, 2022 16:33
@cognifloyd cognifloyd requested a review from a team August 17, 2022 16:34
Comment on lines +16 to +18
PACK_NAME = "test_content_version"
_, PACK_PATH = fixturesloader.get_fixture_name_and_path(__file__)
PACK_PATH = PACK_PATH[: -len("_fixture")]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test_content_version fixture is in a git submodule. I wanted to keep all the fixture details in this repo so that they're available even if someone has not cloned the submodule. So, I put the fixture vars in test_content_version_fixture directory and adjusted the vars.

Copy link
Member Author

@cognifloyd cognifloyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are the sources for many of the pack fixture changes. (FYI - for anyone needs to reference this in the future)

Comment on lines +16 to +17
PACK_NAME = "dummy_pack_10_wrong_deps"
PACK_DIR_NAME, PACK_PATH = fixturesloader.get_fixture_name_and_path(__file__)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name : dummy_pack_10_wrong_deps

Comment on lines +16 to +17
PACK_NAME = "dummy pack 14"
PACK_DIR_NAME, PACK_PATH = fixturesloader.get_fixture_name_and_path(__file__)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# limitations under the License.
from st2tests import fixturesloader

PACK_NAME, PACK_PATH = fixturesloader.get_fixture_name_and_path(__file__)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one did not need special vars. Instead, it needed special handling in one of the tests that counted the number of files in the pack.

Comment on lines +16 to +17
PACK_NAME = "Dummy Pack 18"
PACK_DIR_NAME, PACK_PATH = fixturesloader.get_fixture_name_and_path(__file__)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +16 to +17
PACK_NAME = "dummy_pack_7_name"
PACK_DIR_NAME, PACK_PATH = fixturesloader.get_fixture_name_and_path(__file__)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +16 to +17
PACK_NAME = "Dummy Pack 8 Invalid Name"
PACK_DIR_NAME, PACK_PATH = fixturesloader.get_fixture_name_and_path(__file__)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name : Dummy Pack 8 Invalid Name

Comment on lines +16 to +17
PACK_NAME = "dummy_pack_9_deps"
PACK_DIR_NAME, PACK_PATH = fixturesloader.get_fixture_name_and_path(__file__)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +16 to +17
PACK_NAME = "pack_name_not_the_same_as_dir_name"
PACK_DIR_NAME, PACK_PATH = fixturesloader.get_fixture_name_and_path(__file__)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ref: pack_name_not_the_same_as_dir_name
name: pack_name_not_the_same_as_dir_name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement maintenance pantsbuild refactor size/L PR that changes 100-499 lines. Requires some effort to review. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants