Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/BUILD
Original file line numberDiff line numberDiff line change
Expand Up@@ -54,6 +54,7 @@ bzl_library(
name = "defs",
srcs = [
"//python:defs.bzl",
"//python/py_pytest_main:defs.bzl",
"//python/private:reexports.bzl",
],
deps = [":bazel_python_tools"],
Expand Down
19 changes: 19 additions & 0 deletions docs/python.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion examples/build_file_generation/BUILD
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,14 @@ load("@pip//:requirements.bzl", "all_whl_requirements")
load("@rules_python//gazelle:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
load("@rules_python//gazelle/manifest:defs.bzl", "gazelle_python_manifest")
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_pytest_main", "py_test")

compile_pip_requirements(
name = "pip_compile",
requirements_in = "requirements.txt",
requirements_txt = "requirements_lock.txt",
)

# This rule fetches the metadata for python packages we depend on. That data is
# required for the gazelle_python_manifest rule to update our manifest file.
Expand DownExpand Up@@ -54,3 +61,22 @@ py_binary(
visibility = ["//:__subpackages__"],
deps = [":build_file_generation"],
)

# Generates an entrypoint for pytest that is picked up by Gazelle for the py_test target below.
py_pytest_main(
name = "__test__",
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

How come py_pytest_main becomes a dependency to a py_test, instead of subsuming the py_test rule?

Wholly replacing the py_test rule with a macro/rule/rule+macro seems a cleaner user interface.

@mattemmattemJun 10, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This provides an entry point for pytest, the Python test runner, and doesn't force changing loads or existing rules, and keepspy_test the bazel rule a generic "run a python binary that will return 0 or 1". There are other launchers, perhaps a nose entrypoint etc.

This model also fits with the gazelle generation already present here.


py_test(
name = "build_file_generation_test",
srcs = [
"foo_test.py",
":__test__",
],
main = ":__test__.py",
deps = [
":__test__",
":build_file_generation",
"@pip_pytest//:pkg",
],
)
9 changes: 9 additions & 0 deletions examples/build_file_generation/WORKSPACE
Original file line numberDiff line numberDiff line change
Expand Up@@ -48,10 +48,19 @@ local_repository(
path = "../..",
)

load("@rules_python//python:repositories.bzl", "python_register_toolchains")

python_register_toolchains(
name = "python39",
python_version = "3.9",
)

load("@python39//:defs.bzl", "interpreter")
load("@rules_python//python:pip.bzl", "pip_parse")

pip_parse(
name = "pip",
python_interpreter_target = interpreter,
requirements_lock = "//:requirements_lock.txt",
)

Expand Down
4 changes: 4 additions & 0 deletions examples/build_file_generation/foo_test.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
import pytest

def test_add():
assert 1 + 1 == 2, "Expected 1 + 1 to equal 2"
182 changes: 181 additions & 1 deletion examples/build_file_generation/gazelle_python.yaml
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,6 +5,98 @@

manifest:
modules_mapping:
_pytest: pytest
_pytest.__init__: pytest
_pytest._argcomplete: pytest
_pytest._code: pytest
_pytest._code.__init__: pytest
_pytest._code.code: pytest
_pytest._code.source: pytest
_pytest._io: pytest
_pytest._io.__init__: pytest
_pytest._io.saferepr: pytest
_pytest._io.terminalwriter: pytest
_pytest._io.wcwidth: pytest
_pytest._version: pytest
_pytest.assertion: pytest
_pytest.assertion.__init__: pytest
_pytest.assertion.rewrite: pytest
_pytest.assertion.truncate: pytest
_pytest.assertion.util: pytest
_pytest.cacheprovider: pytest
_pytest.capture: pytest
_pytest.compat: pytest
_pytest.config: pytest
_pytest.config.__init__: pytest
_pytest.config.argparsing: pytest
_pytest.config.compat: pytest
_pytest.config.exceptions: pytest
_pytest.config.findpaths: pytest
_pytest.debugging: pytest
_pytest.deprecated: pytest
_pytest.doctest: pytest
_pytest.faulthandler: pytest
_pytest.fixtures: pytest
_pytest.freeze_support: pytest
_pytest.helpconfig: pytest
_pytest.hookspec: pytest
_pytest.junitxml: pytest
_pytest.legacypath: pytest
_pytest.logging: pytest
_pytest.main: pytest
_pytest.mark: pytest
_pytest.mark.__init__: pytest
_pytest.mark.expression: pytest
_pytest.mark.structures: pytest
_pytest.monkeypatch: pytest
_pytest.nodes: pytest
_pytest.nose: pytest
_pytest.outcomes: pytest
_pytest.pastebin: pytest
_pytest.pathlib: pytest
_pytest.pytester: pytest
_pytest.pytester_assertions: pytest
_pytest.python: pytest
_pytest.python_api: pytest
_pytest.python_path: pytest
_pytest.recwarn: pytest
_pytest.reports: pytest
_pytest.runner: pytest
_pytest.scope: pytest
_pytest.setuponly: pytest
_pytest.setupplan: pytest
_pytest.skipping: pytest
_pytest.stash: pytest
_pytest.stepwise: pytest
_pytest.terminal: pytest
_pytest.threadexception: pytest
_pytest.timing: pytest
_pytest.tmpdir: pytest
_pytest.unittest: pytest
_pytest.unraisableexception: pytest
_pytest.warning_types: pytest
_pytest.warnings: pytest
attr: attrs
attr.__init__: attrs
attr._cmp: attrs
attr._compat: attrs
attr._config: attrs
attr._funcs: attrs
attr._make: attrs
attr._next_gen: attrs
attr._version_info: attrs
attr.converters: attrs
attr.exceptions: attrs
attr.filters: attrs
attr.setters: attrs
attr.validators: attrs
attrs: attrs
attrs.__init__: attrs
attrs.converters: attrs
attrs.exceptions: attrs
attrs.filters: attrs
attrs.setters: attrs
attrs.validators: attrs
certifi: certifi
certifi.__init__: certifi
certifi.__main__: certifi
Expand DownExpand Up@@ -61,6 +153,89 @@ manifest:
idna.intranges: idna
idna.package_data: idna
idna.uts46data: idna
iniconfig: iniconfig
iniconfig.__init__: iniconfig
packaging: packaging
packaging.__about__: packaging
packaging.__init__: packaging
packaging._manylinux: packaging
packaging._musllinux: packaging
packaging._structures: packaging
packaging.markers: packaging
packaging.requirements: packaging
packaging.specifiers: packaging
packaging.tags: packaging
packaging.utils: packaging
packaging.version: packaging
pluggy: pluggy
pluggy.__init__: pluggy
pluggy._callers: pluggy
pluggy._hooks: pluggy
pluggy._manager: pluggy
pluggy._result: pluggy
pluggy._tracing: pluggy
pluggy._version: pluggy
py: py
py.__init__: py
py.__metainfo: py
py._builtin: py
py._code: py
py._code.__init__: py
py._code._assertionnew: py
py._code._assertionold: py
py._code._py2traceback: py
py._code.assertion: py
py._code.code: py
py._code.source: py
py._error: py
py._io: py
py._io.__init__: py
py._io.capture: py
py._io.saferepr: py
py._io.terminalwriter: py
py._log: py
py._log.__init__: py
py._log.log: py
py._log.warning: py
py._path: py
py._path.__init__: py
py._path.cacheutil: py
py._path.common: py
py._path.local: py
py._path.svnurl: py
py._path.svnwc: py
py._process: py
py._process.__init__: py
py._process.cmdexec: py
py._process.forkedfunc: py
py._process.killproc: py
py._std: py
py._vendored_packages: py
py._vendored_packages.__init__: py
py._vendored_packages.apipkg: py
py._vendored_packages.apipkg.__init__: py
py._vendored_packages.apipkg.version: py
py._vendored_packages.iniconfig: py
py._vendored_packages.iniconfig.__init__: py
py._version: py
py._xmlgen: py
py.test: py
pyparsing: pyparsing
pyparsing.__init__: pyparsing
pyparsing.actions: pyparsing
pyparsing.common: pyparsing
pyparsing.core: pyparsing
pyparsing.diagram: pyparsing
pyparsing.diagram.__init__: pyparsing
pyparsing.exceptions: pyparsing
pyparsing.helpers: pyparsing
pyparsing.results: pyparsing
pyparsing.testing: pyparsing
pyparsing.unicode: pyparsing
pyparsing.util: pyparsing
pytest: pytest
pytest.__init__: pytest
pytest.__main__: pytest
requests: requests
requests.__init__: requests
requests.__version__: requests
Expand All@@ -80,6 +255,11 @@ manifest:
requests.status_codes: requests
requests.structures: requests
requests.utils: requests
tomli: tomli
tomli.__init__: tomli
tomli._parser: tomli
tomli._re: tomli
tomli._types: tomli
urllib3: urllib3
urllib3.__init__: urllib3
urllib3._collections: urllib3
Expand DownExpand Up@@ -129,4 +309,4 @@ manifest:
pip_repository:
name: pip
incremental: true
integrity: c47bf2ca0a185cf6b8815d4a61e26e7457564e931de76c70653277e4eccfadc8
integrity: 71c787ef3d0dca470bb9c1913a5ef3ca4373db13cbd24acada1c7fcb40ef10ee
1 change: 1 addition & 0 deletions examples/build_file_generation/requirements.txt
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
requests==2.25.1
pytest~=7.1
34 changes: 33 additions & 1 deletion examples/build_file_generation/requirements_lock.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,8 +2,12 @@
# This file is autogenerated by pip-compile with python 3.9
# To update, run:
#
# pip-compile --generate-hashes --output-file=requirements_lock.txt requirements.txt
# bazel run //:pip_compile.update
#
attrs==21.4.0 \
--hash=sha256:2d27e3784d7a565d36ab851fe94887c5eccd6a463168875832a1be79c82828b4 \
--hash=sha256:626ba8234211db98e869df76230a137c4c40a12d72445c45d5f5b716f076e2fd
# via pytest
certifi==2020.12.5 \
--hash=sha256:1a4995114262bffbc2413b159f2a1a480c969de6e6eb13ee966d470af86af59c \
--hash=sha256:719a74fb9e33b9bd44cc7f3a8d94bc35e4049deebe19ba7d8e108280cfd59830
Expand All@@ -16,10 +20,38 @@ idna==2.10 \
--hash=sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6 \
--hash=sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0
# via requests
iniconfig==1.1.1 \
--hash=sha256:011e24c64b7f47f6ebd835bb12a743f2fbe9a26d4cecaa7f53bc4f35ee9da8b3 \
--hash=sha256:bc3af051d7d14b2ee5ef9969666def0cd1a000e121eaea580d4a313df4b37f32
# via pytest
packaging==21.3 \
--hash=sha256:dd47c42927d89ab911e606518907cc2d3a1f38bbd026385970643f9c5b8ecfeb \
--hash=sha256:ef103e05f519cdc783ae24ea4e2e0f508a9c99b2d4969652eed6a2e1ea5bd522
# via pytest
pluggy==1.0.0 \
--hash=sha256:4224373bacce55f955a878bf9cfa763c1e360858e330072059e10bad68531159 \
--hash=sha256:74134bbf457f031a36d68416e1509f34bd5ccc019f0bcc952c7b909d06b37bd3
# via pytest
py==1.11.0 \
--hash=sha256:51c75c4126074b472f746a24399ad32f6053d1b34b68d2fa41e558e6f4a98719 \
--hash=sha256:607c53218732647dff4acdfcd50cb62615cedf612e72d1724fb1a0cc6405b378
# via pytest
pyparsing==3.0.9 \
--hash=sha256:2b020ecf7d21b687f219b71ecad3631f644a47f01403fa1d1036b0c6416d70fb \
--hash=sha256:5026bae9a10eeaefb61dab2f09052b9f4307d44aee4eda64b309723d8d206bbc
# via packaging
pytest==7.1.2 \
--hash=sha256:13d0e3ccfc2b6e26be000cb6568c832ba67ba32e719443bfe725814d3c42433c \
--hash=sha256:a06a0425453864a270bc45e71f783330a7428defb4230fb5e6a731fde06ecd45
# via -r requirements.txt
requests==2.25.1 \
--hash=sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804 \
--hash=sha256:c210084e36a42ae6b9219e00e48287def368a26d03a048ddad7bfee44f75871e
# via -r requirements.txt
tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
urllib3==1.26.5 \
--hash=sha256:753a0374df26658f99d826cfe40394a686d05985786d946fbe4165b5148f5a7c \
--hash=sha256:a7acd0977125325f516bda9735fa7142b909a8d01e8b2e4c8108d0984e6e0098
Expand Down
4 changes: 4 additions & 0 deletions gazelle/README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -166,6 +166,10 @@ dependencies are added to the `deps` attribute.

Python test files are those ending in `_test.py`.

Gazelle will add a `py_test` target whenever it finds either of the following:
* A target in the same Bazel package named __test__. The macro `py_pytest_main` provide these entrypoint wrappers.
* A file in the same directory named __test__.py.

A `py_test` target is added containing all test files as `srcs`.

### Binaries
Expand Down
3 changes: 3 additions & 0 deletions python/defs.bzl
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,6 +27,7 @@ load(
_py_runtime = "py_runtime",
_py_test = "py_test",
)
load("//python/py_pytest_main:defs.bzl", _py_pytest_main = "py_pytest_main")

# Exports of native-defined providers.

Expand DownExpand Up@@ -140,3 +141,5 @@ py_binary = _py_binary
py_test = _py_test

py_runtime = _py_runtime

py_pytest_main = _py_pytest_main
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/BUILD
Original file line numberDiff line numberDiff line change
Expand Up@@ -54,6 +54,7 @@ bzl_library(
name = "defs",
srcs = [
"//python:defs.bzl",
"//python/py_pytest_main:defs.bzl",
"//python/private:reexports.bzl",
],
deps = [":bazel_python_tools"],
Expand Down
19 changes: 19 additions & 0 deletions docs/python.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion examples/build_file_generation/BUILD
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,14 @@ load("@pip//:requirements.bzl", "all_whl_requirements")
load("@rules_python//gazelle:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
load("@rules_python//gazelle/manifest:defs.bzl", "gazelle_python_manifest")
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_pytest_main", "py_test")

compile_pip_requirements(
name = "pip_compile",
requirements_in = "requirements.txt",
requirements_txt = "requirements_lock.txt",
)

# This rule fetches the metadata for python packages we depend on. That data is
# required for the gazelle_python_manifest rule to update our manifest file.
Expand DownExpand Up@@ -54,3 +61,22 @@ py_binary(
visibility = ["//:__subpackages__"],
deps = [":build_file_generation"],
)

# Generates an entrypoint for pytest that is picked up by Gazelle for the py_test target below.
py_pytest_main(
name = "__test__",
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

How come py_pytest_main becomes a dependency to a py_test, instead of subsuming the py_test rule?

Wholly replacing the py_test rule with a macro/rule/rule+macro seems a cleaner user interface.

@mattemmattemJun 10, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This provides an entry point for pytest, the Python test runner, and doesn't force changing loads or existing rules, and keepspy_test the bazel rule a generic "run a python binary that will return 0 or 1". There are other launchers, perhaps a nose entrypoint etc.

This model also fits with the gazelle generation already present here.


py_test(
name = "build_file_generation_test",
srcs = [
"foo_test.py",
":__test__",
],
main = ":__test__.py",
deps = [
":__test__",
":build_file_generation",
"@pip_pytest//:pkg",
],
)
9 changes: 9 additions & 0 deletions examples/build_file_generation/WORKSPACE
Original file line numberDiff line numberDiff line change
Expand Up@@ -48,10 +48,19 @@ local_repository(
path = "../..",
)

load("@rules_python//python:repositories.bzl", "python_register_toolchains")

python_register_toolchains(
name = "python39",
python_version = "3.9",
)

load("@python39//:defs.bzl", "interpreter")
load("@rules_python//python:pip.bzl", "pip_parse")

pip_parse(
name = "pip",
python_interpreter_target = interpreter,
requirements_lock = "//:requirements_lock.txt",
)

Expand Down
4 changes: 4 additions & 0 deletions examples/build_file_generation/foo_test.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
import pytest

def test_add():
assert 1 + 1 == 2, "Expected 1 + 1 to equal 2"
182 changes: 181 additions & 1 deletion examples/build_file_generation/gazelle_python.yaml
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,6 +5,98 @@

manifest:
modules_mapping:
_pytest: pytest
_pytest.__init__: pytest
_pytest._argcomplete: pytest
_pytest._code: pytest
_pytest._code.__init__: pytest
_pytest._code.code: pytest
_pytest._code.source: pytest
_pytest._io: pytest
_pytest._io.__init__: pytest
_pytest._io.saferepr: pytest
_pytest._io.terminalwriter: pytest
_pytest._io.wcwidth: pytest
_pytest._version: pytest
_pytest.assertion: pytest
_pytest.assertion.__init__: pytest
_pytest.assertion.rewrite: pytest
_pytest.assertion.truncate: pytest
_pytest.assertion.util: pytest
_pytest.cacheprovider: pytest
_pytest.capture: pytest
_pytest.compat: pytest
_pytest.config: pytest
_pytest.config.__init__: pytest
_pytest.config.argparsing: pytest
_pytest.config.compat: pytest
_pytest.config.exceptions: pytest
_pytest.config.findpaths: pytest
_pytest.debugging: pytest
_pytest.deprecated: pytest
_pytest.doctest: pytest
_pytest.faulthandler: pytest
_pytest.fixtures: pytest
_pytest.freeze_support: pytest
_pytest.helpconfig: pytest
_pytest.hookspec: pytest
_pytest.junitxml: pytest
_pytest.legacypath: pytest
_pytest.logging: pytest
_pytest.main: pytest
_pytest.mark: pytest
_pytest.mark.__init__: pytest
_pytest.mark.expression: pytest
_pytest.mark.structures: pytest
_pytest.monkeypatch: pytest
_pytest.nodes: pytest
_pytest.nose: pytest
_pytest.outcomes: pytest
_pytest.pastebin: pytest
_pytest.pathlib: pytest
_pytest.pytester: pytest
_pytest.pytester_assertions: pytest
_pytest.python: pytest
_pytest.python_api: pytest
_pytest.python_path: pytest
_pytest.recwarn: pytest
_pytest.reports: pytest
_pytest.runner: pytest
_pytest.scope: pytest
_pytest.setuponly: pytest
_pytest.setupplan: pytest
_pytest.skipping: pytest
_pytest.stash: pytest
_pytest.stepwise: pytest
_pytest.terminal: pytest
_pytest.threadexception: pytest
_pytest.timing: pytest
_pytest.tmpdir: pytest
_pytest.unittest: pytest
_pytest.unraisableexception: pytest
_pytest.warning_types: pytest
_pytest.warnings: pytest
attr: attrs
attr.__init__: attrs
attr._cmp: attrs
attr._compat: attrs
attr._config: attrs
attr._funcs: attrs
attr._make: attrs
attr._next_gen: attrs
attr._version_info: attrs
attr.converters: attrs
attr.exceptions: attrs
attr.filters: attrs
attr.setters: attrs
attr.validators: attrs
attrs: attrs
attrs.__init__: attrs
attrs.converters: attrs
attrs.exceptions: attrs
attrs.filters: attrs
attrs.setters: attrs
attrs.validators: attrs
certifi: certifi
certifi.__init__: certifi
certifi.__main__: certifi
Expand DownExpand Up@@ -61,6 +153,89 @@ manifest:
idna.intranges: idna
idna.package_data: idna
idna.uts46data: idna
iniconfig: iniconfig
iniconfig.__init__: iniconfig
packaging: packaging
packaging.__about__: packaging
packaging.__init__: packaging
packaging._manylinux: packaging
packaging._musllinux: packaging
packaging._structures: packaging
packaging.markers: packaging
packaging.requirements: packaging
packaging.specifiers: packaging
packaging.tags: packaging
packaging.utils: packaging
packaging.version: packaging
pluggy: pluggy
pluggy.__init__: pluggy
pluggy._callers: pluggy
pluggy._hooks: pluggy
pluggy._manager: pluggy
pluggy._result: pluggy
pluggy._tracing: pluggy
pluggy._version: pluggy
py: py
py.__init__: py
py.__metainfo: py
py._builtin: py
py._code: py
py._code.__init__: py
py._code._assertionnew: py
py._code._assertionold: py
py._code._py2traceback: py
py._code.assertion: py
py._code.code: py
py._code.source: py
py._error: py
py._io: py
py._io.__init__: py
py._io.capture: py
py._io.saferepr: py
py._io.terminalwriter: py
py._log: py
py._log.__init__: py
py._log.log: py
py._log.warning: py
py._path: py
py._path.__init__: py
py._path.cacheutil: py
py._path.common: py
py._path.local: py
py._path.svnurl: py
py._path.svnwc: py
py._process: py
py._process.__init__: py
py._process.cmdexec: py
py._process.forkedfunc: py
py._process.killproc: py
py._std: py
py._vendored_packages: py
py._vendored_packages.__init__: py
py._vendored_packages.apipkg: py
py._vendored_packages.apipkg.__init__: py
py._vendored_packages.apipkg.version: py
py._vendored_packages.iniconfig: py
py._vendored_packages.iniconfig.__init__: py
py._version: py
py._xmlgen: py
py.test: py
pyparsing: pyparsing
pyparsing.__init__: pyparsing
pyparsing.actions: pyparsing
pyparsing.common: pyparsing
pyparsing.core: pyparsing
pyparsing.diagram: pyparsing
pyparsing.diagram.__init__: pyparsing
pyparsing.exceptions: pyparsing
pyparsing.helpers: pyparsing
pyparsing.results: pyparsing
pyparsing.testing: pyparsing
pyparsing.unicode: pyparsing
pyparsing.util: pyparsing
pytest: pytest
pytest.__init__: pytest
pytest.__main__: pytest
requests: requests
requests.__init__: requests
requests.__version__: requests
Expand All@@ -80,6 +255,11 @@ manifest:
requests.status_codes: requests
requests.structures: requests
requests.utils: requests
tomli: tomli
tomli.__init__: tomli
tomli._parser: tomli
tomli._re: tomli
tomli._types: tomli
urllib3: urllib3
urllib3.__init__: urllib3
urllib3._collections: urllib3
Expand DownExpand Up@@ -129,4 +309,4 @@ manifest:
pip_repository:
name: pip
incremental: true
integrity: c47bf2ca0a185cf6b8815d4a61e26e7457564e931de76c70653277e4eccfadc8
integrity: 71c787ef3d0dca470bb9c1913a5ef3ca4373db13cbd24acada1c7fcb40ef10ee
1 change: 1 addition & 0 deletions examples/build_file_generation/requirements.txt
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
requests==2.25.1
pytest~=7.1
34 changes: 33 additions & 1 deletion examples/build_file_generation/requirements_lock.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,8 +2,12 @@
# This file is autogenerated by pip-compile with python 3.9
# To update, run:
#
# pip-compile --generate-hashes --output-file=requirements_lock.txt requirements.txt
# bazel run //:pip_compile.update
#
attrs==21.4.0 \
--hash=sha256:2d27e3784d7a565d36ab851fe94887c5eccd6a463168875832a1be79c82828b4 \
--hash=sha256:626ba8234211db98e869df76230a137c4c40a12d72445c45d5f5b716f076e2fd
# via pytest
certifi==2020.12.5 \
--hash=sha256:1a4995114262bffbc2413b159f2a1a480c969de6e6eb13ee966d470af86af59c \
--hash=sha256:719a74fb9e33b9bd44cc7f3a8d94bc35e4049deebe19ba7d8e108280cfd59830
Expand All@@ -16,10 +20,38 @@ idna==2.10 \
--hash=sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6 \
--hash=sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0
# via requests
iniconfig==1.1.1 \
--hash=sha256:011e24c64b7f47f6ebd835bb12a743f2fbe9a26d4cecaa7f53bc4f35ee9da8b3 \
--hash=sha256:bc3af051d7d14b2ee5ef9969666def0cd1a000e121eaea580d4a313df4b37f32
# via pytest
packaging==21.3 \
--hash=sha256:dd47c42927d89ab911e606518907cc2d3a1f38bbd026385970643f9c5b8ecfeb \
--hash=sha256:ef103e05f519cdc783ae24ea4e2e0f508a9c99b2d4969652eed6a2e1ea5bd522
# via pytest
pluggy==1.0.0 \
--hash=sha256:4224373bacce55f955a878bf9cfa763c1e360858e330072059e10bad68531159 \
--hash=sha256:74134bbf457f031a36d68416e1509f34bd5ccc019f0bcc952c7b909d06b37bd3
# via pytest
py==1.11.0 \
--hash=sha256:51c75c4126074b472f746a24399ad32f6053d1b34b68d2fa41e558e6f4a98719 \
--hash=sha256:607c53218732647dff4acdfcd50cb62615cedf612e72d1724fb1a0cc6405b378
# via pytest
pyparsing==3.0.9 \
--hash=sha256:2b020ecf7d21b687f219b71ecad3631f644a47f01403fa1d1036b0c6416d70fb \
--hash=sha256:5026bae9a10eeaefb61dab2f09052b9f4307d44aee4eda64b309723d8d206bbc
# via packaging
pytest==7.1.2 \
--hash=sha256:13d0e3ccfc2b6e26be000cb6568c832ba67ba32e719443bfe725814d3c42433c \
--hash=sha256:a06a0425453864a270bc45e71f783330a7428defb4230fb5e6a731fde06ecd45
# via -r requirements.txt
requests==2.25.1 \
--hash=sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804 \
--hash=sha256:c210084e36a42ae6b9219e00e48287def368a26d03a048ddad7bfee44f75871e
# via -r requirements.txt
tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
urllib3==1.26.5 \
--hash=sha256:753a0374df26658f99d826cfe40394a686d05985786d946fbe4165b5148f5a7c \
--hash=sha256:a7acd0977125325f516bda9735fa7142b909a8d01e8b2e4c8108d0984e6e0098
Expand Down
4 changes: 4 additions & 0 deletions gazelle/README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -166,6 +166,10 @@ dependencies are added to the `deps` attribute.

Python test files are those ending in `_test.py`.

Gazelle will add a `py_test` target whenever it finds either of the following:
* A target in the same Bazel package named __test__. The macro `py_pytest_main` provide these entrypoint wrappers.
* A file in the same directory named __test__.py.

A `py_test` target is added containing all test files as `srcs`.

### Binaries
Expand Down
3 changes: 3 additions & 0 deletions python/defs.bzl
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,6 +27,7 @@ load(
_py_runtime = "py_runtime",
_py_test = "py_test",
)
load("//python/py_pytest_main:defs.bzl", _py_pytest_main = "py_pytest_main")

# Exports of native-defined providers.

Expand DownExpand Up@@ -140,3 +141,5 @@ py_binary = _py_binary
py_test = _py_test

py_runtime = _py_runtime

py_pytest_main = _py_pytest_main
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/BUILD
Original file line numberDiff line numberDiff line change
Expand Up@@ -54,6 +54,7 @@ bzl_library(
name = "defs",
srcs = [
"//python:defs.bzl",
"//python/py_pytest_main:defs.bzl",
"//python/private:reexports.bzl",
],
deps = [":bazel_python_tools"],
Expand Down
19 changes: 19 additions & 0 deletions docs/python.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion examples/build_file_generation/BUILD
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,14 @@ load("@pip//:requirements.bzl", "all_whl_requirements")
load("@rules_python//gazelle:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
load("@rules_python//gazelle/manifest:defs.bzl", "gazelle_python_manifest")
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_pytest_main", "py_test")

compile_pip_requirements(
name = "pip_compile",
requirements_in = "requirements.txt",
requirements_txt = "requirements_lock.txt",
)

# This rule fetches the metadata for python packages we depend on. That data is
# required for the gazelle_python_manifest rule to update our manifest file.
Expand DownExpand Up@@ -54,3 +61,22 @@ py_binary(
visibility = ["//:__subpackages__"],
deps = [":build_file_generation"],
)

# Generates an entrypoint for pytest that is picked up by Gazelle for the py_test target below.
py_pytest_main(
name = "__test__",
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

How come py_pytest_main becomes a dependency to a py_test, instead of subsuming the py_test rule?

Wholly replacing the py_test rule with a macro/rule/rule+macro seems a cleaner user interface.

@mattemmattemJun 10, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This provides an entry point for pytest, the Python test runner, and doesn't force changing loads or existing rules, and keepspy_test the bazel rule a generic "run a python binary that will return 0 or 1". There are other launchers, perhaps a nose entrypoint etc.

This model also fits with the gazelle generation already present here.


py_test(
name = "build_file_generation_test",
srcs = [
"foo_test.py",
":__test__",
],
main = ":__test__.py",
deps = [
":__test__",
":build_file_generation",
"@pip_pytest//:pkg",
],
)
9 changes: 9 additions & 0 deletions examples/build_file_generation/WORKSPACE
Original file line numberDiff line numberDiff line change
Expand Up@@ -48,10 +48,19 @@ local_repository(
path = "../..",
)

load("@rules_python//python:repositories.bzl", "python_register_toolchains")

python_register_toolchains(
name = "python39",
python_version = "3.9",
)

load("@python39//:defs.bzl", "interpreter")
load("@rules_python//python:pip.bzl", "pip_parse")

pip_parse(
name = "pip",
python_interpreter_target = interpreter,
requirements_lock = "//:requirements_lock.txt",
)

Expand Down
4 changes: 4 additions & 0 deletions examples/build_file_generation/foo_test.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
import pytest

def test_add():
assert 1 + 1 == 2, "Expected 1 + 1 to equal 2"
182 changes: 181 additions & 1 deletion examples/build_file_generation/gazelle_python.yaml
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,6 +5,98 @@

manifest:
modules_mapping:
_pytest: pytest
_pytest.__init__: pytest
_pytest._argcomplete: pytest
_pytest._code: pytest
_pytest._code.__init__: pytest
_pytest._code.code: pytest
_pytest._code.source: pytest
_pytest._io: pytest
_pytest._io.__init__: pytest
_pytest._io.saferepr: pytest
_pytest._io.terminalwriter: pytest
_pytest._io.wcwidth: pytest
_pytest._version: pytest
_pytest.assertion: pytest
_pytest.assertion.__init__: pytest
_pytest.assertion.rewrite: pytest
_pytest.assertion.truncate: pytest
_pytest.assertion.util: pytest
_pytest.cacheprovider: pytest
_pytest.capture: pytest
_pytest.compat: pytest
_pytest.config: pytest
_pytest.config.__init__: pytest
_pytest.config.argparsing: pytest
_pytest.config.compat: pytest
_pytest.config.exceptions: pytest
_pytest.config.findpaths: pytest
_pytest.debugging: pytest
_pytest.deprecated: pytest
_pytest.doctest: pytest
_pytest.faulthandler: pytest
_pytest.fixtures: pytest
_pytest.freeze_support: pytest
_pytest.helpconfig: pytest
_pytest.hookspec: pytest
_pytest.junitxml: pytest
_pytest.legacypath: pytest
_pytest.logging: pytest
_pytest.main: pytest
_pytest.mark: pytest
_pytest.mark.__init__: pytest
_pytest.mark.expression: pytest
_pytest.mark.structures: pytest
_pytest.monkeypatch: pytest
_pytest.nodes: pytest
_pytest.nose: pytest
_pytest.outcomes: pytest
_pytest.pastebin: pytest
_pytest.pathlib: pytest
_pytest.pytester: pytest
_pytest.pytester_assertions: pytest
_pytest.python: pytest
_pytest.python_api: pytest
_pytest.python_path: pytest
_pytest.recwarn: pytest
_pytest.reports: pytest
_pytest.runner: pytest
_pytest.scope: pytest
_pytest.setuponly: pytest
_pytest.setupplan: pytest
_pytest.skipping: pytest
_pytest.stash: pytest
_pytest.stepwise: pytest
_pytest.terminal: pytest
_pytest.threadexception: pytest
_pytest.timing: pytest
_pytest.tmpdir: pytest
_pytest.unittest: pytest
_pytest.unraisableexception: pytest
_pytest.warning_types: pytest
_pytest.warnings: pytest
attr: attrs
attr.__init__: attrs
attr._cmp: attrs
attr._compat: attrs
attr._config: attrs
attr._funcs: attrs
attr._make: attrs
attr._next_gen: attrs
attr._version_info: attrs
attr.converters: attrs
attr.exceptions: attrs
attr.filters: attrs
attr.setters: attrs
attr.validators: attrs
attrs: attrs
attrs.__init__: attrs
attrs.converters: attrs
attrs.exceptions: attrs
attrs.filters: attrs
attrs.setters: attrs
attrs.validators: attrs
certifi: certifi
certifi.__init__: certifi
certifi.__main__: certifi
Expand DownExpand Up@@ -61,6 +153,89 @@ manifest:
idna.intranges: idna
idna.package_data: idna
idna.uts46data: idna
iniconfig: iniconfig
iniconfig.__init__: iniconfig
packaging: packaging
packaging.__about__: packaging
packaging.__init__: packaging
packaging._manylinux: packaging
packaging._musllinux: packaging
packaging._structures: packaging
packaging.markers: packaging
packaging.requirements: packaging
packaging.specifiers: packaging
packaging.tags: packaging
packaging.utils: packaging
packaging.version: packaging
pluggy: pluggy
pluggy.__init__: pluggy
pluggy._callers: pluggy
pluggy._hooks: pluggy
pluggy._manager: pluggy
pluggy._result: pluggy
pluggy._tracing: pluggy
pluggy._version: pluggy
py: py
py.__init__: py
py.__metainfo: py
py._builtin: py
py._code: py
py._code.__init__: py
py._code._assertionnew: py
py._code._assertionold: py
py._code._py2traceback: py
py._code.assertion: py
py._code.code: py
py._code.source: py
py._error: py
py._io: py
py._io.__init__: py
py._io.capture: py
py._io.saferepr: py
py._io.terminalwriter: py
py._log: py
py._log.__init__: py
py._log.log: py
py._log.warning: py
py._path: py
py._path.__init__: py
py._path.cacheutil: py
py._path.common: py
py._path.local: py
py._path.svnurl: py
py._path.svnwc: py
py._process: py
py._process.__init__: py
py._process.cmdexec: py
py._process.forkedfunc: py
py._process.killproc: py
py._std: py
py._vendored_packages: py
py._vendored_packages.__init__: py
py._vendored_packages.apipkg: py
py._vendored_packages.apipkg.__init__: py
py._vendored_packages.apipkg.version: py
py._vendored_packages.iniconfig: py
py._vendored_packages.iniconfig.__init__: py
py._version: py
py._xmlgen: py
py.test: py
pyparsing: pyparsing
pyparsing.__init__: pyparsing
pyparsing.actions: pyparsing
pyparsing.common: pyparsing
pyparsing.core: pyparsing
pyparsing.diagram: pyparsing
pyparsing.diagram.__init__: pyparsing
pyparsing.exceptions: pyparsing
pyparsing.helpers: pyparsing
pyparsing.results: pyparsing
pyparsing.testing: pyparsing
pyparsing.unicode: pyparsing
pyparsing.util: pyparsing
pytest: pytest
pytest.__init__: pytest
pytest.__main__: pytest
requests: requests
requests.__init__: requests
requests.__version__: requests
Expand All@@ -80,6 +255,11 @@ manifest:
requests.status_codes: requests
requests.structures: requests
requests.utils: requests
tomli: tomli
tomli.__init__: tomli
tomli._parser: tomli
tomli._re: tomli
tomli._types: tomli
urllib3: urllib3
urllib3.__init__: urllib3
urllib3._collections: urllib3
Expand DownExpand Up@@ -129,4 +309,4 @@ manifest:
pip_repository:
name: pip
incremental: true
integrity: c47bf2ca0a185cf6b8815d4a61e26e7457564e931de76c70653277e4eccfadc8
integrity: 71c787ef3d0dca470bb9c1913a5ef3ca4373db13cbd24acada1c7fcb40ef10ee
1 change: 1 addition & 0 deletions examples/build_file_generation/requirements.txt
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
requests==2.25.1
pytest~=7.1
34 changes: 33 additions & 1 deletion examples/build_file_generation/requirements_lock.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,8 +2,12 @@
# This file is autogenerated by pip-compile with python 3.9
# To update, run:
#
# pip-compile --generate-hashes --output-file=requirements_lock.txt requirements.txt
# bazel run //:pip_compile.update
#
attrs==21.4.0 \
--hash=sha256:2d27e3784d7a565d36ab851fe94887c5eccd6a463168875832a1be79c82828b4 \
--hash=sha256:626ba8234211db98e869df76230a137c4c40a12d72445c45d5f5b716f076e2fd
# via pytest
certifi==2020.12.5 \
--hash=sha256:1a4995114262bffbc2413b159f2a1a480c969de6e6eb13ee966d470af86af59c \
--hash=sha256:719a74fb9e33b9bd44cc7f3a8d94bc35e4049deebe19ba7d8e108280cfd59830
Expand All@@ -16,10 +20,38 @@ idna==2.10 \
--hash=sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6 \
--hash=sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0
# via requests
iniconfig==1.1.1 \
--hash=sha256:011e24c64b7f47f6ebd835bb12a743f2fbe9a26d4cecaa7f53bc4f35ee9da8b3 \
--hash=sha256:bc3af051d7d14b2ee5ef9969666def0cd1a000e121eaea580d4a313df4b37f32
# via pytest
packaging==21.3 \
--hash=sha256:dd47c42927d89ab911e606518907cc2d3a1f38bbd026385970643f9c5b8ecfeb \
--hash=sha256:ef103e05f519cdc783ae24ea4e2e0f508a9c99b2d4969652eed6a2e1ea5bd522
# via pytest
pluggy==1.0.0 \
--hash=sha256:4224373bacce55f955a878bf9cfa763c1e360858e330072059e10bad68531159 \
--hash=sha256:74134bbf457f031a36d68416e1509f34bd5ccc019f0bcc952c7b909d06b37bd3
# via pytest
py==1.11.0 \
--hash=sha256:51c75c4126074b472f746a24399ad32f6053d1b34b68d2fa41e558e6f4a98719 \
--hash=sha256:607c53218732647dff4acdfcd50cb62615cedf612e72d1724fb1a0cc6405b378
# via pytest
pyparsing==3.0.9 \
--hash=sha256:2b020ecf7d21b687f219b71ecad3631f644a47f01403fa1d1036b0c6416d70fb \
--hash=sha256:5026bae9a10eeaefb61dab2f09052b9f4307d44aee4eda64b309723d8d206bbc
# via packaging
pytest==7.1.2 \
--hash=sha256:13d0e3ccfc2b6e26be000cb6568c832ba67ba32e719443bfe725814d3c42433c \
--hash=sha256:a06a0425453864a270bc45e71f783330a7428defb4230fb5e6a731fde06ecd45
# via -r requirements.txt
requests==2.25.1 \
--hash=sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804 \
--hash=sha256:c210084e36a42ae6b9219e00e48287def368a26d03a048ddad7bfee44f75871e
# via -r requirements.txt
tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
urllib3==1.26.5 \
--hash=sha256:753a0374df26658f99d826cfe40394a686d05985786d946fbe4165b5148f5a7c \
--hash=sha256:a7acd0977125325f516bda9735fa7142b909a8d01e8b2e4c8108d0984e6e0098
Expand Down
4 changes: 4 additions & 0 deletions gazelle/README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -166,6 +166,10 @@ dependencies are added to the `deps` attribute.

Python test files are those ending in `_test.py`.

Gazelle will add a `py_test` target whenever it finds either of the following:
* A target in the same Bazel package named __test__. The macro `py_pytest_main` provide these entrypoint wrappers.
* A file in the same directory named __test__.py.

A `py_test` target is added containing all test files as `srcs`.

### Binaries
Expand Down
3 changes: 3 additions & 0 deletions python/defs.bzl
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,6 +27,7 @@ load(
_py_runtime = "py_runtime",
_py_test = "py_test",
)
load("//python/py_pytest_main:defs.bzl", _py_pytest_main = "py_pytest_main")

# Exports of native-defined providers.

Expand DownExpand Up@@ -140,3 +141,5 @@ py_binary = _py_binary
py_test = _py_test

py_runtime = _py_runtime

py_pytest_main = _py_pytest_main
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/BUILD
Original file line numberDiff line numberDiff line change
Expand Up@@ -54,6 +54,7 @@ bzl_library(
name = "defs",
srcs = [
"//python:defs.bzl",
"//python/py_pytest_main:defs.bzl",
"//python/private:reexports.bzl",
],
deps = [":bazel_python_tools"],
Expand Down
19 changes: 19 additions & 0 deletions docs/python.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion examples/build_file_generation/BUILD
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,14 @@ load("@pip//:requirements.bzl", "all_whl_requirements")
load("@rules_python//gazelle:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
load("@rules_python//gazelle/manifest:defs.bzl", "gazelle_python_manifest")
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_pytest_main", "py_test")

compile_pip_requirements(
name = "pip_compile",
requirements_in = "requirements.txt",
requirements_txt = "requirements_lock.txt",
)

# This rule fetches the metadata for python packages we depend on. That data is
# required for the gazelle_python_manifest rule to update our manifest file.
Expand DownExpand Up@@ -54,3 +61,22 @@ py_binary(
visibility = ["//:__subpackages__"],
deps = [":build_file_generation"],
)

# Generates an entrypoint for pytest that is picked up by Gazelle for the py_test target below.
py_pytest_main(
name = "__test__",
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

How come py_pytest_main becomes a dependency to a py_test, instead of subsuming the py_test rule?

Wholly replacing the py_test rule with a macro/rule/rule+macro seems a cleaner user interface.

@mattemmattemJun 10, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This provides an entry point for pytest, the Python test runner, and doesn't force changing loads or existing rules, and keepspy_test the bazel rule a generic "run a python binary that will return 0 or 1". There are other launchers, perhaps a nose entrypoint etc.

This model also fits with the gazelle generation already present here.


py_test(
name = "build_file_generation_test",
srcs = [
"foo_test.py",
":__test__",
],
main = ":__test__.py",
deps = [
":__test__",
":build_file_generation",
"@pip_pytest//:pkg",
],
)
9 changes: 9 additions & 0 deletions examples/build_file_generation/WORKSPACE
Original file line numberDiff line numberDiff line change
Expand Up@@ -48,10 +48,19 @@ local_repository(
path = "../..",
)

load("@rules_python//python:repositories.bzl", "python_register_toolchains")

python_register_toolchains(
name = "python39",
python_version = "3.9",
)

load("@python39//:defs.bzl", "interpreter")
load("@rules_python//python:pip.bzl", "pip_parse")

pip_parse(
name = "pip",
python_interpreter_target = interpreter,
requirements_lock = "//:requirements_lock.txt",
)

Expand Down
4 changes: 4 additions & 0 deletions examples/build_file_generation/foo_test.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
import pytest

def test_add():
assert 1 + 1 == 2, "Expected 1 + 1 to equal 2"
182 changes: 181 additions & 1 deletion examples/build_file_generation/gazelle_python.yaml
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,6 +5,98 @@

manifest:
modules_mapping:
_pytest: pytest
_pytest.__init__: pytest
_pytest._argcomplete: pytest
_pytest._code: pytest
_pytest._code.__init__: pytest
_pytest._code.code: pytest
_pytest._code.source: pytest
_pytest._io: pytest
_pytest._io.__init__: pytest
_pytest._io.saferepr: pytest
_pytest._io.terminalwriter: pytest
_pytest._io.wcwidth: pytest
_pytest._version: pytest
_pytest.assertion: pytest
_pytest.assertion.__init__: pytest
_pytest.assertion.rewrite: pytest
_pytest.assertion.truncate: pytest
_pytest.assertion.util: pytest
_pytest.cacheprovider: pytest
_pytest.capture: pytest
_pytest.compat: pytest
_pytest.config: pytest
_pytest.config.__init__: pytest
_pytest.config.argparsing: pytest
_pytest.config.compat: pytest
_pytest.config.exceptions: pytest
_pytest.config.findpaths: pytest
_pytest.debugging: pytest
_pytest.deprecated: pytest
_pytest.doctest: pytest
_pytest.faulthandler: pytest
_pytest.fixtures: pytest
_pytest.freeze_support: pytest
_pytest.helpconfig: pytest
_pytest.hookspec: pytest
_pytest.junitxml: pytest
_pytest.legacypath: pytest
_pytest.logging: pytest
_pytest.main: pytest
_pytest.mark: pytest
_pytest.mark.__init__: pytest
_pytest.mark.expression: pytest
_pytest.mark.structures: pytest
_pytest.monkeypatch: pytest
_pytest.nodes: pytest
_pytest.nose: pytest
_pytest.outcomes: pytest
_pytest.pastebin: pytest
_pytest.pathlib: pytest
_pytest.pytester: pytest
_pytest.pytester_assertions: pytest
_pytest.python: pytest
_pytest.python_api: pytest
_pytest.python_path: pytest
_pytest.recwarn: pytest
_pytest.reports: pytest
_pytest.runner: pytest
_pytest.scope: pytest
_pytest.setuponly: pytest
_pytest.setupplan: pytest
_pytest.skipping: pytest
_pytest.stash: pytest
_pytest.stepwise: pytest
_pytest.terminal: pytest
_pytest.threadexception: pytest
_pytest.timing: pytest
_pytest.tmpdir: pytest
_pytest.unittest: pytest
_pytest.unraisableexception: pytest
_pytest.warning_types: pytest
_pytest.warnings: pytest
attr: attrs
attr.__init__: attrs
attr._cmp: attrs
attr._compat: attrs
attr._config: attrs
attr._funcs: attrs
attr._make: attrs
attr._next_gen: attrs
attr._version_info: attrs
attr.converters: attrs
attr.exceptions: attrs
attr.filters: attrs
attr.setters: attrs
attr.validators: attrs
attrs: attrs
attrs.__init__: attrs
attrs.converters: attrs
attrs.exceptions: attrs
attrs.filters: attrs
attrs.setters: attrs
attrs.validators: attrs
certifi: certifi
certifi.__init__: certifi
certifi.__main__: certifi
Expand DownExpand Up@@ -61,6 +153,89 @@ manifest:
idna.intranges: idna
idna.package_data: idna
idna.uts46data: idna
iniconfig: iniconfig
iniconfig.__init__: iniconfig
packaging: packaging
packaging.__about__: packaging
packaging.__init__: packaging
packaging._manylinux: packaging
packaging._musllinux: packaging
packaging._structures: packaging
packaging.markers: packaging
packaging.requirements: packaging
packaging.specifiers: packaging
packaging.tags: packaging
packaging.utils: packaging
packaging.version: packaging
pluggy: pluggy
pluggy.__init__: pluggy
pluggy._callers: pluggy
pluggy._hooks: pluggy
pluggy._manager: pluggy
pluggy._result: pluggy
pluggy._tracing: pluggy
pluggy._version: pluggy
py: py
py.__init__: py
py.__metainfo: py
py._builtin: py
py._code: py
py._code.__init__: py
py._code._assertionnew: py
py._code._assertionold: py
py._code._py2traceback: py
py._code.assertion: py
py._code.code: py
py._code.source: py
py._error: py
py._io: py
py._io.__init__: py
py._io.capture: py
py._io.saferepr: py
py._io.terminalwriter: py
py._log: py
py._log.__init__: py
py._log.log: py
py._log.warning: py
py._path: py
py._path.__init__: py
py._path.cacheutil: py
py._path.common: py
py._path.local: py
py._path.svnurl: py
py._path.svnwc: py
py._process: py
py._process.__init__: py
py._process.cmdexec: py
py._process.forkedfunc: py
py._process.killproc: py
py._std: py
py._vendored_packages: py
py._vendored_packages.__init__: py
py._vendored_packages.apipkg: py
py._vendored_packages.apipkg.__init__: py
py._vendored_packages.apipkg.version: py
py._vendored_packages.iniconfig: py
py._vendored_packages.iniconfig.__init__: py
py._version: py
py._xmlgen: py
py.test: py
pyparsing: pyparsing
pyparsing.__init__: pyparsing
pyparsing.actions: pyparsing
pyparsing.common: pyparsing
pyparsing.core: pyparsing
pyparsing.diagram: pyparsing
pyparsing.diagram.__init__: pyparsing
pyparsing.exceptions: pyparsing
pyparsing.helpers: pyparsing
pyparsing.results: pyparsing
pyparsing.testing: pyparsing
pyparsing.unicode: pyparsing
pyparsing.util: pyparsing
pytest: pytest
pytest.__init__: pytest
pytest.__main__: pytest
requests: requests
requests.__init__: requests
requests.__version__: requests
Expand All@@ -80,6 +255,11 @@ manifest:
requests.status_codes: requests
requests.structures: requests
requests.utils: requests
tomli: tomli
tomli.__init__: tomli
tomli._parser: tomli
tomli._re: tomli
tomli._types: tomli
urllib3: urllib3
urllib3.__init__: urllib3
urllib3._collections: urllib3
Expand DownExpand Up@@ -129,4 +309,4 @@ manifest:
pip_repository:
name: pip
incremental: true
integrity: c47bf2ca0a185cf6b8815d4a61e26e7457564e931de76c70653277e4eccfadc8
integrity: 71c787ef3d0dca470bb9c1913a5ef3ca4373db13cbd24acada1c7fcb40ef10ee
1 change: 1 addition & 0 deletions examples/build_file_generation/requirements.txt
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
requests==2.25.1
pytest~=7.1
34 changes: 33 additions & 1 deletion examples/build_file_generation/requirements_lock.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,8 +2,12 @@
# This file is autogenerated by pip-compile with python 3.9
# To update, run:
#
# pip-compile --generate-hashes --output-file=requirements_lock.txt requirements.txt
# bazel run //:pip_compile.update
#
attrs==21.4.0 \
--hash=sha256:2d27e3784d7a565d36ab851fe94887c5eccd6a463168875832a1be79c82828b4 \
--hash=sha256:626ba8234211db98e869df76230a137c4c40a12d72445c45d5f5b716f076e2fd
# via pytest
certifi==2020.12.5 \
--hash=sha256:1a4995114262bffbc2413b159f2a1a480c969de6e6eb13ee966d470af86af59c \
--hash=sha256:719a74fb9e33b9bd44cc7f3a8d94bc35e4049deebe19ba7d8e108280cfd59830
Expand All@@ -16,10 +20,38 @@ idna==2.10 \
--hash=sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6 \
--hash=sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0
# via requests
iniconfig==1.1.1 \
--hash=sha256:011e24c64b7f47f6ebd835bb12a743f2fbe9a26d4cecaa7f53bc4f35ee9da8b3 \
--hash=sha256:bc3af051d7d14b2ee5ef9969666def0cd1a000e121eaea580d4a313df4b37f32
# via pytest
packaging==21.3 \
--hash=sha256:dd47c42927d89ab911e606518907cc2d3a1f38bbd026385970643f9c5b8ecfeb \
--hash=sha256:ef103e05f519cdc783ae24ea4e2e0f508a9c99b2d4969652eed6a2e1ea5bd522
# via pytest
pluggy==1.0.0 \
--hash=sha256:4224373bacce55f955a878bf9cfa763c1e360858e330072059e10bad68531159 \
--hash=sha256:74134bbf457f031a36d68416e1509f34bd5ccc019f0bcc952c7b909d06b37bd3
# via pytest
py==1.11.0 \
--hash=sha256:51c75c4126074b472f746a24399ad32f6053d1b34b68d2fa41e558e6f4a98719 \
--hash=sha256:607c53218732647dff4acdfcd50cb62615cedf612e72d1724fb1a0cc6405b378
# via pytest
pyparsing==3.0.9 \
--hash=sha256:2b020ecf7d21b687f219b71ecad3631f644a47f01403fa1d1036b0c6416d70fb \
--hash=sha256:5026bae9a10eeaefb61dab2f09052b9f4307d44aee4eda64b309723d8d206bbc
# via packaging
pytest==7.1.2 \
--hash=sha256:13d0e3ccfc2b6e26be000cb6568c832ba67ba32e719443bfe725814d3c42433c \
--hash=sha256:a06a0425453864a270bc45e71f783330a7428defb4230fb5e6a731fde06ecd45
# via -r requirements.txt
requests==2.25.1 \
--hash=sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804 \
--hash=sha256:c210084e36a42ae6b9219e00e48287def368a26d03a048ddad7bfee44f75871e
# via -r requirements.txt
tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
urllib3==1.26.5 \
--hash=sha256:753a0374df26658f99d826cfe40394a686d05985786d946fbe4165b5148f5a7c \
--hash=sha256:a7acd0977125325f516bda9735fa7142b909a8d01e8b2e4c8108d0984e6e0098
Expand Down
4 changes: 4 additions & 0 deletions gazelle/README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -166,6 +166,10 @@ dependencies are added to the `deps` attribute.

Python test files are those ending in `_test.py`.

Gazelle will add a `py_test` target whenever it finds either of the following:
* A target in the same Bazel package named __test__. The macro `py_pytest_main` provide these entrypoint wrappers.
* A file in the same directory named __test__.py.

A `py_test` target is added containing all test files as `srcs`.

### Binaries
Expand Down
3 changes: 3 additions & 0 deletions python/defs.bzl
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,6 +27,7 @@ load(
_py_runtime = "py_runtime",
_py_test = "py_test",
)
load("//python/py_pytest_main:defs.bzl", _py_pytest_main = "py_pytest_main")

# Exports of native-defined providers.

Expand DownExpand Up@@ -140,3 +141,5 @@ py_binary = _py_binary
py_test = _py_test

py_runtime = _py_runtime

py_pytest_main = _py_pytest_main
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/BUILD
Original file line numberDiff line numberDiff line change
Expand Up@@ -54,6 +54,7 @@ bzl_library(
name = "defs",
srcs = [
"//python:defs.bzl",
"//python/py_pytest_main:defs.bzl",
"//python/private:reexports.bzl",
],
deps = [":bazel_python_tools"],
Expand Down
19 changes: 19 additions & 0 deletions docs/python.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion examples/build_file_generation/BUILD
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,14 @@ load("@pip//:requirements.bzl", "all_whl_requirements")
load("@rules_python//gazelle:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
load("@rules_python//gazelle/manifest:defs.bzl", "gazelle_python_manifest")
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_pytest_main", "py_test")

compile_pip_requirements(
name = "pip_compile",
requirements_in = "requirements.txt",
requirements_txt = "requirements_lock.txt",
)

# This rule fetches the metadata for python packages we depend on. That data is
# required for the gazelle_python_manifest rule to update our manifest file.
Expand DownExpand Up@@ -54,3 +61,22 @@ py_binary(
visibility = ["//:__subpackages__"],
deps = [":build_file_generation"],
)

# Generates an entrypoint for pytest that is picked up by Gazelle for the py_test target below.
py_pytest_main(
name = "__test__",
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

How come py_pytest_main becomes a dependency to a py_test, instead of subsuming the py_test rule?

Wholly replacing the py_test rule with a macro/rule/rule+macro seems a cleaner user interface.

@mattemmattemJun 10, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This provides an entry point for pytest, the Python test runner, and doesn't force changing loads or existing rules, and keepspy_test the bazel rule a generic "run a python binary that will return 0 or 1". There are other launchers, perhaps a nose entrypoint etc.

This model also fits with the gazelle generation already present here.


py_test(
name = "build_file_generation_test",
srcs = [
"foo_test.py",
":__test__",
],
main = ":__test__.py",
deps = [
":__test__",
":build_file_generation",
"@pip_pytest//:pkg",
],
)
9 changes: 9 additions & 0 deletions examples/build_file_generation/WORKSPACE
Original file line numberDiff line numberDiff line change
Expand Up@@ -48,10 +48,19 @@ local_repository(
path = "../..",
)

load("@rules_python//python:repositories.bzl", "python_register_toolchains")

python_register_toolchains(
name = "python39",
python_version = "3.9",
)

load("@python39//:defs.bzl", "interpreter")
load("@rules_python//python:pip.bzl", "pip_parse")

pip_parse(
name = "pip",
python_interpreter_target = interpreter,
requirements_lock = "//:requirements_lock.txt",
)

Expand Down
4 changes: 4 additions & 0 deletions examples/build_file_generation/foo_test.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
import pytest

def test_add():
assert 1 + 1 == 2, "Expected 1 + 1 to equal 2"
182 changes: 181 additions & 1 deletion examples/build_file_generation/gazelle_python.yaml
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,6 +5,98 @@

manifest:
modules_mapping:
_pytest: pytest
_pytest.__init__: pytest
_pytest._argcomplete: pytest
_pytest._code: pytest
_pytest._code.__init__: pytest
_pytest._code.code: pytest
_pytest._code.source: pytest
_pytest._io: pytest
_pytest._io.__init__: pytest
_pytest._io.saferepr: pytest
_pytest._io.terminalwriter: pytest
_pytest._io.wcwidth: pytest
_pytest._version: pytest
_pytest.assertion: pytest
_pytest.assertion.__init__: pytest
_pytest.assertion.rewrite: pytest
_pytest.assertion.truncate: pytest
_pytest.assertion.util: pytest
_pytest.cacheprovider: pytest
_pytest.capture: pytest
_pytest.compat: pytest
_pytest.config: pytest
_pytest.config.__init__: pytest
_pytest.config.argparsing: pytest
_pytest.config.compat: pytest
_pytest.config.exceptions: pytest
_pytest.config.findpaths: pytest
_pytest.debugging: pytest
_pytest.deprecated: pytest
_pytest.doctest: pytest
_pytest.faulthandler: pytest
_pytest.fixtures: pytest
_pytest.freeze_support: pytest
_pytest.helpconfig: pytest
_pytest.hookspec: pytest
_pytest.junitxml: pytest
_pytest.legacypath: pytest
_pytest.logging: pytest
_pytest.main: pytest
_pytest.mark: pytest
_pytest.mark.__init__: pytest
_pytest.mark.expression: pytest
_pytest.mark.structures: pytest
_pytest.monkeypatch: pytest
_pytest.nodes: pytest
_pytest.nose: pytest
_pytest.outcomes: pytest
_pytest.pastebin: pytest
_pytest.pathlib: pytest
_pytest.pytester: pytest
_pytest.pytester_assertions: pytest
_pytest.python: pytest
_pytest.python_api: pytest
_pytest.python_path: pytest
_pytest.recwarn: pytest
_pytest.reports: pytest
_pytest.runner: pytest
_pytest.scope: pytest
_pytest.setuponly: pytest
_pytest.setupplan: pytest
_pytest.skipping: pytest
_pytest.stash: pytest
_pytest.stepwise: pytest
_pytest.terminal: pytest
_pytest.threadexception: pytest
_pytest.timing: pytest
_pytest.tmpdir: pytest
_pytest.unittest: pytest
_pytest.unraisableexception: pytest
_pytest.warning_types: pytest
_pytest.warnings: pytest
attr: attrs
attr.__init__: attrs
attr._cmp: attrs
attr._compat: attrs
attr._config: attrs
attr._funcs: attrs
attr._make: attrs
attr._next_gen: attrs
attr._version_info: attrs
attr.converters: attrs
attr.exceptions: attrs
attr.filters: attrs
attr.setters: attrs
attr.validators: attrs
attrs: attrs
attrs.__init__: attrs
attrs.converters: attrs
attrs.exceptions: attrs
attrs.filters: attrs
attrs.setters: attrs
attrs.validators: attrs
certifi: certifi
certifi.__init__: certifi
certifi.__main__: certifi
Expand DownExpand Up@@ -61,6 +153,89 @@ manifest:
idna.intranges: idna
idna.package_data: idna
idna.uts46data: idna
iniconfig: iniconfig
iniconfig.__init__: iniconfig
packaging: packaging
packaging.__about__: packaging
packaging.__init__: packaging
packaging._manylinux: packaging
packaging._musllinux: packaging
packaging._structures: packaging
packaging.markers: packaging
packaging.requirements: packaging
packaging.specifiers: packaging
packaging.tags: packaging
packaging.utils: packaging
packaging.version: packaging
pluggy: pluggy
pluggy.__init__: pluggy
pluggy._callers: pluggy
pluggy._hooks: pluggy
pluggy._manager: pluggy
pluggy._result: pluggy
pluggy._tracing: pluggy
pluggy._version: pluggy
py: py
py.__init__: py
py.__metainfo: py
py._builtin: py
py._code: py
py._code.__init__: py
py._code._assertionnew: py
py._code._assertionold: py
py._code._py2traceback: py
py._code.assertion: py
py._code.code: py
py._code.source: py
py._error: py
py._io: py
py._io.__init__: py
py._io.capture: py
py._io.saferepr: py
py._io.terminalwriter: py
py._log: py
py._log.__init__: py
py._log.log: py
py._log.warning: py
py._path: py
py._path.__init__: py
py._path.cacheutil: py
py._path.common: py
py._path.local: py
py._path.svnurl: py
py._path.svnwc: py
py._process: py
py._process.__init__: py
py._process.cmdexec: py
py._process.forkedfunc: py
py._process.killproc: py
py._std: py
py._vendored_packages: py
py._vendored_packages.__init__: py
py._vendored_packages.apipkg: py
py._vendored_packages.apipkg.__init__: py
py._vendored_packages.apipkg.version: py
py._vendored_packages.iniconfig: py
py._vendored_packages.iniconfig.__init__: py
py._version: py
py._xmlgen: py
py.test: py
pyparsing: pyparsing
pyparsing.__init__: pyparsing
pyparsing.actions: pyparsing
pyparsing.common: pyparsing
pyparsing.core: pyparsing
pyparsing.diagram: pyparsing
pyparsing.diagram.__init__: pyparsing
pyparsing.exceptions: pyparsing
pyparsing.helpers: pyparsing
pyparsing.results: pyparsing
pyparsing.testing: pyparsing
pyparsing.unicode: pyparsing
pyparsing.util: pyparsing
pytest: pytest
pytest.__init__: pytest
pytest.__main__: pytest
requests: requests
requests.__init__: requests
requests.__version__: requests
Expand All@@ -80,6 +255,11 @@ manifest:
requests.status_codes: requests
requests.structures: requests
requests.utils: requests
tomli: tomli
tomli.__init__: tomli
tomli._parser: tomli
tomli._re: tomli
tomli._types: tomli
urllib3: urllib3
urllib3.__init__: urllib3
urllib3._collections: urllib3
Expand DownExpand Up@@ -129,4 +309,4 @@ manifest:
pip_repository:
name: pip
incremental: true
integrity: c47bf2ca0a185cf6b8815d4a61e26e7457564e931de76c70653277e4eccfadc8
integrity: 71c787ef3d0dca470bb9c1913a5ef3ca4373db13cbd24acada1c7fcb40ef10ee
1 change: 1 addition & 0 deletions examples/build_file_generation/requirements.txt
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
requests==2.25.1
pytest~=7.1
34 changes: 33 additions & 1 deletion examples/build_file_generation/requirements_lock.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,8 +2,12 @@
# This file is autogenerated by pip-compile with python 3.9
# To update, run:
#
# pip-compile --generate-hashes --output-file=requirements_lock.txt requirements.txt
# bazel run //:pip_compile.update
#
attrs==21.4.0 \
--hash=sha256:2d27e3784d7a565d36ab851fe94887c5eccd6a463168875832a1be79c82828b4 \
--hash=sha256:626ba8234211db98e869df76230a137c4c40a12d72445c45d5f5b716f076e2fd
# via pytest
certifi==2020.12.5 \
--hash=sha256:1a4995114262bffbc2413b159f2a1a480c969de6e6eb13ee966d470af86af59c \
--hash=sha256:719a74fb9e33b9bd44cc7f3a8d94bc35e4049deebe19ba7d8e108280cfd59830
Expand All@@ -16,10 +20,38 @@ idna==2.10 \
--hash=sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6 \
--hash=sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0
# via requests
iniconfig==1.1.1 \
--hash=sha256:011e24c64b7f47f6ebd835bb12a743f2fbe9a26d4cecaa7f53bc4f35ee9da8b3 \
--hash=sha256:bc3af051d7d14b2ee5ef9969666def0cd1a000e121eaea580d4a313df4b37f32
# via pytest
packaging==21.3 \
--hash=sha256:dd47c42927d89ab911e606518907cc2d3a1f38bbd026385970643f9c5b8ecfeb \
--hash=sha256:ef103e05f519cdc783ae24ea4e2e0f508a9c99b2d4969652eed6a2e1ea5bd522
# via pytest
pluggy==1.0.0 \
--hash=sha256:4224373bacce55f955a878bf9cfa763c1e360858e330072059e10bad68531159 \
--hash=sha256:74134bbf457f031a36d68416e1509f34bd5ccc019f0bcc952c7b909d06b37bd3
# via pytest
py==1.11.0 \
--hash=sha256:51c75c4126074b472f746a24399ad32f6053d1b34b68d2fa41e558e6f4a98719 \
--hash=sha256:607c53218732647dff4acdfcd50cb62615cedf612e72d1724fb1a0cc6405b378
# via pytest
pyparsing==3.0.9 \
--hash=sha256:2b020ecf7d21b687f219b71ecad3631f644a47f01403fa1d1036b0c6416d70fb \
--hash=sha256:5026bae9a10eeaefb61dab2f09052b9f4307d44aee4eda64b309723d8d206bbc
# via packaging
pytest==7.1.2 \
--hash=sha256:13d0e3ccfc2b6e26be000cb6568c832ba67ba32e719443bfe725814d3c42433c \
--hash=sha256:a06a0425453864a270bc45e71f783330a7428defb4230fb5e6a731fde06ecd45
# via -r requirements.txt
requests==2.25.1 \
--hash=sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804 \
--hash=sha256:c210084e36a42ae6b9219e00e48287def368a26d03a048ddad7bfee44f75871e
# via -r requirements.txt
tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
urllib3==1.26.5 \
--hash=sha256:753a0374df26658f99d826cfe40394a686d05985786d946fbe4165b5148f5a7c \
--hash=sha256:a7acd0977125325f516bda9735fa7142b909a8d01e8b2e4c8108d0984e6e0098
Expand Down
4 changes: 4 additions & 0 deletions gazelle/README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -166,6 +166,10 @@ dependencies are added to the `deps` attribute.

Python test files are those ending in `_test.py`.

Gazelle will add a `py_test` target whenever it finds either of the following:
* A target in the same Bazel package named __test__. The macro `py_pytest_main` provide these entrypoint wrappers.
* A file in the same directory named __test__.py.

A `py_test` target is added containing all test files as `srcs`.

### Binaries
Expand Down
3 changes: 3 additions & 0 deletions python/defs.bzl
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,6 +27,7 @@ load(
_py_runtime = "py_runtime",
_py_test = "py_test",
)
load("//python/py_pytest_main:defs.bzl", _py_pytest_main = "py_pytest_main")

# Exports of native-defined providers.

Expand DownExpand Up@@ -140,3 +141,5 @@ py_binary = _py_binary
py_test = _py_test

py_runtime = _py_runtime

py_pytest_main = _py_pytest_main
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/BUILD
Original file line numberDiff line numberDiff line change
Expand Up@@ -54,6 +54,7 @@ bzl_library(
name = "defs",
srcs = [
"//python:defs.bzl",
"//python/py_pytest_main:defs.bzl",
"//python/private:reexports.bzl",
],
deps = [":bazel_python_tools"],
Expand Down
19 changes: 19 additions & 0 deletions docs/python.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion examples/build_file_generation/BUILD
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,14 @@ load("@pip//:requirements.bzl", "all_whl_requirements")
load("@rules_python//gazelle:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
load("@rules_python//gazelle/manifest:defs.bzl", "gazelle_python_manifest")
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_pytest_main", "py_test")

compile_pip_requirements(
name = "pip_compile",
requirements_in = "requirements.txt",
requirements_txt = "requirements_lock.txt",
)

# This rule fetches the metadata for python packages we depend on. That data is
# required for the gazelle_python_manifest rule to update our manifest file.
Expand DownExpand Up@@ -54,3 +61,22 @@ py_binary(
visibility = ["//:__subpackages__"],
deps = [":build_file_generation"],
)

# Generates an entrypoint for pytest that is picked up by Gazelle for the py_test target below.
py_pytest_main(
name = "__test__",
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

How come py_pytest_main becomes a dependency to a py_test, instead of subsuming the py_test rule?

Wholly replacing the py_test rule with a macro/rule/rule+macro seems a cleaner user interface.

@mattemmattemJun 10, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This provides an entry point for pytest, the Python test runner, and doesn't force changing loads or existing rules, and keepspy_test the bazel rule a generic "run a python binary that will return 0 or 1". There are other launchers, perhaps a nose entrypoint etc.

This model also fits with the gazelle generation already present here.


py_test(
name = "build_file_generation_test",
srcs = [
"foo_test.py",
":__test__",
],
main = ":__test__.py",
deps = [
":__test__",
":build_file_generation",
"@pip_pytest//:pkg",
],
)
9 changes: 9 additions & 0 deletions examples/build_file_generation/WORKSPACE
Original file line numberDiff line numberDiff line change
Expand Up@@ -48,10 +48,19 @@ local_repository(
path = "../..",
)

load("@rules_python//python:repositories.bzl", "python_register_toolchains")

python_register_toolchains(
name = "python39",
python_version = "3.9",
)

load("@python39//:defs.bzl", "interpreter")
load("@rules_python//python:pip.bzl", "pip_parse")

pip_parse(
name = "pip",
python_interpreter_target = interpreter,
requirements_lock = "//:requirements_lock.txt",
)

Expand Down
4 changes: 4 additions & 0 deletions examples/build_file_generation/foo_test.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
import pytest

def test_add():
assert 1 + 1 == 2, "Expected 1 + 1 to equal 2"
182 changes: 181 additions & 1 deletion examples/build_file_generation/gazelle_python.yaml
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,6 +5,98 @@

manifest:
modules_mapping:
_pytest: pytest
_pytest.__init__: pytest
_pytest._argcomplete: pytest
_pytest._code: pytest
_pytest._code.__init__: pytest
_pytest._code.code: pytest
_pytest._code.source: pytest
_pytest._io: pytest
_pytest._io.__init__: pytest
_pytest._io.saferepr: pytest
_pytest._io.terminalwriter: pytest
_pytest._io.wcwidth: pytest
_pytest._version: pytest
_pytest.assertion: pytest
_pytest.assertion.__init__: pytest
_pytest.assertion.rewrite: pytest
_pytest.assertion.truncate: pytest
_pytest.assertion.util: pytest
_pytest.cacheprovider: pytest
_pytest.capture: pytest
_pytest.compat: pytest
_pytest.config: pytest
_pytest.config.__init__: pytest
_pytest.config.argparsing: pytest
_pytest.config.compat: pytest
_pytest.config.exceptions: pytest
_pytest.config.findpaths: pytest
_pytest.debugging: pytest
_pytest.deprecated: pytest
_pytest.doctest: pytest
_pytest.faulthandler: pytest
_pytest.fixtures: pytest
_pytest.freeze_support: pytest
_pytest.helpconfig: pytest
_pytest.hookspec: pytest
_pytest.junitxml: pytest
_pytest.legacypath: pytest
_pytest.logging: pytest
_pytest.main: pytest
_pytest.mark: pytest
_pytest.mark.__init__: pytest
_pytest.mark.expression: pytest
_pytest.mark.structures: pytest
_pytest.monkeypatch: pytest
_pytest.nodes: pytest
_pytest.nose: pytest
_pytest.outcomes: pytest
_pytest.pastebin: pytest
_pytest.pathlib: pytest
_pytest.pytester: pytest
_pytest.pytester_assertions: pytest
_pytest.python: pytest
_pytest.python_api: pytest
_pytest.python_path: pytest
_pytest.recwarn: pytest
_pytest.reports: pytest
_pytest.runner: pytest
_pytest.scope: pytest
_pytest.setuponly: pytest
_pytest.setupplan: pytest
_pytest.skipping: pytest
_pytest.stash: pytest
_pytest.stepwise: pytest
_pytest.terminal: pytest
_pytest.threadexception: pytest
_pytest.timing: pytest
_pytest.tmpdir: pytest
_pytest.unittest: pytest
_pytest.unraisableexception: pytest
_pytest.warning_types: pytest
_pytest.warnings: pytest
attr: attrs
attr.__init__: attrs
attr._cmp: attrs
attr._compat: attrs
attr._config: attrs
attr._funcs: attrs
attr._make: attrs
attr._next_gen: attrs
attr._version_info: attrs
attr.converters: attrs
attr.exceptions: attrs
attr.filters: attrs
attr.setters: attrs
attr.validators: attrs
attrs: attrs
attrs.__init__: attrs
attrs.converters: attrs
attrs.exceptions: attrs
attrs.filters: attrs
attrs.setters: attrs
attrs.validators: attrs
certifi: certifi
certifi.__init__: certifi
certifi.__main__: certifi
Expand DownExpand Up@@ -61,6 +153,89 @@ manifest:
idna.intranges: idna
idna.package_data: idna
idna.uts46data: idna
iniconfig: iniconfig
iniconfig.__init__: iniconfig
packaging: packaging
packaging.__about__: packaging
packaging.__init__: packaging
packaging._manylinux: packaging
packaging._musllinux: packaging
packaging._structures: packaging
packaging.markers: packaging
packaging.requirements: packaging
packaging.specifiers: packaging
packaging.tags: packaging
packaging.utils: packaging
packaging.version: packaging
pluggy: pluggy
pluggy.__init__: pluggy
pluggy._callers: pluggy
pluggy._hooks: pluggy
pluggy._manager: pluggy
pluggy._result: pluggy
pluggy._tracing: pluggy
pluggy._version: pluggy
py: py
py.__init__: py
py.__metainfo: py
py._builtin: py
py._code: py
py._code.__init__: py
py._code._assertionnew: py
py._code._assertionold: py
py._code._py2traceback: py
py._code.assertion: py
py._code.code: py
py._code.source: py
py._error: py
py._io: py
py._io.__init__: py
py._io.capture: py
py._io.saferepr: py
py._io.terminalwriter: py
py._log: py
py._log.__init__: py
py._log.log: py
py._log.warning: py
py._path: py
py._path.__init__: py
py._path.cacheutil: py
py._path.common: py
py._path.local: py
py._path.svnurl: py
py._path.svnwc: py
py._process: py
py._process.__init__: py
py._process.cmdexec: py
py._process.forkedfunc: py
py._process.killproc: py
py._std: py
py._vendored_packages: py
py._vendored_packages.__init__: py
py._vendored_packages.apipkg: py
py._vendored_packages.apipkg.__init__: py
py._vendored_packages.apipkg.version: py
py._vendored_packages.iniconfig: py
py._vendored_packages.iniconfig.__init__: py
py._version: py
py._xmlgen: py
py.test: py
pyparsing: pyparsing
pyparsing.__init__: pyparsing
pyparsing.actions: pyparsing
pyparsing.common: pyparsing
pyparsing.core: pyparsing
pyparsing.diagram: pyparsing
pyparsing.diagram.__init__: pyparsing
pyparsing.exceptions: pyparsing
pyparsing.helpers: pyparsing
pyparsing.results: pyparsing
pyparsing.testing: pyparsing
pyparsing.unicode: pyparsing
pyparsing.util: pyparsing
pytest: pytest
pytest.__init__: pytest
pytest.__main__: pytest
requests: requests
requests.__init__: requests
requests.__version__: requests
Expand All@@ -80,6 +255,11 @@ manifest:
requests.status_codes: requests
requests.structures: requests
requests.utils: requests
tomli: tomli
tomli.__init__: tomli
tomli._parser: tomli
tomli._re: tomli
tomli._types: tomli
urllib3: urllib3
urllib3.__init__: urllib3
urllib3._collections: urllib3
Expand DownExpand Up@@ -129,4 +309,4 @@ manifest:
pip_repository:
name: pip
incremental: true
integrity: c47bf2ca0a185cf6b8815d4a61e26e7457564e931de76c70653277e4eccfadc8
integrity: 71c787ef3d0dca470bb9c1913a5ef3ca4373db13cbd24acada1c7fcb40ef10ee
1 change: 1 addition & 0 deletions examples/build_file_generation/requirements.txt
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
requests==2.25.1
pytest~=7.1
34 changes: 33 additions & 1 deletion examples/build_file_generation/requirements_lock.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,8 +2,12 @@
# This file is autogenerated by pip-compile with python 3.9
# To update, run:
#
# pip-compile --generate-hashes --output-file=requirements_lock.txt requirements.txt
# bazel run //:pip_compile.update
#
attrs==21.4.0 \
--hash=sha256:2d27e3784d7a565d36ab851fe94887c5eccd6a463168875832a1be79c82828b4 \
--hash=sha256:626ba8234211db98e869df76230a137c4c40a12d72445c45d5f5b716f076e2fd
# via pytest
certifi==2020.12.5 \
--hash=sha256:1a4995114262bffbc2413b159f2a1a480c969de6e6eb13ee966d470af86af59c \
--hash=sha256:719a74fb9e33b9bd44cc7f3a8d94bc35e4049deebe19ba7d8e108280cfd59830
Expand All@@ -16,10 +20,38 @@ idna==2.10 \
--hash=sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6 \
--hash=sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0
# via requests
iniconfig==1.1.1 \
--hash=sha256:011e24c64b7f47f6ebd835bb12a743f2fbe9a26d4cecaa7f53bc4f35ee9da8b3 \
--hash=sha256:bc3af051d7d14b2ee5ef9969666def0cd1a000e121eaea580d4a313df4b37f32
# via pytest
packaging==21.3 \
--hash=sha256:dd47c42927d89ab911e606518907cc2d3a1f38bbd026385970643f9c5b8ecfeb \
--hash=sha256:ef103e05f519cdc783ae24ea4e2e0f508a9c99b2d4969652eed6a2e1ea5bd522
# via pytest
pluggy==1.0.0 \
--hash=sha256:4224373bacce55f955a878bf9cfa763c1e360858e330072059e10bad68531159 \
--hash=sha256:74134bbf457f031a36d68416e1509f34bd5ccc019f0bcc952c7b909d06b37bd3
# via pytest
py==1.11.0 \
--hash=sha256:51c75c4126074b472f746a24399ad32f6053d1b34b68d2fa41e558e6f4a98719 \
--hash=sha256:607c53218732647dff4acdfcd50cb62615cedf612e72d1724fb1a0cc6405b378
# via pytest
pyparsing==3.0.9 \
--hash=sha256:2b020ecf7d21b687f219b71ecad3631f644a47f01403fa1d1036b0c6416d70fb \
--hash=sha256:5026bae9a10eeaefb61dab2f09052b9f4307d44aee4eda64b309723d8d206bbc
# via packaging
pytest==7.1.2 \
--hash=sha256:13d0e3ccfc2b6e26be000cb6568c832ba67ba32e719443bfe725814d3c42433c \
--hash=sha256:a06a0425453864a270bc45e71f783330a7428defb4230fb5e6a731fde06ecd45
# via -r requirements.txt
requests==2.25.1 \
--hash=sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804 \
--hash=sha256:c210084e36a42ae6b9219e00e48287def368a26d03a048ddad7bfee44f75871e
# via -r requirements.txt
tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
urllib3==1.26.5 \
--hash=sha256:753a0374df26658f99d826cfe40394a686d05985786d946fbe4165b5148f5a7c \
--hash=sha256:a7acd0977125325f516bda9735fa7142b909a8d01e8b2e4c8108d0984e6e0098
Expand Down
4 changes: 4 additions & 0 deletions gazelle/README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -166,6 +166,10 @@ dependencies are added to the `deps` attribute.

Python test files are those ending in `_test.py`.

Gazelle will add a `py_test` target whenever it finds either of the following:
* A target in the same Bazel package named __test__. The macro `py_pytest_main` provide these entrypoint wrappers.
* A file in the same directory named __test__.py.

A `py_test` target is added containing all test files as `srcs`.

### Binaries
Expand Down
3 changes: 3 additions & 0 deletions python/defs.bzl
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,6 +27,7 @@ load(
_py_runtime = "py_runtime",
_py_test = "py_test",
)
load("//python/py_pytest_main:defs.bzl", _py_pytest_main = "py_pytest_main")

# Exports of native-defined providers.

Expand DownExpand Up@@ -140,3 +141,5 @@ py_binary = _py_binary
py_test = _py_test

py_runtime = _py_runtime

py_pytest_main = _py_pytest_main
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/BUILD
Original file line numberDiff line numberDiff line change
Expand Up@@ -54,6 +54,7 @@ bzl_library(
name = "defs",
srcs = [
"//python:defs.bzl",
"//python/py_pytest_main:defs.bzl",
"//python/private:reexports.bzl",
],
deps = [":bazel_python_tools"],
Expand Down
19 changes: 19 additions & 0 deletions docs/python.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion examples/build_file_generation/BUILD
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,14 @@ load("@pip//:requirements.bzl", "all_whl_requirements")
load("@rules_python//gazelle:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
load("@rules_python//gazelle/manifest:defs.bzl", "gazelle_python_manifest")
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_pytest_main", "py_test")

compile_pip_requirements(
name = "pip_compile",
requirements_in = "requirements.txt",
requirements_txt = "requirements_lock.txt",
)

# This rule fetches the metadata for python packages we depend on. That data is
# required for the gazelle_python_manifest rule to update our manifest file.
Expand DownExpand Up@@ -54,3 +61,22 @@ py_binary(
visibility = ["//:__subpackages__"],
deps = [":build_file_generation"],
)

# Generates an entrypoint for pytest that is picked up by Gazelle for the py_test target below.
py_pytest_main(
name = "__test__",
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

How come py_pytest_main becomes a dependency to a py_test, instead of subsuming the py_test rule?

Wholly replacing the py_test rule with a macro/rule/rule+macro seems a cleaner user interface.

@mattemmattemJun 10, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This provides an entry point for pytest, the Python test runner, and doesn't force changing loads or existing rules, and keepspy_test the bazel rule a generic "run a python binary that will return 0 or 1". There are other launchers, perhaps a nose entrypoint etc.

This model also fits with the gazelle generation already present here.


py_test(
name = "build_file_generation_test",
srcs = [
"foo_test.py",
":__test__",
],
main = ":__test__.py",
deps = [
":__test__",
":build_file_generation",
"@pip_pytest//:pkg",
],
)
9 changes: 9 additions & 0 deletions examples/build_file_generation/WORKSPACE
Original file line numberDiff line numberDiff line change
Expand Up@@ -48,10 +48,19 @@ local_repository(
path = "../..",
)

load("@rules_python//python:repositories.bzl", "python_register_toolchains")

python_register_toolchains(
name = "python39",
python_version = "3.9",
)

load("@python39//:defs.bzl", "interpreter")
load("@rules_python//python:pip.bzl", "pip_parse")

pip_parse(
name = "pip",
python_interpreter_target = interpreter,
requirements_lock = "//:requirements_lock.txt",
)

Expand Down
4 changes: 4 additions & 0 deletions examples/build_file_generation/foo_test.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
import pytest

def test_add():
assert 1 + 1 == 2, "Expected 1 + 1 to equal 2"
182 changes: 181 additions & 1 deletion examples/build_file_generation/gazelle_python.yaml
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,6 +5,98 @@

manifest:
modules_mapping:
_pytest: pytest
_pytest.__init__: pytest
_pytest._argcomplete: pytest
_pytest._code: pytest
_pytest._code.__init__: pytest
_pytest._code.code: pytest
_pytest._code.source: pytest
_pytest._io: pytest
_pytest._io.__init__: pytest
_pytest._io.saferepr: pytest
_pytest._io.terminalwriter: pytest
_pytest._io.wcwidth: pytest
_pytest._version: pytest
_pytest.assertion: pytest
_pytest.assertion.__init__: pytest
_pytest.assertion.rewrite: pytest
_pytest.assertion.truncate: pytest
_pytest.assertion.util: pytest
_pytest.cacheprovider: pytest
_pytest.capture: pytest
_pytest.compat: pytest
_pytest.config: pytest
_pytest.config.__init__: pytest
_pytest.config.argparsing: pytest
_pytest.config.compat: pytest
_pytest.config.exceptions: pytest
_pytest.config.findpaths: pytest
_pytest.debugging: pytest
_pytest.deprecated: pytest
_pytest.doctest: pytest
_pytest.faulthandler: pytest
_pytest.fixtures: pytest
_pytest.freeze_support: pytest
_pytest.helpconfig: pytest
_pytest.hookspec: pytest
_pytest.junitxml: pytest
_pytest.legacypath: pytest
_pytest.logging: pytest
_pytest.main: pytest
_pytest.mark: pytest
_pytest.mark.__init__: pytest
_pytest.mark.expression: pytest
_pytest.mark.structures: pytest
_pytest.monkeypatch: pytest
_pytest.nodes: pytest
_pytest.nose: pytest
_pytest.outcomes: pytest
_pytest.pastebin: pytest
_pytest.pathlib: pytest
_pytest.pytester: pytest
_pytest.pytester_assertions: pytest
_pytest.python: pytest
_pytest.python_api: pytest
_pytest.python_path: pytest
_pytest.recwarn: pytest
_pytest.reports: pytest
_pytest.runner: pytest
_pytest.scope: pytest
_pytest.setuponly: pytest
_pytest.setupplan: pytest
_pytest.skipping: pytest
_pytest.stash: pytest
_pytest.stepwise: pytest
_pytest.terminal: pytest
_pytest.threadexception: pytest
_pytest.timing: pytest
_pytest.tmpdir: pytest
_pytest.unittest: pytest
_pytest.unraisableexception: pytest
_pytest.warning_types: pytest
_pytest.warnings: pytest
attr: attrs
attr.__init__: attrs
attr._cmp: attrs
attr._compat: attrs
attr._config: attrs
attr._funcs: attrs
attr._make: attrs
attr._next_gen: attrs
attr._version_info: attrs
attr.converters: attrs
attr.exceptions: attrs
attr.filters: attrs
attr.setters: attrs
attr.validators: attrs
attrs: attrs
attrs.__init__: attrs
attrs.converters: attrs
attrs.exceptions: attrs
attrs.filters: attrs
attrs.setters: attrs
attrs.validators: attrs
certifi: certifi
certifi.__init__: certifi
certifi.__main__: certifi
Expand DownExpand Up@@ -61,6 +153,89 @@ manifest:
idna.intranges: idna
idna.package_data: idna
idna.uts46data: idna
iniconfig: iniconfig
iniconfig.__init__: iniconfig
packaging: packaging
packaging.__about__: packaging
packaging.__init__: packaging
packaging._manylinux: packaging
packaging._musllinux: packaging
packaging._structures: packaging
packaging.markers: packaging
packaging.requirements: packaging
packaging.specifiers: packaging
packaging.tags: packaging
packaging.utils: packaging
packaging.version: packaging
pluggy: pluggy
pluggy.__init__: pluggy
pluggy._callers: pluggy
pluggy._hooks: pluggy
pluggy._manager: pluggy
pluggy._result: pluggy
pluggy._tracing: pluggy
pluggy._version: pluggy
py: py
py.__init__: py
py.__metainfo: py
py._builtin: py
py._code: py
py._code.__init__: py
py._code._assertionnew: py
py._code._assertionold: py
py._code._py2traceback: py
py._code.assertion: py
py._code.code: py
py._code.source: py
py._error: py
py._io: py
py._io.__init__: py
py._io.capture: py
py._io.saferepr: py
py._io.terminalwriter: py
py._log: py
py._log.__init__: py
py._log.log: py
py._log.warning: py
py._path: py
py._path.__init__: py
py._path.cacheutil: py
py._path.common: py
py._path.local: py
py._path.svnurl: py
py._path.svnwc: py
py._process: py
py._process.__init__: py
py._process.cmdexec: py
py._process.forkedfunc: py
py._process.killproc: py
py._std: py
py._vendored_packages: py
py._vendored_packages.__init__: py
py._vendored_packages.apipkg: py
py._vendored_packages.apipkg.__init__: py
py._vendored_packages.apipkg.version: py
py._vendored_packages.iniconfig: py
py._vendored_packages.iniconfig.__init__: py
py._version: py
py._xmlgen: py
py.test: py
pyparsing: pyparsing
pyparsing.__init__: pyparsing
pyparsing.actions: pyparsing
pyparsing.common: pyparsing
pyparsing.core: pyparsing
pyparsing.diagram: pyparsing
pyparsing.diagram.__init__: pyparsing
pyparsing.exceptions: pyparsing
pyparsing.helpers: pyparsing
pyparsing.results: pyparsing
pyparsing.testing: pyparsing
pyparsing.unicode: pyparsing
pyparsing.util: pyparsing
pytest: pytest
pytest.__init__: pytest
pytest.__main__: pytest
requests: requests
requests.__init__: requests
requests.__version__: requests
Expand All@@ -80,6 +255,11 @@ manifest:
requests.status_codes: requests
requests.structures: requests
requests.utils: requests
tomli: tomli
tomli.__init__: tomli
tomli._parser: tomli
tomli._re: tomli
tomli._types: tomli
urllib3: urllib3
urllib3.__init__: urllib3
urllib3._collections: urllib3
Expand DownExpand Up@@ -129,4 +309,4 @@ manifest:
pip_repository:
name: pip
incremental: true
integrity: c47bf2ca0a185cf6b8815d4a61e26e7457564e931de76c70653277e4eccfadc8
integrity: 71c787ef3d0dca470bb9c1913a5ef3ca4373db13cbd24acada1c7fcb40ef10ee
1 change: 1 addition & 0 deletions examples/build_file_generation/requirements.txt
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
requests==2.25.1
pytest~=7.1
34 changes: 33 additions & 1 deletion examples/build_file_generation/requirements_lock.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,8 +2,12 @@
# This file is autogenerated by pip-compile with python 3.9
# To update, run:
#
# pip-compile --generate-hashes --output-file=requirements_lock.txt requirements.txt
# bazel run //:pip_compile.update
#
attrs==21.4.0 \
--hash=sha256:2d27e3784d7a565d36ab851fe94887c5eccd6a463168875832a1be79c82828b4 \
--hash=sha256:626ba8234211db98e869df76230a137c4c40a12d72445c45d5f5b716f076e2fd
# via pytest
certifi==2020.12.5 \
--hash=sha256:1a4995114262bffbc2413b159f2a1a480c969de6e6eb13ee966d470af86af59c \
--hash=sha256:719a74fb9e33b9bd44cc7f3a8d94bc35e4049deebe19ba7d8e108280cfd59830
Expand All@@ -16,10 +20,38 @@ idna==2.10 \
--hash=sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6 \
--hash=sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0
# via requests
iniconfig==1.1.1 \
--hash=sha256:011e24c64b7f47f6ebd835bb12a743f2fbe9a26d4cecaa7f53bc4f35ee9da8b3 \
--hash=sha256:bc3af051d7d14b2ee5ef9969666def0cd1a000e121eaea580d4a313df4b37f32
# via pytest
packaging==21.3 \
--hash=sha256:dd47c42927d89ab911e606518907cc2d3a1f38bbd026385970643f9c5b8ecfeb \
--hash=sha256:ef103e05f519cdc783ae24ea4e2e0f508a9c99b2d4969652eed6a2e1ea5bd522
# via pytest
pluggy==1.0.0 \
--hash=sha256:4224373bacce55f955a878bf9cfa763c1e360858e330072059e10bad68531159 \
--hash=sha256:74134bbf457f031a36d68416e1509f34bd5ccc019f0bcc952c7b909d06b37bd3
# via pytest
py==1.11.0 \
--hash=sha256:51c75c4126074b472f746a24399ad32f6053d1b34b68d2fa41e558e6f4a98719 \
--hash=sha256:607c53218732647dff4acdfcd50cb62615cedf612e72d1724fb1a0cc6405b378
# via pytest
pyparsing==3.0.9 \
--hash=sha256:2b020ecf7d21b687f219b71ecad3631f644a47f01403fa1d1036b0c6416d70fb \
--hash=sha256:5026bae9a10eeaefb61dab2f09052b9f4307d44aee4eda64b309723d8d206bbc
# via packaging
pytest==7.1.2 \
--hash=sha256:13d0e3ccfc2b6e26be000cb6568c832ba67ba32e719443bfe725814d3c42433c \
--hash=sha256:a06a0425453864a270bc45e71f783330a7428defb4230fb5e6a731fde06ecd45
# via -r requirements.txt
requests==2.25.1 \
--hash=sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804 \
--hash=sha256:c210084e36a42ae6b9219e00e48287def368a26d03a048ddad7bfee44f75871e
# via -r requirements.txt
tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
urllib3==1.26.5 \
--hash=sha256:753a0374df26658f99d826cfe40394a686d05985786d946fbe4165b5148f5a7c \
--hash=sha256:a7acd0977125325f516bda9735fa7142b909a8d01e8b2e4c8108d0984e6e0098
Expand Down
4 changes: 4 additions & 0 deletions gazelle/README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -166,6 +166,10 @@ dependencies are added to the `deps` attribute.

Python test files are those ending in `_test.py`.

Gazelle will add a `py_test` target whenever it finds either of the following:
* A target in the same Bazel package named __test__. The macro `py_pytest_main` provide these entrypoint wrappers.
* A file in the same directory named __test__.py.

A `py_test` target is added containing all test files as `srcs`.

### Binaries
Expand Down
3 changes: 3 additions & 0 deletions python/defs.bzl
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,6 +27,7 @@ load(
_py_runtime = "py_runtime",
_py_test = "py_test",
)
load("//python/py_pytest_main:defs.bzl", _py_pytest_main = "py_pytest_main")

# Exports of native-defined providers.

Expand DownExpand Up@@ -140,3 +141,5 @@ py_binary = _py_binary
py_test = _py_test

py_runtime = _py_runtime

py_pytest_main = _py_pytest_main
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/BUILD
Original file line numberDiff line numberDiff line change
Expand Up@@ -54,6 +54,7 @@ bzl_library(
name = "defs",
srcs = [
"//python:defs.bzl",
"//python/py_pytest_main:defs.bzl",
"//python/private:reexports.bzl",
],
deps = [":bazel_python_tools"],
Expand Down
19 changes: 19 additions & 0 deletions docs/python.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion examples/build_file_generation/BUILD
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,14 @@ load("@pip//:requirements.bzl", "all_whl_requirements")
load("@rules_python//gazelle:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
load("@rules_python//gazelle/manifest:defs.bzl", "gazelle_python_manifest")
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_pytest_main", "py_test")

compile_pip_requirements(
name = "pip_compile",
requirements_in = "requirements.txt",
requirements_txt = "requirements_lock.txt",
)

# This rule fetches the metadata for python packages we depend on. That data is
# required for the gazelle_python_manifest rule to update our manifest file.
Expand DownExpand Up@@ -54,3 +61,22 @@ py_binary(
visibility = ["//:__subpackages__"],
deps = [":build_file_generation"],
)

# Generates an entrypoint for pytest that is picked up by Gazelle for the py_test target below.
py_pytest_main(
name = "__test__",
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

How come py_pytest_main becomes a dependency to a py_test, instead of subsuming the py_test rule?

Wholly replacing the py_test rule with a macro/rule/rule+macro seems a cleaner user interface.

@mattemmattemJun 10, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This provides an entry point for pytest, the Python test runner, and doesn't force changing loads or existing rules, and keepspy_test the bazel rule a generic "run a python binary that will return 0 or 1". There are other launchers, perhaps a nose entrypoint etc.

This model also fits with the gazelle generation already present here.


py_test(
name = "build_file_generation_test",
srcs = [
"foo_test.py",
":__test__",
],
main = ":__test__.py",
deps = [
":__test__",
":build_file_generation",
"@pip_pytest//:pkg",
],
)
9 changes: 9 additions & 0 deletions examples/build_file_generation/WORKSPACE
Original file line numberDiff line numberDiff line change
Expand Up@@ -48,10 +48,19 @@ local_repository(
path = "../..",
)

load("@rules_python//python:repositories.bzl", "python_register_toolchains")

python_register_toolchains(
name = "python39",
python_version = "3.9",
)

load("@python39//:defs.bzl", "interpreter")
load("@rules_python//python:pip.bzl", "pip_parse")

pip_parse(
name = "pip",
python_interpreter_target = interpreter,
requirements_lock = "//:requirements_lock.txt",
)

Expand Down
4 changes: 4 additions & 0 deletions examples/build_file_generation/foo_test.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
import pytest

def test_add():
assert 1 + 1 == 2, "Expected 1 + 1 to equal 2"
182 changes: 181 additions & 1 deletion examples/build_file_generation/gazelle_python.yaml
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,6 +5,98 @@

manifest:
modules_mapping:
_pytest: pytest
_pytest.__init__: pytest
_pytest._argcomplete: pytest
_pytest._code: pytest
_pytest._code.__init__: pytest
_pytest._code.code: pytest
_pytest._code.source: pytest
_pytest._io: pytest
_pytest._io.__init__: pytest
_pytest._io.saferepr: pytest
_pytest._io.terminalwriter: pytest
_pytest._io.wcwidth: pytest
_pytest._version: pytest
_pytest.assertion: pytest
_pytest.assertion.__init__: pytest
_pytest.assertion.rewrite: pytest
_pytest.assertion.truncate: pytest
_pytest.assertion.util: pytest
_pytest.cacheprovider: pytest
_pytest.capture: pytest
_pytest.compat: pytest
_pytest.config: pytest
_pytest.config.__init__: pytest
_pytest.config.argparsing: pytest
_pytest.config.compat: pytest
_pytest.config.exceptions: pytest
_pytest.config.findpaths: pytest
_pytest.debugging: pytest
_pytest.deprecated: pytest
_pytest.doctest: pytest
_pytest.faulthandler: pytest
_pytest.fixtures: pytest
_pytest.freeze_support: pytest
_pytest.helpconfig: pytest
_pytest.hookspec: pytest
_pytest.junitxml: pytest
_pytest.legacypath: pytest
_pytest.logging: pytest
_pytest.main: pytest
_pytest.mark: pytest
_pytest.mark.__init__: pytest
_pytest.mark.expression: pytest
_pytest.mark.structures: pytest
_pytest.monkeypatch: pytest
_pytest.nodes: pytest
_pytest.nose: pytest
_pytest.outcomes: pytest
_pytest.pastebin: pytest
_pytest.pathlib: pytest
_pytest.pytester: pytest
_pytest.pytester_assertions: pytest
_pytest.python: pytest
_pytest.python_api: pytest
_pytest.python_path: pytest
_pytest.recwarn: pytest
_pytest.reports: pytest
_pytest.runner: pytest
_pytest.scope: pytest
_pytest.setuponly: pytest
_pytest.setupplan: pytest
_pytest.skipping: pytest
_pytest.stash: pytest
_pytest.stepwise: pytest
_pytest.terminal: pytest
_pytest.threadexception: pytest
_pytest.timing: pytest
_pytest.tmpdir: pytest
_pytest.unittest: pytest
_pytest.unraisableexception: pytest
_pytest.warning_types: pytest
_pytest.warnings: pytest
attr: attrs
attr.__init__: attrs
attr._cmp: attrs
attr._compat: attrs
attr._config: attrs
attr._funcs: attrs
attr._make: attrs
attr._next_gen: attrs
attr._version_info: attrs
attr.converters: attrs
attr.exceptions: attrs
attr.filters: attrs
attr.setters: attrs
attr.validators: attrs
attrs: attrs
attrs.__init__: attrs
attrs.converters: attrs
attrs.exceptions: attrs
attrs.filters: attrs
attrs.setters: attrs
attrs.validators: attrs
certifi: certifi
certifi.__init__: certifi
certifi.__main__: certifi
Expand DownExpand Up@@ -61,6 +153,89 @@ manifest:
idna.intranges: idna
idna.package_data: idna
idna.uts46data: idna
iniconfig: iniconfig
iniconfig.__init__: iniconfig
packaging: packaging
packaging.__about__: packaging
packaging.__init__: packaging
packaging._manylinux: packaging
packaging._musllinux: packaging
packaging._structures: packaging
packaging.markers: packaging
packaging.requirements: packaging
packaging.specifiers: packaging
packaging.tags: packaging
packaging.utils: packaging
packaging.version: packaging
pluggy: pluggy
pluggy.__init__: pluggy
pluggy._callers: pluggy
pluggy._hooks: pluggy
pluggy._manager: pluggy
pluggy._result: pluggy
pluggy._tracing: pluggy
pluggy._version: pluggy
py: py
py.__init__: py
py.__metainfo: py
py._builtin: py
py._code: py
py._code.__init__: py
py._code._assertionnew: py
py._code._assertionold: py
py._code._py2traceback: py
py._code.assertion: py
py._code.code: py
py._code.source: py
py._error: py
py._io: py
py._io.__init__: py
py._io.capture: py
py._io.saferepr: py
py._io.terminalwriter: py
py._log: py
py._log.__init__: py
py._log.log: py
py._log.warning: py
py._path: py
py._path.__init__: py
py._path.cacheutil: py
py._path.common: py
py._path.local: py
py._path.svnurl: py
py._path.svnwc: py
py._process: py
py._process.__init__: py
py._process.cmdexec: py
py._process.forkedfunc: py
py._process.killproc: py
py._std: py
py._vendored_packages: py
py._vendored_packages.__init__: py
py._vendored_packages.apipkg: py
py._vendored_packages.apipkg.__init__: py
py._vendored_packages.apipkg.version: py
py._vendored_packages.iniconfig: py
py._vendored_packages.iniconfig.__init__: py
py._version: py
py._xmlgen: py
py.test: py
pyparsing: pyparsing
pyparsing.__init__: pyparsing
pyparsing.actions: pyparsing
pyparsing.common: pyparsing
pyparsing.core: pyparsing
pyparsing.diagram: pyparsing
pyparsing.diagram.__init__: pyparsing
pyparsing.exceptions: pyparsing
pyparsing.helpers: pyparsing
pyparsing.results: pyparsing
pyparsing.testing: pyparsing
pyparsing.unicode: pyparsing
pyparsing.util: pyparsing
pytest: pytest
pytest.__init__: pytest
pytest.__main__: pytest
requests: requests
requests.__init__: requests
requests.__version__: requests
Expand All@@ -80,6 +255,11 @@ manifest:
requests.status_codes: requests
requests.structures: requests
requests.utils: requests
tomli: tomli
tomli.__init__: tomli
tomli._parser: tomli
tomli._re: tomli
tomli._types: tomli
urllib3: urllib3
urllib3.__init__: urllib3
urllib3._collections: urllib3
Expand DownExpand Up@@ -129,4 +309,4 @@ manifest:
pip_repository:
name: pip
incremental: true
integrity: c47bf2ca0a185cf6b8815d4a61e26e7457564e931de76c70653277e4eccfadc8
integrity: 71c787ef3d0dca470bb9c1913a5ef3ca4373db13cbd24acada1c7fcb40ef10ee
1 change: 1 addition & 0 deletions examples/build_file_generation/requirements.txt
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
requests==2.25.1
pytest~=7.1
34 changes: 33 additions & 1 deletion examples/build_file_generation/requirements_lock.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,8 +2,12 @@
# This file is autogenerated by pip-compile with python 3.9
# To update, run:
#
# pip-compile --generate-hashes --output-file=requirements_lock.txt requirements.txt
# bazel run //:pip_compile.update
#
attrs==21.4.0 \
--hash=sha256:2d27e3784d7a565d36ab851fe94887c5eccd6a463168875832a1be79c82828b4 \
--hash=sha256:626ba8234211db98e869df76230a137c4c40a12d72445c45d5f5b716f076e2fd
# via pytest
certifi==2020.12.5 \
--hash=sha256:1a4995114262bffbc2413b159f2a1a480c969de6e6eb13ee966d470af86af59c \
--hash=sha256:719a74fb9e33b9bd44cc7f3a8d94bc35e4049deebe19ba7d8e108280cfd59830
Expand All@@ -16,10 +20,38 @@ idna==2.10 \
--hash=sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6 \
--hash=sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0
# via requests
iniconfig==1.1.1 \
--hash=sha256:011e24c64b7f47f6ebd835bb12a743f2fbe9a26d4cecaa7f53bc4f35ee9da8b3 \
--hash=sha256:bc3af051d7d14b2ee5ef9969666def0cd1a000e121eaea580d4a313df4b37f32
# via pytest
packaging==21.3 \
--hash=sha256:dd47c42927d89ab911e606518907cc2d3a1f38bbd026385970643f9c5b8ecfeb \
--hash=sha256:ef103e05f519cdc783ae24ea4e2e0f508a9c99b2d4969652eed6a2e1ea5bd522
# via pytest
pluggy==1.0.0 \
--hash=sha256:4224373bacce55f955a878bf9cfa763c1e360858e330072059e10bad68531159 \
--hash=sha256:74134bbf457f031a36d68416e1509f34bd5ccc019f0bcc952c7b909d06b37bd3
# via pytest
py==1.11.0 \
--hash=sha256:51c75c4126074b472f746a24399ad32f6053d1b34b68d2fa41e558e6f4a98719 \
--hash=sha256:607c53218732647dff4acdfcd50cb62615cedf612e72d1724fb1a0cc6405b378
# via pytest
pyparsing==3.0.9 \
--hash=sha256:2b020ecf7d21b687f219b71ecad3631f644a47f01403fa1d1036b0c6416d70fb \
--hash=sha256:5026bae9a10eeaefb61dab2f09052b9f4307d44aee4eda64b309723d8d206bbc
# via packaging
pytest==7.1.2 \
--hash=sha256:13d0e3ccfc2b6e26be000cb6568c832ba67ba32e719443bfe725814d3c42433c \
--hash=sha256:a06a0425453864a270bc45e71f783330a7428defb4230fb5e6a731fde06ecd45
# via -r requirements.txt
requests==2.25.1 \
--hash=sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804 \
--hash=sha256:c210084e36a42ae6b9219e00e48287def368a26d03a048ddad7bfee44f75871e
# via -r requirements.txt
tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
urllib3==1.26.5 \
--hash=sha256:753a0374df26658f99d826cfe40394a686d05985786d946fbe4165b5148f5a7c \
--hash=sha256:a7acd0977125325f516bda9735fa7142b909a8d01e8b2e4c8108d0984e6e0098
Expand Down
4 changes: 4 additions & 0 deletions gazelle/README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -166,6 +166,10 @@ dependencies are added to the `deps` attribute.

Python test files are those ending in `_test.py`.

Gazelle will add a `py_test` target whenever it finds either of the following:
* A target in the same Bazel package named __test__. The macro `py_pytest_main` provide these entrypoint wrappers.
* A file in the same directory named __test__.py.

A `py_test` target is added containing all test files as `srcs`.

### Binaries
Expand Down
3 changes: 3 additions & 0 deletions python/defs.bzl
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,6 +27,7 @@ load(
_py_runtime = "py_runtime",
_py_test = "py_test",
)
load("//python/py_pytest_main:defs.bzl", _py_pytest_main = "py_pytest_main")

# Exports of native-defined providers.

Expand DownExpand Up@@ -140,3 +141,5 @@ py_binary = _py_binary
py_test = _py_test

py_runtime = _py_runtime

py_pytest_main = _py_pytest_main
Loading