Skip to content

Commit

Permalink
chore: support pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Oct 3, 2024
1 parent 93c0044 commit 30783f5
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ jobs:
run: |
mkdir app
>app/__main__.py echo -e 'import requests\nprint(requests.get("https://api.github.com").text)'
>app/app_test.py echo -e 'test_bad():\n assert 1 == 2'
sed -i 's/dependencies = \[/dependencies = ["requests",/' pyproject.toml
./repin.sh
bazel run //app:app_bin
bazel test //app:app_test
working-directory: "${{ steps.scaffold.outputs.dir }}"
if: "${{ matrix.preset == 'py' }}"

Expand Down
1 change: 1 addition & 0 deletions scaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ features:
- "*/requirements/**"
- "*/pyproject.toml"
- "*/gazelle_python.yaml"
- "*/.aspect/cli/py*.star"
- value: "{{ .Computed.go }}"
globs:
- "*/go.mod"
Expand Down
4 changes: 3 additions & 1 deletion {{ .ProjectSnake }}/.aspect/cli/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ configure:
{{- if .Computed.javascript }}
- .aspect/cli/package-json-scripts.star
{{- end }}

{{- if .Computed.python }}
- .aspect/cli/pytest_main.star
{{- end }}
{{- if and .Computed.go .Scaffold.oci }}
- .aspect/cli/go_image.star
{{- end }}
Expand Down
21 changes: 21 additions & 0 deletions {{ .ProjectSnake }}/.aspect/cli/pytest_main.star
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"Generate py_pytest_main targets"
aspect.register_rule_kind("py_pytest_main", {
"From": "@aspect_rules_py//py:defs.bzl",
"ResolveAttrs": ["deps"],
})

aspect.register_configure_extension(
id = "pytest_main",
prepare = lambda cfg: aspect.PrepareResult(
sources = [
aspect.SourceGlobs("*_test.py"),
],
),
declare = lambda ctx: ctx.targets.add(
kind = "py_pytest_main",
name = "__test__",
attrs = {
"deps": ["@pip//pytest"],
},
),
)
4 changes: 4 additions & 0 deletions {{ .ProjectSnake }}/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ js_library(
# aspect:map_kind py_binary py_binary @aspect_rules_py//py:defs.bzl
# aspect:map_kind py_library py_library @aspect_rules_py//py:defs.bzl
# aspect:map_kind py_test py_test @aspect_rules_py//py:defs.bzl
#
# See https://github.com/bazelbuild/rules_python/pull/2044
# gazelle:python_generation_mode_per_package_require_test_entry_point
#
# Fetches metadata for python packages we depend on.
modules_mapping(
name = "modules_map",
Expand Down
10 changes: 8 additions & 2 deletions {{ .ProjectSnake }}/repin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@ bazel run //requirements:runtime.update
bazel run //requirements:requirements.all.update
bazel run //:gazelle_python_manifest.update
{{- end }}
# Exits 110 on success
bazel configure || true

# configure exits 110 on success
set +o errexit

bazel configure
# Workaround: aspect configure runs "native" Go extensions first,
# so we much run a second time for them to see the py_pytest_main we generate
bazel configure

0 comments on commit 30783f5

Please sign in to comment.