-
-
Notifications
You must be signed in to change notification settings - Fork 705
feat: implement pytest_test rule #3931
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
base: main
Are you sure you want to change the base?
Changes from all commits
fd25b38
12a555e
66632db
ea63b9a
b0019c9
0016b1a
aafa133
e64e2a2
3348a42
efd4f4f
12e24f1
abdb8f8
798756f
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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| 9.x | ||
| 9.1.1 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,4 +17,6 @@ dependencies = [ | |
| "pyelftools", | ||
| "macholib", | ||
| "markupsafe", | ||
| "pytest", | ||
| "pytest-bazel", | ||
| ] | ||
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| (pytest) Added `pytest_test` macro to run pytest tests using `pytest-bazel` | ||
| from PyPI. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,27 @@ | ||||||||||||||||||||||||||||
| load("@bazel_skylib//:bzl_library.bzl", "bzl_library") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| package(default_visibility = ["//:__subpackages__"]) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| filegroup( | ||||||||||||||||||||||||||||
| name = "bootstrap_template", | ||||||||||||||||||||||||||||
| srcs = ["pytest_bootstrap_template.py"], | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| bzl_library( | ||||||||||||||||||||||||||||
| name = "pytest_test", | ||||||||||||||||||||||||||||
| deps = [ | ||||||||||||||||||||||||||||
| "//python:py_test", | ||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
Comment on lines
+10
to
+15
Contributor
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. The
Suggested change
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # These aliases are used to avoid duplicate targets in the deps list | ||||||||||||||||||||||||||||
| alias( | ||||||||||||||||||||||||||||
| name = "default_pytest", | ||||||||||||||||||||||||||||
| actual = "@pypi//pytest", | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # These aliases are used to avoid duplicate targets in the deps list | ||||||||||||||||||||||||||||
| alias( | ||||||||||||||||||||||||||||
| name = "default_pytest_bazel", | ||||||||||||||||||||||||||||
| actual = "@pypi//pytest_bazel", | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import sys | ||
|
|
||
| import pytest_bazel | ||
|
|
||
| TEST_FILES = """%TEST_FILES%""".splitlines() | ||
|
|
||
| args = sys.argv[1:] + TEST_FILES | ||
| sys.exit(pytest_bazel.main(args)) | ||
|
rickeylev marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,84 @@ | ||||||
| """pytest_test rule implementation.""" | ||||||
|
|
||||||
| load("//python:py_test.bzl", "py_test") | ||||||
|
|
||||||
| _DEFAULT_PYTEST = Label("//python/private/pytest_test:default_pytest") | ||||||
| _DEFAULT_PYTEST_BAZEL = Label("//python/private/pytest_test:default_pytest_bazel") | ||||||
|
|
||||||
| def pytest_test( | ||||||
| *, | ||||||
| name, | ||||||
| srcs, | ||||||
| pytest = None, | ||||||
| pytest_bazel = None, | ||||||
| **kwargs): | ||||||
| """Run pytest tests. | ||||||
|
|
||||||
| Args: | ||||||
| name: A unique name for this target. | ||||||
| srcs: List of source files (test files). These are the files that | ||||||
| pytest will run as tests. | ||||||
| pytest: The pytest target to use. Defaults to @pypi//pytest. | ||||||
| pytest_bazel: The pytest-bazel target to use. Defaults to | ||||||
| @pypi//pytest_bazel. | ||||||
| **kwargs: Additional arguments passed to py_test. Note that `main` is | ||||||
| not a supported argument. | ||||||
| """ | ||||||
| if pytest == None: | ||||||
| pytest = _DEFAULT_PYTEST | ||||||
| if pytest_bazel == None: | ||||||
| pytest_bazel = _DEFAULT_PYTEST_BAZEL | ||||||
|
|
||||||
| bootstrap_target = name + "_bootstrap" | ||||||
| main_file = name + "_boot.py" | ||||||
| _write_pytest_bootstrap( | ||||||
| name = bootstrap_target, | ||||||
| srcs = srcs, | ||||||
| output_name = main_file, | ||||||
| ) | ||||||
|
|
||||||
| py_test( | ||||||
| name = name, | ||||||
| main = main_file, | ||||||
| srcs = [bootstrap_target] + srcs, | ||||||
|
Contributor
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. If
Suggested change
Collaborator
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. The bootstrap is not needed, we can just use the |
||||||
| deps = kwargs.pop("deps", []) + [ | ||||||
| pytest, | ||||||
| pytest_bazel, | ||||||
| ], | ||||||
| **kwargs | ||||||
| ) | ||||||
|
|
||||||
| def _map_file(f): | ||||||
| return f.short_path | ||||||
|
|
||||||
| def _write_pytest_bootstrap_impl(ctx): | ||||||
| output = ctx.actions.declare_file(ctx.attr.output_name) | ||||||
|
|
||||||
| computed_subs = ctx.actions.template_dict() | ||||||
|
|
||||||
| computed_subs.add_joined( | ||||||
| "%TEST_FILES%", | ||||||
| depset(ctx.files.srcs), | ||||||
| join_with = "\n", | ||||||
| map_each = _map_file, | ||||||
| ) | ||||||
|
|
||||||
| ctx.actions.expand_template( | ||||||
| output = output, | ||||||
| template = ctx.file._bootstrap_template, | ||||||
| substitutions = {}, | ||||||
| computed_substitutions = computed_subs, | ||||||
| ) | ||||||
|
Comment on lines
+57
to
+71
Contributor
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. Since |
||||||
| return [DefaultInfo(files = depset([output]))] | ||||||
|
|
||||||
| _write_pytest_bootstrap = rule( | ||||||
| implementation = _write_pytest_bootstrap_impl, | ||||||
| attrs = { | ||||||
| "output_name": attr.string(mandatory = True), | ||||||
| "srcs": attr.label_list(allow_files = True), | ||||||
| "_bootstrap_template": attr.label( | ||||||
| default = "//python/private/pytest_test:bootstrap_template", | ||||||
| allow_single_file = True, | ||||||
| ), | ||||||
| }, | ||||||
| ) | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| """pytest_test rule re-export.""" | ||
|
|
||
| load("//python/private/pytest_test:pytest_test.bzl", _pytest_test = "pytest_test") | ||
|
|
||
| pytest_test = _pytest_test |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| load("//python:pytest_test.bzl", "pytest_test") | ||
| load("//tests/support:support.bzl", "SUPPORTS_BZLMOD") | ||
|
|
||
| pytest_test( | ||
| name = "pytest_script_venv_test", | ||
| srcs = [ | ||
| "basic_test.py", | ||
| ], | ||
| config_settings = { | ||
| "@rules_python//python/config_settings:bootstrap_impl": "script", | ||
| "@rules_python//python/config_settings:venvs_site_packages": "yes", | ||
| }, | ||
|
rickeylev marked this conversation as resolved.
|
||
| target_compatible_with = SUPPORTS_BZLMOD, | ||
| ) | ||
|
|
||
| pytest_test( | ||
| name = "pytest_default_test", | ||
| srcs = [ | ||
| "basic_test.py", | ||
| ], | ||
| target_compatible_with = SUPPORTS_BZLMOD, | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| def test_foo(): | ||
| assert True |
Uh oh!
There was an error while loading. Please reload this page.