-
-
Notifications
You must be signed in to change notification settings - Fork 707
tests: implement pytest_test rule for internal test usage #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
0dba5a1
2b61b33
f4b84df
814b1f1
fbfc144
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,22 @@ | ||
| load("//tests/support:support.bzl", "SUPPORTS_BZLMOD") | ||
| load("//tests/support/pytest_test:pytest_test.bzl", "pytest_test") | ||
|
|
||
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| load("@bazel_skylib//:bzl_library.bzl", "bzl_library") | ||
| load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility | ||
|
|
||
| package(default_visibility = ["//:__subpackages__"]) | ||
|
|
||
| filegroup( | ||
| name = "bootstrap_template", | ||
| srcs = ["pytest_bootstrap_template.py"], | ||
| ) | ||
|
|
||
| # keep | ||
| bzl_library( | ||
| name = "pytest_test", | ||
| srcs = ["pytest_test.bzl"], | ||
| deps = [ | ||
| "//python:py_test", | ||
| ], | ||
| ) | ||
|
rickeylev marked this conversation as resolved.
|
||
|
|
||
| # These aliases are used to avoid duplicate targets in the deps list | ||
| alias( | ||
| name = "default_pytest", | ||
| actual = "@pypi//pytest" if BZLMOD_ENABLED else "//python/private:empty", | ||
| ) | ||
|
|
||
| # These aliases are used to avoid duplicate targets in the deps list | ||
| alias( | ||
| name = "default_pytest_bazel", | ||
| actual = "@pypi//pytest_bazel" if BZLMOD_ENABLED else "//python/private:empty", | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import sys | ||
|
|
||
| import pytest_bazel | ||
|
|
||
| TEST_FILES = """%TEST_FILES%""".splitlines() | ||
|
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. I still don't understand why this is necessary, because |
||
|
|
||
| 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,74 @@ | ||||||
| """pytest_test rule implementation.""" | ||||||
|
|
||||||
| load("//python:py_test.bzl", "py_test") | ||||||
|
|
||||||
| _DEFAULT_PYTEST = Label("//tests/support/pytest_test:default_pytest") | ||||||
| _DEFAULT_PYTEST_BAZEL = Label("//tests/support/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 _write_pytest_bootstrap_impl(ctx): | ||||||
| output = ctx.actions.declare_file(ctx.attr.output_name) | ||||||
| test_files = "\n".join([f.short_path for f in ctx.files.srcs]) | ||||||
|
|
||||||
| ctx.actions.expand_template( | ||||||
| output = output, | ||||||
| template = ctx.file._bootstrap_template, | ||||||
| substitutions = { | ||||||
| "%TEST_FILES%": test_files, | ||||||
| }, | ||||||
| ) | ||||||
| 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 = "//tests/support/pytest_test:bootstrap_template", | ||||||
| allow_single_file = True, | ||||||
| ), | ||||||
| }, | ||||||
| ) | ||||||
Uh oh!
There was an error while loading. Please reload this page.