Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed May 24, 2024
1 parent 5f3590c commit e1110c3
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 3 deletions.
2 changes: 2 additions & 0 deletions examples/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
common --tool_java_runtime_version=remotejdk_17

# These settings make it harder for bazel_env to do its job and are here to
# verify it still works.
common --incompatible_disallow_empty_glob
Expand Down
31 changes: 30 additions & 1 deletion examples/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@bazel_env.bzl", "bazel_env")
load("@buildozer//:buildozer.bzl", "BUILDOZER_LABEL")
load("@bazel_env.bzl", "bazel_env")

bazel_env(
name = "bazel_env",
Expand All @@ -14,5 +14,34 @@ bazel_env(
"go": "@rules_go//go",
"jar": "$(JAVABASE)/bin/jar",
"java": "$(JAVA)",
"jq": ":jq",
},
)

alias(
name = "jq",
actual = select({
"@platforms//os:linux": "@jq_linux_amd64//file",
"@platforms//os:macos": "@jq_macos_amd64//file",
"@platforms//os:windows": "@jq_windows_amd64//file",
}),
)

# Lines below are not part of the example, they are used to test the example.

sh_test(
name = "bazel_env_test",
srcs = ["bazel_env_test.sh"],
data = [
# Used to cd into the workspace directory.
"MODULE.bazel",
# Enforces that the bazel_env has been built.
":bazel_env",
],
tags = [
# The test only depends on the bazel_env fake outputs, not the individual tools.
"external",
# The test cd's into the workspace directory and expects the bazel_env to be present.
"local",
],
)
30 changes: 29 additions & 1 deletion examples/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,35 @@ local_path_override(
)

bazel_dep(name = "buildifier_prebuilt", version = "6.4.0")
bazel_dep(name = "buildozer", version = "7.1.1.1")
bazel_dep(name = "buildozer", version = "7.1.2")
bazel_dep(name = "rules_go", version = "0.47.1")
bazel_dep(name = "rules_java", version = "7.5.0")
bazel_dep(name = "platforms", version = "0.0.10")

# Don't update the versions below, they are only used to verify the hermeticity of bazel_env.

go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(version = "1.20.14")

http_file = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")

http_file(
name = "jq_linux_amd64",
executable = True,
integrity = "sha256-xrOn19PntwxvUbcGo7kL0BgzhGxU0yyjLwAn8AIm/20=",
urls = ["https://github.com/jqlang/jq/releases/download/jq-1.5/jq-linux64"],
)

http_file(
name = "jq_macos_amd64",
executable = True,
integrity = "sha256-OG6SyYKlb+SFFGjXqTHfyilWDO4wag5mxqG9QGXT2sU=",
urls = ["https://github.com/jqlang/jq/releases/download/jq-1.5/jq-osx-amd64"],
)

http_file(
name = "jq_windows_amd64",
executable = True,
integrity = "sha256-6+zYQLpH779mgihoF4zHIaFRBgk396xAbj0xvQFb3pQ=",
urls = ["https://github.com/jqlang/jq/releases/download/jq-1.5/jq-win64.exe"],
)
66 changes: 66 additions & 0 deletions examples/bazel_env_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env bash

set -euo pipefail

build_workspace_directory="$(dirname "$(readlink -f MODULE.bazel)")"

# Run a command with a minimal PATH including the bazel_env and assert its
# output, possibly with wildcards.
function assert_cmd_output() {
local -r cmd="$1"
local -r expected_output="$2"

local -r bazel_env="$build_workspace_directory/bazel-out/bazel_env-opt/bin/bazel_env/bin"
local -r actual_output="$(PATH="$bazel_env:/bin" $cmd 2>&1 | head -n 1 || true)"

# Allow for wildcard matching first.
if [[ $actual_output == $expected_output ]]; then
return
fi
diff <(echo "$expected_output") <(echo "$actual_output") || exit 1
}

function assert_contains() {
local -r pattern="$1"
local -r content="$2"

echo "$content" | grep -sqF -- "$pattern" || {
echo "Expected to find '$pattern' in:"
echo "$content"
exit 1
}
}

#### Status script ####

# Place a fake direnv tool on the PATH.
tmpdir=$(mktemp -d 2>/dev/null || mktemp -d -t 'tmpdir')
trap 'rm -rf "$tmpdir"' EXIT
touch "$tmpdir/direnv"
chmod +x "$tmpdir/direnv"

# Imitate a bazel run environment for the status script.
status_out=$(PATH="$tmpdir:$build_workspace_directory/bazel-out/bazel_env-opt/bin/bazel_env/bin:/bin:/usr/bin" \
BUILD_WORKSPACE_DIRECTORY="$build_workspace_directory" \
./bazel_env.sh)

assert_contains "direnv is installed" "$status_out"

#### Tools ####

assert_cmd_output "bazel-cc" "* error: no input files"
assert_cmd_output "buildifier --version" "buildifier version: 6.4.0 "
assert_cmd_output "buildozer --version" "buildozer version: 7.1.2 "
case "$(arch)" in
x86_64) goarch="amd64";;
*) goarch="$(arch)";;
esac
assert_cmd_output "go version" "go version go1.20.14 $(uname|tr '[:upper:]' '[:lower:]')/$goarch"
assert_cmd_output "jar --version" "jar 17.0.8.1"
assert_cmd_output "java --version" "openjdk 17.0.8.1 2023-08-24 LTS"
assert_cmd_output "jq --version" "jq-1.5"

#### Toolchains ####

[[ -d "$build_workspace_directory/bazel-out/bazel_env-opt/bin/bazel_env/toolchains/cc_toolchain" ]]
assert_cmd_output "$build_workspace_directory/bazel-out/bazel_env-opt/bin/bazel_env/toolchains/jdk/bin/java --version" "openjdk 17.0.8.1 2023-08-24 LTS"
2 changes: 1 addition & 1 deletion status.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ cleaned=0
for f in '{{bin_dir}}'/*;
do
if basename "$f" | grep -q -v '{{tools_regex}}'; then
rm -rf "$f"
rm -rf ./"$f"
cleaned=1
fi
done
Expand Down

0 comments on commit e1110c3

Please sign in to comment.