Skip to content

Commit 66c5998

Browse files
committed
chore(common): add unit tests for detect
1 parent e678071 commit 66c5998

2 files changed

Lines changed: 122 additions & 1 deletion

File tree

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ ROOT_DIR := $(CURDIR)
66
BUILDPACK_DIR := $(ROOT_DIR)
77
PYTHON_BIN ?= python3
88

9-
.PHONY: test-buildpack clean-test-buildpack smoke-test start-local
9+
.PHONY: test-buildpack clean-test-buildpack test-detect smoke-test start-local
1010

1111
# Reset the temporary staging directories used for local buildpack testing.
1212
clean-test-buildpack:
1313
rm -rf $(BUILD_DIR) $(CACHE_DIR) $(ENV_DIR)
1414

15+
# Run unit tests for the detect script without requiring external test tooling.
16+
test-detect:
17+
@bash ./test/unit/detect_test.sh
18+
1519
# Run the sample app through detect, compile, and release using the same
1620
# temporary directories each time so local testing is repeatable.
1721
test-buildpack: clean-test-buildpack

test/unit/detect_test.sh

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
6+
DETECT_SCRIPT="$ROOT_DIR/bin/detect"
7+
TMP_DIR="$(mktemp -d /tmp/detect-test.XXXXXX)"
8+
9+
cleanup() {
10+
rm -rf "$TMP_DIR"
11+
}
12+
13+
trap cleanup EXIT
14+
15+
assert_exit_code() {
16+
local actual="$1"
17+
local expected="$2"
18+
local message="$3"
19+
20+
if [ "$actual" -ne "$expected" ]; then
21+
echo "FAIL: $message"
22+
echo "Expected exit code: $expected"
23+
echo "Actual exit code: $actual"
24+
exit 1
25+
fi
26+
}
27+
28+
assert_output() {
29+
local actual="$1"
30+
local expected="$2"
31+
local message="$3"
32+
33+
if [ "$actual" != "$expected" ]; then
34+
echo "FAIL: $message"
35+
echo "Expected output: $expected"
36+
echo "Actual output: $actual"
37+
exit 1
38+
fi
39+
}
40+
41+
run_detect() {
42+
local app_dir="$1"
43+
local output_file="$TMP_DIR/output.txt"
44+
45+
set +e
46+
(
47+
cd "$app_dir"
48+
"$DETECT_SCRIPT"
49+
) >"$output_file" 2>&1
50+
status=$?
51+
set -e
52+
53+
output="$(cat "$output_file")"
54+
}
55+
56+
test_detect_succeeds_when_pyproject_and_lockfile_exist() {
57+
# Arrange
58+
local app_dir="$TMP_DIR/success-case"
59+
mkdir -p "$app_dir"
60+
touch "$app_dir/pyproject.toml" "$app_dir/uv.lock"
61+
62+
# Act
63+
run_detect "$app_dir"
64+
65+
# Assert
66+
assert_exit_code "$status" 0 "detect should succeed when both uv files exist"
67+
assert_output "$output" "python-uv" "detect should print the buildpack id when it succeeds"
68+
}
69+
70+
test_detect_fails_when_lockfile_is_missing() {
71+
# Arrange
72+
local app_dir="$TMP_DIR/missing-lock"
73+
mkdir -p "$app_dir"
74+
touch "$app_dir/pyproject.toml"
75+
76+
# Act
77+
run_detect "$app_dir"
78+
79+
# Assert
80+
assert_exit_code "$status" 1 "detect should fail when uv.lock is missing"
81+
assert_output "$output" "" "detect should not print anything when detection fails"
82+
}
83+
84+
test_detect_fails_when_pyproject_is_missing() {
85+
# Arrange
86+
local app_dir="$TMP_DIR/missing-pyproject"
87+
mkdir -p "$app_dir"
88+
touch "$app_dir/uv.lock"
89+
90+
# Act
91+
run_detect "$app_dir"
92+
93+
# Assert
94+
assert_exit_code "$status" 1 "detect should fail when pyproject.toml is missing"
95+
assert_output "$output" "" "detect should not print anything when detection fails"
96+
}
97+
98+
test_detect_succeeds_when_extra_files_are_present() {
99+
# Arrange
100+
local app_dir="$TMP_DIR/extra-files"
101+
mkdir -p "$app_dir"
102+
touch "$app_dir/pyproject.toml" "$app_dir/uv.lock" "$app_dir/README.md"
103+
104+
# Act
105+
run_detect "$app_dir"
106+
107+
# Assert
108+
assert_exit_code "$status" 0 "detect should ignore unrelated files"
109+
assert_output "$output" "python-uv" "detect should still identify a supported uv app"
110+
}
111+
112+
test_detect_succeeds_when_pyproject_and_lockfile_exist
113+
test_detect_fails_when_lockfile_is_missing
114+
test_detect_fails_when_pyproject_is_missing
115+
test_detect_succeeds_when_extra_files_are_present
116+
117+
echo "PASS: detect unit tests"

0 commit comments

Comments
 (0)