File: make_coverage_out

package info (click to toggle)
emacs-bazel-mode 0.0~git20230919.769b30d-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 488 kB
  • sloc: lisp: 3,487; sh: 65; makefile: 32; awk: 18
file content (77 lines) | stat: -rwxr-xr-x 2,981 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash

# Copyright 2021, 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Run this script to regenerate the Bazel output and coverage file in
# coverage.org.

set -eu

# Bazel formats output differently when invoked from Emacs.
export INSIDE_EMACS='28.1,compile'

workspace="$(bazel info workspace)" || exit
testdata="${workspace:?}/testdata"
org="${testdata:?}/coverage.org"

dir="$(mktemp -d)" || exit
trap 'rm -r -f -- "${dir}"' EXIT

# Canonicalize the directory so that the textual replacement below doesn’t
# choke on symbolic links.
dir="$(realpath -e -- "${dir:?}")" || exit

# Set up a basic workspace layout in the temporary directory.
emacs --quick --batch --visit="${org:?}" \
  --eval="(let ((default-directory \"${dir:?}/\")) (org-babel-tangle))" || exit

# Remove tangled “bazel-out” directory containing the previous coverage data.
# This would interfere with the Bazel run below.
rm -r -- "${dir:?}/bazel-out"

execroot="$(cd "${dir:?}" && bazel --bazelrc=/dev/null info execution_root)"
testlogs="$(cd "${dir:?}" && bazel --bazelrc=/dev/null info bazel-testlogs)"

# We explicitly compile with the current directory set to a subdirectory to
# ensure that workspace-relative filenames still work as expected.
out="$(cd "${dir:?}/src" && bazel --bazelrc=/dev/null coverage //... 2>&1)"

# Parse output to find location of coverage.dat.  The output should be
# relatively stable, cf. https://bazel.build/run/scripts#parsing-output.
pattern=$'\n''  (/.+/coverage\.dat)'$'\n'
[[ "${out:?}" =~ ${pattern:?} ]] || exit
dat="${BASH_REMATCH[1]}"

# Replace test log directory in the output with a fixed value.  This gets rid
# of the arbitrary build configuration directory.  This needs to happen before
# replacing the execution root because the test log directory is a subdirectory
# of the execution root.
out="${out//${testlogs}/%ROOTDIR%/bazel-out/k8-fastbuild/testlogs}"

# Replace the execution root in the output with a placeholder.
out="${out//${execroot}/%ROOTDIR%}"

# We can’t use ‘org-babel-detangle’ because there are no links in the tangled
# files.
awk -v out=bazel.out -v src=<(echo "${out:?}") \
  -f "${testdata:?}/detangle.awk" \
  -- "${org:?}" > "${dir:?}/org.tmp" || exit

awk -v out=bazel-out/k8-fastbuild/testlogs/src/test/java/example/example_test/coverage.dat \
  -v src="${dat:?}" \
  -f "${testdata:?}/detangle.awk" \
  -- "${dir:?}/org.tmp" > "${org:?}" || exit

echo 'Successfully refreshed coverage.org.'