Skip to content

Instantly share code, notes, and snippets.

@gvlx
Forked from tvlooy/unit.sh
Last active October 5, 2018 05:47
Show Gist options
  • Select an option

  • Save gvlx/0adfb1137937ad443df2eeaa73dc0ba7 to your computer and use it in GitHub Desktop.

Select an option

Save gvlx/0adfb1137937ad443df2eeaa73dc0ba7 to your computer and use it in GitHub Desktop.
Bash test: get the directory of a script
#!/bin/bash
function test(){
local __MESSAGE__="$1"
local __RECEIVED__="$2"
local __EXPECTED__="$3"
if [ "${__RECEIVED__}" = "${__EXPECTED__}" ]; then
# shellcheck disable=SC1117
echo -e "\033[32m✔ Tested ${__MESSAGE__}"
else
# shellcheck disable=SC1117
echo -e "\033[31m✘ Tested ${__MESSAGE__}"
echo -e " Received: ${__RECEIVED__}"
echo -e " Expected: ${__EXPECTED__}"
fi
# shellcheck disable=SC1117
echo -en "\033[0m"
}
function testSuite(){
test 'absolute call' "$(bash /tmp/1234/test.sh)" /tmp/1234
test 'via symlinked dir' "$(bash /tmp/current/test.sh)" /tmp/1234
test 'via symlinked file' "$(bash /tmp/test.sh)" /tmp/1234
test 'via multiple symlinked dirs' "$(bash /tmp/current/loop/test.sh)" /tmp/1234
pushd /tmp >/dev/null || exit 1
test 'relative call' "$(bash 1234/test.sh)" /tmp/1234
popd >/dev/null || exit 1
test 'with space in dir' "$(bash /tmp/12\ 34/test.sh)" /tmp/1234
test 'with space in file' "$(bash /tmp/1234/te\ st.sh)" /tmp/1234
echo
}
function teardown(){
# remove symlinks before files
local __DIR__="/tmp/1234";
local __FILE__="test.sh";
local __DIR2__="/tmp/12 34";
local __FILE2__="te st.sh";
[[ -e "${__DIR2__}" ]] && rm -rf "${__DIR2__:?}";
[[ -e "${__DIR__}/${__FILE2__}" ]] && rm -f "${__DIR__:?}/${__FILE2__}";
[[ -e "${__DIR2__}/${__FILE__}" ]] && rm -f "${__DIR2__:?}/${__FILE__}";
[[ -e "${__DIR2__}/${__FILE2__}" ]] && rm -f "${__DIR2__:?}/${__FILE2__}";
[[ -e /tmp/"${__FILE__}" ]] && rm -f "/tmp/${__FILE__}";
[[ -e /tmp/current ]] && rm -rf /tmp/current;
[[ -e /tmp/current/loop ]] && rm -rf /tmp/current/loop;
[[ -e "${__DIR__}" ]] && rm -rf "${__DIR__:?}";
[[ -e "${__DIR__}/${__FILE__}" ]] && rm -f "${__DIR__:?}/${__FILE__}";
}
function setup(){
local __DIR__="/tmp/1234";
local __FILE__="test.sh";
local __DIR2__="/tmp/12 34";
local __FILE2__="te st.sh";
mkdir "${__DIR__}"
touch "${__DIR__}/${__FILE__}"
ln -s "${__DIR__}/${__FILE__}" /tmp
ln -s "${__DIR__}" /tmp/current
ln -s "${__DIR__}" /tmp/current/loop
mkdir "${__DIR2__}"
ln -s "${__DIR__}/${__FILE__}" "${__DIR__}/${__FILE2__}"
ln -s "${__DIR__}/${__FILE__}" "${__DIR2__}/${__FILE__}"
ln -s "${__DIR__}/${__FILE__}" "${__DIR2__}/${__FILE2__}"
}
function test1(){
echo 'Test 1: via dirname'
cat <<- EOF >/tmp/1234/test.sh
echo \`dirname \$0\`
EOF
testSuite
}
function test2(){
echo 'Test 2: via pwd'
cat <<- EOF >/tmp/1234/test.sh
echo \$( cd "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd )
EOF
testSuite
}
function test3(){
echo 'Test 3: overcomplicated stackoverflow solution'
cat <<- EOF >/tmp/1234/test.sh
__SOURCE__="\${BASH_SOURCE[0]}"
while [ -h "\$__SOURCE__" ]; do
__DIR__="\$( cd -P "\$( dirname "\$__SOURCE__" )" && pwd )"
__SOURCE__="\$(readlink "\$__SOURCE__" 2> /dev/null)"
[[ \$__SOURCE__ != /* ]] && __SOURCE__="\$__DIR__/\$__SOURCE__"
done
__DIR__="\$( cd -P "\$( dirname "\$__SOURCE__" )" && pwd )"
echo \$__DIR__
EOF
testSuite
}
function test4(){
echo 'Test 4: via readlink'
cat <<- EOF >/tmp/1234/test.sh
echo \`dirname \$(readlink -f \$0 2> /dev/null)\`
EOF
testSuite
}
function test5(){
echo 'Test 5: via readlink with space'
cat <<- EOF >/tmp/1234/test.sh
echo \`dirname \$(readlink -f "\$0" 2> /dev/null)\`
EOF
testSuite
}
function test6(){
echo 'Test 6: as Test 2 but with cd -P';
cat <<- EOF >/tmp/1234/test.sh
echo \$( cd -P "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd )
EOF
testSuite
}
function test7(){
echo 'Test 7: via cd -P and pwd, testing for symlinked file first';
cat <<- EOF >/tmp/1234/test.sh
__SOURCE__="\${BASH_SOURCE[0]}"
while [[ -h "\${__SOURCE__}" ]]; do
__SOURCE__=\$(find "\${__SOURCE__}" -type l -ls | sed -n 's/^.* -> \(.*\)/\1/p');
done;
echo \$(cd -P "\$( dirname "\${__SOURCE__}" )" && pwd)
EOF
testSuite
}
echo;
teardown; # sanity check (in case last run was interrupted)
setup;
if [ "$1" != "" ]; then
$1;
else
test1
test2
test3
test4
test5
test6
test7
fi
teardown;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment