Skip to content

Commit c65e843

Browse files
committed
Add a conftest target type
# Rust tests and lints will be skipped. Delete if not intended. [ci skip-rust] # Building wheels and fs_util will be skipped. Delete if not intended. [ci skip-build-wheels]
1 parent ed40678 commit c65e843

3 files changed

Lines changed: 34 additions & 8 deletions

File tree

src/python/pants/backend/python/dependency_inference/rules_test.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
)
1111
from pants.backend.python.dependency_inference.rules import rules as dependency_inference_rules
1212
from pants.backend.python.target_types import (
13+
ConftestTarget,
1314
PythonLibrary,
1415
PythonRequirementLibrary,
1516
PythonSources,
@@ -47,7 +48,7 @@ def rules(cls):
4748

4849
@classmethod
4950
def target_types(cls):
50-
return [PythonLibrary, PythonRequirementLibrary, PythonTests]
51+
return [ConftestTarget, PythonLibrary, PythonRequirementLibrary, PythonTests]
5152

5253
def test_infer_python_imports(self) -> None:
5354
self.add_to_build_file(
@@ -173,15 +174,15 @@ def test_infer_python_conftests(self) -> None:
173174
)
174175

175176
self.create_file("src/python/root/conftest.py")
176-
self.add_to_build_file("src/python/root", "python_library()")
177+
self.add_to_build_file("src/python/root", "conftest()")
177178

178179
self.create_file("src/python/root/mid/conftest.py")
179-
self.add_to_build_file("src/python/root/mid", "python_library()")
180+
self.add_to_build_file("src/python/root/mid", "conftest()")
180181

181182
self.create_file("src/python/root/mid/leaf/conftest.py")
182183
self.create_file("src/python/root/mid/leaf/this_is_a_test.py")
183184
self.add_to_build_file(
184-
"src/python/root/mid/leaf", "python_tests()\npython_library(name='conftest')"
185+
"src/python/root/mid/leaf", "python_tests()\nconftest(name='conftest')"
185186
)
186187

187188
def run_dep_inference(address: Address) -> InferredDependencies:

src/python/pants/backend/python/rules/pytest_runner_integration_test.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
from pants.backend.python.rules import pex, pex_from_targets, pytest_runner, python_sources
1212
from pants.backend.python.rules.coverage import create_coverage_config
1313
from pants.backend.python.rules.pytest_runner import PythonTestFieldSet
14-
from pants.backend.python.target_types import PythonLibrary, PythonRequirementLibrary, PythonTests
14+
from pants.backend.python.target_types import (
15+
ConftestTarget,
16+
PythonLibrary,
17+
PythonRequirementLibrary,
18+
PythonTests,
19+
)
1520
from pants.build_graph.build_file_aliases import BuildFileAliases
1621
from pants.core.goals.test import Status, TestDebugRequest, TestResult
1722
from pants.core.util_rules import source_files, stripped_source_files
@@ -112,7 +117,7 @@ def alias_groups(cls) -> BuildFileAliases:
112117

113118
@classmethod
114119
def target_types(cls):
115-
return [PythonLibrary, PythonTests, PythonRequirementLibrary]
120+
return [ConftestTarget, PythonLibrary, PythonTests, PythonRequirementLibrary]
116121

117122
@classmethod
118123
def rules(cls):
@@ -369,7 +374,7 @@ def test_conftest_injection(self) -> None:
369374
relpath=PurePath(self.source_root, self.conftest_source.path).as_posix(),
370375
contents=self.conftest_source.content.decode(),
371376
)
372-
self.add_to_build_file(self.source_root, "python_library()")
377+
self.add_to_build_file(self.source_root, "conftest()")
373378

374379
result = self.run_pytest(passthrough_args="-s")
375380
assert result.status == Status.SUCCESS

src/python/pants/backend/python/target_types.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ class PythonTests(Target):
300300

301301

302302
class PythonLibrarySources(PythonSources):
303-
default = ("*.py",) + tuple(f"!{pat}" for pat in PythonTestsSources.default)
303+
default = ("*.py", "!conftest.py") + tuple(f"!{pat}" for pat in PythonTestsSources.default)
304304

305305

306306
class PythonLibrary(Target):
@@ -310,6 +310,26 @@ class PythonLibrary(Target):
310310
core_fields = (*COMMON_PYTHON_FIELDS, PythonLibrarySources)
311311

312312

313+
# -----------------------------------------------------------------------------------------------
314+
# `conftest` target
315+
# -----------------------------------------------------------------------------------------------
316+
317+
318+
class ConftestSources(PythonLibrarySources):
319+
default = ("conftest.py",)
320+
321+
322+
class ConftestTarget(Target):
323+
"""A `conftest.py` file used by Pytest-style tests.
324+
325+
A `conftest` target behaves the same as a `python_library` target, and is sugar for
326+
`python_library(sources=['conftest.py']`.
327+
"""
328+
329+
alias = "conftest"
330+
core_fields = (*COMMON_PYTHON_FIELDS, ConftestSources)
331+
332+
313333
# -----------------------------------------------------------------------------------------------
314334
# `python_requirement_library` target
315335
# -----------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)