# 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.

* 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-workspace :tangle WORKSPACE
workspace(name = "test")
#+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
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
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
Loading: 
Loading: 0 packages loaded
INFO: Using default value for --instrumentation_filter: "^//src/main/java/example[/:]".
INFO: Override the above default with --instrumentation_filter
Analyzing: 2 targets (2 packages loaded, 0 targets configured)
Analyzing: 2 targets (13 packages loaded, 49 targets configured)
Analyzing: 2 targets (13 packages loaded, 49 targets configured)
Analyzing: 2 targets (23 packages loaded, 362 targets configured)
Analyzing: 2 targets (25 packages loaded, 423 targets configured)
Analyzing: 2 targets (26 packages loaded, 587 targets configured)
INFO: Analyzed 2 targets (27 packages loaded, 764 targets configured).
INFO: Found 1 target and 1 test target...
bazel: Entering directory `%ROOTDIR%/'
[0 / 10] [Prepa] BazelWorkspaceStatusAction stable-status.txt
[8 / 13] Action external/bazel_tools/tools/jdk/platformclasspath_classes/DumpPlatformClassPath.class; 1s darwin-sandbox
[10 / 13] Compiling Java headers src/main/java/example/libexample-hjar.jar (1 source file); 0s darwin-sandbox ... (2 actions running)
[19 / 22] Action external/bazel_tools/tools/jdk/platformclasspath.jar; 0s darwin-sandbox
bazel: Leaving directory `%ROOTDIR%/'
INFO: Elapsed time: 13,442s, Critical Path: 4,12s
INFO: 22 processes: 12 internal, 7 darwin-sandbox, 3 worker.
INFO: Build completed successfully, 22 total actions
//src/test/java/example:example_test                                     PASSED in 0.8s
  %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.
INFO: Build completed successfully, 22 total actions
#+END_SRC
