Skip to content

Commit

Permalink
Configure bats and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
fuchs-fabian committed Sep 11, 2024
1 parent 8385fbc commit f1ce673
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/.shellcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
external-sources=true
48 changes: 48 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Use bats in Repositories

Create a repository...

Create a structure with submodules, e.g:

```plain
src/
<project>.bash
...
test/
bats/ <- submodule
test_helper/
bats-support/ <- submodule
bats-assert/ <- submodule
<test>.bats
...
```

```bash
git submodule add https://github.com/bats-core/bats-core.git test/bats
```

```bash
git submodule add https://github.com/bats-core/bats-support.git test/test_helper/bats-support
```

```bash
git submodule add https://github.com/bats-core/bats-assert.git test/test_helper/bats-assert
```

```bash
git add .
```

```bash
git commit -m "Add bats as submodules"
```

```bash
git push
```

Init and update all submodules:

```bash
git submodule update --init --recursive
```
20 changes: 20 additions & 0 deletions test/test-call-with-arguments.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Run from project dir:
# ./test/bats/bin/bats test/test-call-with-arguments.bats

setup() {
load 'test_helper/test-setup'
init_bats

chmod +x src/simbashlog.bash
}

teardown() {
chmod -x src/simbashlog.bash
}

@test "simplest call with arguments" {
run simbashlog.bash -a console -s error --message 'An error occured'
remove_ansi_codes_from_output

[[ "$output" == "[ERROR] - An error occured" ]]
}
19 changes: 19 additions & 0 deletions test/test_helper/test-setup.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

function init_bats {
load 'test_helper/bats-support/load'
load 'test_helper/bats-assert/load'

# get the containing directory of this file
# use $BATS_TEST_FILENAME instead of ${BASH_SOURCE[0]} or $0,
# as those will point to the bats executable's location or the preprocessed file respectively
PROJECT_ROOT="$(cd "$(dirname "$BATS_TEST_FILENAME")/.." >/dev/null 2>&1 && pwd)"
# make executables in src/ visible to PATH
PATH="$PROJECT_ROOT/src:$PATH"
}

function remove_ansi_codes_from_output {
echo "Actual output with ansi codes: '$output'"
output=$(echo "$output" | sed -r 's/\x1B\[[0-9;]*[mK]//g')
echo "Actual output without ansi codes: '$output'"
}

0 comments on commit f1ce673

Please sign in to comment.