65 lines
4.7 KiB
Nu
65 lines
4.7 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 plain = (call $target $root tar [-cf /plain.tar -C /input/sub file] | success)
|
|
check ($plain.stdout == '') 'tar -cf'
|
|
let native_list = (^tar -tf ($root | path join plain.tar) | complete)
|
|
let world_list = (call $target $root tar [-tf /plain.tar])
|
|
check ($native_list.stdout == $world_list.stdout and $native_list.exit_code == $world_list.exit_code) 'tar -tf matches native'
|
|
let native_verbose = (^tar -cvf ($root | path join verbose-native.tar) -C ($root | path join input sub) file | complete)
|
|
let world_verbose = (call $target $root tar [-cvf /verbose.tar -C /input/sub file])
|
|
check ($native_verbose.stdout == $world_verbose.stdout and $native_verbose.exit_code == $world_verbose.exit_code and $world_verbose.stderr == '') 'tar create verbose named archive'
|
|
let stream = (call $target $root tar [-cvf - -C /input/sub file] | success)
|
|
check (($stream.stderr | str trim) == 'file') 'tar create verbose stream uses stderr'
|
|
let native_stream = ($stream.stdout | ^tar -tf - | complete)
|
|
check ($native_stream.stdout == $native_list.stdout and $native_stream.exit_code == 0) 'native reads tar stdout stream'
|
|
mkdir ($root | path join stream-extract)
|
|
call $target $root tar [-xf - -C /stream-extract] $stream.stdout | success | ignore
|
|
check ((open --raw ($root | path join stream-extract file)) == 'hello from a nested file') 'tar -xf stdin stream'
|
|
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'
|
|
for tool in [gzip zcat] {
|
|
let flags = if $tool == 'gzip' { [-dc] } else { [] }
|
|
let decoded = (call $target $root $tool $flags $native_compressed.stdout | success)
|
|
check ($decoded.stdout == 'reverse payload') $'($tool) native gzip 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)'
|
|
}
|