Skip to content

Commit 1507eb6

Browse files
Merge pull request #245 from h0adp0re/fix/output-colors
Increase contrast of test results
2 parents f339462 + 19ca2f6 commit 1507eb6

20 files changed

+120
-70
lines changed

.editorconfig

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,25 @@ end_of_line = lf
55
insert_final_newline = true
66
charset = utf-8
77
trim_trailing_whitespace = true
8-
9-
[**.sh]
108
indent_style = space
119
indent_size = 2
10+
11+
[**.sh]
1212
max_line_length = 120
1313

1414
[**.md]
15-
indent_style = space
1615
indent_size = 4
1716

1817
[**.ts]
19-
indent_style = space
20-
indent_size = 2
2118
max_line_length = 120
2219

2320
[**.css]
24-
indent_style = space
25-
indent_size = 2
2621
max_line_length = 120
2722

28-
[**.json]
29-
indent_style = space
30-
indent_size = 2
31-
3223
[docs/**.md] # YAML support
3324
indent_size = 2
3425

35-
[{Makefile,**.mk}]
26+
[{Makefile,**.mk,.git*}]
3627
indent_style = tab
3728

3829
[{tests/acceptance/**.sh,src/console_header.sh}]

Makefile

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ endif
3737

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

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

7070
env/example:
71-
@echo "Copy the .env into the .env.example file without the values"
71+
@echo "Copying variables without the values from .env into .env.example"
7272
@sed 's/=.*/=/' .env > .env.example
7373

7474
pre_commit/install:
75-
@echo "Installing pre-commit hooks"
75+
@echo "Installing pre-commit hook"
7676
cp $(PRE_COMMIT_SCRIPTS_FILE) $(GIT_DIR)/hooks/
7777

7878
pre_commit/run: test sa lint env/example
7979

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

8787
lint:
8888
ifndef LINTER_CHECKER
89-
@printf "\e[1m\e[31m%s\e[0m\n" "Editorconfig not installed: Lint not preformed!" && exit 1
89+
@printf "\e[1m\e[31m%s\e[0m\n" "Editorconfig not installed: Lint not performed!" && exit 1
9090
else
9191
@ec -config .editorconfig && printf "\e[1m\e[32m%s\e[0m\n" "editorconfig-check: OK!"
9292
endif

docs/command-line.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ Arguments:
150150
Specifies the directory or file containing [...]
151151
152152
Options:
153-
-f|--filer
153+
-f|--filter
154154
Filters the tests to run based on the test name.
155155
156156
[...]

src/colors.sh

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
#!/bin/bash
22

3-
# shellcheck disable=SC2034
4-
_COLOR_DEFAULT=$'\e[0m'
5-
_COLOR_BOLD=$'\e[1m'
6-
_COLOR_FAINT=$'\e[2m'
7-
_COLOR_FAILED=$'\e[31m'
8-
_COLOR_PASSED=$'\e[32m'
9-
_COLOR_SKIPPED=$'\e[33m'
10-
_COLOR_INCOMPLETE=$'\e[36m'
11-
_COLOR_SNAPSHOT=$'\e[34m'
12-
_COLOR_RETURN_ERROR=$'\e[41m'
13-
_COLOR_RETURN_SUCCESS=$'\e[42m'
14-
_COLOR_RETURN_SKIPPED=$'\e[43m'
15-
_COLOR_RETURN_INCOMPLETE=$'\e[46m'
16-
_COLOR_RETURN_SNAPSHOT=$'\e[44m'
3+
# Pass in any number of ANSI SGR codes.
4+
#
5+
# Code reference:
6+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
7+
# Credit:
8+
# https://superuser.com/a/1119396
9+
sgr() {
10+
local codes=${1:-0}
11+
shift
12+
13+
for c in "$@"; do
14+
codes="$codes;$c"
15+
done
16+
17+
echo $'\e'"[${codes}m"
18+
}
19+
20+
_COLOR_DEFAULT="$(sgr 0)"
21+
_COLOR_BOLD="$(sgr 1)"
22+
_COLOR_FAINT="$(sgr 2)"
23+
_COLOR_BLACK="$(sgr 30)"
24+
_COLOR_FAILED="$(sgr 31)"
25+
_COLOR_PASSED="$(sgr 32)"
26+
_COLOR_SKIPPED="$(sgr 33)"
27+
_COLOR_INCOMPLETE="$(sgr 36)"
28+
_COLOR_SNAPSHOT="$(sgr 34)"
29+
_COLOR_RETURN_ERROR="$(sgr 41)$_COLOR_BLACK$_COLOR_BOLD"
30+
_COLOR_RETURN_SUCCESS="$(sgr 42)$_COLOR_BLACK$_COLOR_BOLD"
31+
_COLOR_RETURN_SKIPPED="$(sgr 43)$_COLOR_BLACK$_COLOR_BOLD"
32+
_COLOR_RETURN_INCOMPLETE="$(sgr 46)$_COLOR_BLACK$_COLOR_BOLD"
33+
_COLOR_RETURN_SNAPSHOT="$(sgr 44)$_COLOR_BLACK$_COLOR_BOLD"

src/console_header.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Arguments:
3333
If you use wildcards, bashunit will run any tests it finds.
3434
3535
Options:
36-
-f|--filer <filter>
36+
-f|--filter <filter>
3737
Filters the tests to run based on the test name.
3838
3939
-s|simple || -v|verbose

src/console_results.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,36 +65,36 @@ function console_results::render_result() {
6565
printf " %s total\n" "$total_assertions"
6666

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

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

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

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

9191
if [[ $total_tests -eq 0 ]]; then
92-
printf "%s%s%s\n" "$_COLOR_RETURN_ERROR" "No tests found" "$_COLOR_DEFAULT"
92+
printf "\n%s%s%s\n" "$_COLOR_RETURN_ERROR" " No tests found " "$_COLOR_DEFAULT"
9393
console_results::print_execution_time
9494
exit 1
9595
fi
9696

97-
printf "%s%s%s\n" "$_COLOR_RETURN_SUCCESS" "All tests passed" "$_COLOR_DEFAULT"
97+
printf "\n%s%s%s\n" "$_COLOR_RETURN_SUCCESS" " All tests passed " "$_COLOR_DEFAULT"
9898
console_results::print_execution_time
9999
exit 0
100100
}

tests/acceptance/bashunit_execution_error_test.sh

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,48 @@ function set_up_before_script() {
66

77
function test_bashunit_when_a_execution_error() {
88
local test_file=./tests/acceptance/fixtures/test_bashunit_when_a_execution_error.sh
9-
local fixture_start
10-
fixture_start=$(printf "Running ./tests/acceptance/fixtures/test_bashunit_when_a_execution_error.sh
11-
\e[31m✗ Failed\e[0m: Error
12-
\e[2mExpected\e[0m \e[1m\'127\'\e[0m
13-
\e[2mto be exactly\e[0m \e[1m\'1\'\e[0m
14-
\e[31m✗ Failed\e[0m: Error
15-
\e[2m./tests/acceptance/fixtures/test_bashunit_when_a_execution_error.sh:")
16-
local fixture_end
17-
fixture_end=$(printf "\e[0m
18-
19-
\e[2mTests: \e[0m \e[31m1 failed\e[0m, 1 total
20-
\e[2mAssertions:\e[0m \e[31m1 failed\e[0m, 1 total")
9+
local fixture_start fixture_end
10+
local color_default color_red color_dim color_bold
11+
12+
color_default="$(sgr 0)"
13+
color_bold="$(sgr 1)"
14+
color_dim="$(sgr 2)"
15+
color_red="$(sgr 31)"
16+
17+
function format_fail_title() {
18+
printf "\n%s%s%s%s" "${color_red}" "$1" "${color_default}" "$2"
19+
}
20+
21+
function format_expect_title() {
22+
printf "\n %s%s%s" "${color_dim}" "$1" "${color_default}"
23+
}
24+
25+
function format_expect_value() {
26+
printf " %s%s%s" "${color_bold}" "$1" "${color_default}"
27+
}
28+
29+
function format_summary_title() {
30+
printf "\n%s%s%s" "${color_dim}" "$1" "${color_default}"
31+
}
32+
33+
function format_summary_value() {
34+
printf " %s%s%s%s" "${color_red}" "$1" "${color_default}" "$2"
35+
}
36+
37+
fixture_start=$(
38+
printf "Running ./tests/acceptance/fixtures/test_bashunit_when_a_execution_error.sh"
39+
format_fail_title "✗ Failed" ": Error"
40+
format_expect_title "Expected"
41+
format_expect_value "'127'"
42+
format_expect_title "to be exactly"
43+
format_expect_value "'1'"
44+
)
45+
fixture_end=$(
46+
format_summary_title "Tests: "
47+
format_summary_value "1 failed" ", 1 total"
48+
format_summary_title "Assertions:"
49+
format_summary_value "1 failed" ", 1 total"
50+
)
2151

2252
todo "Add snapshots with regex to assert this test (part of the error message is localized)"
2353
todo "Add snapshots with simple/verbose modes as in bashunit_pass_test and bashunit_fail_test"

tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_env.snapshot

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ Running ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh
99

1010
Tests:  4 passed, 1 failed, 5 total
1111
Assertions: 6 passed, 1 failed, 7 total
12-
Some tests failed
12+
13+
 Some tests failed 

tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_option.snapshot

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ Running ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh
99

1010
Tests:  4 passed, 1 failed, 5 total
1111
Assertions: 6 passed, 1 failed, 7 total
12-
Some tests failed
12+
13+
 Some tests failed 

tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_with_wildcard.snapshot

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ Running ./tests/acceptance/fixtures/tests_path/other_test.sh
77

88
Tests:  4 passed, 4 total
99
Assertions: 6 passed, 6 total
10-
All tests passed
10+
11+
 All tests passed 

0 commit comments

Comments
 (0)