Skip to content

Commit 891c350

Browse files
update scripts/test.sh and xtask/src/main.rs
1 parent eee8b7a commit 891c350

File tree

2 files changed

+219
-63
lines changed

2 files changed

+219
-63
lines changed

scripts/test.sh

Lines changed: 163 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,233 @@
11
#!/bin/bash
2+
# StarryOS test script – mirrors the structure of app-helloworld/scripts/test.sh.
3+
#
4+
# Usage:
5+
# ./scripts/test.sh [STEP]
6+
#
7+
# STEP (optional): run only the specified step (1–5). Omit to run all steps.
8+
# 1 – check required tools
9+
# 2 – code format check (cargo fmt --all)
10+
# 3 – per-architecture rootfs download + kernel build + QEMU boot test
11+
# 4 – publish readiness check (cargo publish --dry-run)
12+
# 5 – print test summary
13+
#
14+
# Environment variables:
15+
# SKIP_QEMU=true skip QEMU boot tests in step 3 (build only)
16+
#
17+
# Examples:
18+
# ./scripts/test.sh # run all five steps
19+
# ./scripts/test.sh 1 # tool check only
20+
# ./scripts/test.sh 3 # build + QEMU boot tests only
21+
# ./scripts/test.sh 4 # publish dry-run only
22+
# SKIP_QEMU=true ./scripts/test.sh 3 # build only, no QEMU
23+
224
set -e
325

4-
echo "=== StarryOS Test Script ==="
5-
echo ""
26+
ARCHS=("riscv64" "aarch64" "loongarch64" "x86_64")
627

728
# --------------------------------------------------------------------------
8-
# [1/4] Check required tools
29+
# Step 1 – Check required tools
930
# --------------------------------------------------------------------------
10-
check_tools() {
11-
echo "[1/4] Checking required tools..."
31+
step1_check_tools() {
32+
echo "[1/5] Checking required tools..."
1233

1334
local missing=false
1435

1536
for tool in make cargo python3; do
1637
if ! command -v "$tool" &> /dev/null; then
17-
echo "Error: '$tool' is not installed"
38+
echo " ERROR: '$tool' is not installed"
1839
missing=true
1940
fi
2041
done
2142

22-
for arch in riscv64 aarch64 loongarch64 x86_64; do
23-
qemu_cmd="qemu-system-${arch}"
24-
if ! command -v "$qemu_cmd" &> /dev/null; then
25-
echo "Warning: '$qemu_cmd' not found (QEMU test for $arch will be skipped)"
43+
for arch in "${ARCHS[@]}"; do
44+
if ! command -v "qemu-system-${arch}" &> /dev/null; then
45+
echo " WARNING: 'qemu-system-${arch}' not found (QEMU test for ${arch} will be skipped)"
2646
fi
2747
done
2848

2949
if [ "$missing" = "true" ]; then
30-
echo "Error: missing required tools, aborting."
50+
echo "ERROR: missing required tools aborting."
3151
exit 1
3252
fi
3353

34-
echo "All required tools are available"
54+
echo " OK: all required tools are available"
3555
echo ""
3656
}
3757

3858
# --------------------------------------------------------------------------
39-
# [2/4] Code format check
59+
# Step 2 – Code format check
4060
# --------------------------------------------------------------------------
41-
check_format() {
42-
echo "[2/4] Checking code format..."
61+
step2_check_format() {
62+
echo "[2/5] Checking code format..."
4363
cargo fmt --all -- --check
44-
echo "Code format check passed"
64+
echo " OK: code format check passed"
4565
echo ""
4666
}
4767

4868
# --------------------------------------------------------------------------
49-
# [3/4] Per-architecture: rootfs download + build + QEMU boot test
69+
# Step 3 – Per-architecture: rootfs + build + QEMU boot test
5070
# --------------------------------------------------------------------------
51-
run_arch_tests() {
52-
echo "[3/4] Running architecture-specific build and boot tests..."
71+
step3_arch_tests() {
72+
local skip_qemu="${SKIP_QEMU:-false}"
73+
echo "[3/5] Running architecture-specific build and boot tests..."
74+
if [ "$skip_qemu" = "true" ]; then
75+
echo " NOTE: SKIP_QEMU=true – QEMU boot tests will be skipped"
76+
fi
5377

54-
local archs=("riscv64" "aarch64" "loongarch64" "x86_64")
5578
local all_passed=true
5679

57-
for arch in "${archs[@]}"; do
80+
for arch in "${ARCHS[@]}"; do
5881
echo ""
59-
echo "--- Architecture: $arch ---"
82+
echo " --- Architecture: ${arch} ---"
83+
84+
# Rootfs download (skipped by Makefile if image already exists)
85+
echo " [${arch}] Downloading rootfs..."
86+
cargo xtask rootfs --arch="${arch}"
87+
88+
# Kernel build
89+
echo " [${arch}] Building kernel..."
90+
cargo xtask build --arch="${arch}"
91+
92+
# QEMU boot test
93+
if [ "$skip_qemu" = "true" ]; then
94+
echo " [${arch}] Skipping QEMU boot test (SKIP_QEMU=true)"
95+
continue
96+
fi
6097

61-
# Check if QEMU is available for this arch
6298
if ! command -v "qemu-system-${arch}" &> /dev/null; then
63-
echo "Warning: qemu-system-${arch} not found, skipping $arch"
99+
echo " [${arch}] WARNING: qemu-system-${arch} not found, skipping boot test"
64100
continue
65101
fi
66102

67-
echo " Step 1: downloading rootfs for $arch..."
68-
cargo xtask rootfs --arch="$arch"
103+
echo " [${arch}] Running QEMU boot test..."
104+
if cargo xtask test --arch="${arch}"; then
105+
echo " [${arch}] OK: boot test passed"
106+
else
107+
echo " [${arch}] ERROR: boot test FAILED"
108+
all_passed=false
109+
fi
110+
done
111+
112+
echo ""
113+
if [ "$all_passed" = "true" ]; then
114+
echo " OK: all architecture tests passed"
115+
else
116+
echo "ERROR: one or more architecture tests failed"
117+
exit 1
118+
fi
119+
echo ""
120+
}
121+
122+
# --------------------------------------------------------------------------
123+
# Step 4 – Publish readiness check
124+
# --------------------------------------------------------------------------
125+
step4_publish() {
126+
echo "[4/5] Checking publish readiness (cargo publish --dry-run)..."
69127

70-
echo " Step 2: building kernel for $arch..."
71-
cargo xtask build --arch="$arch"
128+
local all_passed=true
72129

73-
echo " Step 3: running boot test for $arch..."
74-
if cargo xtask test --arch="$arch"; then
75-
echo "$arch boot test passed"
130+
for arch in "${ARCHS[@]}"; do
131+
echo ""
132+
echo " --- Publish check: ${arch} ---"
133+
if cargo xtask publish --arch="${arch}"; then
134+
echo " [${arch}] OK: publish check passed"
76135
else
77-
echo "Error: $arch boot test FAILED"
136+
echo " [${arch}] ERROR: publish check FAILED"
78137
all_passed=false
79138
fi
80139
done
81140

82141
echo ""
83142
if [ "$all_passed" = "true" ]; then
84-
echo "All architecture tests passed"
143+
echo " OK: all architecture publish checks passed"
85144
else
86-
echo "Error: one or more architecture tests failed"
145+
echo "ERROR: one or more publish checks failed"
87146
exit 1
88147
fi
89148
echo ""
90149
}
91150

92151
# --------------------------------------------------------------------------
93-
# [4/4] Summary
152+
# Step 5 – Summary
94153
# --------------------------------------------------------------------------
95-
print_summary() {
96-
echo "[4/4] Test Summary"
97-
echo "=================="
98-
echo "All checks passed successfully!"
154+
step5_summary() {
155+
echo "[5/5] Test Summary"
156+
echo " =============================="
157+
echo " OK: all requested steps completed successfully!"
99158
echo ""
100-
echo "The following checks were performed:"
101-
echo " 1. Tool availability check (make, cargo, python3, qemu-system-*)"
102-
echo " 2. Code format check (cargo fmt --all)"
103-
echo " 3. Architecture build + boot tests (riscv64, aarch64, loongarch64, x86_64)"
159+
echo " Steps available in this script:"
160+
echo " 1 – tool availability check (make, cargo, python3, qemu-system-*)"
161+
echo " 2 – code format check (cargo fmt --all)"
162+
echo " 3 – per-arch build + boot (riscv64, aarch64, loongarch64, x86_64)"
163+
echo " 4 – publish readiness check (cargo publish --dry-run)"
164+
echo " 5 – summary"
104165
echo ""
105166
}
106167

168+
# --------------------------------------------------------------------------
169+
# Usage / help (prints only the leading comment block of this file)
170+
# --------------------------------------------------------------------------
171+
usage() {
172+
# Print lines 2..N of this file that start with '#', stop at first blank
173+
# or non-comment line – those lines form the usage documentation block.
174+
tail -n +2 "$0" | while IFS= read -r line; do
175+
case "$line" in
176+
'#'*) printf '%s\n' "${line}" | sed 's/^#[[:space:]]*//' ;;
177+
*) break ;;
178+
esac
179+
done
180+
exit 0
181+
}
182+
107183
# --------------------------------------------------------------------------
108184
# Main
109185
# --------------------------------------------------------------------------
110186
main() {
111-
local skip_qemu="${SKIP_QEMU:-false}"
112-
113-
check_tools
114-
check_format
115-
116-
if [ "$skip_qemu" = "true" ]; then
117-
echo "[3/4] Skipping architecture tests (SKIP_QEMU=true)"
118-
echo ""
119-
else
120-
run_arch_tests
121-
fi
122-
123-
print_summary
187+
local step="${1:-all}"
188+
189+
case "$step" in
190+
-h|--help) usage ;;
191+
all)
192+
echo "=== StarryOS Test Script (all steps) ==="
193+
echo ""
194+
step1_check_tools
195+
step2_check_format
196+
step3_arch_tests
197+
step4_publish
198+
step5_summary
199+
;;
200+
1)
201+
echo "=== StarryOS Test Script (step 1: tool check) ==="
202+
echo ""
203+
step1_check_tools
204+
;;
205+
2)
206+
echo "=== StarryOS Test Script (step 2: format check) ==="
207+
echo ""
208+
step2_check_format
209+
;;
210+
3)
211+
echo "=== StarryOS Test Script (step 3: arch build + boot tests) ==="
212+
echo ""
213+
step3_arch_tests
214+
;;
215+
4)
216+
echo "=== StarryOS Test Script (step 4: publish readiness check) ==="
217+
echo ""
218+
step4_publish
219+
;;
220+
5)
221+
echo "=== StarryOS Test Script (step 5: summary) ==="
222+
echo ""
223+
step5_summary
224+
;;
225+
*)
226+
echo "ERROR: unknown step '${step}'. Valid values: 1, 2, 3, 4, 5, or omit for all."
227+
echo "Run '$0 --help' for usage."
228+
exit 1
229+
;;
230+
esac
124231
}
125232

126233
main "$@"

xtask/src/main.rs

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ enum Cmd {
3939
#[arg(long, default_value = "riscv64")]
4040
arch: String,
4141
},
42+
/// Run `cargo publish --dry-run` to verify crate packaging readiness
43+
Publish {
44+
/// Target architecture: riscv64, aarch64, x86_64, loongarch64
45+
#[arg(long, default_value = "riscv64")]
46+
arch: String,
47+
},
48+
}
49+
50+
/// Map a short architecture name to its Rust bare-metal target triple.
51+
fn arch_target(arch: &str) -> &'static str {
52+
match arch {
53+
"riscv64" => "riscv64gc-unknown-none-elf",
54+
"aarch64" => "aarch64-unknown-none-softfloat",
55+
"x86_64" => "x86_64-unknown-none",
56+
"loongarch64" => "loongarch64-unknown-none",
57+
_ => unreachable!("validate_arch should have caught this"),
58+
}
4259
}
4360

4461
/// Returns true when running inside WSL / WSL2.
@@ -100,6 +117,35 @@ fn do_run(root: &Path, arch: &str) {
100117
run(cmd);
101118
}
102119

120+
fn do_publish(root: &Path, arch: &str) {
121+
// Generate .axconfig.toml via the Make defconfig target so that
122+
// architecture-specific config crates resolve correctly during packaging.
123+
println!("==> make ARCH={arch} defconfig (generate .axconfig.toml)");
124+
let mut cfg_cmd = make_cmd(root, arch);
125+
cfg_cmd.arg("defconfig");
126+
run(cfg_cmd);
127+
128+
let target = arch_target(arch);
129+
println!("==> cargo publish --dry-run --allow-dirty --target={target} --features qemu");
130+
let status = Command::new("cargo")
131+
.current_dir(root)
132+
.args([
133+
"publish",
134+
"--dry-run",
135+
"--allow-dirty",
136+
"--target", target,
137+
"--features", "qemu",
138+
])
139+
.status()
140+
.unwrap_or_else(|e| {
141+
eprintln!("xtask: failed to spawn cargo publish: {e}");
142+
process::exit(1);
143+
});
144+
if !status.success() {
145+
process::exit(status.code().unwrap_or(1));
146+
}
147+
}
148+
103149
fn do_test(root: &Path, arch: &str) {
104150
let script = root.join("scripts").join("ci-test.py");
105151
if !script.exists() {
@@ -119,16 +165,19 @@ fn main() {
119165
let root = project_root();
120166

121167
validate_arch(match &cli.command {
122-
Cmd::Rootfs { arch } | Cmd::Build { arch } | Cmd::Run { arch } | Cmd::Test { arch } => {
123-
arch
124-
}
168+
Cmd::Rootfs { arch }
169+
| Cmd::Build { arch }
170+
| Cmd::Run { arch }
171+
| Cmd::Test { arch }
172+
| Cmd::Publish { arch } => arch,
125173
});
126174

127175
match &cli.command {
128-
Cmd::Rootfs { arch } => do_rootfs(&root, arch),
129-
Cmd::Build { arch } => do_build(&root, arch),
130-
Cmd::Run { arch } => do_run(&root, arch),
131-
Cmd::Test { arch } => do_test(&root, arch),
176+
Cmd::Rootfs { arch } => do_rootfs(&root, arch),
177+
Cmd::Build { arch } => do_build(&root, arch),
178+
Cmd::Run { arch } => do_run(&root, arch),
179+
Cmd::Test { arch } => do_test(&root, arch),
180+
Cmd::Publish { arch } => do_publish(&root, arch),
132181
}
133182
}
134183

0 commit comments

Comments
 (0)