From e944041c4a04401201ea470fca44491f2a80fd37 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Thu, 3 Oct 2024 17:33:50 +0200 Subject: [PATCH 1/5] refactor: rename tests/globals.sh to tests/bootstrap.sh --- .github/workflows/tests.yml | 4 ++-- Makefile | 2 +- src/globals.sh | 10 ++++++++++ src/helpers.sh | 2 +- tests/{globals.sh => bootstrap.sh} | 0 tests/unit/globals_test.sh | 10 ++++++++++ 6 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 src/globals.sh rename tests/{globals.sh => bootstrap.sh} (100%) create mode 100644 tests/unit/globals_test.sh diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 18e159a3..ebe90351 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -18,7 +18,7 @@ jobs: - windows-latest include: - os: windows-latest - script_name: 'bash -c "./bashunit -e tests/globals.sh tests/**/*_test.sh"' + script_name: 'bash -c "./bashunit -e tests/bootstrap.sh tests/**/*_test.sh"' - os: macos-latest script_name: 'make test' - os: ubuntu-latest @@ -43,7 +43,7 @@ jobs: - name: Run Tests run: | - ./bashunit -e tests/globals.sh --simple tests/ + ./bashunit -e tests/bootstrap.sh --simple tests/ alpine: name: "Run tests on alpine-latest" diff --git a/Makefile b/Makefile index 1a1a3cf2..1ed321e2 100644 --- a/Makefile +++ b/Makefile @@ -62,7 +62,7 @@ test/list: @echo $(TEST_SCRIPTS) | tr ' ' '\n' test: $(TEST_SCRIPTS) - @./bashunit $(TEST_SCRIPTS) -e tests/globals.sh + @./bashunit $(TEST_SCRIPTS) -e tests/bootstrap.sh test/watch: $(TEST_SCRIPTS) @./bashunit $(TEST_SCRIPTS) diff --git a/src/globals.sh b/src/globals.sh new file mode 100644 index 00000000..50e90788 --- /dev/null +++ b/src/globals.sh @@ -0,0 +1,10 @@ +#!/bin/bash +set -euo pipefail + +function current_dir() { + dirname "${BASH_SOURCE[1]}" +} + +function current_filename() { + basename "${BASH_SOURCE[1]}" +} diff --git a/src/helpers.sh b/src/helpers.sh index 1bca074c..419c9a1b 100755 --- a/src/helpers.sh +++ b/src/helpers.sh @@ -96,7 +96,7 @@ function helper::find_files_recursive() { fi } -helper::normalize_variable_name() { +function helper::normalize_variable_name() { local input_string="$1" local normalized_string diff --git a/tests/globals.sh b/tests/bootstrap.sh similarity index 100% rename from tests/globals.sh rename to tests/bootstrap.sh diff --git a/tests/unit/globals_test.sh b/tests/unit/globals_test.sh new file mode 100644 index 00000000..51c9d26b --- /dev/null +++ b/tests/unit/globals_test.sh @@ -0,0 +1,10 @@ +#!/bin/bash +set -euo pipefail + +function test_globals_current_dir() { + assert_same "tests/unit" "$(current_dir)" +} + +function test_globals_current_filename() { + assert_same "globals_test.sh" "$(current_filename)" +} From 1d27cb568297813f23e22f88f69f8fb15006c177 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Thu, 3 Oct 2024 17:34:56 +0200 Subject: [PATCH 2/5] feat: add src/globals.sh to bashunit --- bashunit | 1 + src/helpers.sh | 1 - tests/bootstrap.sh | 9 --------- 3 files changed, 1 insertion(+), 10 deletions(-) diff --git a/bashunit b/bashunit index 1efa42dc..640b38bc 100755 --- a/bashunit +++ b/bashunit @@ -10,6 +10,7 @@ export BASHUNIT_ROOT_DIR source "$BASHUNIT_ROOT_DIR/src/dev/debug.sh" source "$BASHUNIT_ROOT_DIR/src/str.sh" +source "$BASHUNIT_ROOT_DIR/src/globals.sh" source "$BASHUNIT_ROOT_DIR/src/dependencies.sh" source "$BASHUNIT_ROOT_DIR/src/io.sh" source "$BASHUNIT_ROOT_DIR/src/math.sh" diff --git a/src/helpers.sh b/src/helpers.sh index 419c9a1b..a143948e 100755 --- a/src/helpers.sh +++ b/src/helpers.sh @@ -23,7 +23,6 @@ function helper::normalize_test_function_name() { echo "$result" } - function helper::check_duplicate_functions() { local script="$1" diff --git a/tests/bootstrap.sh b/tests/bootstrap.sh index 8c633987..948669ed 100644 --- a/tests/bootstrap.sh +++ b/tests/bootstrap.sh @@ -1,15 +1,6 @@ #!/bin/bash set -euo pipefail -function current_dir() { - dirname "${BASH_SOURCE[1]}" -} - -function current_filename() { - basename "${BASH_SOURCE[1]}" -} - - function mock_non_existing_fn() { return 127; } From 3fcceaa5a65fa1ddf82ec261d36d2961062327d0 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Thu, 3 Oct 2024 18:13:55 +0200 Subject: [PATCH 3/5] feat: adding more global functions --- src/globals.sh | 43 ++++++++++++++++++++++++++++++++ tests/unit/globals_test.sh | 50 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/src/globals.sh b/src/globals.sh index 50e90788..dd9fbcc0 100644 --- a/src/globals.sh +++ b/src/globals.sh @@ -1,6 +1,8 @@ #!/bin/bash set -euo pipefail +# This file provides a set of global functions to developers. + function current_dir() { dirname "${BASH_SOURCE[1]}" } @@ -8,3 +10,44 @@ function current_dir() { function current_filename() { basename "${BASH_SOURCE[1]}" } + +function current_timestamp() { + date +"%Y-%m-%d %H:%M:%S" +} + +function is_command_available() { + command -v "$1" >/dev/null 2>&1 +} + +function random_str() { + local length=${1:-6} + LC_ALL=C tr -dc A-Za-z0-9 &2 +} diff --git a/tests/unit/globals_test.sh b/tests/unit/globals_test.sh index 51c9d26b..0a81f5ff 100644 --- a/tests/unit/globals_test.sh +++ b/tests/unit/globals_test.sh @@ -8,3 +8,53 @@ function test_globals_current_dir() { function test_globals_current_filename() { assert_same "globals_test.sh" "$(current_filename)" } + +function test_globals_current_timestamp() { + assert_matches \ + "^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$" \ + "$(current_timestamp)" +} + +function test_globals_is_command_available() { + assert_successful_code "$(is_command_available ls)" +} + +function test_globals_is_command_not_available() { + assert_general_error "$(is_command_available non-existing-command)" +} + +function test_globals_random_str_default() { + assert_matches "^[A-Za-z0-9]{6}$" "$(random_str)" +} + +function test_globals_random_str_custom() { + assert_matches "^[A-Za-z0-9]{3}$" "$(random_str 3)" +} + +function test_globals_temp_file() { + # shellcheck disable=SC2155 + local temp_file=$(temp_file) + assert_file_exists "$temp_file" + cleanup_temp_files + assert_file_not_exists "$temp_file" +} + +function test_globals_temp_dir() { + # shellcheck disable=SC2155 + local temp_dir=$(temp_dir) + assert_directory_exists "$temp_dir" + cleanup_temp_files + assert_directory_not_exists "$temp_dir" +} + +function test_globals_log_info() { + assert_matches \ + "^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} \[INFO\]: hello, world$" \ + "$(log_info "hello," "world")" +} + +function test_globals_log_error() { + assert_matches \ + "^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} \[ERROR\]: hello, luna$" \ + "$(log_error "hello," "luna" 2>&1)" +} From b5d06dad3100c8ad0451598a35ebb23e352282ce Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Thu, 3 Oct 2024 18:35:26 +0200 Subject: [PATCH 4/5] docs: update changelog --- CHANGELOG.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9708c5b..94a8d11c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,17 @@ ## [Unreleased](https://github.com/TypedDevs/bashunit/compare/0.17.0...main) -- ... +- Added global util functions + - current_dir + - current_filename + - current_timestamp + - is_command_available + - random_str + - temp_file + - temp_dir + - cleanup_temp_files + - log_info + - log_error ## [0.17.0](https://github.com/TypedDevs/bashunit/compare/0.16.0...0.17.0) - 2024-10-01 From 4981f50e6615da1c968819aba435b3b6553b8bf3 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Thu, 3 Oct 2024 18:46:44 +0200 Subject: [PATCH 5/5] docs: add new section page Globals --- CHANGELOG.md | 20 ++++----- docs/.vitepress/config.ts | 3 ++ docs/globals.md | 94 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 10 deletions(-) create mode 100644 docs/globals.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 94a8d11c..c8f12aef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,16 +3,16 @@ ## [Unreleased](https://github.com/TypedDevs/bashunit/compare/0.17.0...main) - Added global util functions - - current_dir - - current_filename - - current_timestamp - - is_command_available - - random_str - - temp_file - - temp_dir - - cleanup_temp_files - - log_info - - log_error + - current_dir + - current_filename + - current_timestamp + - is_command_available + - random_str + - temp_file + - temp_dir + - cleanup_temp_files + - log_info + - log_error ## [0.17.0](https://github.com/TypedDevs/bashunit/compare/0.16.0...0.17.0) - 2024-10-01 diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index 2e170ab0..cc3252c7 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -78,6 +78,9 @@ export default defineConfig({ }, { text: 'Standalone', link: '/standalone' + }, { + text: 'Globals', + link: '/globals' }, { text: 'Custom asserts', link: '/custom-asserts' diff --git a/docs/globals.md b/docs/globals.md new file mode 100644 index 00000000..a796a620 --- /dev/null +++ b/docs/globals.md @@ -0,0 +1,94 @@ +# Globals + +You can use this list of global/common functions that exists to primarily to improve your developer experience. + +> Using the existing tests as documentation. + +## current_dir + +```bash +function test_globals_current_dir() { + assert_same "tests/unit" "$(current_dir)" +} +``` + +## current_filename + +```bash +function test_globals_current_filename() { + assert_same "globals_test.sh" "$(current_filename)" +} +``` + +## current_timestamp + +```bash + +function test_globals_current_timestamp() { + assert_matches \ + "^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$" \ + "$(current_timestamp)" +} +``` + +## is_command_available + +```bash +function test_globals_is_command_available() { + assert_successful_code "$(is_command_available ls)" + assert_general_error "$(is_command_available non-existing-command)" +} +``` + +## random_str + +```bash +function test_globals_random_str_default() { + assert_matches "^[A-Za-z0-9]{6}$" "$(random_str)" + assert_matches "^[A-Za-z0-9]{3}$" "$(random_str 3)" +} +``` + +## temp_file + +```bash +function test_globals_temp_file() { + # shellcheck disable=SC2155 + local temp_file=$(temp_file) + assert_file_exists "$temp_file" + cleanup_temp_files + assert_file_not_exists "$temp_file" +} +``` + +## temp_dir + +```bash +function test_globals_temp_dir() { + # shellcheck disable=SC2155 + local temp_dir=$(temp_dir) + assert_directory_exists "$temp_dir" + cleanup_temp_files + assert_directory_not_exists "$temp_dir" +} +``` + +## log_info + +```bash +function test_globals_log_info() { + assert_matches \ + "^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} \[INFO\]: hello, world$" \ + "$(log_info "hello," "world")" +} +``` + +## log_error + +```bash +function test_globals_log_error() { + assert_matches \ + "^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} \[ERROR\]: hello, luna$" \ + "$(log_error "hello," "luna" 2>&1)" +} +```