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

66 lines
3.8 KiB
Nu

#!/usr/bin/env nu
# Compare portable metadata, apparent file sizes, and the fixed root identity.
def check [ok: bool, message: string] {
if not $ok { error make {msg: $message} }
print $'ok - ($message)'
}
def call [root: path, tool: string, args: list<string>] {
^wasmtime run -S cli-exit-with-code=y --dir $root ($env.CARGO_TARGET_DIR | path join wasm32-wasip2 release $'($tool).wasm') ...$args | complete
}
def main [] {
let root = (^mktemp -d /tmp/t42-fs-XXXXXXXX | str trim)
mkdir ($root | path join tree sub)
'hello' | save ($root | path join tree file)
'world!' | save ($root | path join tree sub nested)
let file = ($root | path join tree file)
for flags in [[-b] [-k] [-sh] [-ab] [-bc]] {
let native = (^du --apparent-size ...$flags $file | complete)
let wasm = (call $root du ($flags | append $file))
check ($native.stdout == $wasm.stdout and $native.exit_code == $wasm.exit_code) $'du ($flags | str join " ")'
}
let tree = ($root | path join tree)
let total = (call $root du [-sb $tree])
check ($total.stdout == $'11(char tab)($tree)(char newline)' and $total.exit_code == 0) 'du sums files without directory storage'
let depth = (call $root du [--max-depth 0 -b $tree])
check ($depth.stdout == $total.stdout and $depth.exit_code == 0) 'du max-depth'
let all = (call $root du [-ab $tree])
check (($all.stdout | lines | length) == 4 and $all.exit_code == 0) 'du all files and directories'
let total_all = (call $root du [-sbc $tree])
check ($total_all.stdout == $'11(char tab)($tree)(char newline)11(char tab)total(char newline)') 'du total'
for flags in [[] [-u] [-g] [-un] [-gn]] {
let native = (^id ...$flags root | complete)
let wasm = (call $root id $flags)
check ($native.stdout == $wasm.stdout and $native.exit_code == $wasm.exit_code) $'id ($flags | str join " ")'
}
check ((call $root id [-n]).stdout == $'root(char newline)') 'id name alone'
for format in ['%n %s %F' '%Y %X' '%y %x' '%N' '%%'] {
let native = (with-env {TZ: UTC} { ^stat -c $format $file | complete })
let wasm = (call $root stat [-c $format $file])
check ($native.stdout == $wasm.stdout and $native.exit_code == $wasm.exit_code) $'stat ($format)'
}
check ((call $root stat [-c '%a %A %u %g %U %G' $file]).stdout == $'644 -rw-r--r-- 0 0 root root(char newline)') 'stat virtual mode and identity'
check ((call $root stat [-c '%a %A %F' $tree]).stdout == $'755 drwxr-xr-x directory(char newline)') 'stat directory mode'
let link = ($root | path join link)
^ln -s tree/file $link
for flags in [[-c '%n %s %F %N'] [-L -c '%n %s %F %N']] {
let native = (^stat ...$flags $link | complete)
let wasm = (call $root stat ($flags | append $link))
check ($native.stdout == $wasm.stdout and $native.exit_code == $wasm.exit_code) $'stat symlink ($flags | str join " ")'
}
let default_stat = (call $root stat [$file])
check ($default_stat.exit_code == 0 and ($default_stat.stdout | str contains 'root')) 'stat default output'
let sized = ($root | path join sized)
for bytes in [1024 1025 10138 11000 1048575 1048576] {
^truncate -s $bytes $sized
let native = (^du --apparent-size -sh $sized | complete)
let wasm = (call $root du [-sh $sized])
check ($native.stdout == $wasm.stdout and $native.exit_code == $wasm.exit_code) $'du human size ($bytes)'
}
for tool in [du stat] {
let native = (if $tool == 'du' { ^du --apparent-size /t42-does-not-exist | complete } else { ^stat /t42-does-not-exist | complete })
let wasm = (call $root $tool [/t42-does-not-exist])
check ($wasm.exit_code == $native.exit_code) $'($tool) missing path status'
}
print $'evidence: ($root)'
}