Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

install bashunit and test pre-commit hook #307

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ charset = utf-8

[{*.yml,*.yaml}]
indent_size = 2

[*.sh]
indent_size = 2
1 change: 1 addition & 0 deletions tools/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib/
16 changes: 16 additions & 0 deletions tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## Tools

Here you can find developer tools to improve your DX.

### Testing

Install testing library with:
```bash
cd tools
./install-dependencies.sh
```

Run bash tests:
```bash
lib/bashunit tests
```
20 changes: 20 additions & 0 deletions tools/install-dependencies.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

# Ensure bashdep is installed
[ ! -f lib/bashdep ] && {
mkdir -p lib
curl -sLo lib/bashdep \
https://github.com/Chemaclass/bashdep/releases/download/0.1/bashdep
chmod +x lib/bashdep
}

# Add latest bashunit release to your dependencies
DEPENDENCIES=(
"https://github.com/TypedDevs/bashunit/releases/download/0.18.0/bashunit"
"https://github.com/Chemaclass/bash-dumper/releases/download/0.1/dumper.sh@dev"
)

# Load, configure and run bashdep
source lib/bashdep
bashdep::setup dir="lib" dev-dir="src/dev" silent=false
bashdep::install "${DEPENDENCIES[@]}"
58 changes: 58 additions & 0 deletions tools/src/dev/dumper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash

# Pass in any number of ANSI SGR codes.
#
# Code reference:
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
# Credit:
# https://superuser.com/a/1119396
sgr() {
local codes=${1:-0}
shift

for c in "$@"; do
codes="$codes;$c"
done

echo $'\e'"[${codes}m"
}

_COLOR_BOLD="$(sgr 1)"
_COLOR_FAINT="$(sgr 2)"
_COLOR_RED="$(sgr 31)"
_COLOR_YELLOW="$(sgr 33)"
_COLOR_BLACK="$(sgr 30)"
_COLOR_GREEN="$(sgr 32)"
_COLOR_DEFAULT="$(sgr 0)"

function trace() {
set -x
}

function untrace() {
set +x
}

# An alternative to echo when debugging.
function dump() {
printf "[%s] %s: %s\n" "${_COLOR_YELLOW}DUMP${_COLOR_DEFAULT}" \
"${_COLOR_GREEN}${BASH_SOURCE[1]}:${BASH_LINENO[0]}" \
"${_COLOR_DEFAULT}$*"
}

# Dump and Die.
function dd() {
printf "[%s] %s: %s\n" "${_COLOR_RED}DUMP${_COLOR_DEFAULT}" \
"${_COLOR_GREEN}${BASH_SOURCE[1]}:${BASH_LINENO[0]}" \
"${_COLOR_DEFAULT}$*"

kill -9 $$
}

function debug_var() {
local var_name=$1
local var_value=${!var_name}
printf "[%s] %s: %s=%s\n" "${_COLOR_FAINT}DEBUG${_COLOR_DEFAULT}" \
"${_COLOR_GREEN}${BASH_SOURCE[1]}:${BASH_LINENO[0]}" \
"$var_name" "$var_value"
}
11 changes: 11 additions & 0 deletions tools/tests/pre-commit_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

function set_up() {
SCRIPT="$(current_dir)"/../git-hooks/pre-commit.sh
}

function test_pre_commit() {
mock composer echo "mocked composer"

assert_match_snapshot "$($SCRIPT)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mocked composer
mocked composer
Loading