#!/usr/bin/env bash

# This file is a symlink from 'tmux-test' plugin.
# You probably don't want to edit it.

# This script should be run within an isolated enviroment (Vagrant, travis).
# Depending on what the tests do, it might NOT be safe to run this script
# directly on the development machine.

CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

EXIT_VALUE=0 # running a test suite is successful by default

all_test_files() {
	ls -1 "$CURRENT_DIR" |   # test files are in the current dir
		\grep -i "^test" |   # test file names start with "test"
		xargs                # file names in a single line
}

set_exit_val_to_false() {
	EXIT_VALUE=1
}

run_tests() {
	local test_file tests_files
	if [ "$#" -gt 0 ]; then
		test_files="${@//tests\//}" # remove 'tests/' directory prefix
	else
		test_files="$(all_test_files)"
	fi
	for test_file in $test_files; do
		echo "Running test: $test_file"
		"${CURRENT_DIR}/${test_file}"

		# handling exit value
		local test_exit_value="$?"
		if [ "$test_exit_value" -ne 0 ]; then
			set_exit_val_to_false
		fi
	done
}

main() {
	run_tests "$@"
	exit "$EXIT_VALUE"
}
main "$@"
