diff --git a/tests/helpers.sh b/tests/helpers.sh index 9dc4139..f1de6d2 100644 --- a/tests/helpers.sh +++ b/tests/helpers.sh @@ -1,21 +1,50 @@ -#!/usr/bin/env bash +#!/bin/sh FAIL="false" +mkdir_p() +{ #portable mkdir -p function + local dir subdir + for dir; do + mkdir_p__IFS="$IFS" + IFS="/" + set -- $dir + IFS="$mkdir_p__IFS" + ( + case "$dir" in + /*) cd /; shift ;; + esac + for subdir; do + [ -z "${subdir}" ] && continue + if [ -d "${subdir}" ] || mkdir "${subdir}"; then + if cd "${subdir}"; then + : + else + printf "%s\\n" "mkdir_p: Can't enter ${subdir} while creating ${dir}" + exit 1 + fi + else + exit 1 + fi + done + ) + done +} + set_tmux_conf_helper() { > ~/.tmux.conf # empty filename while read -r line; do - echo $line >> ~/.tmux.conf + printf "%s\\n" "${line}" >> ~/.tmux.conf done } create_test_plugin_helper() { local plugin_path="$HOME/.tmux/plugins/tmux_test_plugin/" - rm -rf $plugin_path - mkdir -p $plugin_path + rm -rf "$plugin_path" + mkdir_p "$plugin_path" while read -r line; do - echo $line >> "$plugin_path/test_plugin.tmux" + printf "%s\\n" "$line" >> "$plugin_path/test_plugin.tmux" done chmod +x "$plugin_path/test_plugin.tmux" } @@ -37,19 +66,19 @@ check_dir_exists_helper() { fail_helper() { local message="$1" - echo "$message" >&2 + printf "%s\\n" "$message" >&2 FAIL="true" } exit_value_helper() { local fail="$1" - if [ "$FAIL" == "true" ]; then - echo "FAIL!" - echo + if [ "$FAIL" = "true" ]; then + printf "%s\\n" "FAIL!" + printf "\\n" exit 1 else - echo "SUCCESS" - echo + printf "%s\\n" "SUCCESS" + printf "\\n" exit 0 fi } diff --git a/tests/run-tests-within-vm b/tests/run-tests-within-vm index 224a07c..5b7a9a1 100755 --- a/tests/run-tests-within-vm +++ b/tests/run-tests-within-vm @@ -1,34 +1,47 @@ -#!/usr/bin/env bash +#!/bin/sh # A test runner file. # Intended to be run from `./run-tests` script *within* a virtual machine. # DO NOT run it locally as it might overwrite your `.tmux.conf` (that's what it # does during the tests). -CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +_dirname() +{ #portable dirname + [ -z "${1}" ] && return 1 + + #http://www.linuxselfhelp.com/gnu/autoconf/html_chapter/autoconf_10.html + case "${1}" in + /*|*/*) local dir; dir=$(expr "x${1}" : 'x\(.*\)/[^/]*' \| '.' : '.') + printf "%s\\n" "${dir}" ;; + *) printf "%s\\n" ".";; + esac +} + +CURRENT_DIR="$( cd "$( _dirname "$0" )" && pwd )" # running test suite is successful by default -tests_exit_value=0 +tests_exit_value="0" set_global_exit_val_to_false() { - tests_exit_value=1 + tests_exit_value="1" } test_files() { - ls -1 "$CURRENT_DIR" | # test files are in the current dir + ls -1 "$CURRENT_DIR" | # test files are in the current dir grep -i '^test' | # test file names start with 'test' xargs # file names in one line } run_tests() { local test_file - for test_file in $(test_files); do - echo "Running test: $test_file" - $CURRENT_DIR/$test_file + for test_file in "$CURRENT_DIR"/test*; do + [ -f "${test_file}" ] || continue + printf "%s\\n" "Running test: ${test_file##*/}" + "$test_file" # handling exit value local exit_value="$?" - if [ "$exit_value" != 0 ]; then + if [ "$exit_value" != "0" ]; then set_global_exit_val_to_false fi done diff --git a/tests/test_plugin_installation.sh b/tests/test_plugin_installation.sh index d013972..3a5162f 100755 --- a/tests/test_plugin_installation.sh +++ b/tests/test_plugin_installation.sh @@ -1,8 +1,20 @@ -#!/usr/bin/env bash +#!/bin/sh -CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +_dirname() +{ #portable dirname + [ -z "${1}" ] && return 1 -source $CURRENT_DIR/helpers.sh + #http://www.linuxselfhelp.com/gnu/autoconf/html_chapter/autoconf_10.html + case "${1}" in + /*|*/*) local dir; dir=$(expr "x${1}" : 'x\(.*\)/[^/]*' \| '.' : '.') + printf "%s\\n" "${dir}" ;; + *) printf "%s\\n" ".";; + esac +} + +CURRENT_DIR="$( cd "$( _dirname "$0" )" && pwd )" + +. "$CURRENT_DIR"/helpers.sh test_plugin_installation() { set_tmux_conf_helper <<- HERE diff --git a/tests/test_plugin_sourcing.sh b/tests/test_plugin_sourcing.sh index 9cab80f..c835a72 100755 --- a/tests/test_plugin_sourcing.sh +++ b/tests/test_plugin_sourcing.sh @@ -1,8 +1,20 @@ -#!/usr/bin/env bash +#!/bin/sh -CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +_dirname() +{ #portable dirname + [ -z "${1}" ] && return 1 -source $CURRENT_DIR/helpers.sh + #http://www.linuxselfhelp.com/gnu/autoconf/html_chapter/autoconf_10.html + case "${1}" in + /*|*/*) local dir; dir=$(expr "x${1}" : 'x\(.*\)/[^/]*' \| '.' : '.') + printf "%s\\n" "${dir}" ;; + *) printf "%s\\n" ".";; + esac +} + +CURRENT_DIR="$( cd "$( _dirname "$0" )" && pwd )" + +. "$CURRENT_DIR"/helpers.sh check_binding_defined() { local binding="$1" diff --git a/tests/test_plugin_update.sh b/tests/test_plugin_update.sh index 8e135f9..6530f40 100755 --- a/tests/test_plugin_update.sh +++ b/tests/test_plugin_update.sh @@ -1,11 +1,23 @@ -#!/usr/bin/env bash +#!/bin/sh -CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +_dirname() +{ #portable dirname + [ -z "${1}" ] && return 1 -source $CURRENT_DIR/helpers.sh + #http://www.linuxselfhelp.com/gnu/autoconf/html_chapter/autoconf_10.html + case "${1}" in + /*|*/*) local dir; dir=$(expr "x${1}" : 'x\(.*\)/[^/]*' \| '.' : '.') + printf "%s\\n" "${dir}" ;; + *) printf "%s\\n" ".";; + esac +} + +CURRENT_DIR="$( cd "$( _dirname "$0" )" && pwd )" + +. "$CURRENT_DIR"/helpers.sh manually_install_the_plugin() { - mkdir -p ~/.tmux/plugins/ + mkdir_p ~/.tmux/plugins/ cd ~/.tmux/plugins/ git clone --quiet https://github.com/tmux-plugins/tmux-example-plugin } @@ -19,10 +31,10 @@ test_plugin_installation() { manually_install_the_plugin # opens tmux and test it with `expect` - $CURRENT_DIR/expect_successful_update_of_all_plugins || + "$CURRENT_DIR"/expect_successful_update_of_all_plugins || fail_helper "Tmux 'update all plugins' fails" - $CURRENT_DIR/expect_successful_update_of_a_single_plugin || + "$CURRENT_DIR"/expect_successful_update_of_a_single_plugin || fail_helper "Tmux 'update single plugin' fails" teardown_helper