Skip to content

Commit 44bb03a

Browse files
authored
Merge pull request #443 from TypedDevs/feat/allow-init-with-dir
Support custom dir to --init
2 parents 17c4b93 + 6e9b22d commit 44bb03a

File tree

8 files changed

+63
-7
lines changed

8 files changed

+63
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
## Unreleased
44

5-
- Add `--init` option to get you started quickly
5+
- Add `--init [dir]` option to get you started quickly
6+
- Init with custom directory updates `.env` with `BASHUNIT_BOOTSTRAP`
67
- Fix process time always shows as 0 ms
78
- Fixed terminal width detection first tput and fall back stty
89
- Refactor clock optimizing the implementation used to get the time

bashunit

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ while [[ $# -gt 0 ]]; do
103103
trap '' EXIT && exit 0
104104
;;
105105
--init)
106-
init::init
106+
if [[ -n ${2:-} && ${2:0:1} != "-" ]]; then
107+
init::project "$2"
108+
shift
109+
else
110+
init::project
111+
fi
107112
trap '' EXIT && exit 0
108113
;;
109114
-h|--help)

docs/command-line.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,15 @@ Upgrade **bashunit** to latest version.
372372

373373
## Init
374374

375-
> `bashunit --init`
375+
> `bashunit --init [dir]`
376376
377-
Generates a `tests` folder with a sample test and bootstrap file to get you started quickly.
377+
Generates a `tests` folder (or the provided `dir`) with a sample test and bootstrap file to get you started quickly.
378+
When a custom directory is used, bashunit writes `BASHUNIT_BOOTSTRAP=<dir>/bootstrap.sh` to a `.env` file. If `.env` already defines
379+
`BASHUNIT_BOOTSTRAP`, the existing line is commented out and the new value appended.
378380

379381
::: code-group
380382
```bash [Example]
381-
./bashunit --init
383+
./bashunit --init tests
382384
```
383385
```bash [Output]
384386
> bashunit initialized in tests

src/console_header.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ Options:
9797
--upgrade
9898
Upgrade to latest version of bashunit.
9999
100+
--init [dir]
101+
Generate a tests folder (or the provided directory) with a sample test
102+
and bootstrap file to get you started quickly.
103+
100104
-h, --help
101105
This message.
102106

src/init.sh

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22

3-
function init::init() {
4-
local tests_dir="$BASHUNIT_DEFAULT_PATH"
3+
function init::project() {
4+
local tests_dir="${1:-$BASHUNIT_DEFAULT_PATH}"
55
mkdir -p "$tests_dir"
66

77
local bootstrap_file="$tests_dir/bootstrap.sh"
@@ -28,5 +28,20 @@ SH
2828
echo "> Created $example_test"
2929
fi
3030

31+
local env_file=".env"
32+
local env_line="BASHUNIT_BOOTSTRAP=$bootstrap_file"
33+
if [[ -f "$env_file" ]]; then
34+
if grep -q "^BASHUNIT_BOOTSTRAP=" "$env_file"; then
35+
if check_os::is_macos; then
36+
sed -i '' -e "s/^BASHUNIT_BOOTSTRAP=/#&/" "$env_file"
37+
else
38+
sed -i -e "s/^BASHUNIT_BOOTSTRAP=/#&/" "$env_file"
39+
fi
40+
fi
41+
echo "$env_line" >> "$env_file"
42+
else
43+
echo "$env_line" > "$env_file"
44+
fi
45+
3146
echo "> bashunit initialized in $tests_dir"
3247
}

tests/acceptance/bashunit_init_test.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env bash
2+
# shellcheck disable=SC2317
3+
24
set -euo pipefail
35

46
TMP_DIR="tmp/init"
@@ -22,3 +24,22 @@ function test_bashunit_init_creates_structure() {
2224
# return to the original working directory
2325
popd >/dev/null
2426
}
27+
28+
function test_bashunit_init_custom_directory() {
29+
pushd "$TMP_DIR" >/dev/null
30+
../../bashunit --init custom > /tmp/init.log
31+
assert_file_exists custom/example_test.sh
32+
assert_file_exists custom/bootstrap.sh
33+
popd >/dev/null
34+
}
35+
36+
function test_bashunit_init_updates_env() {
37+
pushd "$TMP_DIR" >/dev/null
38+
echo "BASHUNIT_BOOTSTRAP=old/bootstrap.sh" > .env
39+
../../bashunit --init custom > /tmp/init.log
40+
assert_file_exists custom/example_test.sh
41+
assert_file_exists custom/bootstrap.sh
42+
assert_file_contains .env "#BASHUNIT_BOOTSTRAP=old/bootstrap.sh"
43+
assert_file_contains .env "BASHUNIT_BOOTSTRAP=custom/bootstrap.sh"
44+
popd >/dev/null
45+
}

tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_without_path_env_nor_argument.snapshot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ Options:
4444
--upgrade
4545
Upgrade to latest version of bashunit.
4646

47+
--init [dir]
48+
Generate a tests folder (or the provided directory) with a sample test
49+
and bootstrap file to get you started quickly.
50+
4751
-h, --help
4852
This message.
4953

tests/acceptance/snapshots/bashunit_test_sh.test_bashunit_should_display_help.snapshot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ Options:
4343
--upgrade
4444
Upgrade to latest version of bashunit.
4545

46+
--init [dir]
47+
Generate a tests folder (or the provided directory) with a sample test
48+
and bootstrap file to get you started quickly.
49+
4650
-h, --help
4751
This message.
4852

0 commit comments

Comments
 (0)