Created
June 19, 2016 07:16
-
-
Save Klortho/edf1ff0bd7a83004b17596d62d5d6332 to your computer and use it in GitHub Desktop.
shell script utilities, complete with unit test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# What directory are we in? | |
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
# TeamCity messages | |
teamcity () { | |
echo "##teamcity[message text='$*']" | |
} | |
# Parse a filename, see http://stackoverflow.com/revisions/1403489/2 | |
parse_filename () { | |
fullpath=$1 | |
part=$2 | |
# Strip longest match of */ from start | |
filename="${fullpath##*/}" | |
# Substring from 0 thru pos of filename | |
dir="${fullpath:0:${#fullpath} - ${#filename}}" | |
# Strip shortest match of . plus at least one non-dot char from end | |
base="${filename%.[^.]*}" | |
# Substring from len of base thru end | |
ext="${filename:${#base} + 1}" | |
# If we have an extension and no base, it's really the base | |
if [[ -z "$base" && -n "$ext" ]]; then | |
base=".$ext" | |
ext="" | |
fi | |
case "$part" in | |
dir) | |
echo "$dir" | |
;; | |
base) | |
echo "$base" | |
;; | |
ext) | |
echo "$ext" | |
;; | |
*) | |
echo -e "$fullpath:\n\tdir = \"$dir\"\n\tbase = \"$base\"\n\text = \"$ext\"" | |
esac | |
} | |
# Get directory from a pathname | |
directory () { | |
echo $(parse_filename $1 dir) | |
} | |
# Get a file's basename (filename without extension), given its full pathname | |
base () { | |
echo $(parse_filename $1 base) | |
} | |
# Get a file's extension from its full pathname | |
extension () { | |
echo $(parse_filename $1 ext) | |
} | |
# Returns a filename with a default extension appended, if there wasn't one | |
with_default_extension () { | |
FILENAME=$1 | |
DEFAULT_EXT=$2 | |
EXT=$(extension $FILENAME) | |
if [ "$EXT" = "" ]; then | |
FILENAME=$FILENAME$DEFAULT_EXT | |
fi | |
echo $FILENAME | |
} | |
# To test, run this with `./utils.sh --test-utils` | |
if [[ "$1" == "--test-utils" ]]; then | |
# Generic test function. Usage: | |
# | |
# test_function <expected> \ | |
# <function> <args...> | |
# | |
# (I put `<function> <args...>` on the second line for readability) | |
test_function () { | |
EXPECTED=$1 | |
FUNCTION=$2 | |
shift; shift | |
RESULT=$($FUNCTION $*) | |
if [ "$RESULT" != "$EXPECTED" ]; then | |
echo "Failed: function $FUNCTION, actual '$RESULT', expected '$EXPECTED'" | |
exit 1 | |
fi | |
} | |
test_function snap \ | |
extension foo.snap | |
test_function "" \ | |
extension .bash_profile | |
test_function "" \ | |
extension no-extension-file | |
test_function foo.snap \ | |
with_default_extension foo.snap | |
test_function .bash_profile.zumbulus \ | |
with_default_extension .bash_profile .zumbulus | |
test_function no-extension-file.zumbulus \ | |
with_default_extension no-extension-file .zumbulus | |
echo 'Passed' | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment