# Copyright 2021-2022, 2026 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.

* Test data for coverage tests

Set up a fake workspace and execution root in the current directory.  We use a
Java library because C++ and Python coverage is quite broken.  Specifically,
[[https://github.com/bazelbuild/bazel/issues/10457][C++ coverage doesn’t work
on macOS]], and [[https://github.com/bazelbuild/bazel/issues/10660][Python
coverage doesn’t work at all]].  Java coverage still
[[https://github.com/bazelbuild/bazel/issues/13358][suffers from some bugs]],
but can at least be made to work somewhat reliably.

#+PROPERTY: header-args :mkdirp yes :main no

#+BEGIN_SRC bazel-module :tangle MODULE.bazel
bazel_dep(name = "rules_java", version = "9.6.1")
#+END_src

We follow the
[[https://bazel.build/docs/bazel-and-java#best-practices][Java-specific best
practices]].

#+BEGIN_SRC bazel-build :tangle src/main/java/example/BUILD
load("@rules_java//java:java_library.bzl", "java_library")

java_library(
    name = "example",
    srcs = glob(["*.java"]),
    visibility = ["//src/test/java/example:__pkg__"],
)
#+END_SRC

#+BEGIN_SRC bazel-build :tangle src/test/java/example/BUILD
load("@rules_java//java:java_test.bzl", "java_test")

java_test(
    name = "example_test",
    srcs = glob(["*.java"]),
    test_class = "example.ExampleTest",
    deps = ["//src/main/java/example"],
)
#+END_SRC

#+BEGIN_SRC java :tangle src/main/java/example/Example.java
package example;

public class Example {
    public static int function(boolean arg) {
        if (arg) {
            return 137;
        } else {
            return 42;
        }
    }
}
#+END_SRC

#+BEGIN_SRC java :tangle src/test/java/example/ExampleTest.java
package example;

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class ExampleTest {
    @Test
    public void function() {
        assertEquals(137, Example.function(true));
    }
}
#+END_SRC

#+BEGIN_SRC fundamental :tangle bazel-out/k8-fastbuild/testlogs/src/test/java/example/example_test/coverage.dat
SF:src/main/java/example/Example.java
FN:3,example/Example::<init> ()V
FN:5,example/Example::function (Z)I
FNDA:0,example/Example::<init> ()V
FNDA:1,example/Example::function (Z)I
FNF:2
FNH:1
BRDA:5,0,0,1
BRDA:5,0,1,0
BRF:2
BRH:1
DA:3,0
DA:5,1
DA:6,1
DA:8,0
LH:2
LF:4
end_of_record
#+END_SRC

Merged standard output and error from Bazel:

#+BEGIN_SRC fundamental :tangle bazel.out
WARNING: The following rc files are no longer being read, please transfer their contents or import their path into one of the standard rc files:
/Users/p/.bazelrc
Computing main repo mapping: 
Loading: 
Loading: 0 packages loaded
INFO: Using default value for --instrumentation_filter: "^//src/main/java/example[/:]".
INFO: Override the above default with --instrumentation_filter
bazel: Entering directory `%ROOTDIR%/'
Analyzing: 2 targets (2 packages loaded, 0 targets configured)
Analyzing: 2 targets (2 packages loaded, 0 targets configured)

Analyzing: 2 targets (87 packages loaded, 1412 targets configured)

Analyzing: 2 targets (87 packages loaded, 1412 targets configured)

Analyzing: 2 targets (91 packages loaded, 1446 targets configured)

Analyzing: 2 targets (97 packages loaded, 3889 targets configured)

Analyzing: 2 targets (98 packages loaded, 3910 targets configured)

Analyzing: 2 targets (98 packages loaded, 3910 targets configured)

INFO: Analyzed 2 targets (101 packages loaded, 4079 targets configured).
[9 / 15] [Prepa] JavaToolchainCompileBootClasspath external/rules_java+/toolchains/platformclasspath_unstripped.jar
[10 / 15] [Prepa] Extracting interfaces from external/rules_java+/toolchains/platformclasspath_unstripped.jar
[11 / 15] Compiling Java headers src/main/java/example/libexample-hjar.jar (1 source file); 0s darwin-sandbox ... (2 actions running)
[25 / 31] JavaToolchainCompileClasses external/rules_java+/toolchains/platformclasspath_classes [for tool]; 0s darwin-sandbox
[27 / 31] Extracting interfaces from external/rules_java+/toolchains/platformclasspath_unstripped.jar [for tool]; 0s darwin-sandbox
[30 / 31] Testing //src/test/java/example:example_test; 0s darwin-sandbox
INFO: LCOV coverage report is located at %ROOTDIR%/bazel-out/_coverage/_baseline_report.dat
 and execpath is bazel-out/_coverage/_baseline_report.dat
INFO: LCOV coverage report is located at %ROOTDIR%/bazel-out/_coverage/_coverage_report.dat
 and execpath is bazel-out/_coverage/_coverage_report.dat
INFO: Found 1 target and 1 test target...
bazel: Leaving directory `%ROOTDIR%/'
INFO: Elapsed time: 15.886s, Critical Path: 5.77s
INFO: 36 processes: 22 internal, 11 darwin-sandbox, 3 worker.
INFO: Build completed successfully, 36 total actions
//src/test/java/example:example_test                                     PASSED in 1.3s
  %ROOTDIR%/bazel-out/k8-fastbuild/testlogs/src/test/java/example/example_test/coverage.dat

Executed 1 out of 1 test: 1 test passes.
There were tests whose specified size is too big. Use the --test_verbose_timeout_warnings command line option to see which ones these are.
#+END_SRC
