#!/usr/bin/env nu # The single source of truth for names and flags: drives dispatch, zsh/bash # completion and the self-check. Nushell completion still comes from the # signatures below; the self-check keeps both in step. const command_table = [ { name: "unit", usage: "cargo test --lib --bins", flags: [] } { name: "integration", usage: "cargo test --tests [filter] [--nocapture]", flags: ["--nocapture"] } { name: "lint", usage: "fmt, clippy and the openssl check", flags: [] } { name: "vm", usage: "nix build of a NixOS VM test: vm or peers", flags: [] } { name: "all", usage: "lint, unit, integration; stops at the first failure", flags: [] } { name: "ci", usage: "all plus the vm test", flags: [] } { name: "completions", usage: "print a zsh or bash completion script", flags: [] } { name: "self-check", usage: "check that the table, commands and help agree", flags: [] } ] # One entry point for the repository's checks. # # Subcommands: # unit cargo test --lib --bins # integration cargo test --tests [filter] [--nocapture] # lint cargo fmt --check, cargo clippy --all-targets -- -D warnings, cargo tree -i openssl (empty) # vm nix build of a NixOS VM test: vm (checks) or peers (tests..peers) # all lint, unit, integration; stops at the first failure # ci all plus the vm test # completions print a zsh or bash completion script # self-check fail when the command table, the commands and the help text diverge # # Loaded as a module (`use dev/test.nu *`) this file provides `test` plus one # command per subcommand, so Nushell completes names and flags natively. # # The checks are read-only against the host: no /etc, systemd or Docker changes. export def main [ ...args: string # subcommand and its arguments, as in `test integration peers` --nocapture # forward to `test integration` ] { if ($args | is-empty) { print-help return } let sub = ($args | first) let rest = ($args | skip 1) let positional = ($rest | where {|a| not ($a | str starts-with "-") }) if $sub not-in ($command_table | get name) { error make { msg: $"unknown subcommand '($sub)'; run 'test --help' for the list" } } match $sub { "unit" => { test unit } "integration" => { test integration ($positional | get 0?) --nocapture=$nocapture } "lint" => { test lint } "vm" => { test vm ($positional | get 0?) } "all" => { test all } "ci" => { test ci } "completions" => { test completions ($positional | get 0?) } "self-check" => { test self-check } _ => { error make { msg: $"subcommand '($sub)' has no dispatch arm" } } } } # Run the unit tests: no sockets, no host access. export def "test unit" [] { run-steps (unit-steps) } # Run the integration tests (plain, tls, integration, binary_smoke, peers). export def "test integration" [ filter?: string@"nu-complete test filters" # cargo name filter, e.g. a test name or file --nocapture # do not capture test output ] { run-steps (integration-steps $filter $nocapture) } # Formatting, lints and the dependency check. export def "test lint" [] { run-steps (lint-steps) } # Run a NixOS VM test. Slow: it builds and boots VMs. export def "test vm" [ name?: string@"nu-complete test vm" # vm (checks) or peers (tests..peers) ] { let name = ($name | default "vm") if $name not-in (nu-complete test vm) { error make { msg: $"unknown VM test '($name)'; expected one of: (nu-complete test vm | str join ', ')" } } run-steps (vm-steps $name) } # lint, unit and integration; stops at the first failure. export def "test all" [] { run-steps ((lint-steps) | append (unit-steps) | append (integration-steps null false)) } # `test all` plus the VM test. export def "test ci" [] { run-steps ((lint-steps) | append (unit-steps) | append (integration-steps null false) | append (vm-steps "vm")) } # Print a completion script for a shell that cannot load this module (zsh, bash). export def "test completions" [ shell?: string@"nu-complete test shells" # zsh or bash ] { let shell = ($shell | default "zsh") match $shell { "zsh" => { print (completion-script zsh) } "bash" => { print (completion-script bash) } _ => { error make { msg: $"unknown shell '($shell)'; expected one of: (nu-complete test shells | str join ', ')" } } } } # Fail if the command table, the defined commands or the help text diverge. export def "test self-check" [] { let problems = (check-consistency) if not ($problems | is-empty) { print -e "test self-check failed:" $problems | each {|p| print -e $" - ($p)" } | ignore exit 1 } print "test self-check: command table, commands and help agree" } def "nu-complete test vm" [] { ["vm" "peers"] } def "nu-complete test shells" [] { ["zsh" "bash"] } # Test names come from cargo; the first Tab compiles in debug mode and is slow, # so the list is kept for the rest of the session. def "nu-complete test filters" [] { if ($env.SKYG_TEST_FILTERS? | is-not-empty) { return $env.SKYG_TEST_FILTERS } let listed = (do -i { run-external "cargo" "test" "--tests" "--" "--list" | complete }) let names = ($listed | default {stdout: ""} | get stdout | lines | where {|l| $l =~ ': (test|benchmark)$'} | each {|l| $l | str replace -r ': (test|benchmark)$' '' | str replace -r '^.*: ' '' } | uniq) if not ($names | is-empty) { $env.SKYG_TEST_FILTERS = $names } $names } def step-record [label: string, command: string, body: closure] { { label: $label, command: $command, body: $body } } def unit-steps [] { [(step-record "unit" "cargo test --lib --bins" {|| run-checked "cargo" ["test" "--lib" "--bins"] })] } def require-kvm [] { if not ("/dev/kvm" | path exists) { error make { msg: "/dev/kvm is missing: NixOS VM tests need hardware virtualisation. Run on a Linux host with KVM, or in CI with KVM exposed; there is no software fallback." } } } def vm-steps [name: string] { require-kvm let attr = (match $name { "peers" => "tests" _ => "checks" }) let system = $"($nu.os-info.arch)-($nu.os-info.name)" [(step-record $"vm ($name)" $"nix build .#($attr).($system).($name) -L" {|| print "NixOS VM tests are slow: expect several minutes." run-checked "nix" ["build" $".#($attr).($system).($name)" "-L"] })] } def integration-steps [filter: any, nocapture: bool] { let filter_args = (if ($filter | is-empty) { [] } else { ["--", $filter] }) let capture_args = (if $nocapture { ["--nocapture"] } else { [] }) let args = (["test" "--tests"] | append $filter_args | append $capture_args) let label = (if ($filter | is-empty) { "integration" } else { $"integration ($filter)" }) [(step-record $label $"cargo ($args | str join ' ')" {|| run-checked "cargo" $args })] } def lint-steps [] { [ (step-record "fmt" "cargo fmt --check" {|| run-checked "cargo" ["fmt" "--check"] }) (step-record "clippy" "cargo clippy --all-targets -- -D warnings" {|| run-checked "cargo" ["clippy" "--all-targets" "--" "-D" "warnings"] }) # An empty tree is the only acceptable answer; cargo itself exits non-zero # when nothing matches, so the exit code is not the signal here. (step-record "openssl" "cargo tree -i openssl (must be empty)" {|| let found = (run-external "cargo" "tree" "-i" "openssl" | complete) if not (($found.stdout | str trim) | is-empty) { error make { msg: $"openssl is in the dependency tree:\n($found.stdout)" } } }) ] } # Run each step in order, stop at the first failure, always print the summary # table and exit non-zero if anything failed. def run-steps [steps: list] { mut results = [] for s in $steps { let r = (run-step $s) $results = ($results | append $r) if $r.result != "ok" { break } } let width = ([26 ($results | each {|r| $r.step | str length } | math max)] | math max) print "" print "Summary:" print ((pad "step" $width "left") + (pad "result" 8 "left") + (pad "seconds" 9 "right") + " command") for r in $results { let status = (pad $r.result 8 "left") print ((pad $r.step $width "left") + (if $r.result == "ok" { $"(ansi green)($status)(ansi reset)" } else { $"(ansi red)($status)(ansi reset)" }) + (pad $"(($r.seconds))s" 9 "right") + $" ($r.command)") } print "" if ($results | any {|r| $r.result != "ok" }) { exit 1 } } def pad [text: string, width: int, alignment: string] { $text | fill -a $alignment -w $width } def run-step [s: record] { print "" print $"(ansi cyan)==> ($s.label)(ansi reset) ($s.command)" let started = (date now) let ok = (try { do $s.body | ignore true } catch {|e| print -e $"(ansi red)($s.label) failed: ($e.msg)(ansi reset)" false }) { step: $s.label command: $s.command result: (if $ok { "ok" } else { "FAILED" }) seconds: ((((date now) - $started) / 1sec) | math round --precision 1) } } # Redirect straight to stdout so long builds and tests show progress; Nushell # raises on a non-zero exit, which the enclosing step records as a failure. def run-checked [cmd: string, args: list] { run-external $cmd ...$args o> /dev/stdout } def print-help [] { print "Run the repository's checks." print "" print "Usage: test [args]" print "" $command_table | each {|c| {subcommand: $c.name, flags: ($c.flags | str join " "), what: $c.usage} } | table -e | print print "" print "In Nushell: use dev/test.nu * (then `test integration peers`). In zsh/bash: source <(test completions zsh|bash)." print "Underlying commands: test --help, or `test --help`." } def completion-script [shell: string] { let subcommands = ($command_table | get name | str join " ") let flags = ($command_table | get flags | flatten | uniq | str join " ") let replacements = { "__SUBCOMMANDS__": $subcommands "__FLAGS__": $flags "__VM__": (nu-complete test vm | str join " ") "__SHELLS__": (nu-complete test shells | str join " ") } let template = (match $shell { "zsh" => (zsh-template) _ => (bash-template) }) $replacements | transpose key value | reduce --fold $template {|it, acc| $acc | str replace -a $it.key $it.value } } def zsh-template [] { # A raw string cannot start with `#` in Nushell, so the template starts on the next line. r#' #compdef test # Completion for the repository test runner: source <(test completions zsh) _test_runner() { local -a subcommands subcommands=(__SUBCOMMANDS__) if (( CURRENT == 2 )); then if [[ "${words[2]}" == -* ]]; then compadd -- __FLAGS__ else _describe 'subcommand' subcommands fi return fi case "${words[2]}" in vm) compadd -- __VM__ __FLAGS__ ;; completions) compadd -- __SHELLS__ ;; *) compadd -- __FLAGS__ ;; esac } compdef _test_runner test '# | str trim --left } def bash-template [] { r#' # bash completion for the repository test runner: source <(test completions bash) _test_runner() { local cur sub i cur="${COMP_WORDS[COMP_CWORD]}" sub="" for ((i=1; i