Skip to content

Commit

Permalink
Merge pull request #245 from h0adp0re/fix/output-colors
Browse files Browse the repository at this point in the history
Increase contrast of test results
  • Loading branch information
antonio-gg-dev authored Mar 2, 2024
2 parents f339462 + 19ca2f6 commit 1507eb6
Show file tree
Hide file tree
Showing 20 changed files with 120 additions and 70 deletions.
15 changes: 3 additions & 12 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,25 @@ end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true

[**.sh]
indent_style = space
indent_size = 2

[**.sh]
max_line_length = 120

[**.md]
indent_style = space
indent_size = 4

[**.ts]
indent_style = space
indent_size = 2
max_line_length = 120

[**.css]
indent_style = space
indent_size = 2
max_line_length = 120

[**.json]
indent_style = space
indent_size = 2

[docs/**.md] # YAML support
indent_size = 2

[{Makefile,**.mk}]
[{Makefile,**.mk,.git*}]
indent_style = tab

[{tests/acceptance/**.sh,src/console_header.sh}]
Expand Down
22 changes: 11 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ endif

help:
@echo ""
@echo "usage: make COMMAND"
@echo "Usage: make [command]"
@echo ""
@echo "Commands:"
@echo " test Run the test"
@echo " test/list List all the test under the tests directory"
@echo " test/watch Automatically run the test every second"
@echo " env/example Makes a copy of the keys on your .env file"
@echo " pre_commit/install Installs the pre-commit hook"
@echo " pre_commit/run Function that will be called when the pre-commit runs"
@echo " test Run the tests"
@echo " test/list List all tests under the tests directory"
@echo " test/watch Automatically run tests every second"
@echo " env/example Copy variables without the values from .env into .env.example"
@echo " pre_commit/install Install the pre-commit hook"
@echo " pre_commit/run Function that will be called when the pre-commit hook runs"
@echo " sa Run shellcheck static analysis tool"
@echo " lint Run editorconfig linter tool"

Expand All @@ -68,25 +68,25 @@ test/watch: $(TEST_SCRIPTS)
@fswatch -m poll_monitor -or $(SRC_SCRIPTS_DIR) $(TEST_SCRIPTS_DIR) .env Makefile | xargs -n1 ./bashunit $(TEST_SCRIPTS)

env/example:
@echo "Copy the .env into the .env.example file without the values"
@echo "Copying variables without the values from .env into .env.example"
@sed 's/=.*/=/' .env > .env.example

pre_commit/install:
@echo "Installing pre-commit hooks"
@echo "Installing pre-commit hook"
cp $(PRE_COMMIT_SCRIPTS_FILE) $(GIT_DIR)/hooks/

pre_commit/run: test sa lint env/example

sa:
ifndef STATIC_ANALYSIS_CHECKER
@printf "\e[1m\e[31m%s\e[0m\n" "Shellcheck not installed: Static analisys not preformed!" && exit 1
@printf "\e[1m\e[31m%s\e[0m\n" "Shellcheck not installed: Static analysis not performed!" && exit 1
else
@shellcheck ./**/*.sh -C && printf "\e[1m\e[32m%s\e[0m\n" "ShellCheck: OK!"
endif

lint:
ifndef LINTER_CHECKER
@printf "\e[1m\e[31m%s\e[0m\n" "Editorconfig not installed: Lint not preformed!" && exit 1
@printf "\e[1m\e[31m%s\e[0m\n" "Editorconfig not installed: Lint not performed!" && exit 1
else
@ec -config .editorconfig && printf "\e[1m\e[32m%s\e[0m\n" "editorconfig-check: OK!"
endif
2 changes: 1 addition & 1 deletion docs/command-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Arguments:
Specifies the directory or file containing [...]
Options:
-f|--filer
-f|--filter
Filters the tests to run based on the test name.
[...]
Expand Down
45 changes: 31 additions & 14 deletions src/colors.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
#!/bin/bash

# shellcheck disable=SC2034
_COLOR_DEFAULT=$'\e[0m'
_COLOR_BOLD=$'\e[1m'
_COLOR_FAINT=$'\e[2m'
_COLOR_FAILED=$'\e[31m'
_COLOR_PASSED=$'\e[32m'
_COLOR_SKIPPED=$'\e[33m'
_COLOR_INCOMPLETE=$'\e[36m'
_COLOR_SNAPSHOT=$'\e[34m'
_COLOR_RETURN_ERROR=$'\e[41m'
_COLOR_RETURN_SUCCESS=$'\e[42m'
_COLOR_RETURN_SKIPPED=$'\e[43m'
_COLOR_RETURN_INCOMPLETE=$'\e[46m'
_COLOR_RETURN_SNAPSHOT=$'\e[44m'
# 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_DEFAULT="$(sgr 0)"
_COLOR_BOLD="$(sgr 1)"
_COLOR_FAINT="$(sgr 2)"
_COLOR_BLACK="$(sgr 30)"
_COLOR_FAILED="$(sgr 31)"
_COLOR_PASSED="$(sgr 32)"
_COLOR_SKIPPED="$(sgr 33)"
_COLOR_INCOMPLETE="$(sgr 36)"
_COLOR_SNAPSHOT="$(sgr 34)"
_COLOR_RETURN_ERROR="$(sgr 41)$_COLOR_BLACK$_COLOR_BOLD"
_COLOR_RETURN_SUCCESS="$(sgr 42)$_COLOR_BLACK$_COLOR_BOLD"
_COLOR_RETURN_SKIPPED="$(sgr 43)$_COLOR_BLACK$_COLOR_BOLD"
_COLOR_RETURN_INCOMPLETE="$(sgr 46)$_COLOR_BLACK$_COLOR_BOLD"
_COLOR_RETURN_SNAPSHOT="$(sgr 44)$_COLOR_BLACK$_COLOR_BOLD"
2 changes: 1 addition & 1 deletion src/console_header.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Arguments:
If you use wildcards, bashunit will run any tests it finds.
Options:
-f|--filer <filter>
-f|--filter <filter>
Filters the tests to run based on the test name.
-s|simple || -v|verbose
Expand Down
12 changes: 6 additions & 6 deletions src/console_results.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,36 +65,36 @@ function console_results::render_result() {
printf " %s total\n" "$total_assertions"

if [[ "$(state::get_tests_failed)" -gt 0 ]]; then
printf "%s%s%s\n" "$_COLOR_RETURN_ERROR" "Some tests failed" "$_COLOR_DEFAULT"
printf "\n%s%s%s\n" "$_COLOR_RETURN_ERROR" " Some tests failed " "$_COLOR_DEFAULT"
console_results::print_execution_time
exit 1
fi

if [[ "$(state::get_tests_incomplete)" -gt 0 ]]; then
printf "%s%s%s\n" "$_COLOR_RETURN_INCOMPLETE" "Some tests incomplete" "$_COLOR_DEFAULT"
printf "\n%s%s%s\n" "$_COLOR_RETURN_INCOMPLETE" " Some tests incomplete " "$_COLOR_DEFAULT"
console_results::print_execution_time
exit 0
fi

if [[ "$(state::get_tests_skipped)" -gt 0 ]]; then
printf "%s%s%s\n" "$_COLOR_RETURN_SKIPPED" "Some tests skipped" "$_COLOR_DEFAULT"
printf "\n%s%s%s\n" "$_COLOR_RETURN_SKIPPED" " Some tests skipped " "$_COLOR_DEFAULT"
console_results::print_execution_time
exit 0
fi

if [[ "$(state::get_tests_snapshot)" -gt 0 ]]; then
printf "%s%s%s\n" "$_COLOR_RETURN_SNAPSHOT" "Some snapshots created" "$_COLOR_DEFAULT"
printf "\n%s%s%s\n" "$_COLOR_RETURN_SNAPSHOT" " Some snapshots created " "$_COLOR_DEFAULT"
console_results::print_execution_time
exit 0
fi

if [[ $total_tests -eq 0 ]]; then
printf "%s%s%s\n" "$_COLOR_RETURN_ERROR" "No tests found" "$_COLOR_DEFAULT"
printf "\n%s%s%s\n" "$_COLOR_RETURN_ERROR" " No tests found " "$_COLOR_DEFAULT"
console_results::print_execution_time
exit 1
fi

printf "%s%s%s\n" "$_COLOR_RETURN_SUCCESS" "All tests passed" "$_COLOR_DEFAULT"
printf "\n%s%s%s\n" "$_COLOR_RETURN_SUCCESS" " All tests passed " "$_COLOR_DEFAULT"
console_results::print_execution_time
exit 0
}
Expand Down
54 changes: 42 additions & 12 deletions tests/acceptance/bashunit_execution_error_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,48 @@ function set_up_before_script() {

function test_bashunit_when_a_execution_error() {
local test_file=./tests/acceptance/fixtures/test_bashunit_when_a_execution_error.sh
local fixture_start
fixture_start=$(printf "Running ./tests/acceptance/fixtures/test_bashunit_when_a_execution_error.sh
\e[31m✗ Failed\e[0m: Error
\e[2mExpected\e[0m \e[1m\'127\'\e[0m
\e[2mto be exactly\e[0m \e[1m\'1\'\e[0m
\e[31m✗ Failed\e[0m: Error
\e[2m./tests/acceptance/fixtures/test_bashunit_when_a_execution_error.sh:")
local fixture_end
fixture_end=$(printf "\e[0m
\e[2mTests: \e[0m \e[31m1 failed\e[0m, 1 total
\e[2mAssertions:\e[0m \e[31m1 failed\e[0m, 1 total")
local fixture_start fixture_end
local color_default color_red color_dim color_bold

color_default="$(sgr 0)"
color_bold="$(sgr 1)"
color_dim="$(sgr 2)"
color_red="$(sgr 31)"

function format_fail_title() {
printf "\n%s%s%s%s" "${color_red}" "$1" "${color_default}" "$2"
}

function format_expect_title() {
printf "\n %s%s%s" "${color_dim}" "$1" "${color_default}"
}

function format_expect_value() {
printf " %s%s%s" "${color_bold}" "$1" "${color_default}"
}

function format_summary_title() {
printf "\n%s%s%s" "${color_dim}" "$1" "${color_default}"
}

function format_summary_value() {
printf " %s%s%s%s" "${color_red}" "$1" "${color_default}" "$2"
}

fixture_start=$(
printf "Running ./tests/acceptance/fixtures/test_bashunit_when_a_execution_error.sh"
format_fail_title "✗ Failed" ": Error"
format_expect_title "Expected"
format_expect_value "'127'"
format_expect_title "to be exactly"
format_expect_value "'1'"
)
fixture_end=$(
format_summary_title "Tests: "
format_summary_value "1 failed" ", 1 total"
format_summary_title "Assertions:"
format_summary_value "1 failed" ", 1 total"
)

todo "Add snapshots with regex to assert this test (part of the error message is localized)"
todo "Add snapshots with simple/verbose modes as in bashunit_pass_test and bashunit_fail_test"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ Running ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh

Tests:  4 passed, 1 failed, 5 total
Assertions: 6 passed, 1 failed, 7 total
Some tests failed

 Some tests failed 
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ Running ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh

Tests:  4 passed, 1 failed, 5 total
Assertions: 6 passed, 1 failed, 7 total
Some tests failed

 Some tests failed 
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ Running ./tests/acceptance/fixtures/tests_path/other_test.sh

Tests:  4 passed, 4 total
Assertions: 6 passed, 6 total
All tests passed

 All tests passed 
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ Running ./tests/acceptance/fixtures/tests_path/other_test.sh

Tests:  4 passed, 4 total
Assertions: 6 passed, 6 total
All tests passed

 All tests passed 
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ Running ./tests/acceptance/fixtures/tests_path/a_test.sh

Tests:  2 passed, 2 total
Assertions: 3 passed, 3 total
All tests passed

 All tests passed 
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
....
Tests:  4 passed, 4 total
Assertions: 6 passed, 6 total
All tests passed

 All tests passed 
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
....
Tests:  4 passed, 4 total
Assertions: 6 passed, 6 total
All tests passed

 All tests passed 
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ Running ./tests/acceptance/fixtures/test_bashunit_when_a_test_passes.sh

Tests:  4 passed, 4 total
Assertions: 6 passed, 6 total
All tests passed

 All tests passed 
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ Running ./tests/acceptance/fixtures/test_bashunit_when_a_test_passes.sh

Tests:  4 passed, 4 total
Assertions: 6 passed, 6 total
All tests passed

 All tests passed 
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

Tests:  0 total
Assertions: 0 total
No tests found

 No tests found 
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ Running tests/acceptance/fixtures/tests_path/other_test.sh

Tests:  4 passed, 4 total
Assertions: 6 passed, 6 total
All tests passed

 All tests passed 
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ Running tests/acceptance/fixtures/tests_path/other_test.sh

Tests:  4 passed, 4 total
Assertions: 6 passed, 6 total
All tests passed

 All tests passed 
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Arguments:
If you use wildcards, bashunit will run any tests it finds.

Options:
-f|--filer <filter>
-f|--filter <filter>
Filters the tests to run based on the test name.

-s|simple || -v|verbose
Expand Down

0 comments on commit 1507eb6

Please sign in to comment.