Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add python+coverage example #46

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ build:engflow_common --java_language_version=11

build:without_bytes --experimental_remote_download_outputs=minimal

build:remote_coverage --strategy=CoverageReport=local,remote
build:remote_coverage --combined_report=lcov
build:remote_coverage --nocache_test_results
build:remote_coverage --experimental_split_coverage_postprocessing
build:remote_coverage --experimental_fetch_all_coverage_outputs

# Options for a private EngFlow cluster.
# - Change "10.0.0.10:8080" to the actual cluster IP and port.
# - You'll also need to set flags for your authentication method.
Expand Down
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.2.0
6.0.0-pre.20221020.1
32 changes: 18 additions & 14 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

http_archive(
name = "rules_python",
sha256 = "8c8fe44ef0a9afc256d1e75ad5f448bb59b81aba149b8958f02f7b3a98f5d9b4",
strip_prefix = "rules_python-0.13.0",
url = "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.13.0.tar.gz",
)

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

pip_parse(
name = "coveragepy",
requirements = "//python:requirements.txt",
)

load("@coveragepy//:requirements.bzl", "install_deps")

install_deps()

# Some file dependencies
http_file(
name = "emacs",
Expand Down Expand Up @@ -167,17 +185,3 @@ load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_depe
go_rules_dependencies()

go_register_toolchains(version = "1.19.3")

http_archive(
name = "io_bazel_rules_kotlin",
sha256 = "a57591404423a52bd6b18ebba7979e8cd2243534736c5c94d35c89718ea38f94",
urls = ["https://github.com/bazelbuild/rules_kotlin/releases/download/v1.6.0/rules_kotlin_release.tgz"],
)

load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories")

kotlin_repositories()

load("@io_bazel_rules_kotlin//kotlin:core.bzl", "kt_register_toolchains")

kt_register_toolchains()
40 changes: 40 additions & 0 deletions python/BUILD
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 added python/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions python/example.py
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)
1 change: 1 addition & 0 deletions python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage==6.5.0
19 changes: 19 additions & 0 deletions python/test_example.py
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()