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