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
18 changes: 18 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Define hooks that this repository provides for pre-commit
- id: bash-unit
name: Run tests with `bash_unit`
description: This hook runs tests using `bash_unit`
entry: ./bash_unit
language: script
files: ^tests/(.*/)?test_.*\.sh$
types: [shell]
pass_filenames: true
# Duplicated with different id name for convienience
- id: bash_unit
name: Run tests with `bash_unit`
description: This hook runs tests using `bash_unit`
entry: ./bash_unit
language: script
files: ^tests/(.*/)?test_.*\.sh$
types: [shell]
pass_filenames: true
44 changes: 44 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,50 @@ You can also download it from the https://github.com/pgrange/bash_unit/releases[

endif::[]

=== https://pre-commit.com[pre-commit] hook

You can run `+bash_unit+` as a https://pre-commit.com[pre-commit] hook.

Add the following to your https://pre-commit.com[pre-commit] configuration. By default it will run scripts that are identified as shell scripts that match the path `+^tests/(.*/)?test_.*\.sh$+`.

[.pre-commit-config,yaml]
----
repos:
- repo: https://github.com/pgrange/bash_unit
rev: v1.7.0
hooks:
- id: bash-unit
----

== Running `+bash_unit+` in the test script
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this section in the documentation as test_doc, in my opinion, is really advanced and odd use of bash_unit. One should not have to do that in 99.99% of the cases so that describing it here introduces some sort of noise in the doc, in my opinion and can even be a bit scary for new users :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


In some cases you want to run `+bash_unit+` from inside the test script.

One example is this project’s `+test_doc.sh+` test script.

One method to define a `+BASH_UNIT+` variable is shown below:

[test_script,bash]
----
# Function to recursively search upwards for file
_find_file() {
local dir="$1"
local file="$2"
while [ "${dir}" != "/" ]; do
if [ -f "${dir}/${file}" ]; then
echo "${dir}/${file}"
return 0
fi
dir=$(dirname "${dir}")
done
return 1
}

B_U=$(_find_file "$(dirname "$(realpath "$0")")" bash_unit)
# shellcheck disable=2089
BASH_UNIT="eval FORCE_COLOR=false \"$B_U\""
----

== How to run tests

To run tests, simply call *bash_unit* with all your tests files as parameter. For instance to run some *bash_unit* tests, from *bash_unit* directory:
Expand Down