mirror of
https://github.com/lloeki/apply.git
synced 2025-12-06 09:24:38 +01:00
lib: more helper functions
This commit is contained in:
parent
dafb44313c
commit
a519746560
2 changed files with 68 additions and 0 deletions
40
lib
40
lib
|
|
@ -40,3 +40,43 @@ substituteInPlace() {
|
||||||
# Overwrite now that we've succeeded
|
# Overwrite now that we've succeeded
|
||||||
mv "$tmpfile" "$fpath"
|
mv "$tmpfile" "$fpath"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Shell-quotes an arbitrary string.
|
||||||
|
#
|
||||||
|
# From: http://www.etalabs.net/sh_tricks.html
|
||||||
|
#
|
||||||
|
# This function simply replaces every instance of «'» (single quote) within
|
||||||
|
# the string with «'\''» (single quote, backslash, single quote, single
|
||||||
|
# quote), then puts single quotes at the beginning and end of the string.
|
||||||
|
# Since the only character whose meaning is special within single quotes is
|
||||||
|
# the single quote character itself, this is totally safe. Trailing
|
||||||
|
# newlines are handled correctly, and the single quote at the end doubles
|
||||||
|
# as a safety character to prevent command substitution from clobbering the
|
||||||
|
# trailing newlines, should one want to do something like:
|
||||||
|
# quoted=$(quote "$var")
|
||||||
|
quote() {
|
||||||
|
printf "%s\n" "$1" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Returns whether or not a string matches a glob; similar to the bash [[ test,
|
||||||
|
# but portable.
|
||||||
|
fnmatch() {
|
||||||
|
# We explicitly want this to act as a glob
|
||||||
|
# shellcheck disable=SC2254
|
||||||
|
case "$2" in
|
||||||
|
$1) return 0 ;;
|
||||||
|
*) return 1 ;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
# Portable equivalent to GNU's `date +"%s"`
|
||||||
|
#
|
||||||
|
# From: http://www.etalabs.net/sh_tricks.html
|
||||||
|
epochseconds() {
|
||||||
|
# This is horrible, but... it tells 'date' to generate a string that
|
||||||
|
# contains shell math to generate the actual epoch date.
|
||||||
|
#
|
||||||
|
# TODO(andrew-d): break down and verify the math here
|
||||||
|
echo $(( $(TZ=GMT0 date +"((%Y-1600)*365+(%Y-1600)/4-(%Y-1600)/100+(%Y-1600)/400+1%j-1000-135140)*86400+(1%H-100)*3600+(1%M-100)*60+(1%S-100)") ))
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -67,5 +67,33 @@ testSubstituteInPlace() {
|
||||||
assertEquals "hi hi hi" "$(cat "$TEST_TMPDIR/sub.txt")"
|
assertEquals "hi hi hi" "$(cat "$TEST_TMPDIR/sub.txt")"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
testQuote() {
|
||||||
|
local input expected
|
||||||
|
|
||||||
|
{ input="$(cat)"; } <<EOF
|
||||||
|
foo'bar"baz'
|
||||||
|
EOF
|
||||||
|
|
||||||
|
{ expected="$(cat)"; } <<EOF
|
||||||
|
'foo'\\''bar"baz'\\'''
|
||||||
|
EOF
|
||||||
|
|
||||||
|
assertEquals "$expected" "$(quote "$input")"
|
||||||
|
}
|
||||||
|
|
||||||
|
testFnmatch() {
|
||||||
|
assertTrue 'fnmatch "f??*" "foobar"'
|
||||||
|
assertFalse 'fnmatch "f??*" "fo"'
|
||||||
|
assertFalse 'fnmatch "f??*" "loob"'
|
||||||
|
}
|
||||||
|
|
||||||
|
testEpochseconds() {
|
||||||
|
# TODO(andrew-d): better test
|
||||||
|
|
||||||
|
# Assert that it's numeric by deleting all numbers and verifying that
|
||||||
|
# there's nothing left.
|
||||||
|
assertEquals "" "$(epochseconds | tr -d '[0-9]')"
|
||||||
|
}
|
||||||
|
|
||||||
# Load shUnit2
|
# Load shUnit2
|
||||||
. ./shunit2
|
. ./shunit2
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue