Skip to content

Commit e948999

Browse files
committed
ci: create minimal unit test script
Signed-off-by: ANISH-SR <rawatanish08@gmail.com>
1 parent d3c6f97 commit e948999

File tree

2 files changed

+132
-4
lines changed

2 files changed

+132
-4
lines changed

.buildkite/main-tests.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
{
2929
"test_name": "unittests-gnu",
30-
"command": "cargo test --all-features --workspace",
30+
"command": ".buildkite/selective-tests.sh unittests-gnu",
3131
"platform": [
3232
"x86_64",
3333
"aarch64",
@@ -39,7 +39,7 @@
3939
},
4040
{
4141
"test_name": "unittests-musl",
42-
"command": "cargo test --all-features --workspace --target {target_platform}-unknown-linux-musl --exclude vhost-device-gpu",
42+
"command": ".buildkite/selective-tests.sh unittests-musl {target_platform}",
4343
"platform": [
4444
"x86_64",
4545
"aarch64"
@@ -50,7 +50,7 @@
5050
},
5151
{
5252
"test_name": "unittests-gnu-release",
53-
"command": "cargo test --release --all-features --workspace",
53+
"command": ".buildkite/selective-tests.sh unittests-gnu-release",
5454
"platform": [
5555
"x86_64",
5656
"aarch64",
@@ -62,7 +62,7 @@
6262
},
6363
{
6464
"test_name": "unittests-musl-release",
65-
"command": "cargo test --release --all-features --workspace --target {target_platform}-unknown-linux-musl --exclude vhost-device-gpu",
65+
"command": ".buildkite/selective-tests.sh unittests-musl-release {target_platform}",
6666
"platform": [
6767
"x86_64",
6868
"aarch64"

.buildkite/selective-tests.sh

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
mode="${1:-}"; shift || true
5+
6+
if [[ -z "${mode}" ]]; then
7+
echo "Usage: selective-tests.sh <mode> [args...]" >&2
8+
exit 1
9+
fi
10+
11+
# Determine changed files compared to main
12+
if git fetch origin main >/dev/null 2>&1; then
13+
changed_files=$(git diff --name-only origin/main...HEAD || true)
14+
else
15+
changed_files=$(git diff --name-only HEAD~1..HEAD || true)
16+
fi
17+
18+
if [[ -z "${changed_files}" ]]; then
19+
# No diff detected, run full workspace tests to be safe
20+
changed_files="__none__"
21+
fi
22+
23+
workspace_changed=0
24+
25+
# Files that we consider "workspace-wide" changes
26+
for f in ${changed_files}; do
27+
case "${f}" in
28+
Cargo.toml|Cargo.lock|rustfmt.toml|.buildkite/*|xtask/*)
29+
workspace_changed=1
30+
;;
31+
esac
32+
done
33+
34+
# Map paths to crates
35+
crates=(
36+
vhost-device-can
37+
vhost-device-console
38+
vhost-device-gpio
39+
vhost-device-gpu
40+
vhost-device-i2c
41+
vhost-device-input
42+
vhost-device-rng
43+
vhost-device-scsi
44+
vhost-device-scmi
45+
vhost-device-sound
46+
vhost-device-spi
47+
vhost-device-template
48+
vhost-device-vsock
49+
)
50+
51+
affected_crates=()
52+
for f in ${changed_files}; do
53+
for c in "${crates[@]}"; do
54+
case "${f}" in
55+
"${c}"/*)
56+
affected_crates+=("${c}")
57+
;;
58+
esac
59+
done
60+
done
61+
62+
# Unique affected crates
63+
if ((${#affected_crates[@]} > 0)); then
64+
mapfile -t affected_crates < <(printf '%s
65+
' "${affected_crates[@]}" | sort -u)
66+
fi
67+
68+
run_full_workspace=0
69+
if [[ "${workspace_changed}" -eq 1 || ${#affected_crates[@]} -eq 0 ]]; then
70+
run_full_workspace=1
71+
fi
72+
73+
cmd=(cargo test)
74+
75+
case "${mode}" in
76+
unittests-gnu)
77+
cmd+=(--all-features)
78+
;;
79+
unittests-gnu-release)
80+
cmd+=(--release --all-features)
81+
;;
82+
unittests-musl)
83+
target_platform="${1:-}"; shift || true
84+
if [[ -z "${target_platform:-}" ]]; then
85+
echo "target_platform argument required for unittests-musl" >&2
86+
exit 1
87+
fi
88+
cmd+=(--all-features --target "${target_platform}-unknown-linux-musl")
89+
;;
90+
unittests-musl-release)
91+
target_platform="${1:-}"; shift || true
92+
if [[ -z "${target_platform:-}" ]]; then
93+
echo "target_platform argument required for unittests-musl-release" >&2
94+
exit 1
95+
fi
96+
cmd+=(--release --all-features --target "${target_platform}-unknown-linux-musl")
97+
;;
98+
*)
99+
echo "Unknown mode: ${mode}" >&2
100+
exit 1
101+
;;
102+
esac
103+
104+
if [[ "${run_full_workspace}" -eq 1 ]]; then
105+
# Preserve previous behaviour: run on entire workspace.
106+
if [[ "${mode}" == "unittests-musl" || "${mode}" == "unittests-musl-release" ]]; then
107+
# Original musl commands excluded vhost-device-gpu.
108+
cmd+=(--workspace --exclude vhost-device-gpu)
109+
else
110+
cmd+=(--workspace)
111+
fi
112+
else
113+
# Run only for affected crates. For musl jobs, skip vhost-device-gpu as before.
114+
for c in "${affected_crates[@]}"; do
115+
if [[ "${mode}" == unittests-musl* && "${c}" == "vhost-device-gpu" ]]; then
116+
continue
117+
fi
118+
cmd+=(-p "${c}")
119+
done
120+
121+
if ((${#affected_crates[@]} == 0)); then
122+
# Nothing to run
123+
echo "No affected crates to test; exiting." >&2
124+
exit 0
125+
fi
126+
fi
127+
128+
exec "${cmd[@]}"

0 commit comments

Comments
 (0)