50 lines
3.1 KiB
Nu
50 lines
3.1 KiB
Nu
#!/usr/bin/env nu
|
|
# Compare find and xargs with native tools in a real world.
|
|
def main [host: path, bundle: path] {
|
|
let world = (mktemp -d)
|
|
mkdir ($world | path join world)
|
|
cp -r ($bundle | path join bin) ($world | path join world bin)
|
|
let fixture = ($world | path join world fixture)
|
|
mkdir ($fixture | path join skip/deep) ($fixture | path join keep/deep) ($fixture | path join empty)
|
|
'hidden' | save ($fixture | path join skip/deep/file)
|
|
'shown' | save ($fixture | path join keep/deep/file)
|
|
mkdir ($fixture | path join target)
|
|
'artifact' | save ($fixture | path join target output)
|
|
'' | save ($fixture | path join zero)
|
|
'' | save ($fixture | path join reference)
|
|
^touch -d '2001-01-01 UTC' ($fixture | path join reference)
|
|
for args in [[-name skip -prune -o -type f -print] [-path './skip' -prune -o -print] [-name skip -prune -o -type f] [-maxdepth 1] [-mindepth 1 -maxdepth 2] [-empty] [-newer reference] [-path './keep/*'] [-type f -not -path './target/*'] ['!' -type d] [-not -not -type f] ['(' -name zero -o -name reference ')' -a -type f] [-name zero -o -name reference -a -type d] [-not '(' -name skip -o -name keep ')' -a -type d] [-name skip -prune -o '(' -type f -a '!' -name reference ')' -print] [-name zero -o -name reference -print] ['(' -name zero -o -name reference ')' -print] [-not] ['(' -type f] [-type f -a]] {
|
|
let native = (do { cd $fixture; ^find . ...$args | complete })
|
|
let command = ($args | each {|arg| $arg | into string | to json -r} | str join ' ')
|
|
let guest = (^$host sh $world -c $"cd /fixture; find . ($command)" | complete)
|
|
if $native.exit_code != $guest.exit_code or ($native.stdout | lines | sort) != ($guest.stdout | lines | sort) {
|
|
error make {msg: $"find mismatch ($args): native=($native) guest=($guest). World: ($world)"}
|
|
}
|
|
print $"PASS find ($args)"
|
|
}
|
|
let nul = (char nul)
|
|
let cases = [
|
|
{input: "a\nb\n", args: [-I{} printf '<%s>\n' '{}']}
|
|
{input: "a b\nc d\n", args: [-I REPL printf '<%s>\n' preREPLpost REPL]}
|
|
{input: " 'a b'\nc\\ d\n\n", args: [-I{} printf '<%s>\n' '{}']}
|
|
{input: $"'a b'($nul)two\nlines($nul)", args: ['-0' -I{} printf '<%s>\n' '{}']}
|
|
{input: $"a b($nul)c($nul)d($nul)", args: ['-0' -n 2 printf '<%s>\n']}
|
|
{input: "a b c\n", args: [-n 2 echo]}
|
|
{input: "'a b' c\n", args: [-n1 printf '<%s>\n']}
|
|
{input: '', args: [echo]}
|
|
{input: '', args: [-r echo]}
|
|
{input: '', args: [-I{} echo '{}']}
|
|
{input: "a\n", args: [sh -c 'exit 7']}
|
|
]
|
|
for case in $cases {
|
|
$case.input | save -f ($world | path join world input)
|
|
let native = ($case.input | ^xargs ...$case.args | complete)
|
|
let command = ($case.args | each {|arg| $arg | into string | to json -r} | str join ' ')
|
|
let guest = (^$host sh $world -c $"xargs ($command) < /input" | complete)
|
|
if $native.stdout != $guest.stdout or $native.exit_code != $guest.exit_code {
|
|
error make {msg: $"xargs mismatch ($case): native=($native) guest=($guest). World: ($world)"}
|
|
}
|
|
print $"PASS xargs ($case.args)"
|
|
}
|
|
}
|