From b11343b73a13b1f0679ec7c1686c4bd6ed4ee2ee Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sat, 27 Jul 2019 11:42:54 +0000 Subject: [PATCH] Add bats unit testing to repository Will recursively look for `*.bats` files and run them with bats-core test suite. Simple sample unit tests for existing scripts are included in `.config/shell/rc.d/test`. --- .config/nvim/init.vim | 1 + .config/shell/rc.d/env-wiki-root.sh | 2 +- .config/shell/rc.d/test/env-wiki-root.bats | 23 ++++++++++++++++++++++ .config/shell/rc.d/test/locale-utf-8.bats | 15 ++++++++++++++ .gitlab-ci.yml | 10 ++++++++++ 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 .config/shell/rc.d/test/env-wiki-root.bats create mode 100644 .config/shell/rc.d/test/locale-utf-8.bats diff --git a/.config/nvim/init.vim b/.config/nvim/init.vim index 4391256..430d6c5 100644 --- a/.config/nvim/init.vim +++ b/.config/nvim/init.vim @@ -64,6 +64,7 @@ Plug 'stephpy/vim-yaml' Plug 'mhartington/nvim-typescript', {'for': 'typescript','do': './install.sh'} Plug 'fatih/vim-go', { 'do': ':GoUpdateBinaries' } Plug 'deoplete-plugins/deoplete-go', { 'do': 'make'} +Plug 'aliou/bats.vim' " HTML Workflow Plug 'valloric/matchtagalways', { 'on': [] } diff --git a/.config/shell/rc.d/env-wiki-root.sh b/.config/shell/rc.d/env-wiki-root.sh index 25035fe..713a572 100644 --- a/.config/shell/rc.d/env-wiki-root.sh +++ b/.config/shell/rc.d/env-wiki-root.sh @@ -2,4 +2,4 @@ # set wiki root to nextcloud notes folder, # will be picked up by vim wiki plugin as root -export WIKIROOT="$HOME/Nextcloud/Notes/" +export WIKIROOT=${WIKIROOT:-"$HOME/Nextcloud/Notes/"} diff --git a/.config/shell/rc.d/test/env-wiki-root.bats b/.config/shell/rc.d/test/env-wiki-root.bats new file mode 100644 index 0000000..1254e81 --- /dev/null +++ b/.config/shell/rc.d/test/env-wiki-root.bats @@ -0,0 +1,23 @@ +setup() { + testfile="$BATS_TEST_DIRNAME/../env-wiki-root.sh" +} + +teardown() { + export WIKIROOT="" +} + +@test "Populates WIKIROOT environment variable" { + source $testfile + [ -n "$WIKIROOT" ] +} + +@test "Sets WIKIROOT to correct default path" { + source $testfile + [ "$WIKIROOT" = "$HOME/Nextcloud/Notes/" ] +} + +@test "Leaves WIKIROOT as is if it is already set" { + export WIKIROOT="MY/CUSTOM/DIR" + source $testfile + [ "$WIKIROOT" = "MY/CUSTOM/DIR" ] +} diff --git a/.config/shell/rc.d/test/locale-utf-8.bats b/.config/shell/rc.d/test/locale-utf-8.bats new file mode 100644 index 0000000..2188a00 --- /dev/null +++ b/.config/shell/rc.d/test/locale-utf-8.bats @@ -0,0 +1,15 @@ +setup() { + testfile="$BATS_TEST_DIRNAME/../locale-utf-8.sh" +} + +teardown() { + export LC_ALL="" + export LANG="" +} + +@test "Sets LC_ALL and LANG to en_US.utf-8" { + source $testfile + expected="en_US.utf-8" + [ "$LC_ALL" = "en_US.utf-8" ] + [ "$LANG" = "en_US.utf-8" ] +} diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f9b1f23..36dcf8b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -24,3 +24,13 @@ lint: - find . -type f -name '*.sh' | xargs shfmt -d -i 2 - echo "--------- CHECKING ZSH SHELLSCRIPTS -------------" - find . -type f -name '*.zsh' | xargs shfmt -d -i 2 + +test: + stage: test + image: alpine + before_script: + - apk add git bash + - git clone https://github.com/bats-core/bats-core.git /bats + - /bats/bin/bats --version + script: + - /bats/bin/bats -r .