Skip to content

Commit

Permalink
Merge pull request #2033 from gaelicWizard/bats-precommit
Browse files Browse the repository at this point in the history
  • Loading branch information
seefood authored Nov 7, 2024
2 parents 6949272 + c5be0e7 commit 775044b
Show file tree
Hide file tree
Showing 25 changed files with 639 additions and 590 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.sh text eol=lf
*.bash text eol=lf
*.bats text eol=lf

# Docs allow trailing whitespaces
*.md whitespace=-blank-at-eol
Expand Down
4 changes: 1 addition & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ repos:
hooks:
- id: git-check # Configure in .gitattributes
- id: shellcheck
exclude: ".bats$"
- id: shfmt
exclude: ".bats$"
- repo: https://github.com/Lucas-C/pre-commit-hooks
Expand All @@ -38,10 +37,9 @@ repos:
types: [file]
- id: dot-bash
name: Check .bash files against bash-it requirements
exclude: "test/test_helper.bash"
entry: ./hooks/dot-bash.sh
language: system
files: "\\.bash$"
files: "\\.ba[ts][sh]$"
types: [file]
- id: clean-files-txt
name: Check that clean_files.txt is sorted alphabetically.
Expand Down
1 change: 1 addition & 0 deletions clean_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ docs/
hooks/
lib/
scripts/
test/

# root files
#
Expand Down
3 changes: 2 additions & 1 deletion hooks/dot-bash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ for file in "$@"; do
# Confirm expected schellcheck header
#
LINE1="$(head -n 1 "${file}")"
if [[ "${LINE1}" != "# shellcheck shell=bash" ]]; then
SCSH="${file##*.}"
if [[ "${LINE1}" != "# shellcheck shell=${SCSH}" ]]; then
echo "Bash include file \`${file}\` has bad/missing shellcheck header"
exit_code=1
fi
Expand Down
258 changes: 111 additions & 147 deletions test/bash_it/bash_it.bats

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/completion/aliases.completion.bats
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ function local_setup_file() {
alias rm='rm -v'
run load "${BASH_IT?}/completion/available/aliases.completion.bash"

refute_output
assert_output ""
}
160 changes: 82 additions & 78 deletions test/completion/bash-it.completion.bats
100755 → 100644

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/fixtures/bash_it/aliases/available/a.aliases.bash
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash
# shellcheck shell=bash

alias test_alias="a"
2 changes: 1 addition & 1 deletion test/fixtures/bash_it/aliases/available/b.aliases.bash
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash
# shellcheck shell=bash

alias test_alias="b"
2 changes: 1 addition & 1 deletion test/fixtures/bash_it/plugins/available/c.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash
# shellcheck shell=bash

alias test_alias="c"
21 changes: 11 additions & 10 deletions test/install/install.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
load "${MAIN_BASH_IT_DIR?}/test/test_helper.bash"

function local_setup() {
export HOME="$BATS_TEST_TMPDIR"
export HOME="${BATS_TEST_TMPDIR?}"
}

function local_setup_file() {
Expand All @@ -20,7 +20,7 @@ function local_setup_file() {
}

@test "install: verify that the install script exists" {
assert_file_exist "$BASH_IT/install.sh"
assert_file_exist "${BASH_IT?}/install.sh"
}

@test "install: run the install script silently" {
Expand All @@ -30,26 +30,27 @@ function local_setup_file() {

assert_file_exist "$HOME/$BASH_IT_CONFIG_FILE"

assert_link_exist "$BASH_IT/enabled/150---general.aliases.bash"
assert_link_exist "$BASH_IT/enabled/250---base.plugin.bash"
assert_link_exist "$BASH_IT/enabled/800---aliases.completion.bash"
assert_link_exist "$BASH_IT/enabled/350---bash-it.completion.bash"
assert_link_exist "$BASH_IT/enabled/325---system.completion.bash"
assert_link_exist "${BASH_IT?}/enabled/150---general.aliases.bash"
assert_link_exist "${BASH_IT?}/enabled/250---base.plugin.bash"
assert_link_exist "${BASH_IT?}/enabled/800---aliases.completion.bash"
assert_link_exist "${BASH_IT?}/enabled/350---bash-it.completion.bash"
assert_link_exist "${BASH_IT?}/enabled/325---system.completion.bash"
}

@test "install: verify that a backup file is created" {
cd "$BASH_IT"
local md5_orig md5_bak
cd "${BASH_IT?}"

touch "$HOME/$BASH_IT_CONFIG_FILE"
echo "test file content" > "$HOME/$BASH_IT_CONFIG_FILE"
local md5_orig=$(md5sum "$HOME/$BASH_IT_CONFIG_FILE" | awk '{print $1}')
md5_orig=$(md5sum "$HOME/$BASH_IT_CONFIG_FILE" | awk '{print $1}')

./install.sh --silent

assert_file_exist "$HOME/$BASH_IT_CONFIG_FILE"
assert_file_exist "$HOME/$BASH_IT_CONFIG_FILE.bak"

local md5_bak=$(md5sum "$HOME/$BASH_IT_CONFIG_FILE.bak" | awk '{print $1}')
md5_bak=$(md5sum "$HOME/$BASH_IT_CONFIG_FILE.bak" | awk '{print $1}')

assert_equal "$md5_orig" "$md5_bak"
}
Expand Down
18 changes: 10 additions & 8 deletions test/install/uninstall.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
load "${MAIN_BASH_IT_DIR?}/test/test_helper.bash"

function local_setup() {
export HOME="$BATS_TEST_TMPDIR"
export HOME="${BATS_TEST_TMPDIR?}"
}

function local_setup_file() {
Expand All @@ -20,15 +20,16 @@ function local_setup_file() {
}

@test "uninstall: verify that the uninstall script exists" {
assert_file_exist "$BASH_IT/uninstall.sh"
assert_file_exist "${BASH_IT?}/uninstall.sh"
}

@test "uninstall: run the uninstall script with an existing backup file" {
cd "$BASH_IT"
local md5_bak md5_conf
cd "${BASH_IT?}"

echo "test file content for backup" > "$HOME/$BASH_IT_CONFIG_FILE.bak"
echo "test file content for original file" > "$HOME/$BASH_IT_CONFIG_FILE"
local md5_bak=$(md5sum "$HOME/$BASH_IT_CONFIG_FILE.bak" | awk '{print $1}')
md5_bak=$(md5sum "$HOME/$BASH_IT_CONFIG_FILE.bak" | awk '{print $1}')

run ./uninstall.sh
assert_success
Expand All @@ -37,16 +38,17 @@ function local_setup_file() {
assert_file_not_exist "$HOME/$BASH_IT_CONFIG_FILE.bak"
assert_file_exist "$HOME/$BASH_IT_CONFIG_FILE"

local md5_conf=$(md5sum "$HOME/$BASH_IT_CONFIG_FILE" | awk '{print $1}')
md5_conf=$(md5sum "$HOME/$BASH_IT_CONFIG_FILE" | awk '{print $1}')

assert_equal "$md5_bak" "$md5_conf"
}

@test "uninstall: run the uninstall script without an existing backup file" {
cd "$BASH_IT"
local md5_orig md5_uninstall
cd "${BASH_IT?}"

echo "test file content for original file" > "$HOME/$BASH_IT_CONFIG_FILE"
local md5_orig=$(md5sum "$HOME/$BASH_IT_CONFIG_FILE" | awk '{print $1}')
md5_orig=$(md5sum "$HOME/$BASH_IT_CONFIG_FILE" | awk '{print $1}')

run ./uninstall.sh
assert_success
Expand All @@ -55,7 +57,7 @@ function local_setup_file() {
assert_file_not_exist "$HOME/$BASH_IT_CONFIG_FILE.bak"
assert_file_not_exist "$HOME/$BASH_IT_CONFIG_FILE"

local md5_uninstall=$(md5sum "$HOME/$BASH_IT_CONFIG_FILE.uninstall" | awk '{print $1}')
md5_uninstall=$(md5sum "$HOME/$BASH_IT_CONFIG_FILE.uninstall" | awk '{print $1}')

assert_equal "$md5_orig" "$md5_uninstall"
}
Loading

0 comments on commit 775044b

Please sign in to comment.