Skip to content

Commit 987b29d

Browse files
authored
improve test coverage (#4529)
1 parent 7d7aad7 commit 987b29d

File tree

6 files changed

+512
-472
lines changed

6 files changed

+512
-472
lines changed

.bazelrc

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,25 @@ build:windows --copt='-Wno-unused-command-line-argument' --host_copt='-Wno-unuse
395395
# set the correct value. We currently don't check __cplusplus, but some dependencies do.
396396
build:windows --cxxopt='/Zc:__cplusplus' --host_cxxopt='/Zc:__cplusplus'
397397

398-
# enable clang coverage: https://clang.llvm.org/docs/SourceBasedCodeCoverage.html
399-
build:clang-coverage --copt="-fprofile-instr-generate" --linkopt="-fprofile-instr-generate"
400-
build:clang-coverage --copt="-fcoverage-mapping" --linkopt="-fcoverage-mapping"
401-
# disabled due to clang: warning: argument unused during compilation: '--coverage'
402-
build:clang-coverage --copt='-Wno-unused-command-line-argument' --host_copt='-Wno-unused-command-line-argument'
398+
build:coverage --action_env=GCOV=llvm-profdata-19
399+
build:coverage --action_env=BAZEL_USE_LLVM_NATIVE_COVERAGE=1
400+
build:coverage --copt=-DNDEBUG
401+
build:coverage --combined_report=lcov
402+
build:coverage --strategy=TestRunner=remote,sandboxed,local
403+
build:coverage --strategy=CoverageReport=sandboxed,local
404+
build:coverage --experimental_use_llvm_covmap
405+
build:coverage --experimental_generate_llvm_lcov
406+
build:coverage --experimental_fetch_all_coverage_outputs
407+
build:coverage --collect_code_coverage
408+
build:coverage --instrumentation_filter="^//,-//external"
409+
build:coverage --remote_download_minimal
410+
build:coverage --define=tcmalloc=gperftools
411+
build:coverage --test_timeout=120,120,120,120
412+
build:coverage --instrument_test_targets
403413

404414
# This config is defined internally and enabled on many machines.
405415
# Defining it as empty just so these machines can run build commands from the workerd repo
406416
build:rosetta-arm64 --define=rosetta_arm64_no_op=1
407417

408-
coverage --test_timeout=120,120,120,120 --strategy=CoverageReport=local --instrument_test_targets --combined_report=lcov
418+
coverage --build_tests_only
419+
coverage --config=coverage

build/wd_cc_binary.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ def wd_cc_binary(
4242
"@//build/config:prebuilt_binaries_arm64": "@//:bin.arm64/tmp/{}/{}.aarch64-linux-gnu".format(pkg, prebuilt_binary_name),
4343
"//conditions:default": name,
4444
}),
45+
# Propagate InstrumentedFilesInfo through the alias for coverage support
46+
testonly = kwargs.get("testonly", False),
4547
)

build/wd_test.bzl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def wd_test(
5454
] + args
5555

5656
_wd_test(
57+
src = src,
5758
name = name,
5859
data = data,
5960
args = args,
@@ -173,17 +174,43 @@ def _wd_test_impl(ctx):
173174
if default_runfiles:
174175
runfiles = runfiles.merge(default_runfiles)
175176

177+
# IMPORTANT: The workerd binary must be listed in dependency_attributes
178+
# to ensure its transitive dependencies (all the C++ source files) are
179+
# included in the coverage instrumentation. Without this, coverage data
180+
# won't be collected for the actual workerd implementation code.
181+
instrumented_files_info = coverage_common.instrumented_files_info(
182+
ctx,
183+
source_attributes = ["src", "data"],
184+
dependency_attributes = ["workerd", "sidecar", "sidecar_supervisor"],
185+
# Include all file types that might contain testable code
186+
extensions = ["cc", "c++", "cpp", "cxx", "c", "h", "hh", "hpp", "hxx", "inc", "js", "ts", "mjs", "wd-test", "capnp"],
187+
)
188+
176189
return [
177190
DefaultInfo(
178191
executable = executable,
179192
runfiles = runfiles,
180193
),
194+
instrumented_files_info,
181195
]
182196

183197
_wd_test = rule(
184198
implementation = _wd_test_impl,
185199
test = True,
186200
attrs = {
201+
# Implicit dependencies used by Bazel to generate coverage reports.
202+
"_lcov_merger": attr.label(
203+
default = configuration_field(fragment = "coverage", name = "output_generator"),
204+
executable = True,
205+
cfg = config.exec(exec_group = "test"),
206+
),
207+
"_collect_cc_coverage": attr.label(
208+
default = "@bazel_tools//tools/test:collect_cc_coverage",
209+
executable = True,
210+
cfg = config.exec(exec_group = "test"),
211+
),
212+
# Source file
213+
"src": attr.label(allow_single_file = True),
187214
# The workerd executable is used to run all tests
188215
"workerd": attr.label(
189216
allow_single_file = True,
@@ -216,7 +243,7 @@ _wd_test = rule(
216243
allow_single_file = True,
217244
executable = True,
218245
cfg = "exec",
219-
default = "//src/workerd/api/node:sidecar-supervisor",
246+
default = "//src/workerd/api/node/tests:sidecar-supervisor",
220247
),
221248
"python_snapshot_test": attr.bool(),
222249
# A reference to the Windows platform label, needed for the implementation of wd_test

justfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ eslint:
112112
just stream-test //src/node:node@eslint
113113

114114
coverage path="//...":
115-
bazel coverage --nocache_test_results --config=clang-coverage --instrumentation_filter="^//,-//external" {{path}}
116-
genhtml --branch-coverage --synthesize-missing --keep-going --output coverage "$(bazel info output_path)/_coverage/_coverage_report.dat"
115+
bazel coverage {{path}}
116+
genhtml --branch-coverage --output coverage "$(bazel info output_path)/_coverage/_coverage_report.dat"
117117
open coverage/index.html
118118

119119
profile path:

0 commit comments

Comments
 (0)