Skip to content

Commit

Permalink
test: Add tests for various OSes and Bazel versions (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyawk authored Apr 11, 2024
1 parent f704f10 commit 81dbb45
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 44 deletions.
1 change: 0 additions & 1 deletion .bazeliskrc

This file was deleted.

5 changes: 5 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ test --test_verbose_timeout_warnings
# For testing convenience
common --heap_dump_on_oom
test --test_output=errors

# For `hermetic_cc_toolchain`
common --enable_platform_specific_config
build:linux --sandbox_add_mount_pair=/tmp
build:macos --sandbox_add_mount_pair=/var/tmp
3 changes: 1 addition & 2 deletions .bcr/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ bcr_test_module:
matrix:
platform:
- "debian10"
- "macos"
- "ubuntu2004"
bazel:
- "6.x"
Expand All @@ -14,5 +13,5 @@ bcr_test_module:
name: "Run test module"
platform: ${{ platform }}
bazel: ${{ bazel }}
build_targets:
test_targets:
- "//..."
24 changes: 22 additions & 2 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
---
name: Set up environment

inputs:
bazel-version:
description: Bazel version
required: true

runs:
using: composite
steps:
- name: Generate .bazeliskrc
shell: bash
run: |
echo "USE_BAZEL_VERSION=${{ inputs.bazel-version }}" > .bazeliskrc
echo "USE_BAZEL_VERSION=${{ inputs.bazel-version }}" > examples/.bazeliskrc
- name: Install bazelisk
shell: bash
run: |
bazelisk_dir="$(realpath "$(mktemp -d -p .)")"
wget https://github.com/bazelbuild/bazelisk/releases/download/v1.19.0/bazelisk-linux-amd64 \
bazelisk_dir=bazelisk-bin-dir
mkdir "${bazelisk_dir}"
if [ "${RUNNER_OS}" == "Linux" ]; then
part_os="linux"
elif [ "${RUNNER_OS}" == "macOS" ]; then
part_os="darwin"
else
echo "${RUNNER_OS} not supported"
exit 1
fi
wget "https://github.com/bazelbuild/bazelisk/releases/download/v1.19.0/bazelisk-${part_os}-amd64" \
-O "${bazelisk_dir}/bazelisk"
chmod +x "${bazelisk_dir}/bazelisk"
echo "${bazelisk_dir}" >> "${GITHUB_PATH}"
24 changes: 22 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,46 @@ concurrency:

jobs:
unit-test:
runs-on: ubuntu-22.04
strategy:
matrix:
bazel-version:
- 6.5.0
- 7.1.1
runner:
- ubuntu-20.04
- ubuntu-22.04
runs-on: ${{ matrix.runner }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
- name: Set up
uses: ./.github/actions/setup
with:
bazel-version: ${{ matrix.bazel-version }}
- name: Run unit tests
run: ./scripts/execute_tests.bash

example:
runs-on: ubuntu-22.04
strategy:
matrix:
bazel-version:
- 6.5.0
- 7.1.1
runner:
- ubuntu-20.04
- ubuntu-22.04
runs-on: ${{ matrix.runner }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
- name: Set up
uses: ./.github/actions/setup
with:
bazel-version: ${{ matrix.bazel-version }}
- name: Validate example
run: ./scripts/validate_example.bash

Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Bazel
/bazel-*
MODULE.bazel.lock
.bazeliskrc

# For CI
/bazelisk-bin-dir
20 changes: 11 additions & 9 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ module(
)

bazel_dep(name = "bazel_skylib", version = "1.5.0", dev_dependency = True)
bazel_dep(name = "toolchains_llvm", version = "1.0.0", dev_dependency = True)

llvm = use_extension(
"@toolchains_llvm//toolchain/extensions:llvm.bzl",
"llvm",
bazel_dep(
name = "hermetic_cc_toolchain",
version = "3.0.1",
dev_dependency = True,
)
llvm.toolchain(
llvm_version = "17.0.6",

cc_toolchains = use_extension(
"@hermetic_cc_toolchain//toolchain:ext.bzl",
"toolchains",
dev_dependency = True,
)
use_repo(llvm, "llvm_toolchain")
use_repo(cc_toolchains, "zig_sdk")

register_toolchains(
"@llvm_toolchain//:all",
"@zig_sdk//toolchain:linux_amd64_gnu.2.31",
"@zig_sdk//toolchain:linux_arm64_gnu.2.31",
dev_dependency = True,
)

Expand Down
8 changes: 5 additions & 3 deletions build_script/check_emptiness.bash
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ if [[ "${#files_to_touch[@]}" -gt 0 ]]; then
trap 'touch "${files_to_touch[@]}"' EXIT
fi

for file_to_check in "${files_to_check[@]}"; do
exit_if_empty_file "${file_to_check}"
done
if [[ "${#files_to_check[@]}" -gt 0 ]]; then
for file_to_check in "${files_to_check[@]}"; do
exit_if_empty_file "${file_to_check}"
done
fi

# Exit with error if there's no empty file
echo "${error_message}" >&2
Expand Down
8 changes: 5 additions & 3 deletions build_script/conditionally_execute.bash
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ if [[ "${#files_to_touch[@]}" -gt 0 ]]; then
trap 'touch "${files_to_touch[@]}"' EXIT
fi

for file_to_check in "${files_to_check[@]}"; do
exit_if_empty_file "${file_to_check}"
done
if [[ "${#files_to_check[@]}" -gt 0 ]]; then
for file_to_check in "${files_to_check[@]}"; do
exit_if_empty_file "${file_to_check}"
done
fi

if [[ "${ignore_error}" == "true" ]]; then
"$@" >"${stdout_file:-"/dev/null"}" 2>"${stderr_file:-"/dev/null"}" || true
Expand Down
1 change: 1 addition & 0 deletions examples/.bazelignore
1 change: 1 addition & 0 deletions examples/.bazelrc
2 changes: 0 additions & 2 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Copied from the original workspace
.bazelrc
.bazeliskrc
.bazelignore

# Bazel
/bazel-*
Expand Down
25 changes: 16 additions & 9 deletions examples/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,23 @@ local_path_override(
path = "..",
)

# Register LLVM toolchain for C/C++
bazel_dep(name = "toolchains_llvm", version = "1.0.0")
# For testing utility
bazel_dep(name = "bazel_skylib", version = "1.5.0")

llvm = use_extension(
"@toolchains_llvm//toolchain/extensions:llvm.bzl",
"llvm",
# Register toolchain for C/C++

bazel_dep(
name = "hermetic_cc_toolchain",
version = "3.0.1",
)
llvm.toolchain(
llvm_version = "17.0.6",

cc_toolchains = use_extension(
"@hermetic_cc_toolchain//toolchain:ext.bzl",
"toolchains",
)
use_repo(llvm, "llvm_toolchain")
use_repo(cc_toolchains, "zig_sdk")

register_toolchains("@llvm_toolchain//:all")
register_toolchains(
"@zig_sdk//toolchain:linux_amd64_gnu.2.31",
"@zig_sdk//toolchain:linux_arm64_gnu.2.31",
)
8 changes: 8 additions & 0 deletions examples/cc/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@rules_build_error//lang/cc:defs.bzl", "cc_build_error")
load("@rules_build_error//matcher:defs.bzl", "matcher")

Expand All @@ -6,3 +7,10 @@ cc_build_error(
src = "compile_error.cpp",
compile_stderr = matcher.has_substr("Example compile error"),
)

build_test(
name = "build_test",
targets = [
":example",
],
)
9 changes: 9 additions & 0 deletions scripts/execute_tests.bash
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

set -euo pipefail

BAZEL_VERSION_DEFAULT="7.1.1"

# Bazel executable with some arguments
BAZEL_EXECUTABLE=(
"env"
Expand Down Expand Up @@ -35,6 +37,13 @@ check_bazel_build_error() {
fi
}


if [[ ! -f .bazeliskrc ]]; then
echo "WARN: .bazeliskrc not found." >&2
echo "WARN: Creating it with a default version ${BAZEL_VERSION_DEFAULT}." >&2
echo "USE_BAZEL_VERSION=7.1.1" > .bazeliskrc
fi

echo "Executing the test cases which should succeed in straightforward 'bazel test'"
"${BAZEL_EXECUTABLE[@]}" test //...

Expand Down
20 changes: 9 additions & 11 deletions scripts/validate_example.bash
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

set -euo pipefail

BAZEL_VERSION_DEFAULT="7.1.1"

# Bazel executable with some arguments
BAZEL_EXECUTABLE=(
"env"
Expand All @@ -15,16 +17,12 @@ BAZEL_EXECUTABLE=(
bazelisk
)

copied_files=(
".bazelignore"
".bazeliskrc"
".bazelrc"
)

for copied_file in "${copied_files[@]}"; do
cp "${copied_file}" examples
done

cd examples

"${BAZEL_EXECUTABLE[@]}" build //...
if [[ ! -f .bazeliskrc ]]; then
echo "WARN: .bazeliskrc not found." >&2
echo "WARN: Creating it with a default version ${BAZEL_VERSION_DEFAULT}." >&2
echo "USE_BAZEL_VERSION=7.1.1" > .bazeliskrc
fi

"${BAZEL_EXECUTABLE[@]}" test //...

0 comments on commit 81dbb45

Please sign in to comment.