Skip to content

Commit 68ddfc8

Browse files
EhrjaCopilot
andcommitted
Derive lobster Given/When/Then from gtest test names
lobster-gtest learned to derive the Given/When/Then specification from conventionally named test cases of the form [<Prefix>_]Given<X>[_When<Y>]_(Expect|Then)<Z> Enable that option for the gtest traces collected by the component rule, so tests no longer have to repeat the information as record properties. Record properties still take precedence over the test name. The option does not exist before lobster 1.0.7, and passing an unknown keyword argument to a subrule is an analysis error. Since Starlark cannot introspect a subrule signature, a module extension probes lobster's own rule definition and exposes the result as a load-time constant. Versions without the option keep the previous behaviour, and bumping to a version that provides it enables the derivation without a further change here. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent abcb983 commit 68ddfc8

3 files changed

Lines changed: 85 additions & 1 deletion

File tree

‎MODULE.bazel‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ bazel_dep(name = "flatbuffers", version = "25.9.23")
3838
bazel_dep(name = "download_utils", version = "1.2.2")
3939
bazel_dep(name = "trlc", version = "3.0.1")
4040
bazel_dep(name = "lobster", version = "1.0.6")
41+
42+
# Probes which optional features the resolved lobster module provides, so that
43+
# the rules can keep working with versions that predate them.
44+
lobster_capabilities = use_extension(
45+
"//bazel/rules/rules_score/private:lobster_capabilities.bzl",
46+
"lobster_capabilities",
47+
)
48+
use_repo(lobster_capabilities, "lobster_capabilities")
4149
bazel_dep(name = "rules_distroless", version = "0.8.0")
4250
bazel_dep(name = "googletest", version = "1.17.0.bcr.2")
4351

‎bazel/rules/rules_score/private/component.bzl‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ with associated requirements and tests.
2020
"""
2121

2222
load("@lobster//:lobster.bzl", "subrule_lobster_gtest", "subrule_lobster_html_report", "subrule_lobster_report")
23+
load("@lobster_capabilities//:capabilities.bzl", "SUPPORTS_DERIVE_GWT_FROM_TEST_NAME")
2324
load("//bazel/rules/rules_score:providers.bzl", "AssumedSystemRequirementsInfo", "CertifiedScope", "ComponentInfo", "ComponentRequirementsInfo", "ComponentTestCaseCoverageInfo", "FeatureRequirementsInfo", "SphinxSourcesInfo", "UnitInfo")
2425
load("//bazel/rules/rules_score/private:lobster_config.bzl", "format_lobster_sources")
2526

@@ -243,7 +244,15 @@ def _component_impl(ctx):
243244
collected_architecture.append(component[ComponentInfo].architecture)
244245

245246
# Convert unit test XML files to a single gtest.lobster file
246-
gtest_lobster_file, provider = subrule_lobster_gtest(depset(transitive = collected_unit_test_files).to_list())
247+
# Given/When/Then is derived from conventionally named test cases, so that
248+
# tests do not have to repeat the information as record properties. Older
249+
# lobster versions do not offer the option and keep relying on the record
250+
# properties alone.
251+
derive_gwt = {"derive_gwt_from_test_name": True} if SUPPORTS_DERIVE_GWT_FROM_TEST_NAME else {}
252+
gtest_lobster_file, provider = subrule_lobster_gtest(
253+
depset(transitive = collected_unit_test_files).to_list(),
254+
**derive_gwt
255+
)
247256

248257
# -------------------------------------------------------------------------
249258
# Architecture Lobster: generate architecture.lobster for this component
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# *******************************************************************************
2+
# Copyright (c) 2026 Contributors to the Eclipse Foundation
3+
#
4+
# See the NOTICE file(s) distributed with this work for additional
5+
# information regarding copyright ownership.
6+
#
7+
# This program and the accompanying materials are made available under the
8+
# terms of the Apache License Version 2.0 which is available at
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# SPDX-License-Identifier: Apache-2.0
12+
# *******************************************************************************
13+
"""Detect optional features of the resolved ``lobster`` module.
14+
15+
``subrule_lobster_gtest`` gained the keyword argument
16+
``derive_gwt_from_test_name`` after the 1.0.6 release. Passing an unknown
17+
keyword argument to a subrule is an analysis error, and Starlark offers no way
18+
to introspect a subrule's signature, so the capability is probed by reading
19+
lobster's own rule definition.
20+
21+
The result is exposed as a load-time constant through a generated repository,
22+
which lets the call sites choose the matching invocation. Pinning a lobster
23+
version that predates the feature therefore degrades to the previous behaviour
24+
instead of breaking the build, and bumping to a version that provides it
25+
enables the feature without any further change.
26+
"""
27+
28+
_DERIVE_GWT_FROM_TEST_NAME = "derive_gwt_from_test_name"
29+
30+
# Definition of the gtest rule inside the lobster module. A restructuring of
31+
# lobster makes the probe report "unsupported", which is the safe answer.
32+
_LOBSTER_GTEST_BZL = Label("@lobster//bazel/private:lobster_gtest.bzl")
33+
34+
_CAPABILITIES_TEMPLATE = """\
35+
\"\"\"Generated by //bazel/rules/rules_score/private:lobster_capabilities.bzl.
36+
37+
Do not edit. Describes the features of the lobster module that the current
38+
dependency resolution selected.
39+
\"\"\"
40+
41+
# Whether subrule_lobster_gtest accepts the keyword argument
42+
# "derive_gwt_from_test_name".
43+
SUPPORTS_DERIVE_GWT_FROM_TEST_NAME = {derive_gwt_from_test_name}
44+
"""
45+
46+
def _lobster_capabilities_repo_impl(repository_ctx):
47+
definition = repository_ctx.read(repository_ctx.path(_LOBSTER_GTEST_BZL))
48+
repository_ctx.file("BUILD.bazel", "")
49+
repository_ctx.file(
50+
"capabilities.bzl",
51+
_CAPABILITIES_TEMPLATE.format(
52+
derive_gwt_from_test_name = repr(_DERIVE_GWT_FROM_TEST_NAME in definition),
53+
),
54+
)
55+
56+
_lobster_capabilities_repo = repository_rule(
57+
implementation = _lobster_capabilities_repo_impl,
58+
doc = "Generates a .bzl file holding the probed lobster feature flags.",
59+
)
60+
61+
def _lobster_capabilities_impl(_module_ctx):
62+
_lobster_capabilities_repo(name = "lobster_capabilities")
63+
64+
lobster_capabilities = module_extension(
65+
implementation = _lobster_capabilities_impl,
66+
doc = "Provides the @lobster_capabilities repository.",
67+
)

0 commit comments

Comments
 (0)