- 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>
54 lines
1.8 KiB
Bash
54 lines
1.8 KiB
Bash
#!/usr/bin/env bash
|
|
# Shared test helper — sets up mocked environment for BATS tests
|
|
|
|
BATS_SUPPORT="${BATS_SUPPORT:-$HOME/.local/share/bats-support}"
|
|
BATS_ASSERT="${BATS_ASSERT:-$HOME/.local/share/bats-assert}"
|
|
BATS_FILE="${BATS_FILE:-$HOME/.local/share/bats-file}"
|
|
|
|
load "$BATS_SUPPORT/load.bash"
|
|
load "$BATS_ASSERT/load.bash"
|
|
|
|
PROJECT_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME}")/.." && pwd)"
|
|
FIXTURES_DIR="$PROJECT_ROOT/tests/fixtures"
|
|
|
|
# Create a temporary mock sysfs tree for GPU detection tests
|
|
setup_mock_sysfs() {
|
|
MOCK_SYSFS="$(mktemp -d)"
|
|
local gpu_dir="$MOCK_SYSFS/class/drm/card0/device"
|
|
mkdir -p "$gpu_dir/hwmon/hwmon0"
|
|
|
|
echo "0x1002" > "$gpu_dir/vendor"
|
|
echo "0x1586" > "$gpu_dir/device"
|
|
echo "${MOCK_VRAM_TOTAL:-536870912}" > "$gpu_dir/mem_info_vram_total"
|
|
echo "${MOCK_VRAM_USED:-104857600}" > "$gpu_dir/mem_info_vram_used"
|
|
echo "${MOCK_GTT_TOTAL:-64424509440}" > "$gpu_dir/mem_info_gtt_total"
|
|
echo "${MOCK_GTT_USED:-209715200}" > "$gpu_dir/mem_info_gtt_used"
|
|
echo "${MOCK_GPU_BUSY:-5}" > "$gpu_dir/gpu_busy_percent"
|
|
echo "${MOCK_GPU_TEMP:-50000}" > "$gpu_dir/hwmon/hwmon0/temp1_input"
|
|
echo "${MOCK_GPU_POWER:-25000000}" > "$gpu_dir/hwmon/hwmon0/power1_average"
|
|
|
|
export GPU_SYSFS="$gpu_dir"
|
|
}
|
|
|
|
teardown_mock_sysfs() {
|
|
[[ -n "${MOCK_SYSFS:-}" ]] && rm -rf "$MOCK_SYSFS"
|
|
}
|
|
|
|
# Create a mock /proc/cmdline
|
|
setup_mock_cmdline() {
|
|
MOCK_PROC="$(mktemp -d)"
|
|
echo "${MOCK_CMDLINE:-BOOT_IMAGE=/boot/vmlinuz root=UUID=abc ro quiet}" > "$MOCK_PROC/cmdline"
|
|
}
|
|
|
|
teardown_mock_proc() {
|
|
[[ -n "${MOCK_PROC:-}" ]] && rm -rf "$MOCK_PROC"
|
|
}
|
|
|
|
# Source a lib file with PROJECT_ROOT set correctly
|
|
source_lib() {
|
|
local lib="$1"
|
|
# Disable color for predictable test output
|
|
export TERM=dumb
|
|
source "$PROJECT_ROOT/lib/$lib"
|
|
}
|