Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add globals/common functions #347

Merged
merged 5 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions bashunit
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ export default defineConfig({
}, {
text: 'Standalone',
link: '/standalone'
}, {
text: 'Globals',
link: '/globals'
}, {
text: 'Custom asserts',
link: '/custom-asserts'
Expand Down
94 changes: 94 additions & 0 deletions docs/globals.md
Original file line number Diff line number Diff line change
@@ -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)"
}
```
53 changes: 53 additions & 0 deletions src/globals.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash
set -euo pipefail

# This file provides a set of global functions to developers.

function current_dir() {
dirname "${BASH_SOURCE[1]}"
}

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 </dev/urandom | head -c "$length"
}

function temp_file() {
# shellcheck disable=SC2155
local path="/tmp/bashunit_temp.$(random_str)"
touch "$path"
echo "$path"
}

function temp_dir() {
# shellcheck disable=SC2155
local dir="/tmp/bashunit_tempdir.$(random_str 5)}"
mkdir -p "$dir"
echo "$dir"
}

function cleanup_temp_files() {
rm -rf /tmp/bashunit_temp*
}

function log_info() {
# shellcheck disable=SC2145
echo "$(current_timestamp) [INFO]: $@"
}

function log_error() {
# shellcheck disable=SC2145
echo "$(current_timestamp) [ERROR]: $@" >&2
}
3 changes: 1 addition & 2 deletions src/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ function helper::normalize_test_function_name() {
echo "$result"
}


function helper::check_duplicate_functions() {
local script="$1"

Expand Down Expand Up @@ -96,7 +95,7 @@ function helper::find_files_recursive() {
fi
}

helper::normalize_variable_name() {
function helper::normalize_variable_name() {
local input_string="$1"
local normalized_string

Expand Down
9 changes: 0 additions & 9 deletions tests/globals.sh β†’ tests/bootstrap.sh
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
60 changes: 60 additions & 0 deletions tests/unit/globals_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/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)"
}

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)"
}
Loading