From 16b2b77c284a3a6852ad3dbb525bc05215ff9730 Mon Sep 17 00:00:00 2001 From: Maciej Sypien Date: Sun, 22 Sep 2024 17:13:24 +0200 Subject: [PATCH] feat: add tests for linux (#39) * feat: add dummy test for linux * feat: add multiple test execution * fix: update exec of multiple scripts for linux * fix: execute tests with code from the branch * fix: fail test exec if at least one test fail * fix: typo * wip: test if symlink work * feat: add new test for check setup of light256 theme * feat: when theme enabled and not set it default to dark256 * feat: increase tests coverage of checking themes activation * refactor: rename tests_linux job * chore: add changelog entry --- .github/workflows/dev-push-check.yml | 34 +++++++++++ .github/workflows/github-actions.yml | 22 ------- CHANGELOG.md | 1 + src/gruvbox-main.sh | 21 ++++++- tests/linux/README.md | 6 ++ .../test_check_gruvbox_dark256_enabled.sh | 57 ++++++++++++++++++ ..._check_gruvbox_dark_transparent_enabled.sh | 57 ++++++++++++++++++ .../test_check_gruvbox_light256_enabled.sh | 57 ++++++++++++++++++ ...check_gruvbox_light_transparent_enabled.sh | 57 ++++++++++++++++++ ...thout_theme_fallback_to_gruvbox_dark256.sh | 59 +++++++++++++++++++ tests/run_all_linux_tests.sh | 32 ++++++++++ tests/tmux_helpers.sh | 58 ++++++++++++++++++ 12 files changed, 436 insertions(+), 25 deletions(-) create mode 100644 .github/workflows/dev-push-check.yml delete mode 100644 .github/workflows/github-actions.yml create mode 100644 tests/linux/README.md create mode 100755 tests/linux/test_check_gruvbox_dark256_enabled.sh create mode 100755 tests/linux/test_check_gruvbox_dark_transparent_enabled.sh create mode 100755 tests/linux/test_check_gruvbox_light256_enabled.sh create mode 100755 tests/linux/test_check_gruvbox_light_transparent_enabled.sh create mode 100755 tests/linux/test_without_theme_fallback_to_gruvbox_dark256.sh create mode 100755 tests/run_all_linux_tests.sh create mode 100644 tests/tmux_helpers.sh diff --git a/.github/workflows/dev-push-check.yml b/.github/workflows/dev-push-check.yml new file mode 100644 index 0000000..f40befc --- /dev/null +++ b/.github/workflows/dev-push-check.yml @@ -0,0 +1,34 @@ +name: dev-push-check +run-name: ${{ github.actor }} pushed new code 💻 +on: [push] #, pull_request] +jobs: + lint_shellcheck: + runs-on: ubuntu-latest + steps: + - name: Install shellcheck + run: sudo apt-get install -y shellcheck + - name: Checkout repo + uses: actions/checkout@main + - name: Lint files against shellcheck + run: make lint_shellcheck + lint_shfmt: + runs-on: ubuntu-latest + steps: + - name: Install shfmt + run: sudo apt-get install -y shfmt + - name: Checkout repository + uses: actions/checkout@main + - name: Lint files against shfmt + run: make lint_shfmt + tests_linux: + runs-on: ubuntu-latest + needs: + - lint_shfmt + - lint_shellcheck + steps: + - name: Install required software + run: sudo apt install -y tmux git + - name: Checkout repository + uses: actions/checkout@main + - name: Execute all linux tests and check results + run: ./tests/run_all_linux_tests.sh diff --git a/.github/workflows/github-actions.yml b/.github/workflows/github-actions.yml deleted file mode 100644 index 6f76a98..0000000 --- a/.github/workflows/github-actions.yml +++ /dev/null @@ -1,22 +0,0 @@ -name: GitHub Actions -run-name: ${{ github.actor }} is testing out GitHub Actions 🚀 -on: [push] -jobs: - lint-shellcheck: - runs-on: ubuntu-latest - steps: - - name: install shellcheck - run: sudo apt-get install -y shellcheck - - name: checkout repo - uses: actions/checkout@main - - name: lint files against shellcheck - run: make lint_shellcheck - lint-shfmt: - runs-on: ubuntu-latest - steps: - - name: install shfmt - run: sudo apt-get install -y shfmt - - name: checkout repo - uses: actions/checkout@main - - name: lint files against shfmt - run: make lint_shfmt diff --git a/CHANGELOG.md b/CHANGELOG.md index f7a6751..c94c9ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Added light and dark transparent themes [#18](https://github.com/egel/tmux-gruvbox/issues/18) - Added editorconfig - Added code linters for shellcheck & shfmt [#33](https://github.com/egel/tmux-gruvbox/issues/33) +- Added tests (for linux) [#39](https://github.com/egel/tmux-gruvbox/issues/39) ### Changed diff --git a/src/gruvbox-main.sh b/src/gruvbox-main.sh index aac1460..0773393 100755 --- a/src/gruvbox-main.sh +++ b/src/gruvbox-main.sh @@ -18,9 +18,24 @@ get_theme() { } main() { - local theme - theme=$(get_theme "$THEME_OPTION" "$DEFAULT_THEME") - tmux source-file "$CURRENT_DIR/tmux-gruvbox-${theme}.conf" + local _theme _path + _theme=$(get_theme "$THEME_OPTION" "$DEFAULT_THEME") + case "$_theme" in + light-transparent) + _theme="light-transparent" + ;; + dark-transparent) + _theme="dark-transparent" + ;; + light | light256) + _theme="light" + ;; + dark | dark256 | *) + _theme="dark" + ;; + esac + + tmux source-file "${CURRENT_DIR}/tmux-gruvbox-${_theme}.conf" } main "$@" diff --git a/tests/linux/README.md b/tests/linux/README.md new file mode 100644 index 0000000..fe1095b --- /dev/null +++ b/tests/linux/README.md @@ -0,0 +1,6 @@ +# Linux tests + +Those tests are meant to run on linux: + +- ubuntu 20.04 LTS +- with bash diff --git a/tests/linux/test_check_gruvbox_dark256_enabled.sh b/tests/linux/test_check_gruvbox_dark256_enabled.sh new file mode 100755 index 0000000..5cf29b1 --- /dev/null +++ b/tests/linux/test_check_gruvbox_dark256_enabled.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +# shellcheck disable=SC1091 +source "${CURRENT_DIR}/../tmux_helpers.sh" + +main() { + helper_tearup_linux + + cat <~/.tmux.conf +# List of plugins +set -g @plugin 'tmux-plugins/tpm' +set -g @plugin 'tmux-plugins/tmux-sensible' + +# Other plugins +set -g @plugin 'egel/tmux-gruvbox' +set -g @tmux-gruvbox 'dark256' + +# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf) +run '~/.tmux/plugins/tpm/tpm' +EOF + + cat ~/.tmux.conf + + # it's essential to link current repo to the plugins' directory + ln -sfv "$CURRENT_DIR/../../../tmux-gruvbox" "${HOME}/.tmux/plugins/tmux-gruvbox" + + helper_install_tpm_plugins + + # start new detached session + tmux new -d + + # check if left side match + _status_left_expected="#[bg=colour241,fg=colour248] #S #[bg=colour237,fg=colour241,nobold,noitalics,nounderscore]" + _status_left_current=$(tmux show-option -gqv status-left) + if [[ "$_status_left_current" != "$_status_left_expected" ]]; then + helper_print_fail "status-left did not match" "$_status_left_current" "$_status_left_expected" + helper_teardown + exit 1 + fi + # check if status is not transparent + _status_style_expected="bg=colour237,fg=colour223" + _status_style_current=$(tmux show-option -gqv status-style) + if [[ "$_status_style_current" != "$_status_style_expected" ]]; then + helper_print_fail "status-style did not match" "$_status_style_current" "$_status_style_expected" + helper_teardown + exit 1 + fi + + helper_print_success "status-left match" + helper_teardown + exit 0 + +} + +main "$@" diff --git a/tests/linux/test_check_gruvbox_dark_transparent_enabled.sh b/tests/linux/test_check_gruvbox_dark_transparent_enabled.sh new file mode 100755 index 0000000..a1008d5 --- /dev/null +++ b/tests/linux/test_check_gruvbox_dark_transparent_enabled.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +# shellcheck disable=SC1091 +source "${CURRENT_DIR}/../tmux_helpers.sh" + +main() { + helper_tearup_linux + + cat <~/.tmux.conf +# List of plugins +set -g @plugin 'tmux-plugins/tpm' +set -g @plugin 'tmux-plugins/tmux-sensible' + +# Other plugins +set -g @plugin 'egel/tmux-gruvbox' +set -g @tmux-gruvbox 'dark-transparent' + +# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf) +run '~/.tmux/plugins/tpm/tpm' +EOF + + cat ~/.tmux.conf + + # it's essential to link current repo to the plugins' directory + ln -sfv "$CURRENT_DIR/../../../tmux-gruvbox" "${HOME}/.tmux/plugins/tmux-gruvbox" + + helper_install_tpm_plugins + + # start new detached session + tmux new -d + + # check if left side match + _status_left_expected="#[bg=colour241,fg=colour248] #S #[bg=colour237,fg=colour241,nobold,noitalics,nounderscore]" + _status_left_current=$(tmux show-option -gqv status-left) + if [[ "$_status_left_current" != "$_status_left_expected" ]]; then + helper_print_fail "status-left did not match" "$_status_left_current" "$_status_left_expected" + helper_teardown + exit 1 + fi + # check if status is not transparent + _status_style_expected="bg=default,fg=colour223" + _status_style_current=$(tmux show-option -gqv status-style) + if [[ "$_status_style_current" != "$_status_style_expected" ]]; then + helper_print_fail "status-style did not match" "$_status_style_current" "$_status_style_expected" + helper_teardown + exit 1 + fi + + helper_print_success "status-left match" + helper_teardown + exit 0 + +} + +main "$@" diff --git a/tests/linux/test_check_gruvbox_light256_enabled.sh b/tests/linux/test_check_gruvbox_light256_enabled.sh new file mode 100755 index 0000000..0d4426d --- /dev/null +++ b/tests/linux/test_check_gruvbox_light256_enabled.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +# shellcheck disable=SC1091 +source "${CURRENT_DIR}/../tmux_helpers.sh" + +main() { + helper_tearup_linux + + cat <~/.tmux.conf +# List of plugins +set -g @plugin 'tmux-plugins/tpm' +set -g @plugin 'tmux-plugins/tmux-sensible' + +# Other plugins +set -g @plugin 'egel/tmux-gruvbox' +set -g @tmux-gruvbox 'light256' + +# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf) +run '~/.tmux/plugins/tpm/tpm' +EOF + + cat ~/.tmux.conf + + # it's essential to link current repo to the plugins' directory + ln -sfv "$CURRENT_DIR/../../../tmux-gruvbox" "${HOME}/.tmux/plugins/tmux-gruvbox" + + helper_install_tpm_plugins + + # start new detached session + tmux new -d + + # check if left side match + _status_left_expected="#[bg=colour243,fg=colour255] #S #[bg=colour252,fg=colour243,nobold,noitalics,nounderscore]" + _status_left_current=$(tmux show-option -gqv status-left) + if [[ "$_status_left_current" != "$_status_left_expected" ]]; then + helper_print_fail "status-left did not match" "$_status_left_current" "$_status_left_expected" + helper_teardown + exit 1 + fi + # check if status is not transparent + _status_style_expected="bg=colour252,fg=colour239" + _status_style_current=$(tmux show-option -gqv status-style) + if [[ "$_status_style_current" != "$_status_style_expected" ]]; then + helper_print_fail "status-style did not match" "$_status_style_current" "$_status_style_expected" + helper_teardown + exit 1 + fi + + helper_print_success "status-left match" + helper_teardown + exit 0 + +} + +main "$@" diff --git a/tests/linux/test_check_gruvbox_light_transparent_enabled.sh b/tests/linux/test_check_gruvbox_light_transparent_enabled.sh new file mode 100755 index 0000000..f177d0a --- /dev/null +++ b/tests/linux/test_check_gruvbox_light_transparent_enabled.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +# shellcheck disable=SC1091 +source "${CURRENT_DIR}/../tmux_helpers.sh" + +main() { + helper_tearup_linux + + cat <~/.tmux.conf +# List of plugins +set -g @plugin 'tmux-plugins/tpm' +set -g @plugin 'tmux-plugins/tmux-sensible' + +# Other plugins +set -g @plugin 'egel/tmux-gruvbox' +set -g @tmux-gruvbox 'light-transparent' + +# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf) +run '~/.tmux/plugins/tpm/tpm' +EOF + + cat ~/.tmux.conf + + # it's essential to link current repo to the plugins' directory + ln -sfv "$CURRENT_DIR/../../../tmux-gruvbox" "${HOME}/.tmux/plugins/tmux-gruvbox" + + helper_install_tpm_plugins + + # start new detached session + tmux new -d + + # check if left side match + _status_left_expected="#[bg=colour243,fg=colour255] #S #[bg=colour252,fg=colour243,nobold,noitalics,nounderscore]" + _status_left_current=$(tmux show-option -gqv status-left) + if [[ "$_status_left_current" != "$_status_left_expected" ]]; then + helper_print_fail "status-left did not match" "$_status_left_current" "$_status_left_expected" + helper_teardown + exit 1 + fi + # check if status is not transparent + _status_style_expected="bg=default,fg=colour239" + _status_style_current=$(tmux show-option -gqv status-style) + if [[ "$_status_style_current" != "$_status_style_expected" ]]; then + helper_print_fail "status-style did not match" "$_status_style_current" "$_status_style_expected" + helper_teardown + exit 1 + fi + + helper_print_success "status-left match" + helper_teardown + exit 0 + +} + +main "$@" diff --git a/tests/linux/test_without_theme_fallback_to_gruvbox_dark256.sh b/tests/linux/test_without_theme_fallback_to_gruvbox_dark256.sh new file mode 100755 index 0000000..0022de5 --- /dev/null +++ b/tests/linux/test_without_theme_fallback_to_gruvbox_dark256.sh @@ -0,0 +1,59 @@ +#!/bin/bash + +CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +# shellcheck disable=SC1091 +source "${CURRENT_DIR}/../tmux_helpers.sh" + +#### +# Test: when the theme is not defined in config it should fallback to 'dark256' +# +main() { + helper_tearup_linux + + # set config without theme defined + cat <~/.tmux.conf +# List of plugins +set -g @plugin 'tmux-plugins/tpm' +set -g @plugin 'tmux-plugins/tmux-sensible' + +# Other plugins +set -g @plugin 'egel/tmux-gruvbox' + +# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf) +run '~/.tmux/plugins/tpm/tpm' +EOF + + cat ~/.tmux.conf + + # it's essential to link current repo to the plugins' directory + ln -sfv "$CURRENT_DIR/../../../tmux-gruvbox" "${HOME}/.tmux/plugins/tmux-gruvbox" + + helper_install_tpm_plugins + + # start new detached session + tmux new -d + + # check if left side match + _status_left_expected="#[bg=colour241,fg=colour248] #S #[bg=colour237,fg=colour241,nobold,noitalics,nounderscore]" + _status_left_current=$(tmux show-option -gqv status-left) + if [[ "$_status_left_current" != "$_status_left_expected" ]]; then + helper_print_fail "status-left did not match" "$_status_left_current" "$_status_left_expected" + helper_teardown + exit 1 + fi + # check if status is not transparent + _status_style_expected="bg=colour237,fg=colour223" + _status_style_current=$(tmux show-option -gqv status-style) + if [[ "$_status_style_current" != "$_status_style_expected" ]]; then + helper_print_fail "status-style did not match" "$_status_style_current" "$_status_style_expected" + helper_teardown + exit 1 + fi + + helper_print_success "status-left match" + helper_teardown + exit 0 +} + +main "$@" diff --git a/tests/run_all_linux_tests.sh b/tests/run_all_linux_tests.sh new file mode 100755 index 0000000..77cde66 --- /dev/null +++ b/tests/run_all_linux_tests.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +main() { + set -e # exit on error + declare -i _countFailures + local _files + _countFailures=0 + _files=$(find "$CURRENT_DIR/linux" -name "test_*" -type f) + for test in $_files; do + printf "\n" + printf "\n==============================================" + printf "\n %s" "$test" + printf "\n==============================================" + printf "\n" + bash -c "$test" + + # run all and count failures + retVal=$? + if [ $retVal -eq 1 ]; then + _countFailures+=1 + fi + done + + # check if anything failed and fail + if [ "$_countFailures" -gt 0 ]; then + exit 1 + fi +} + +main "$@" diff --git a/tests/tmux_helpers.sh b/tests/tmux_helpers.sh new file mode 100644 index 0000000..c07f217 --- /dev/null +++ b/tests/tmux_helpers.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +helper_teardown() { + echo "TEARDOWN" + rm -rf ~/.tmux.conf + rm -rf ~/.tmux/ + tmux kill-server >/dev/null 2>&1 +} + +helper_tearup_linux() { + if [[ "$(uname)" != "Linux" ]]; then + echo "NOT LINUX. Failed & exit." + exit 1 + fi + echo "TEARUP LINUX" + + # install software + sudo apt update -y + sudo apt install -y tmux git + + # download TPM + mkdir -p ~/.tmux/plugins/ + git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm +} + +helper_print_fail() { + local _msg="${1}" + local _current_val="${2}" + local _expected_val="${3}" + printf "FAIL. %s\n" "${_msg}" + printf "current value:\t%s\n" "$_current_val" + printf "expected value:\t%s\n" "$_expected_val" +} + +helper_print_success() { + local _msg="${1:-}" + printf "SUCCESS. %s\n" "${_msg}" +} + +helper_print_fail_and_exit() { + helper_print_fail "$1" "$2" "$3" + exit 1 +} + +helper_print_success_and_exit() { + helper_print_success "$1" "$2" "$3" + exit 0 +} + +# install TMP plugins with command +helper_install_tpm_plugins() { + bash -c "${HOME}/.tmux/plugins/tpm/scripts/install_plugins.sh install_plugins" +} + +helper_get_project_root_dir() { + _current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + echo "${_current_dir}/../" +}