diff --git a/.editorconfig b/.editorconfig index 41e0a8d6..2efdbab8 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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}] diff --git a/Makefile b/Makefile index 4807657f..23759c85 100644 --- a/Makefile +++ b/Makefile @@ -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" @@ -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 diff --git a/docs/command-line.md b/docs/command-line.md index 5b2628e5..ab17690e 100644 --- a/docs/command-line.md +++ b/docs/command-line.md @@ -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. [...] diff --git a/src/colors.sh b/src/colors.sh index 737e82fd..59b9f4d0 100644 --- a/src/colors.sh +++ b/src/colors.sh @@ -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" diff --git a/src/console_header.sh b/src/console_header.sh index 5d2249d0..3d413cf5 100644 --- a/src/console_header.sh +++ b/src/console_header.sh @@ -33,7 +33,7 @@ Arguments: If you use wildcards, bashunit will run any tests it finds. Options: - -f|--filer + -f|--filter Filters the tests to run based on the test name. -s|simple || -v|verbose diff --git a/src/console_results.sh b/src/console_results.sh index 8ef68c1a..c3deb958 100644 --- a/src/console_results.sh +++ b/src/console_results.sh @@ -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 } diff --git a/tests/acceptance/bashunit_execution_error_test.sh b/tests/acceptance/bashunit_execution_error_test.sh index 7230c0be..512c35ff 100644 --- a/tests/acceptance/bashunit_execution_error_test.sh +++ b/tests/acceptance/bashunit_execution_error_test.sh @@ -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" diff --git a/tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_env.snapshot b/tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_env.snapshot index ad66557b..5096d3cc 100644 --- a/tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_env.snapshot +++ b/tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_env.snapshot @@ -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  diff --git a/tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_option.snapshot b/tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_option.snapshot index ad66557b..5096d3cc 100644 --- a/tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_option.snapshot +++ b/tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_option.snapshot @@ -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  diff --git a/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_with_wildcard.snapshot b/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_with_wildcard.snapshot index 0ee25e47..84c9109b 100644 --- a/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_with_wildcard.snapshot +++ b/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_with_wildcard.snapshot @@ -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  diff --git a/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_within_a_directory.snapshot b/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_within_a_directory.snapshot index 0ee25e47..84c9109b 100644 --- a/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_within_a_directory.snapshot +++ b/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_within_a_directory.snapshot @@ -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  diff --git a/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_within_a_file.snapshot b/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_within_a_file.snapshot index 2f112e44..d814a6f1 100644 --- a/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_within_a_file.snapshot +++ b/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_within_a_file.snapshot @@ -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  diff --git a/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_simple_output_env.snapshot b/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_simple_output_env.snapshot index f576e636..3d4daf1d 100644 --- a/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_simple_output_env.snapshot +++ b/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_simple_output_env.snapshot @@ -1,4 +1,5 @@ .... Tests:  4 passed, 4 total Assertions: 6 passed, 6 total -All tests passed + + All tests passed  diff --git a/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_simple_output_option.snapshot b/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_simple_output_option.snapshot index f576e636..3d4daf1d 100644 --- a/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_simple_output_option.snapshot +++ b/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_simple_output_option.snapshot @@ -1,4 +1,5 @@ .... Tests:  4 passed, 4 total Assertions: 6 passed, 6 total -All tests passed + + All tests passed  diff --git a/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_verbose_output_env.snapshot b/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_verbose_output_env.snapshot index a442a102..b0afa5a2 100644 --- a/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_verbose_output_env.snapshot +++ b/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_verbose_output_env.snapshot @@ -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  diff --git a/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_verbose_output_option.snapshot b/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_verbose_output_option.snapshot index a442a102..b0afa5a2 100644 --- a/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_verbose_output_option.snapshot +++ b/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_verbose_output_option.snapshot @@ -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  diff --git a/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_argument_overloads_default_path.snapshot b/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_argument_overloads_default_path.snapshot index 913c8837..e0ec79df 100644 --- a/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_argument_overloads_default_path.snapshot +++ b/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_argument_overloads_default_path.snapshot @@ -1,4 +1,5 @@ Tests:  0 total Assertions: 0 total -No tests found + + No tests found  diff --git a/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_with_argument_path.snapshot b/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_with_argument_path.snapshot index 02a1abb9..55814a43 100644 --- a/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_with_argument_path.snapshot +++ b/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_with_argument_path.snapshot @@ -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  diff --git a/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_with_env_default_path.snapshot b/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_with_env_default_path.snapshot index 02a1abb9..55814a43 100644 --- a/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_with_env_default_path.snapshot +++ b/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_with_env_default_path.snapshot @@ -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  diff --git a/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_without_path_env_nor_argument.snapshot b/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_without_path_env_nor_argument.snapshot index 4b33b17e..c8d0b7d1 100644 --- a/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_without_path_env_nor_argument.snapshot +++ b/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_without_path_env_nor_argument.snapshot @@ -7,7 +7,7 @@ Arguments: If you use wildcards, bashunit will run any tests it finds. Options: - -f|--filer + -f|--filter Filters the tests to run based on the test name. -s|simple || -v|verbose