Skip to content

Instantly share code, notes, and snippets.

@tvlooy
Last active September 20, 2024 20:53

Revisions

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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