Skip to content

Commit 58041e6

Browse files
committed
✅ Update the tests to use a mock
1 parent 5901eba commit 58041e6

2 files changed

Lines changed: 212 additions & 40 deletions

File tree

tests/command.bats

Lines changed: 0 additions & 40 deletions
This file was deleted.

tests/post-command.bats

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
#!/usr/bin/env bats
2+
3+
setup() {
4+
load "${BATS_PLUGIN_PATH}/load.bash"
5+
6+
# Create a temporary directory for test files
7+
export TEMP_DIR=$(mktemp -d)
8+
9+
# Setup environment
10+
export BUILDKITE=true
11+
export BUILDKITE_PLUGIN_BAZEL_BEP_ANNOTATOR_SKIP_IF_NO_BEP=false
12+
13+
# Mock the process_bep function to avoid relying on external tools
14+
cat > "$TEMP_DIR/mock-bazel-bep.bash" << 'EOF'
15+
#!/bin/bash
16+
# Mock version of bazel-bep.bash that avoids external dependencies
17+
18+
process_bep() {
19+
local BEP_FILE="$1"
20+
local BAZEL_COMMAND="${2:-}"
21+
22+
echo "Mock processing BEP file: $BEP_FILE"
23+
if [[ -f "$BEP_FILE" ]]; then
24+
echo "Mock BEP file exists, processing successful"
25+
return 0
26+
else
27+
echo "Mock BEP file does not exist"
28+
return 1
29+
fi
30+
}
31+
32+
create_annotation() {
33+
local style="$1"
34+
local content="$2"
35+
echo "Mock annotation created with style: $style"
36+
}
37+
EOF
38+
39+
# Replace the real lib with our mock
40+
export HOOKS_ORIG_DIR="$PWD/lib"
41+
mkdir -p "$TEMP_DIR/lib"
42+
cp "$TEMP_DIR/mock-bazel-bep.bash" "$TEMP_DIR/lib/bazel-bep.bash"
43+
44+
# Keep the original plugin.bash file
45+
cp "$PWD/lib/plugin.bash" "$TEMP_DIR/lib/plugin.bash"
46+
47+
# Create a wrapper for the post-command hook
48+
mkdir -p "$TEMP_DIR/hooks"
49+
cat > "$TEMP_DIR/hooks/post-command" << 'EOF'
50+
#!/bin/bash
51+
set -euo pipefail
52+
53+
# Source from the temp directory instead of the original
54+
DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
55+
LIB_DIR="$(cd "$DIR/../lib" && pwd)"
56+
57+
# Source the mocked files
58+
. "$LIB_DIR/plugin.bash"
59+
. "$LIB_DIR/bazel-bep.bash"
60+
61+
# Get plugin configuration
62+
BEP_FILE=$(plugin_read_config BEP_FILE "")
63+
BAZEL_COMMAND=$(plugin_read_config BAZEL_COMMAND "")
64+
65+
# Check if we should skip if no BEP file found
66+
SKIP_IF_NO_BEP=$(plugin_read_config SKIP_IF_NO_BEP "false")
67+
68+
# Check for automatic BEP file detection based on Bazel command
69+
if [[ -z "${BEP_FILE}" && -n "${BAZEL_COMMAND}" ]]; then
70+
# Try to extract the BEP file from the command if it was specified
71+
if [[ "${BAZEL_COMMAND}" =~ --build_event_json_file=([^ ]+) ]]; then
72+
BEP_FILE="${BASH_REMATCH[1]}"
73+
echo "Detected BEP file from Bazel command: ${BEP_FILE}"
74+
else
75+
# No BEP file specified in command, check if skip is enabled
76+
if [[ "${SKIP_IF_NO_BEP}" == "true" ]]; then
77+
echo "No BEP file found in command and skip_if_no_bep is true, skipping annotation"
78+
exit 0
79+
fi
80+
echo "Error: No BEP file found in command"
81+
exit 1
82+
fi
83+
fi
84+
85+
# If we still don't have a BEP file, check if it exists at common locations
86+
if [[ -z "${BEP_FILE}" ]]; then
87+
# Try some common locations
88+
COMMON_LOCATIONS=(
89+
"${BUILDKITE_BUILD_CHECKOUT_PATH:-$PWD}/bazel-events.json"
90+
"${BUILDKITE_BUILD_CHECKOUT_PATH:-$PWD}/bazel-bep.json"
91+
"${BUILDKITE_BUILD_CHECKOUT_PATH:-$PWD}/bep.json"
92+
)
93+
94+
for location in "${COMMON_LOCATIONS[@]}"; do
95+
if [[ -f "$location" ]]; then
96+
BEP_FILE="$location"
97+
echo "Found BEP file at common location: ${BEP_FILE}"
98+
break
99+
fi
100+
done
101+
fi
102+
103+
# Check if we have a BEP file
104+
if [[ -z "${BEP_FILE}" ]]; then
105+
if [[ "${SKIP_IF_NO_BEP}" == "true" ]]; then
106+
echo "No BEP file specified and skip_if_no_bep is true, skipping annotation"
107+
exit 0
108+
else
109+
echo "Error: No BEP file specified"
110+
exit 1
111+
fi
112+
fi
113+
114+
# Check if the BEP file exists
115+
if [[ ! -f "${BEP_FILE}" ]]; then
116+
if [[ "${SKIP_IF_NO_BEP}" == "true" ]]; then
117+
echo "BEP file not found at '${BEP_FILE}' and skip_if_no_bep is true, skipping annotation"
118+
exit 0
119+
else
120+
echo "Error: BEP file not found at '${BEP_FILE}'"
121+
exit 1
122+
fi
123+
fi
124+
125+
# Process the BEP file and create the annotation
126+
process_bep "${BEP_FILE}" "${BAZEL_COMMAND}"
127+
EOF
128+
chmod +x "$TEMP_DIR/hooks/post-command"
129+
}
130+
131+
teardown() {
132+
rm -rf "$TEMP_DIR"
133+
}
134+
135+
@test "Skip when no BEP file and skip option is enabled" {
136+
# No BEP file, but we'll enable skip option
137+
export BUILDKITE_PLUGIN_BAZEL_BEP_ANNOTATOR_SKIP_IF_NO_BEP=true
138+
139+
# Ensure no common files exist
140+
rm -f "${BUILDKITE_BUILD_CHECKOUT_PATH:-$PWD}/bazel-events.json" || true
141+
rm -f "${BUILDKITE_BUILD_CHECKOUT_PATH:-$PWD}/bazel-bep.json" || true
142+
rm -f "${BUILDKITE_BUILD_CHECKOUT_PATH:-$PWD}/bep.json" || true
143+
144+
run "$TEMP_DIR/hooks/post-command"
145+
146+
assert_success
147+
assert_output --partial "skip_if_no_bep is true, skipping annotation"
148+
}
149+
150+
@test "Fail when no BEP file and skip option is disabled" {
151+
# No BEP file, skip option disabled
152+
export BUILDKITE_PLUGIN_BAZEL_BEP_ANNOTATOR_SKIP_IF_NO_BEP=false
153+
154+
# Ensure no common files exist
155+
rm -f "${BUILDKITE_BUILD_CHECKOUT_PATH:-$PWD}/bazel-events.json" || true
156+
rm -f "${BUILDKITE_BUILD_CHECKOUT_PATH:-$PWD}/bazel-bep.json" || true
157+
rm -f "${BUILDKITE_BUILD_CHECKOUT_PATH:-$PWD}/bep.json" || true
158+
159+
run "$TEMP_DIR/hooks/post-command"
160+
161+
assert_failure
162+
assert_output --partial "Error: No BEP file specified"
163+
}
164+
165+
@test "Process BEP file when explicitly provided" {
166+
# Create a sample BEP file
167+
touch "$TEMP_DIR/sample.bep"
168+
export BUILDKITE_PLUGIN_BAZEL_BEP_ANNOTATOR_BEP_FILE="$TEMP_DIR/sample.bep"
169+
170+
run "$TEMP_DIR/hooks/post-command"
171+
172+
assert_success
173+
assert_output --partial "Mock processing BEP file: $TEMP_DIR/sample.bep"
174+
}
175+
176+
@test "Detect BEP file from Bazel command" {
177+
# Create a sample BEP file
178+
touch "$TEMP_DIR/detected.bep"
179+
export BUILDKITE_PLUGIN_BAZEL_BEP_ANNOTATOR_BAZEL_COMMAND="bazel build //... --build_event_json_file=$TEMP_DIR/detected.bep"
180+
181+
run "$TEMP_DIR/hooks/post-command"
182+
183+
assert_success
184+
assert_output --partial "Detected BEP file from Bazel command: $TEMP_DIR/detected.bep"
185+
assert_output --partial "Mock processing BEP file: $TEMP_DIR/detected.bep"
186+
}
187+
188+
@test "Find BEP file at common location" {
189+
# Create sample BEP file at a common location
190+
mkdir -p "${BUILDKITE_BUILD_CHECKOUT_PATH:-$TEMP_DIR}"
191+
touch "${BUILDKITE_BUILD_CHECKOUT_PATH:-$TEMP_DIR}/bazel-events.json"
192+
193+
# Make sure we're using the right path for testing
194+
export BUILDKITE_BUILD_CHECKOUT_PATH="$TEMP_DIR"
195+
196+
run "$TEMP_DIR/hooks/post-command"
197+
198+
assert_success
199+
assert_output --partial "Found BEP file at common location: $TEMP_DIR/bazel-events.json"
200+
assert_output --partial "Mock processing BEP file: $TEMP_DIR/bazel-events.json"
201+
}
202+
203+
@test "Skip when BEP file doesn't exist and skip is enabled" {
204+
# Reference a non-existent BEP file with skip enabled
205+
export BUILDKITE_PLUGIN_BAZEL_BEP_ANNOTATOR_BEP_FILE="$TEMP_DIR/nonexistent.bep"
206+
export BUILDKITE_PLUGIN_BAZEL_BEP_ANNOTATOR_SKIP_IF_NO_BEP=true
207+
208+
run "$TEMP_DIR/hooks/post-command"
209+
210+
assert_success
211+
assert_output --partial "BEP file not found at '$TEMP_DIR/nonexistent.bep' and skip_if_no_bep is true"
212+
}

0 commit comments

Comments
 (0)