xref: /aosp_15_r20/external/bazelbuild-kotlin-rules/kokoro/presubmit.sh (revision 3a22c0a33dd99bcca39a024d43e6fbcc55c2806e)
1*3a22c0a3SAlix#!/bin/bash -e
2*3a22c0a3SAlix# Copyright 2022 Google LLC. All rights reserved.
3*3a22c0a3SAlix#
4*3a22c0a3SAlix# Licensed under the Apache License, Version 2.0 (the License);
5*3a22c0a3SAlix# you may not use this file except in compliance with the License.
6*3a22c0a3SAlix# You may obtain a copy of the License at
7*3a22c0a3SAlix#
8*3a22c0a3SAlix#     http://www.apache.org/licenses/LICENSE-2.0
9*3a22c0a3SAlix#
10*3a22c0a3SAlix# Unless required by applicable law or agreed to in writing, software
11*3a22c0a3SAlix# distributed under the License is distributed on an "AS IS" BASIS,
12*3a22c0a3SAlix# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*3a22c0a3SAlix# See the License for the specific language governing permissions and
14*3a22c0a3SAlix# limitations under the License.
15*3a22c0a3SAlix
16*3a22c0a3SAlix# DO NOT SET -x. It will leak any secret credentials into logs.
17*3a22c0a3SAlix
18*3a22c0a3SAlixkokoro_scm_name="presubmit"
19*3a22c0a3SAlixworkspace_root="${KOKORO_ARTIFACTS_DIR}/git/${kokoro_scm_name}"
20*3a22c0a3SAlix
21*3a22c0a3SAlix
22*3a22c0a3SAlixfunction DownloadBazelisk()  {
23*3a22c0a3SAlix    # Downloads bazelisk to a temp directory.
24*3a22c0a3SAlix
25*3a22c0a3SAlix    local version="${1:-1.18.0}"
26*3a22c0a3SAlix    local platform="${2:-linux}"
27*3a22c0a3SAlix    local arch="${3:-amd64}"
28*3a22c0a3SAlix
29*3a22c0a3SAlix    local dest=$(mktemp -d)
30*3a22c0a3SAlix    (
31*3a22c0a3SAlix        set -euxo pipefail
32*3a22c0a3SAlix
33*3a22c0a3SAlix        echo "== Downloading bazelisk ====================================="
34*3a22c0a3SAlix
35*3a22c0a3SAlix        download_url="https://github.com/bazelbuild/bazelisk/releases/download/v${version}/bazelisk-${platform}-${arch}"
36*3a22c0a3SAlix        mkdir -p "${dest}"
37*3a22c0a3SAlix        wget -nv ${download_url} -O "${dest}/bazelisk"
38*3a22c0a3SAlix        chmod +x "${dest}/bazelisk"
39*3a22c0a3SAlix
40*3a22c0a3SAlix        echo "============================================================="
41*3a22c0a3SAlix    ) &> /dev/stderr
42*3a22c0a3SAlix
43*3a22c0a3SAlix    echo "${dest}"
44*3a22c0a3SAlix}
45*3a22c0a3SAlix
46*3a22c0a3SAlixbazelisk_dir=$(DownloadBazelisk "1.18.0" linux amd64)
47*3a22c0a3SAlixexport PATH="${bazelisk_dir}:${PATH}"
48*3a22c0a3SAlix
49*3a22c0a3SAlixfunction Cleanup() {
50*3a22c0a3SAlix  # Clean up all temporary directories: bazelisk install, sandbox, and
51*3a22c0a3SAlix  # android_tools.
52*3a22c0a3SAlix  rm -rf "$bazelisk_dir"
53*3a22c0a3SAlix}
54*3a22c0a3SAlixtrap Cleanup EXIT
55*3a22c0a3SAlix
56*3a22c0a3SAlix# Default JDK on GCP_UBUNTU is JDK8
57*3a22c0a3SAlixsudo update-java-alternatives --set java-1.11.0-openjdk-amd64
58*3a22c0a3SAlix# Bazel reads JAVA_HOME
59*3a22c0a3SAlixexport JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/
60*3a22c0a3SAlix
61*3a22c0a3SAlix# Create a tmpfs in the sandbox at "/tmp/hsperfdata_$USERNAME" to avoid the
62*3a22c0a3SAlix# problems described in https://github.com/bazelbuild/bazel/issues/3236
63*3a22c0a3SAlix# Basically, the JVM creates a file at /tmp/hsperfdata_$USERNAME/$PID, but
64*3a22c0a3SAlix# processes all get a PID of 2 in the sandbox, so concurrent Java build actions
65*3a22c0a3SAlix# could crash because they're trying to modify the same file. So, tell the
66*3a22c0a3SAlix# sandbox to mount a tmpfs at /tmp/hsperfdata_$(whoami) so that each JVM gets
67*3a22c0a3SAlix# its own version of that directory.
68*3a22c0a3SAlixhsperfdata_dir="/tmp/hsperfdata_$(whoami)_rules_kotlin"
69*3a22c0a3SAlixmkdir -p "$hsperfdata_dir"
70*3a22c0a3SAlix
71*3a22c0a3SAlixcd "${workspace_root}"
72*3a22c0a3SAlix# Run test coverage for all the test targets except excluded by
73*3a22c0a3SAlix# --instrumentation_filter - code coverage doesn't work for them and they
74*3a22c0a3SAlix# would only be tested
75*3a22c0a3SAlixbazelisk coverage \
76*3a22c0a3SAlix    --sandbox_tmpfs_path="$hsperfdata_dir" \
77*3a22c0a3SAlix    --verbose_failures \
78*3a22c0a3SAlix    --experimental_google_legacy_api \
79*3a22c0a3SAlix    --instrumentation_filter=-//tests/jvm/java/multijarimport[/:],-//tests/jvm/java/functions[/:] \
80*3a22c0a3SAlix    //tests/...
81*3a22c0a3SAlix
82*3a22c0a3SAlix# For a specific test //tools:source_jar_zipper_freshness_test run test only
83*3a22c0a3SAlixbazelisk test \
84*3a22c0a3SAlix    --sandbox_tmpfs_path="$hsperfdata_dir" \
85*3a22c0a3SAlix    --verbose_failures \
86*3a22c0a3SAlix    --experimental_google_legacy_api \
87*3a22c0a3SAlix    //tools:source_jar_zipper_freshness_test