test: add BATS test suite (79 tests)

- tests/common.bats: PROJECT_ROOT detection, is_cmd, timestamp, data_dir,
  logging functions, color handling, require_root
- tests/detect.bats: GPU sysfs reads with mock sysfs tree, kernel param
  parsing (word boundary, dot escaping, edge positions), recommended
  GTT/pages computation (64GB, 128GB, tiny, zero), firmware bad detection,
  stack detection
- tests/format.bats: human_bytes (0, KiB, MiB, GiB boundaries, 64GiB),
  human_mib (sub-GiB, exact-GiB, recommended values, empty input)
- tests/benchmark_compare.bats: improvement/regression display, empty
  results, missing files, usage output, config change detection
- tests/log_metrics.bats: CSV header, data format, field count, input
  validation, unknown argument handling
- tests/test_helper.sh: mock sysfs tree builder, bats-assert/support setup

Makefile: add 'make test' target

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Felipe Cardoso
2026-03-25 22:15:34 +01:00
parent da2c4c6b8a
commit a403dd9ce0
7 changed files with 679 additions and 1 deletions

121
tests/common.bats Normal file
View File

@@ -0,0 +1,121 @@
#!/usr/bin/env bats
# Tests for lib/common.sh — core utilities
load test_helper.sh
setup() {
source_lib common.sh
}
# ── PROJECT_ROOT detection ───────────────────────────────
@test "PROJECT_ROOT points to a directory containing Makefile" {
[ -f "$PROJECT_ROOT/Makefile" ]
}
@test "PROJECT_ROOT is not the filesystem root" {
[ "$PROJECT_ROOT" != "/" ]
}
# ── is_cmd ───────────────────────────────────────────────
@test "is_cmd: bash exists" {
run is_cmd bash
assert_success
}
@test "is_cmd: nonexistent command fails" {
run is_cmd this_command_does_not_exist_xyz123
assert_failure
}
@test "is_cmd: empty argument fails" {
run is_cmd ""
assert_failure
}
# ── timestamp ────────────────────────────────────────────
@test "timestamp: returns YYYYMMDD-HHMMSS format" {
run timestamp
assert_success
[[ "$output" =~ ^[0-9]{8}-[0-9]{6}$ ]]
}
@test "timestamp: returns a reasonable date (not epoch)" {
local ts
ts="$(timestamp)"
local year="${ts:0:4}"
(( year >= 2025 ))
}
# ── data_dir ─────────────────────────────────────────────
@test "data_dir: creates directory and returns path" {
local dir
dir="$(data_dir "test-tmp-$$")"
[ -d "$dir" ]
rmdir "$dir"
}
@test "data_dir: default returns data/ under project root" {
local dir
dir="$(data_dir ".")"
[[ "$dir" == "$PROJECT_ROOT/data/." ]]
}
# ── logging functions produce output ─────────────────────
@test "log_info: produces output" {
run log_info "test message"
assert_success
assert_output --partial "test message"
}
@test "log_success: produces output" {
run log_success "it worked"
assert_success
assert_output --partial "it worked"
}
@test "log_warn: produces output" {
run log_warn "warning"
assert_success
assert_output --partial "warning"
}
@test "log_error: produces output on stderr" {
run log_error "failure"
assert_success
# log_error writes to stderr but bats captures both
assert_output --partial "failure"
}
@test "log_header: produces output with delimiters" {
run log_header "Section Title"
assert_success
assert_output --partial "Section Title"
}
# ── Color handling ───────────────────────────────────────
@test "colors are empty strings when not a terminal" {
export TERM=dumb
# Re-source to pick up non-terminal detection
unset RED GREEN YELLOW BLUE CYAN BOLD DIM RESET
source_lib common.sh
# When stdout is not a tty (as in bats), colors should be empty
[ -z "$RED" ] || [ "$RED" = "" ]
}
# ── require_root ─────────────────────────────────────────
@test "require_root: exits with error when not root" {
# We're not running tests as root
if [ "$EUID" -eq 0 ]; then
skip "Test must not run as root"
fi
run require_root
assert_failure
assert_output --partial "root"
}