Skip to content

Instantly share code, notes, and snippets.

@CodeShane
Forked from ptc-mrucci/unit.sh
Created June 24, 2021 21:26
Show Gist options
  • Save CodeShane/24e43c15a6f334efdf593a05db98bbcd to your computer and use it in GitHub Desktop.
Save CodeShane/24e43c15a6f334efdf593a05db98bbcd to your computer and use it in GitHub Desktop.

Revisions

  1. @ptc-mrucci ptc-mrucci revised this gist Jun 20, 2021. 1 changed file with 77 additions and 31 deletions.
    108 changes: 77 additions & 31 deletions unit.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    #!/bin/bash

    tmp=$(python -c "import os, sys; print(os.path.realpath('/tmp'))")

    function test {
    MESSAGE=$1
    RECEIVED=$2
    @@ -16,27 +18,27 @@ function test {
    }

    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
    test 'relative call' `bash 1234/test.sh` /tmp/1234
    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
    test 'relative call' `bash 1234/test.sh` $tmp/1234
    popd >/dev/null
    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
    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 setup {
    DIR=/tmp/1234
    DIR=$tmp/1234
    FILE=test.sh
    if [ -e $DIR ]; then rm -rf $DIR; fi; mkdir $DIR
    if [ -f $DIR/$FILE ]; then rm -rf $DIR/$FILE; fi; touch $DIR/$FILE
    if [ -f /tmp/$FILE ]; then rm /tmp/$FILE; fi; ln -s $DIR/$FILE /tmp
    if [ -f /tmp/current ]; then rm /tmp/current; fi; ln -s $DIR /tmp/current
    if [ -f /tmp/current/loop ]; then rm /tmp/current/loop; fi; ln -s $DIR /tmp/current/loop
    DIR2="/tmp/12 34"
    if [ -f $tmp/$FILE ]; then rm $tmp/$FILE; fi; ln -s $DIR/$FILE $tmp
    if [ -f $tmp/current ]; then rm $tmp/current; fi; ln -s $DIR $tmp/current
    if [ -f $tmp/current/loop ]; then rm $tmp/current/loop; fi; ln -s $DIR $tmp/current/loop
    DIR2="$tmp/12 34"
    FILE2="te st.sh"
    if [ -e "$DIR2" ]; then rm -rf "$DIR2"; fi; mkdir "$DIR2"
    if [ -f "$DIR/$FILE2" ]; then rm -rf "$DIR/$FILE2"; fi; ln -s $DIR/$FILE "$DIR/$FILE2"
    @@ -46,51 +48,91 @@ function setup {

    function test1 {
    echo 'Test 1: via dirname'
    cat <<- EOF >/tmp/1234/test.sh
    echo \`dirname \$0\`
    cat <<- 'EOF' >$tmp/1234/test.sh
    echo `dirname $0`
    EOF
    testSuite
    }

    function test2 {
    echo 'Test 2: via pwd'
    cat <<- EOF >/tmp/1234/test.sh
    CACHE_DIR=\$( cd "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd )
    echo \$CACHE_DIR
    cat <<- 'EOF' >$tmp/1234/test.sh
    CACHE_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
    echo $CACHE_DIR
    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")"
    [[ \$SOURCE != /* ]] && SOURCE="\$DIR/\$SOURCE"
    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
    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)\`
    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")\`
    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
    CACHE_DIR=$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
    echo $CACHE_DIR
    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
    # shellcheck disable=SC1117
    __SOURCE__=$(find "${__SOURCE__}" -type l -ls | sed -n 's/^.* -> \(.*\)/\1/p');
    done;
    echo $(cd -P "$( dirname "${__SOURCE__}" )" && pwd)
    EOF
    testSuite
    }

    function test8 {
    echo 'Test 8: ${0%/*}'
    cat <<- 'EOF' >$tmp/1234/test.sh
    echo "${0%/*}"
    EOF
    testSuite
    }

    function test9 {
    echo 'Test 9: python os.path functions'
    cat <<- 'EOF' >$tmp/1234/test.sh
    echo $(python -c "import os, sys; print(os.path.dirname(os.path.realpath('${BASH_SOURCE[0]}')))")
    EOF
    testSuite
    }

    echo
    setup
    if [ "$1" != "" ]; then
    @@ -101,4 +143,8 @@ else
    test3
    test4
    test5
    fi
    test6
    test7
    test8
    test9
    fi
  2. @tvlooy tvlooy revised this gist Oct 29, 2015. 1 changed file with 25 additions and 5 deletions.
    30 changes: 25 additions & 5 deletions unit.sh
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ function test {
    RECEIVED=$2
    EXPECTED=$3

    if [ $RECEIVED = $EXPECTED ]; then
    if [ "$RECEIVED" = "$EXPECTED" ]; then
    echo -e "\033[32m✔︎ Tested $MESSAGE"
    else
    echo -e "\033[31m✘ Tested $MESSAGE"
    @@ -23,6 +23,8 @@ function testSuite {
    pushd /tmp >/dev/null
    test 'relative call' `bash 1234/test.sh` /tmp/1234
    popd >/dev/null
    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
    }

    @@ -34,6 +36,12 @@ function setup {
    if [ -f /tmp/$FILE ]; then rm /tmp/$FILE; fi; ln -s $DIR/$FILE /tmp
    if [ -f /tmp/current ]; then rm /tmp/current; fi; ln -s $DIR /tmp/current
    if [ -f /tmp/current/loop ]; then rm /tmp/current/loop; fi; ln -s $DIR /tmp/current/loop
    DIR2="/tmp/12 34"
    FILE2="te st.sh"
    if [ -e "$DIR2" ]; then rm -rf "$DIR2"; fi; mkdir "$DIR2"
    if [ -f "$DIR/$FILE2" ]; then rm -rf "$DIR/$FILE2"; fi; ln -s $DIR/$FILE "$DIR/$FILE2"
    if [ -f "$DIR2/$FILE" ]; then rm -rf "$DIR2/$FILE"; fi; ln -s $DIR/$FILE "$DIR2/$FILE"
    if [ -f "$DIR2/$FILE2" ]; then rm -rf "$DIR2/$FILE2"; fi; ln -s $DIR/$FILE "$DIR2/$FILE2"
    }

    function test1 {
    @@ -76,9 +84,21 @@ function test4 {
    testSuite
    }

    function test5 {
    echo 'Test 5: via readlink with space'
    cat <<- EOF >/tmp/1234/test.sh
    echo \`dirname \$(readlink -f "\$0")\`
    EOF
    testSuite
    }
    echo
    setup
    test1
    test2
    test3
    test4
    if [ "$1" != "" ]; then
    $1
    else
    test1
    test2
    test3
    test4
    test5
    fi
  3. @tvlooy tvlooy revised this gist Jun 9, 2015. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions unit.sh
    Original file line number Diff line number Diff line change
    @@ -23,6 +23,7 @@ function testSuite {
    pushd /tmp >/dev/null
    test 'relative call' `bash 1234/test.sh` /tmp/1234
    popd >/dev/null
    echo
    }

    function setup {
    @@ -36,15 +37,15 @@ function setup {
    }

    function test1 {
    echo -e "\nTest 1: via dirname"
    echo 'Test 1: via dirname'
    cat <<- EOF >/tmp/1234/test.sh
    echo \`dirname \$0\`
    EOF
    testSuite
    }

    function test2 {
    echo -e "\nTest 2: via pwd"
    echo 'Test 2: via pwd'
    cat <<- EOF >/tmp/1234/test.sh
    CACHE_DIR=\$( cd "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd )
    echo \$CACHE_DIR
    @@ -53,7 +54,7 @@ function test2 {
    }

    function test3 {
    echo -e "\nTest 3: overcomplicated stackoverflow solution"
    echo 'Test 3: overcomplicated stackoverflow solution'
    cat <<- EOF >/tmp/1234/test.sh
    SOURCE="\${BASH_SOURCE[0]}"
    while [ -h "\$SOURCE" ]; do
    @@ -68,13 +69,14 @@ function test3 {
    }

    function test4 {
    echo -e "\nTest 4: via readlink"
    echo 'Test 4: via readlink'
    cat <<- EOF >/tmp/1234/test.sh
    echo \`dirname \$(readlink -f \$0)\`
    EOF
    testSuite
    }

    echo
    setup
    test1
    test2
  4. @tvlooy tvlooy revised this gist Jun 9, 2015. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions unit.sh
    Original file line number Diff line number Diff line change
    @@ -36,15 +36,15 @@ function setup {
    }

    function test1 {
    echo 'Test 1: via dirname'
    echo -e "\nTest 1: via dirname"
    cat <<- EOF >/tmp/1234/test.sh
    echo \`dirname \$0\`
    EOF
    testSuite
    }

    function test2 {
    echo 'Test 2: via pwd'
    echo -e "\nTest 2: via pwd"
    cat <<- EOF >/tmp/1234/test.sh
    CACHE_DIR=\$( cd "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd )
    echo \$CACHE_DIR
    @@ -53,7 +53,7 @@ function test2 {
    }

    function test3 {
    echo "Test 3: overcomplicated stackoverflow solution"
    echo -e "\nTest 3: overcomplicated stackoverflow solution"
    cat <<- EOF >/tmp/1234/test.sh
    SOURCE="\${BASH_SOURCE[0]}"
    while [ -h "\$SOURCE" ]; do
    @@ -68,7 +68,7 @@ function test3 {
    }

    function test4 {
    echo 'Test 4: via readlink'
    echo -e "\nTest 4: via readlink"
    cat <<- EOF >/tmp/1234/test.sh
    echo \`dirname \$(readlink -f \$0)\`
    EOF
  5. @tvlooy tvlooy revised this gist Jun 9, 2015. 1 changed file with 20 additions and 2 deletions.
    22 changes: 20 additions & 2 deletions unit.sh
    Original file line number Diff line number Diff line change
    @@ -19,6 +19,7 @@ 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
    test 'relative call' `bash 1234/test.sh` /tmp/1234
    popd >/dev/null
    @@ -31,6 +32,7 @@ function setup {
    if [ -f $DIR/$FILE ]; then rm -rf $DIR/$FILE; fi; touch $DIR/$FILE
    if [ -f /tmp/$FILE ]; then rm /tmp/$FILE; fi; ln -s $DIR/$FILE /tmp
    if [ -f /tmp/current ]; then rm /tmp/current; fi; ln -s $DIR /tmp/current
    if [ -f /tmp/current/loop ]; then rm /tmp/current/loop; fi; ln -s $DIR /tmp/current/loop
    }

    function test1 {
    @@ -42,7 +44,7 @@ function test1 {
    }

    function test2 {
    echo 'Test 2: via pwd'
    echo 'Test 2: via pwd'
    cat <<- EOF >/tmp/1234/test.sh
    CACHE_DIR=\$( cd "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd )
    echo \$CACHE_DIR
    @@ -51,7 +53,22 @@ echo 'Test 2: via pwd'
    }

    function test3 {
    echo 'Test 3: via readlink'
    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")"
    [[ \$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)\`
    EOF
    @@ -62,3 +79,4 @@ setup
    test1
    test2
    test3
    test4
  6. @tvlooy tvlooy revised this gist Jun 9, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion unit.sh
    Original file line number Diff line number Diff line change
    @@ -51,7 +51,7 @@ echo 'Test 2: via pwd'
    }

    function test3 {
    echo 'Test 2: via readlink'
    echo 'Test 3: via readlink'
    cat <<- EOF >/tmp/1234/test.sh
    echo \`dirname \$(readlink -f \$0)\`
    EOF
  7. @tvlooy tvlooy revised this gist Jun 9, 2015. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion unit.sh
    Original file line number Diff line number Diff line change
    @@ -42,6 +42,15 @@ function test1 {
    }

    function test2 {
    echo 'Test 2: via pwd'
    cat <<- EOF >/tmp/1234/test.sh
    CACHE_DIR=\$( cd "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd )
    echo \$CACHE_DIR
    EOF
    testSuite
    }

    function test3 {
    echo 'Test 2: via readlink'
    cat <<- EOF >/tmp/1234/test.sh
    echo \`dirname \$(readlink -f \$0)\`
    @@ -51,4 +60,5 @@ function test2 {

    setup
    test1
    test2
    test2
    test3
  8. @tvlooy tvlooy created this gist Jun 9, 2015.
    54 changes: 54 additions & 0 deletions unit.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    #!/bin/bash

    function test {
    MESSAGE=$1
    RECEIVED=$2
    EXPECTED=$3

    if [ $RECEIVED = $EXPECTED ]; then
    echo -e "\033[32m✔︎ Tested $MESSAGE"
    else
    echo -e "\033[31m✘ Tested $MESSAGE"
    echo -e " Received: $RECEIVED"
    echo -e " Expected: $EXPECTED"
    fi
    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
    pushd /tmp >/dev/null
    test 'relative call' `bash 1234/test.sh` /tmp/1234
    popd >/dev/null
    }

    function setup {
    DIR=/tmp/1234
    FILE=test.sh
    if [ -e $DIR ]; then rm -rf $DIR; fi; mkdir $DIR
    if [ -f $DIR/$FILE ]; then rm -rf $DIR/$FILE; fi; touch $DIR/$FILE
    if [ -f /tmp/$FILE ]; then rm /tmp/$FILE; fi; ln -s $DIR/$FILE /tmp
    if [ -f /tmp/current ]; then rm /tmp/current; fi; ln -s $DIR /tmp/current
    }

    function test1 {
    echo 'Test 1: via dirname'
    cat <<- EOF >/tmp/1234/test.sh
    echo \`dirname \$0\`
    EOF
    testSuite
    }

    function test2 {
    echo 'Test 2: via readlink'
    cat <<- EOF >/tmp/1234/test.sh
    echo \`dirname \$(readlink -f \$0)\`
    EOF
    testSuite
    }

    setup
    test1
    test2