-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currently WIP because: - `$(location)` and `$(rootpath)` make vars aren't working correctly - This is what we tried to fix with the `PYTHON_COVERAGE_TARGET` variable, which was ultimately not accepted. See also next point. - Does not use the new toolchain feature that supports adding coveragepy directly to the python toolchain - Added together with the new coveragepy support in bazelbuild/bazel#15590, and is probably better to use than the `$(rootpath)` and `pip_install` solution we're showing here - While I'd love to rewrite the blog post to show this instead, I'd need a bit more time for that - Python imports are wonky - For some reason, the sibling module can only be imported under a parent `python` module. I confirmed this by looking through `PYTHON_PATH` - Seems to be a Bazel regression? Strangely nobody is complaining about this yet, we should probably raise an issue - Not tested in remote execution - No access to an engflow setup to test this right this instant - Since we're not using a toolchain (and even if we were we're using `pip_install`), this requires a python on the host, so there's a decent chance it won't work - Requires bumping Bazel to a version with bazelbuild/bazel#15590 merged, which currently is only available in prereleases
- Loading branch information
Tristan Daniël Maat
committed
Nov 11, 2022
1 parent
163ad01
commit 8ea46d3
Showing
7 changed files
with
89 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
5.2.0 | ||
6.0.0-pre.20221020.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""Example of a python build/test with coverage. | ||
""" | ||
|
||
load("@coveragepy//:requirements.bzl", "entry_point") | ||
|
||
alias( | ||
name = "python_coverage_tools", | ||
actual = entry_point("coverage"), | ||
) | ||
|
||
py_library( | ||
name = "example", | ||
srcs = [ | ||
"__init__.py", | ||
"example.py", | ||
], | ||
) | ||
|
||
py_test( | ||
name = "test_example", | ||
size = "small", | ||
srcs = ["test_example.py"], | ||
env = { | ||
# In theory this should be `$(execpath :python_coverage_tool)`, | ||
# | ||
# however this will not resolve correctly as Bazel will | ||
# resolve this relative to the runfiles directory. Instead, we | ||
# just manually specify the path in the runfiles directory the | ||
# coverage tools will be placed in. | ||
# | ||
# For a better solution involving setting the coverage tool in | ||
# the toolchain, see | ||
# https://github.com/bazelbuild/bazel/pull/15590 | ||
"PYTHON_COVERAGE": "coveragepy_coverage/rules_python_wheel_entry_point_coverage", | ||
}, | ||
deps = [ | ||
":example", | ||
":python_coverage_tools", | ||
], | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
def fizzbuzz(i: int) -> str: | ||
if i % 3 == 0: | ||
if i % 5 == 0: | ||
return "FizzBuzz" | ||
else: | ||
return "Fizz" | ||
elif i % 5 == 0: | ||
return "Buzz" | ||
else: | ||
return str(i) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
coverage==6.5.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import unittest | ||
|
||
# Bazel seems to be setting the PYTHON_PATH wrong, resulting in a | ||
# parent "python" module. This might be a regression. | ||
from python.example import fizzbuzz | ||
|
||
|
||
class ExampleTest(unittest.TestCase): | ||
def test_fizzbuzz(self): | ||
self.assertEqual(fizzbuzz(1), "1") | ||
self.assertEqual(fizzbuzz(2), "2") | ||
self.assertEqual(fizzbuzz(3), "Fizz") | ||
self.assertEqual(fizzbuzz(4), "4") | ||
self.assertEqual(fizzbuzz(5), "Buzz") | ||
self.assertEqual(fizzbuzz(15), "FizzBuzz") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |