-
-
Notifications
You must be signed in to change notification settings - Fork 636
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[internal] better align test JVM lockfile support with pytest concepts (
#15804) The JVM test lockfile support had a significant limitation where only one `jvm_lockfile` fixture can be passed into a test function. This is because of a limitation with pytest marks where a mark can only be applied to a test function and not to fixture functions. Thus, the `jvm_lockfile` fixture was essentially a singleton because it was not possible to declare separate fixtures which were marked individually with the `pytest.mark.jvm_lockfile`. This PR rewrites the test lockfile support to operate like regular pytest fixture functions. The main caveat is that the test lockfile definition must be placed in a separate fixture function to allow extracting it easily. Also, the file containing a definition must use `from __future__ import annotations` so that the type annotations are available to the extractor. With this PR, there is no longer any need to register `JvmLockfilePlugin` in a `conftest.py file`, which further simplifies using the test lockfile support. Moreover, it allows using multiple lockfiles in the `test_compile_with_multiple_scala_versions` test which was not possible without this PR. [ci skip-rust] [ci skip-build-wheels]
- Loading branch information
Tom Dyas
authored
Jun 16, 2022
1 parent
1c65b9c
commit 07963b5
Showing
14 changed files
with
417 additions
and
299 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
pants-plugins/internal_plugins/test_lockfile_fixtures/collect_fixtures.py
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,81 @@ | ||
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
import inspect | ||
import json | ||
import sys | ||
from pathlib import Path | ||
|
||
import pytest | ||
from _pytest import fixtures | ||
from _pytest.compat import get_real_func | ||
|
||
|
||
def get_func_path(func): | ||
real_func = get_real_func(func) | ||
return inspect.getfile(real_func) | ||
|
||
|
||
def get_fixturedef(fixture_request, name): | ||
fixturedef = fixture_request._fixture_defs.get(name) | ||
if fixturedef: | ||
return fixturedef | ||
try: | ||
return fixture_request._getnextfixturedef(name) | ||
except fixtures.FixtureLookupError: | ||
return None | ||
|
||
|
||
def process_fixtures(item): | ||
lockfile_definitions = [] | ||
fixture_request = fixtures.FixtureRequest(item, _ispytest=True) | ||
for fixture_name in fixture_request.fixturenames: | ||
fixture_def = get_fixturedef(fixture_request, fixture_name) | ||
if not fixture_def: | ||
continue | ||
|
||
func = fixture_def.func | ||
annotations = getattr(func, "__annotations__") | ||
if not annotations or annotations.get("return") != "JVMLockfileFixtureDefinition": | ||
continue | ||
|
||
# Note: We just invoke the fixture_def function assuming it takes no arguments. The other two | ||
# ways of invoking for the fixture value cause errors. I have left them here commented-out as an example | ||
# of what failed: | ||
# lockfile_definition = fixture_request.getfixturevalue(fixture_name) | ||
# lockfile_definition = fixture_def.execute(request=request) | ||
lockfile_definition = func() | ||
if lockfile_definition.__class__.__name__ != "JVMLockfileFixtureDefinition": | ||
continue | ||
|
||
cwd = Path.cwd() | ||
func_path = Path(get_func_path(func)).relative_to(cwd) | ||
lockfile_definitions.append( | ||
{ | ||
"lockfile_rel_path": str(lockfile_definition.lockfile_rel_path), | ||
"requirements": [c.to_coord_str() for c in lockfile_definition.requirements], | ||
"test_file_path": str(func_path), | ||
} | ||
) | ||
return lockfile_definitions | ||
|
||
|
||
class CollectionPlugin: | ||
def __init__(self): | ||
self.collected = [] | ||
|
||
def pytest_collection_modifyitems(self, items): | ||
for item in items: | ||
self.collected.append(item) | ||
|
||
|
||
collection_plugin = CollectionPlugin() | ||
pytest.main(["--setup-only", *sys.argv[1:]], plugins=[collection_plugin]) | ||
|
||
output = [] | ||
cwd = Path.cwd() | ||
|
||
for item in collection_plugin.collected: | ||
output.extend(process_fixtures(item)) | ||
|
||
with open("tests.json", "w") as f: | ||
f.write(json.dumps(output)) |
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
Oops, something went wrong.