Skip to content

Commit

Permalink
add bats tests
Browse files Browse the repository at this point in the history
  • Loading branch information
siakhooi committed Apr 11, 2024
1 parent b9ba0dc commit 84e2839
Show file tree
Hide file tree
Showing 27 changed files with 210 additions and 2 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/test-with-bats.yml
Original file line number Diff line number Diff line change
@@ -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-*
2 changes: 2 additions & 0 deletions .github/workflows/workflow-build-with-quality-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ on:
jobs:
test:
uses: ./.github/workflows/build-with-shellcheck.yml
bats-test:
uses: ./.github/workflows/test-with-bats.yml
6 changes: 4 additions & 2 deletions .github/workflows/workflow-deployments.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ target
*.deb
*.deb.sha256sum
*.deb.sha512sum
test_helper
.gitmodules
bats-test-*
12 changes: 12 additions & 0 deletions scripts/bats-test-run.sh
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions scripts/bats-test-setup.sh
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions tests/cat/cat.bats
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions tests/cat/cat.bats.cat.blue.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello World From cat.*!

2 changes: 2 additions & 0 deletions tests/cat/cat.bats.cat.cyan.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello World From cat.*!

2 changes: 2 additions & 0 deletions tests/cat/cat.bats.cat.green.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello World From cat.*!

2 changes: 2 additions & 0 deletions tests/cat/cat.bats.cat.grey.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello World From cat.*!

2 changes: 2 additions & 0 deletions tests/cat/cat.bats.cat.magenta.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello World From cat.*!

2 changes: 2 additions & 0 deletions tests/cat/cat.bats.cat.red.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello World From cat.*!

2 changes: 2 additions & 0 deletions tests/cat/cat.bats.cat.yellow.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello World From cat.*!

1 change: 1 addition & 0 deletions tests/cat/cat_test_data
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World From cat.*!
3 changes: 3 additions & 0 deletions tests/clear-colors/cat_test_data
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hello World From cat.*! (Line 1)
Hello World From cat.*! (Line 2)
Hello World From cat.*! (Line 3)
37 changes: 37 additions & 0 deletions tests/clear-colors/clear-colors-echo.bats
Original file line number Diff line number Diff line change
@@ -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
}

1 change: 1 addition & 0 deletions tests/clear-colors/clear-colors-echo.bats.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
18 changes: 18 additions & 0 deletions tests/common-setup.bash
Original file line number Diff line number Diff line change
@@ -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"
}
36 changes: 36 additions & 0 deletions tests/echo/echo.bats
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions tests/echo/echo.bats.echo.blue.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello World!

2 changes: 2 additions & 0 deletions tests/echo/echo.bats.echo.cyan.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello World!

2 changes: 2 additions & 0 deletions tests/echo/echo.bats.echo.green.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello World!

2 changes: 2 additions & 0 deletions tests/echo/echo.bats.echo.grey.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello World!

2 changes: 2 additions & 0 deletions tests/echo/echo.bats.echo.magenta.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello World!

2 changes: 2 additions & 0 deletions tests/echo/echo.bats.echo.red.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello World!

2 changes: 2 additions & 0 deletions tests/echo/echo.bats.echo.yellow.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello World!


0 comments on commit 84e2839

Please sign in to comment.