Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Add `--init` option to get you started quickly
- Fix process time always shows as 0 ms
- Fixed terminal width detection first tput and fall back stty
- Refactor clock optimizing the implementation used to get the time
Expand Down
5 changes: 5 additions & 0 deletions bashunit
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ source "$BASHUNIT_ROOT_DIR/src/assertions.sh"
source "$BASHUNIT_ROOT_DIR/src/reports.sh"
source "$BASHUNIT_ROOT_DIR/src/runner.sh"
source "$BASHUNIT_ROOT_DIR/src/bashunit.sh"
source "$BASHUNIT_ROOT_DIR/src/init.sh"
source "$BASHUNIT_ROOT_DIR/src/main.sh"

_ASSERT_FN=""
Expand Down Expand Up @@ -101,6 +102,10 @@ while [[ $# -gt 0 ]]; do
upgrade::upgrade
trap '' EXIT && exit 0
;;
--init)
init::init
trap '' EXIT && exit 0
;;
-h|--help)
console_header::print_help
trap '' EXIT && exit 0
Expand Down
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function build::dependencies() {
"src/assertions.sh"
"src/reports.sh"
"src/runner.sh"
"src/init.sh"
"src/bashunit.sh"
"src/main.sh"
)
Expand Down
15 changes: 15 additions & 0 deletions docs/command-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,21 @@ Upgrade **bashunit** to latest version.
```
:::

## Init

> `bashunit --init`

Generates a `tests` folder with a sample test and bootstrap file to get you started quickly.

::: code-group
```bash [Example]
./bashunit --init
```
```bash [Output]
> bashunit initialized in tests
```
:::

## Help

> `bashunit --help`
Expand Down
32 changes: 32 additions & 0 deletions src/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

function init::init() {
local tests_dir="$BASHUNIT_DEFAULT_PATH"
mkdir -p "$tests_dir"

local bootstrap_file="$tests_dir/bootstrap.sh"
if [[ ! -f "$bootstrap_file" ]]; then
cat >"$bootstrap_file" <<'SH'
#!/usr/bin/env bash
set -euo pipefail
# Place your common test setup here
SH
chmod +x "$bootstrap_file"
echo "> Created $bootstrap_file"
fi

local example_test="$tests_dir/example_test.sh"
if [[ ! -f "$example_test" ]]; then
cat >"$example_test" <<'SH'
#!/usr/bin/env bash

function test_bashunit_is_installed() {
assert_same "bashunit is installed" "bashunit is installed"
}
SH
chmod +x "$example_test"
echo "> Created $example_test"
fi

echo "> bashunit initialized in $tests_dir"
}
24 changes: 24 additions & 0 deletions tests/acceptance/bashunit_init_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
set -euo pipefail

TMP_DIR="tmp/init"
function set_up() {
rm -rf "$TMP_DIR"
mkdir -p "$TMP_DIR"
}

function tear_down() {
rm -rf "$TMP_DIR"
}

function test_bashunit_init_creates_structure() {
# switch into a clean temporary directory
pushd "$TMP_DIR" >/dev/null
# generate test scaffolding
../../bashunit --init > /tmp/init.log
# perform the assertions
assert_file_exists tests/example_test.sh
assert_file_exists tests/bootstrap.sh
# return to the original working directory
popd >/dev/null
}
Loading