Files
sausage/userland/archive-tools/test.nu
T

45 lines
3.1 KiB
Nu

#!/usr/bin/env nu
# Compare archive tools and pinned preview1 commands against native tools.
def check [ok: bool, message: string] { if not $ok { error make {msg: $message} }; print $'ok - ($message)' }
def call [target: path, root: path, tool: string, args: list<string>, input?: any] {
let input = ($input | default '')
$input | ^wasmtime run -S cli-exit-with-code=y --dir $'($root)::/' ($target | path join wasm32-wasip2 release $'($tool).wasm') ...$args | complete
}
def success [] { let r = $in; if $r.exit_code != 0 { error make {msg: $r.stderr} }; $r }
def main [] {
let target = ($env.CARGO_TARGET_DIR | path expand)
let root = (^mktemp -d /tmp/t42-tools-XXXXXXXX | str trim)
mkdir ($root | path join input sub)
'hello from a nested file' | save ($root | path join input sub file)
let created = (call $target $root tar [-czf /ours.tgz -C / input] | success)
check ($created.stdout == '') 'tar create stdout'
^tar -xzf ($root | path join ours.tgz) -C $root
check ($env.LAST_EXIT_CODE == 0) 'native tar reads world gzip archive'
^tar -czf ($root | path join native.tgz) -C $root input
mkdir ($root | path join extracted)
call $target $root tar [-xzf /native.tgz -C /extracted --strip-components 1] | success | ignore
check ((open --raw ($root | path join extracted sub file)) == 'hello from a nested file') 'world extracts native archive with strip-components'
let listed = (call $target $root tar [-tf /missing.tar])
let native_error = (^tar -tf ($root | path join missing.tar) | complete)
check ($listed.exit_code == $native_error.exit_code) 'tar missing archive exit code'
let compressed = (call $target $root gzip [-c] 'binary payload' | success)
let decoded = ($compressed.stdout | ^gzip -dc | complete)
check ($decoded.exit_code == 0 and $decoded.stdout == 'binary payload') 'native gzip decodes world stream'
let native_compressed = ('reverse payload' | ^gzip -c | complete)
let decoded = (call $target $root gunzip [-c] $native_compressed.stdout | success)
check ($decoded.stdout == 'reverse payload') 'world gunzip decodes native stream'
call $target $root gzip [-k /input/sub/file] | success | ignore
check (($root | path join input sub file.gz | path exists) and ($root | path join input sub file | path exists)) 'gzip -k keeps input'
let conflict = (call $target $root gzip [/input/sub/file])
check ($conflict.exit_code == 1) 'gzip refuses overwrite'
call $target $root gunzip [-fk /input/sub/file.gz] | success | ignore
for program in ['{sum += $1} END {print sum}' '{print NR, NF, $2}' 'BEGIN {for (i=1;i<=4;i++) print i*i}'] {
let native = ("2 a\n3 b\n" | ^awk $program | complete)
let wasm = ("2 a\n3 b\n" | ^wasmtime run --dir $'($root)::/' ($target | path join awk.wasm) $program | complete)
check ($native.stdout == $wasm.stdout and $native.exit_code == $wasm.exit_code) $'awk ($program)'
}
let qjs = (^wasmtime run ($env.FILE_PWD | path join .. qjs qjs.wasm) -e 'console.log(6*7)' | complete)
check ($qjs.exit_code == 0 and ($qjs.stdout | str trim) == '42') 'qjs -e'
print $'evidence: ($root)'
}