Skip to content

Commit e6f3246

Browse files
committed
feat: Add integration tests for tmux plugin
1 parent 84b3857 commit e6f3246

File tree

6 files changed

+126
-27
lines changed

6 files changed

+126
-27
lines changed

bin/t

+12-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ fi
3535
if ! tmux info &>/dev/null; then
3636
TMP_SESSION_DIR=$(mktemp -d)
3737
TMP_SESSION_NAME=$(session_name --full-path "$TMP_SESSION_DIR")
38-
tmux new-session -d -s "$TMP_SESSION_NAME" -c "$TMP_SESSION_DIR"
38+
# Allow for custom tmux config (mostly for testing)
39+
if [ -n "$TMUX_CONFIG" ]; then
40+
tmux -f "$TMUX_CONFIG" new-session -d -s "$TMP_SESSION_NAME" -c "$TMP_SESSION_DIR"
41+
else
42+
tmux new-session -d -s "$TMP_SESSION_NAME" -c "$TMP_SESSION_DIR"
43+
fi
44+
3945
fi
4046

4147
# Get or create session
@@ -79,7 +85,11 @@ fi
7985
# https://github.com/tmux/tmux/blob/master/cmd-find.c#L1024C51-L1024C57
8086
SESSION=$(echo "$SESSION" | sed 's/^~$/\\~/')
8187
if [ -z "$TMUX" ]; then
82-
tmux attach -t "$SESSION"
88+
if [ -n "$INTEGRATION_TEST" ]; then
89+
echo "Running integrtion test, so not attaching to tmux session"
90+
else
91+
tmux attach -t "$SESSION"
92+
fi
8393
else
8494
tmux switch-client -t "$SESSION"
8595
fi

scripts/run-tests.sh

+35-24
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,45 @@
44

55
PROJECT_ROOT="$(dirname "$(dirname "$(realpath "$0")")")"
66
IMAGE="tmux-session-wizard:dev"
7+
FILTER=""
78

8-
while getopts "crwh" opt; do
9+
while getopts "crwhui" opt; do
910
case $opt in
10-
c) CONTAINER=true
11-
;;
12-
r) REBUILD=true
13-
;;
14-
w) WATCH=true
15-
;;
16-
h)
17-
echo "Usage: run-tests.sh"
18-
echo "Run tests for the project"
19-
echo " -c Run tests inside a container (image: ${IMAGE})"
20-
echo " -r Rebuild the container image before running tests, set also -c opiton by default"
21-
echo " -w Watch changes in project and then run tests"
22-
echo " -h Display this help message"
23-
exit 0
24-
;;
25-
\?)
26-
echo "Invalid option: -$OPTARG" >&2
27-
exit 1
28-
;;
11+
c)
12+
CONTAINER=true
13+
;;
14+
r)
15+
REBUILD=true
16+
;;
17+
w)
18+
WATCH=true
19+
;;
20+
u)
21+
FILTER="$FILTER --filter-tags unit"
22+
;;
23+
i)
24+
FILTER="$FILTER --filter-tags integration"
25+
;;
26+
h)
27+
echo "Usage: run-tests.sh"
28+
echo "Run tests for the project"
29+
echo " -c Run tests inside a container (image: ${IMAGE})"
30+
echo " -r Rebuild the container image before running tests, set also -c opiton by default"
31+
echo " -w Watch changes in project and then run tests"
32+
echo " -u Run only unit tests"
33+
echo " -i Run only integration tests"
34+
echo " -h Display this help message"
35+
exit 0
36+
;;
37+
\?)
38+
echo "Invalid option: -$OPTARG" >&2
39+
exit 1
40+
;;
2941
esac
3042
done
3143

3244
# Basic command to run tests
33-
CMD=(bats "$PROJECT_ROOT/tests")
45+
CMD=(bats $FILTER --recursive "$PROJECT_ROOT/tests")
3446

3547
# Run tests in watch mode
3648
if [ "$WATCH" = true ]; then
@@ -43,13 +55,12 @@ if [ "$CONTAINER" = true ] || [ "$REBUILD" = true ]; then
4355
IS_IMAGE_EXISTS=$(docker images -q ${IMAGE})
4456
fi
4557

46-
if [ -z "$IS_IMAGE_EXISTS" ] && [ "$CONTAINER" = true ] || [ "$REBUILD" = true ] ; then
58+
if [ -z "$IS_IMAGE_EXISTS" ] && [ "$CONTAINER" = true ] || [ "$REBUILD" = true ]; then
4759
docker build -t ${IMAGE} -f "$PROJECT_ROOT/Dockerfile" "$PROJECT_ROOT"
4860
fi
4961

5062
echo "----------------------------------------------------------------------------"
63+
echo "Filter: $FILTER"
5164
echo "Running tests with command: ${CMD[*]}"
5265
echo "----------------------------------------------------------------------------"
5366
"${CMD[@]}"
54-
55-

session-wizard.tmux

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ default_height=40
1010
tmux_option_session_wizard_width="@session-wizard-width"
1111
default_width=80
1212

13+
set_default_session_wizard_options() {
14+
set_tmux_option "@session-wizard" "T"
15+
set_tmux_option "@session-wizard-height" "40"
16+
set_tmux_option "@session-wizard-width" "80"
17+
}
18+
1319
# Multiple bindings can be set. Default binding is "T".
1420
set_session_wizard_options() {
1521
local key_bindings
@@ -25,6 +31,7 @@ set_session_wizard_options() {
2531
}
2632

2733
function main {
34+
set_default_session_wizard_options
2835
set_session_wizard_options
2936
}
3037
main

src/helpers.sh

+11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ get_tmux_option() {
1515
fi
1616
}
1717

18+
# Prevents overriding user's options
19+
set_tmux_option() {
20+
local option="$1"
21+
local default_value="$2"
22+
local option_value
23+
option_value=$(tmux show-option -gqv "$option")
24+
if [ -z "$option_value" ]; then
25+
tmux set-option -g "$option" "$default_value"
26+
fi
27+
}
28+
1829
session_name() {
1930
if [ "$1" = "--directory" ]; then
2031
shift

tests/helpers.bats

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# bats file_tags=unit
12
setup() {
23
bats_load_library 'bats-support'
34
bats_load_library 'bats-assert'
@@ -48,4 +49,3 @@ unset() {
4849
run session_name --short-path "$TEST_PATH"
4950
assert_output "/mo/-f/-moo-foo-bar-baz"
5051
}
51-

tests/integration/tmux.bats

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# bats file_tags=integration
2+
_stop_tmux() {
3+
run pgrep tmux
4+
if [ "$status" -eq 0 ]; then
5+
tmux kill-server
6+
fi
7+
}
8+
9+
_add_tmux_plugin() {
10+
export _ZO_DATA_DIR=/tmp/zoxite
11+
DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" >/dev/null 2>&1 && pwd)"
12+
echo "run-shell $DIR/../../session-wizard.tmux" >/tmp/tmux.conf
13+
export TMUX_CONFIG=/tmp/tmux.conf
14+
}
15+
16+
setup() {
17+
bats_require_minimum_version 1.5.0
18+
bats_load_library 'bats-support'
19+
bats_load_library 'bats-assert'
20+
21+
DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" >/dev/null 2>&1 && pwd)"
22+
PATH="$DIR/../../bin:$PATH"
23+
24+
if [ -n "$TMUX" ]; then
25+
fail "Plase run these tests outisde of tmux"
26+
fi
27+
export INTEGRATION_TEST=true
28+
_stop_tmux
29+
_add_tmux_plugin
30+
}
31+
32+
assert_tmux_running() {
33+
run pgrep tmux
34+
assert_success
35+
}
36+
37+
teardown() {
38+
_stop_tmux
39+
rm -rf /tmp/zoxite
40+
rm -rf /tmp/tmux.conf
41+
}
42+
43+
@test "Can run 't' with loaded plugin" {
44+
t .
45+
assert_tmux_running
46+
# Checking default key binding for session wizard plugin
47+
option=$(tmux show-option -gqv @session-wizard)
48+
assert_equal "$option" "T"
49+
}
50+
51+
@test "Can overwrite default options" {
52+
echo "set-option -g @session-wizard 't'" >>/tmp/tmux.conf
53+
t .
54+
assert_tmux_running
55+
# Checking default key binding for session wizard plugin
56+
option=$(tmux show-option -gqv @session-wizard-width)
57+
assert_equal "$option" "80"
58+
option=$(tmux show-option -gqv @session-wizard)
59+
assert_equal "$option" "t"
60+
}

0 commit comments

Comments
 (0)