repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
opencode/test-c-skills/build.nu
Raw#!/usr/bin/env nu
# Build and test script for C skills
# Usage: nu build.nu [clean|build|test|all|asan]
const SRC_DIR = "src"
const BIN_DIR = "bin"
const CC = "gcc"
const CFLAGS = ["-Wall", "-Wextra", "-g", "-I./src"]
const CFLAGS_ASAN = ["-Wall", "-Wextra", "-g", "-I./src", "-fsanitize=address"]
const TEST_FILES = [
"test-arena",
"test-strings",
"test-platform",
"test-macros",
"test-ub-safety",
"test-single-header"
]
# Main entry point
def main [action: string = "all"] {
match $action {
"clean" => clean,
"build" => build,
"test" => test,
"all" => { build; test },
"asan" => asan_build,
_ => {
print $"Unknown action: ($action)"
print "Usage: nu build.nu [clean|build|test|all|asan]"
error make { msg: $"Unknown action: ($action)" }
}
}
}
# Clean
def clean [] {
print "Cleaning..."
mkdir $BIN_DIR --verbose
try {
ls $"($BIN_DIR)/*" | each { |f| rm $f.name --permanent --verbose }
}
print "Clean complete!"
}
# Build
def build [] {
print "Building all test files..."
mkdir $BIN_DIR --verbose
mut built = 0
for test_name in $TEST_FILES {
if (build_one $test_name $CFLAGS) { $built += 1 }
}
print $"\nBuild complete: ($built) binaries"
}
# ASAN build
def asan_build [] {
print "Building with ASAN enabled..."
mkdir $BIN_DIR --verbose
mut built = 0
for test_name in $TEST_FILES {
let cflags = if $test_name == "test-ub-safety" { $CFLAGS_ASAN } else { $CFLAGS }
if (build_one $test_name $cflags) { $built += 1 }
}
print $"\nASAN build complete: ($built) binaries"
}
# Build one test
def build_one [name: string, flags: list<string>]: nothing -> bool {
let src = $"($SRC_DIR)/($name).c"
let out = $"($BIN_DIR)/($name)"
if not ($src | path exists) {
print $" ✗ ($name) - source not found"
return false
}
let result = do { ^$CC ...$flags -o $out $src $"($SRC_DIR)/base.c" } | complete
if $result.exit_code == 0 {
let is_asan = ($flags | describe) | str contains "address"
let tag = if $is_asan { " (ASAN)" } else { "" }
print $" ✓ ($name)($tag)"
return true
} else {
print $" ✗ ($name) FAILED"
print $result.stderr
return false
}
}
# Run tests
def test [] {
print "\nRunning all tests...\n"
mut passed = 0
mut failures = 0
for test_name in $TEST_FILES {
let test_path = $"($BIN_DIR)/($test_name)"
if not ($test_path | path exists) {
print $" ⊘ ($test_name) - binary not found (run 'build' first)"
$failures += 1
continue
}
print $"Running ($test_name)..."
let result = do { ^$test_path } | complete
if $result.exit_code == 0 {
print $" ✓ ($test_name) PASSED"
$passed += 1
} else {
print $" ✗ ($test_name) FAILED (exit: ($result.exit_code))"
if not ($result.stderr | is-empty) { print $result.stderr }
$failures += 1
}
}
print $"\n================================"
print $"Tests: ($passed) passed, ($failures) failed"
if $failures > 0 { error make { msg: $"Tests failed: ($failures)" } }
}