From 30783f5b640bda264ccfd660f0999648505924fe Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Thu, 3 Oct 2024 10:51:04 -0700 Subject: [PATCH] chore: support pytest --- .github/workflows/ci.yaml | 2 ++ scaffold.yaml | 1 + {{ .ProjectSnake }}/.aspect/cli/config.yaml | 4 +++- .../.aspect/cli/pytest_main.star | 21 +++++++++++++++++++ {{ .ProjectSnake }}/BUILD.bazel | 4 ++++ {{ .ProjectSnake }}/repin.sh | 10 +++++++-- 6 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 {{ .ProjectSnake }}/.aspect/cli/pytest_main.star diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index db10b66..438a7ad 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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' }}" diff --git a/scaffold.yaml b/scaffold.yaml index 34967a2..b69d69e 100644 --- a/scaffold.yaml +++ b/scaffold.yaml @@ -86,6 +86,7 @@ features: - "*/requirements/**" - "*/pyproject.toml" - "*/gazelle_python.yaml" + - "*/.aspect/cli/py*.star" - value: "{{ .Computed.go }}" globs: - "*/go.mod" diff --git a/{{ .ProjectSnake }}/.aspect/cli/config.yaml b/{{ .ProjectSnake }}/.aspect/cli/config.yaml index a62f51f..548ef8a 100644 --- a/{{ .ProjectSnake }}/.aspect/cli/config.yaml +++ b/{{ .ProjectSnake }}/.aspect/cli/config.yaml @@ -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 }} diff --git a/{{ .ProjectSnake }}/.aspect/cli/pytest_main.star b/{{ .ProjectSnake }}/.aspect/cli/pytest_main.star new file mode 100644 index 0000000..26ccdf5 --- /dev/null +++ b/{{ .ProjectSnake }}/.aspect/cli/pytest_main.star @@ -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"], + }, + ), +) diff --git a/{{ .ProjectSnake }}/BUILD.bazel b/{{ .ProjectSnake }}/BUILD.bazel index 19a0731..c73279d 100644 --- a/{{ .ProjectSnake }}/BUILD.bazel +++ b/{{ .ProjectSnake }}/BUILD.bazel @@ -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", diff --git a/{{ .ProjectSnake }}/repin.sh b/{{ .ProjectSnake }}/repin.sh index 861ba73..7284ffe 100755 --- a/{{ .ProjectSnake }}/repin.sh +++ b/{{ .ProjectSnake }}/repin.sh @@ -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