diff --git a/.github/workflows/test-with-bats.yml b/.github/workflows/test-with-bats.yml new file mode 100644 index 0000000..c0f38e0 --- /dev/null +++ b/.github/workflows/test-with-bats.yml @@ -0,0 +1,19 @@ +name: Test With Bats + +on: workflow_call + +jobs: + bats-test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - run: sudo apt install bats + - run: ./scripts/bats-test-setup.sh + - run: ./scripts/scripts/bats-test-run.sh + - uses: actions/upload-artifact@v4 + with: + name: bats-test-resutls + path: | + ./bats-test-* diff --git a/.github/workflows/workflow-build-with-quality-checks.yml b/.github/workflows/workflow-build-with-quality-checks.yml index 6d58f65..fe19c2f 100644 --- a/.github/workflows/workflow-build-with-quality-checks.yml +++ b/.github/workflows/workflow-build-with-quality-checks.yml @@ -7,3 +7,5 @@ on: jobs: test: uses: ./.github/workflows/build-with-shellcheck.yml + bats-test: + uses: ./.github/workflows/test-with-bats.yml diff --git a/.github/workflows/workflow-deployments.yml b/.github/workflows/workflow-deployments.yml index 78a73fe..d547fc4 100644 --- a/.github/workflows/workflow-deployments.yml +++ b/.github/workflows/workflow-deployments.yml @@ -6,13 +6,15 @@ on: jobs: build: uses: ./.github/workflows/build-with-shellcheck.yml + bats-test: + uses: ./.github/workflows/test-with-bats.yml release-assets: - needs: build + needs: [build, bats-test] uses: ./.github/workflows/deploy-github-release-assets.yml secrets: TOKEN_TO_UPLOAD_ASSETS: ${{ secrets.TOKEN_TO_UPLOAD_ASSETS }} siakhooi-apt: - needs: build + needs: [build, bats-test] uses: ./.github/workflows/publish-to-siakhooi-apt.yml secrets: PUBLISH_TO_APT_GITHUB_TOKEN: ${{ secrets.PUBLISH_TO_APT_GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index eb2c38a..1aec08f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ target *.deb *.deb.sha256sum *.deb.sha512sum +test_helper +.gitmodules +bats-test-* \ No newline at end of file diff --git a/scripts/bats-test-run.sh b/scripts/bats-test-run.sh new file mode 100755 index 0000000..cabcaec --- /dev/null +++ b/scripts/bats-test-run.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +BATS_HELPER=test_helper +export BATS_HELPER + +CUSTOM_LOG=bats-test-custom.log +export CUSTOM_LOG + +SOURCE_BIN=$(pwd)/src/bin +export SOURCE_BIN + +bats -r tests | tee bats-test-result.log diff --git a/scripts/bats-test-setup.sh b/scripts/bats-test-setup.sh new file mode 100755 index 0000000..a1bd65d --- /dev/null +++ b/scripts/bats-test-setup.sh @@ -0,0 +1,8 @@ +#!/bin/sh +set -e + +sudo apt update -y +sudo apt install bats -y + +git submodule add https://github.com/bats-core/bats-support.git test_helper/bats-support +git submodule add https://github.com/bats-core/bats-assert.git test_helper/bats-assert diff --git a/tests/cat/cat.bats b/tests/cat/cat.bats new file mode 100644 index 0000000..389ecdb --- /dev/null +++ b/tests/cat/cat.bats @@ -0,0 +1,38 @@ +setup() { + load '../common-setup' + common_setup + + testDatafile=$BATS_TEST_DIRNAME/cat_test_data + outputExpected=$BATS_TEST_FILENAME.$BATS_TEST_DESCRIPTION.expected + outputActual="$TEST_TEMP_DIR/$(basename "$BATS_TEST_FILENAME").$BATS_TEST_DESCRIPTION.actual" + +} + +cat_test(){ + command=$1 + + run bash -c "cat $testDatafile | $command | tee $outputActual" + + assert_output - < $outputExpected +} +@test "cat.blue" { + cat_test cat.blue +} +@test "cat.cyan" { + cat_test cat.cyan +} +@test "cat.green" { + cat_test cat.green +} +@test "cat.grey" { + cat_test cat.grey +} +@test "cat.magenta" { + cat_test cat.magenta +} +@test "cat.red" { + cat_test cat.red +} +@test "cat.yellow" { + cat_test cat.yellow +} diff --git a/tests/cat/cat.bats.cat.blue.expected b/tests/cat/cat.bats.cat.blue.expected new file mode 100644 index 0000000..177cdf4 --- /dev/null +++ b/tests/cat/cat.bats.cat.blue.expected @@ -0,0 +1,2 @@ +Hello World From cat.*! + \ No newline at end of file diff --git a/tests/cat/cat.bats.cat.cyan.expected b/tests/cat/cat.bats.cat.cyan.expected new file mode 100644 index 0000000..74c2d6a --- /dev/null +++ b/tests/cat/cat.bats.cat.cyan.expected @@ -0,0 +1,2 @@ +Hello World From cat.*! + \ No newline at end of file diff --git a/tests/cat/cat.bats.cat.green.expected b/tests/cat/cat.bats.cat.green.expected new file mode 100644 index 0000000..0885c68 --- /dev/null +++ b/tests/cat/cat.bats.cat.green.expected @@ -0,0 +1,2 @@ +Hello World From cat.*! + \ No newline at end of file diff --git a/tests/cat/cat.bats.cat.grey.expected b/tests/cat/cat.bats.cat.grey.expected new file mode 100644 index 0000000..3d33d7b --- /dev/null +++ b/tests/cat/cat.bats.cat.grey.expected @@ -0,0 +1,2 @@ +Hello World From cat.*! + \ No newline at end of file diff --git a/tests/cat/cat.bats.cat.magenta.expected b/tests/cat/cat.bats.cat.magenta.expected new file mode 100644 index 0000000..77aba07 --- /dev/null +++ b/tests/cat/cat.bats.cat.magenta.expected @@ -0,0 +1,2 @@ +Hello World From cat.*! + \ No newline at end of file diff --git a/tests/cat/cat.bats.cat.red.expected b/tests/cat/cat.bats.cat.red.expected new file mode 100644 index 0000000..63cee22 --- /dev/null +++ b/tests/cat/cat.bats.cat.red.expected @@ -0,0 +1,2 @@ +Hello World From cat.*! + \ No newline at end of file diff --git a/tests/cat/cat.bats.cat.yellow.expected b/tests/cat/cat.bats.cat.yellow.expected new file mode 100644 index 0000000..5f409ca --- /dev/null +++ b/tests/cat/cat.bats.cat.yellow.expected @@ -0,0 +1,2 @@ +Hello World From cat.*! + \ No newline at end of file diff --git a/tests/cat/cat_test_data b/tests/cat/cat_test_data new file mode 100644 index 0000000..eec9d3d --- /dev/null +++ b/tests/cat/cat_test_data @@ -0,0 +1 @@ +Hello World From cat.*! diff --git a/tests/clear-colors/cat_test_data b/tests/clear-colors/cat_test_data new file mode 100644 index 0000000..496a43f --- /dev/null +++ b/tests/clear-colors/cat_test_data @@ -0,0 +1,3 @@ +Hello World From cat.*! (Line 1) +Hello World From cat.*! (Line 2) +Hello World From cat.*! (Line 3) diff --git a/tests/clear-colors/clear-colors-echo.bats b/tests/clear-colors/clear-colors-echo.bats new file mode 100644 index 0000000..df10a9a --- /dev/null +++ b/tests/clear-colors/clear-colors-echo.bats @@ -0,0 +1,37 @@ +setup() { + load '../common-setup' + common_setup + + outputExpected=$BATS_TEST_FILENAME.expected + outputActual="$TEST_TEMP_DIR/$(basename "$BATS_TEST_FILENAME").$BATS_TEST_DESCRIPTION.actual" + + testLine="Hello World!" +} +clear-colors-echo-test(){ + command=$1 + + run bash -c "$command $testLine | clear-colors | tee $outputActual" + assert_output - <$outputExpected +} +@test "clear-colors_echo.blue" { + clear-colors-echo-test echo.blue +} +@test "clear-colors_echo.cyan" { + clear-colors-echo-test echo.cyan +} +@test "clear-colors_echo.green" { + clear-colors-echo-test echo.green +} +@test "clear-colors_echo.grey" { + clear-colors-echo-test echo.grey +} +@test "clear-colors_echo.magenta" { + clear-colors-echo-test echo.magenta +} +@test "clear-colors_echo.red" { + clear-colors-echo-test echo.red +} +@test "clear-colors_echo.yellow" { + clear-colors-echo-test echo.yellow +} + diff --git a/tests/clear-colors/clear-colors-echo.bats.expected b/tests/clear-colors/clear-colors-echo.bats.expected new file mode 100644 index 0000000..980a0d5 --- /dev/null +++ b/tests/clear-colors/clear-colors-echo.bats.expected @@ -0,0 +1 @@ +Hello World! diff --git a/tests/common-setup.bash b/tests/common-setup.bash new file mode 100644 index 0000000..84d0a2a --- /dev/null +++ b/tests/common-setup.bash @@ -0,0 +1,18 @@ +common_setup(){ + load "../../$BATS_HELPER/bats-support/load" + load "../../$BATS_HELPER/bats-assert/load" + + PATH="$SOURCE_BIN:$PATH" + + echo "PATH: $PATH" >>"$CUSTOM_LOG" + + if [[ -z $BATS_RUN_TMPDIR ]]; then + TEST_TEMP_DIR=/tmp/$CUSTOM_LOG + mkdir -p $TEST_TEMP_DIR + else + TEST_TEMP_DIR=$BATS_RUN_TMPDIR + fi + + echo "BATS_RUN_TMPDIR: $BATS_RUN_TMPDIR" >>"$CUSTOM_LOG" + echo "TEST_TEMP_DIR: $TEST_TEMP_DIR" >>"$CUSTOM_LOG" +} \ No newline at end of file diff --git a/tests/echo/echo.bats b/tests/echo/echo.bats new file mode 100644 index 0000000..e8129d6 --- /dev/null +++ b/tests/echo/echo.bats @@ -0,0 +1,36 @@ +setup(){ + load '../common-setup' + common_setup + + outputExpected=$BATS_TEST_FILENAME.$BATS_TEST_DESCRIPTION.expected + outputActual="$TEST_TEMP_DIR/$(basename "$BATS_TEST_FILENAME").$BATS_TEST_DESCRIPTION.actual" + + testLine="Hello World!" +} +echo_test(){ + command=$1 + + run bash -c "$command $testLine | tee $outputActual" + assert_output - <$outputExpected +} +@test "echo.blue" { + echo_test echo.blue +} +@test "echo.cyan" { + echo_test echo.cyan +} +@test "echo.green" { + echo_test echo.green +} +@test "echo.grey" { + echo_test echo.grey +} +@test "echo.magenta" { + echo_test echo.magenta +} +@test "echo.red" { + echo_test echo.red +} +@test "echo.yellow" { + echo_test echo.yellow +} diff --git a/tests/echo/echo.bats.echo.blue.expected b/tests/echo/echo.bats.echo.blue.expected new file mode 100644 index 0000000..2f7f555 --- /dev/null +++ b/tests/echo/echo.bats.echo.blue.expected @@ -0,0 +1,2 @@ +Hello World! + \ No newline at end of file diff --git a/tests/echo/echo.bats.echo.cyan.expected b/tests/echo/echo.bats.echo.cyan.expected new file mode 100644 index 0000000..bbf5238 --- /dev/null +++ b/tests/echo/echo.bats.echo.cyan.expected @@ -0,0 +1,2 @@ +Hello World! + \ No newline at end of file diff --git a/tests/echo/echo.bats.echo.green.expected b/tests/echo/echo.bats.echo.green.expected new file mode 100644 index 0000000..4d2cbce --- /dev/null +++ b/tests/echo/echo.bats.echo.green.expected @@ -0,0 +1,2 @@ +Hello World! + \ No newline at end of file diff --git a/tests/echo/echo.bats.echo.grey.expected b/tests/echo/echo.bats.echo.grey.expected new file mode 100644 index 0000000..fe192ac --- /dev/null +++ b/tests/echo/echo.bats.echo.grey.expected @@ -0,0 +1,2 @@ +Hello World! + \ No newline at end of file diff --git a/tests/echo/echo.bats.echo.magenta.expected b/tests/echo/echo.bats.echo.magenta.expected new file mode 100644 index 0000000..1e9ca9d --- /dev/null +++ b/tests/echo/echo.bats.echo.magenta.expected @@ -0,0 +1,2 @@ +Hello World! + \ No newline at end of file diff --git a/tests/echo/echo.bats.echo.red.expected b/tests/echo/echo.bats.echo.red.expected new file mode 100644 index 0000000..4223e45 --- /dev/null +++ b/tests/echo/echo.bats.echo.red.expected @@ -0,0 +1,2 @@ +Hello World! + \ No newline at end of file diff --git a/tests/echo/echo.bats.echo.yellow.expected b/tests/echo/echo.bats.echo.yellow.expected new file mode 100644 index 0000000..c6c5bf8 --- /dev/null +++ b/tests/echo/echo.bats.echo.yellow.expected @@ -0,0 +1,2 @@ +Hello World! + \ No newline at end of file