Skip to content

Commit 58b4f6b

Browse files
committed
feat(skip-exist): add _SKIPFILE template, file check
1 parent 6b4c319 commit 58b4f6b

2 files changed

Lines changed: 70 additions & 4 deletions

File tree

skip-exist

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
set -euo pipefail
33
#
44
# 20260226WF - init
5-
#
5+
#
66
# TODO: add 'verb' or 'warn' instead of echo >&2
7+
# or some way to suppress SKIP and already have messsages
8+
#
79

810
REDO=${REDO:-0} # default: skip if exists. REDO !=0 means overwrite
911

@@ -15,10 +17,15 @@ USAGE:
1517
1618
$0 sub-1/mri/aseg.nii.gz recon-all -s sub-1
1719
$0 sub-1/mri/aseg.nii.gz dryrun recon-all -s sub-1
20+
REDO=1 $0 sub-1/mri/aseg.nii.gz recon-all -s sub-1
21+
22+
$0 /tmp/date.txt sh -c "date > _SKIPFILE"
1823
1924
SYNOPSIS:
2025
'test -s' wrapper with REDO and verbose messaging support
2126
27+
_SKIPFILE in cmd will be replaced by first argument
28+
2229
ENVIRONMENT:
2330
REDO=1 - overwrite (redo) even if output file exists
2431
@@ -34,7 +41,7 @@ NOTES:
3441
or a dedicated shell
3542
3643
$0 /tmp/date.txt sh -c 'date > /tmp/date.txt'
37-
44+
3845
But maybe '$0' is overkill for your usage. Consider:
3946
4047
test -s /tmp/date.txt || date > \$_
@@ -53,10 +60,22 @@ outfile="${1:?need two args. first is outfile}"; shift
5360
usage "ERROR: no command to run if '$outfile' doesn't exist" >&2 &&
5461
exit 1
5562

63+
cmd=("${@//_SKIPFILE/"$outfile"}")
5664
if [ -s "$outfile" ]; then
5765
[ "$REDO" == 0 ] &&
5866
echo "# SKIP: Already have '$outfile', not running '$*'; set REDO=1 to overwrite." >&2 &&
5967
exit 0
6068
echo " # Already have '$outfile' but overwritting: REDO=$REDO." >&2
6169
fi
62-
"$@"
70+
71+
# run the thing that should make the input
72+
"${cmd[@]}"
73+
74+
# check that what we did worked
75+
cmd_status=$?
76+
77+
# if there is an error (cmd_status > 0), should we still warn?
78+
test $cmd_status -eq 0 -a -s "$outfile" ||
79+
echo "# WARNING: '$outfile' DNE or is empty after '$*'" >&2
80+
81+
exit $cmd_status

t/skip-exist.bats

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
setup(){
2-
writedate() { date +%S%N > "$1"; }
2+
writedate() { date +%N > "$1"; }
33
export -f writedate
44
}
55

66
test_skipexist() { # @test
77
local output
88
o=$BATS_TEST_TMPDIR/d
99
run skip-exist "$o" writedate "$o"
10+
[[ ! $output =~ WARNING ]]
1011
test -s "$o"
1112
dt=$(cat "$o")
1213
run skip-exist "$o" writedate "$o"
1314
[[ $output =~ SKIP ]]
1415
[[ $(cat "$o") == "$dt" ]]
1516
}
1617

18+
test_skipexist_help() { # @test
19+
local output status
20+
o=$BATS_TEST_TMPDIR/d
21+
run skip-exist
22+
[ ! -r "$o" ]
23+
[ "$status" -eq 0 ]
24+
[[ "$output" =~ USAGE ]]
25+
26+
run skip-exist "$o"
27+
[ "$status" -gt 0 ]
28+
[ ! -r "$o" ]
29+
[[ "$output" =~ ERROR ]]
30+
}
31+
1732
test_skipexist_redo() { # @test
1833
export REDO=1
1934
o=$BATS_TEST_TMPDIR/d
@@ -24,3 +39,35 @@ test_skipexist_redo() { # @test
2439
[[ "$output" =~ "overwrit" ]]
2540
[[ $(cat "$o") -gt "$dt" ]]
2641
}
42+
43+
test_skipexist_nodata() { # @test
44+
o=$BATS_TEST_TMPDIR/d
45+
run skip-exist "$o" :
46+
! test -s "$o"
47+
[[ $output =~ WARN ]]
48+
49+
# CHECK HERE:
50+
# maybe we *do* want to warn even if command fails?
51+
badfunc() { return 1; }
52+
export -f badfunc
53+
run skip-exist "$o" badfunc
54+
[[ ! $output =~ WARN ]]
55+
}
56+
57+
test_skipexist_space() { # @test
58+
o=$BATS_TEST_TMPDIR/"a b"
59+
run skip-exist "$o" writedate "$o"
60+
test -s "$o"
61+
dt=$(cat "$o")
62+
run skip-exist "$o" writedate "$o"
63+
[[ $(cat "$o") == "$dt" ]]
64+
}
65+
66+
test_skipexist_SKIPFILE() { # @test
67+
o=$BATS_TEST_TMPDIR/"a b"
68+
run skip-exist "$o" writedate _SKIPFILE
69+
test -s "$o"
70+
dt=$(cat "$o")
71+
run skip-exist "$o" writedate "$o"
72+
[[ $(cat "$o") == "$dt" ]]
73+
}

0 commit comments

Comments
 (0)