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

Improve loading extra file with BASHUNIT_LOAD_FILE #330

Merged
merged 9 commits into from
Sep 17, 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
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ BASHUNIT_STOP_ON_FAILURE=
BASHUNIT_SHOW_EXECUTION_TIME=
BASHUNIT_LOG_JUNIT=
BASHUNIT_REPORT_HTML=
BASHUNIT_TESTS_ENV=
BASHUNIT_LOAD_FILE=
2 changes: 1 addition & 1 deletion .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 tests/**/*_test.sh"'
script_name: 'bash -c "./bashunit -e tests/globals.sh tests/**/*_test.sh"'
- os: ubuntu-latest
script_name: 'make test'
- os: macos-latest
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ docs/.vitepress/cache
docs/.vitepress/dist

# installation
lib/
bin/bashunit
bin/checksum
lib/bashunit

# logger
log-junit.xml
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Rename `BASHUNIT_TESTS_ENV` to `BASHUNIT_LOAD_FILE`

## [0.16.0](https://github.com/TypedDevs/bashunit/compare/0.15.0...0.16.0) - 2024-09-15

- Fixed `clock::now` can't locate Time when is not available
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ test/list:
@echo $(TEST_SCRIPTS) | tr ' ' '\n'

test: $(TEST_SCRIPTS)
@./bashunit $(TEST_SCRIPTS)
@./bashunit $(TEST_SCRIPTS) -e tests/globals.sh

test/watch: $(TEST_SCRIPTS)
@./bashunit $(TEST_SCRIPTS)
Expand Down
4 changes: 2 additions & 2 deletions bashunit
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ while [[ $# -gt 0 ]]; do
-S|--stop-on-failure)
export BASHUNIT_STOP_ON_FAILURE=true
;;
-e|--env)
-e|--env|--load)
# shellcheck disable=SC1090
source "$2"
shift
Expand Down Expand Up @@ -87,7 +87,7 @@ while [[ $# -gt 0 ]]; do
done

# shellcheck disable=SC1090
[[ -f "$BASHUNIT_TESTS_ENV" ]] && source "$BASHUNIT_TESTS_ENV"
[[ -f "$BASHUNIT_LOAD_FILE" ]] && source "$BASHUNIT_LOAD_FILE"

set +eu

Expand Down
4 changes: 2 additions & 2 deletions docs/command-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ Enables a shell mode in which all executed commands are printed to the terminal.

## Environment

> `bashunit -e|--env "file path"`
> `bashunit -e|--env|--load "file path"`

Loads a custom env file overriding the `.env` environment variables.

You can use `BASHUNIT_TESTS_ENV` option in your [configuration](/configuration#tests-env).
You can use `BASHUNIT_LOAD_FILE` option in your [configuration](/configuration#tests-env).

::: code-group
```bash [Example]
Expand Down
14 changes: 9 additions & 5 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,22 @@ BASHUNIT_REPORT_HTML=report.html
```
:::

## Tests env
## Load file

> `BASHUNIT_TESTS_ENV=file`
> `BASHUNIT_LOAD_FILE=file`

Specifies an additional `.env` file to be loaded for all tests cases.
Useful to set up global variables accessible in all your tests.
Specifies an additional file to be loaded for all tests cases.
Useful to set up global variables or functions accessible in all your tests.

Similarly, you can use load an additional `.env` via the [command line](/command-line#environment).

::: code-group
```bash [Example]
BASHUNIT_TESTS_ENV=.env.tests
# a simple .env file
BASHUNIT_LOAD_FILE=".env.tests"

# or a complete script file
BASHUNIT_LOAD_FILE="tests/globals.sh"
```
:::

Expand Down
4 changes: 2 additions & 2 deletions src/console_header.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ Options:
--debug
Print all executed shell commands to the terminal.

-e|--env <file-path>
Load a custom env file overriding the .env environment variables.
-e|--env|--load <file-path>
Load a custom file, overriding the existing .env variables or loading a file with global functions.

-f|--filter <filter>
Filters the tests to run based on the test name.
Expand Down
2 changes: 1 addition & 1 deletion src/default_env_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ _DEFAULT_SHOW_EXECUTION_TIME=true
_DEFAULT_DEFAULT_PATH=
_DEFAULT_LOG_JUNIT=
_DEFAULT_REPORT_HTML=
_DEFAULT_BASHUNIT_TESTS_ENV=
_DEFAULT_BASHUNIT_LOAD_FILE=
CAT="$(which cat)"
2 changes: 1 addition & 1 deletion src/env_configuration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ set +o allexport
: "${BASHUNIT_DEFAULT_PATH:=${DEFAULT_PATH:=$_DEFAULT_DEFAULT_PATH}}"
: "${BASHUNIT_LOG_JUNIT:=${LOG_JUNIT:=$_DEFAULT_LOG_JUNIT}}"
: "${BASHUNIT_REPORT_HTML:=${REPORT_HTML:=$_DEFAULT_REPORT_HTML}}"
: "${BASHUNIT_TESTS_ENV:=${TESTS_ENV:=$_DEFAULT_BASHUNIT_TESTS_ENV}}"
: "${BASHUNIT_LOAD_FILE:=${LOAD_FILE:=$_DEFAULT_BASHUNIT_LOAD_FILE}}"
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Options:
--debug
Print all executed shell commands to the terminal.

-e|--env <file-path>
Load a custom env file overriding the .env environment variables.
-e|--env|--load <file-path>
Load a custom file, overriding the existing .env variables or loading a file with global functions.

-f|--filter <filter>
Filters the tests to run based on the test name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ Options:
--debug
Print all executed shell commands to the terminal.

-e|--env <file-path>
Load a custom env file overriding the .env environment variables.
-e|--env|--load <file-path>
Load a custom file, overriding the existing .env variables or loading a file with global functions.

-f|--filter <filter>
Filters the tests to run based on the test name.
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/custom_asserts_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -euo pipefail

function set_up() {
_ROOT_DIR="$(dirname "${BASH_SOURCE[0]}")"
_ROOT_DIR="$(current_dir)"
source "$_ROOT_DIR/custom_asserts.sh"
}

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/logic_test.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
set -euo pipefail

ROOT_DIR="$(dirname "${BASH_SOURCE[0]}")"
ROOT_DIR="$(current_dir)"

SCRIPT="$ROOT_DIR/logic.sh"

Expand Down
10 changes: 10 additions & 0 deletions tests/globals.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -euo pipefail

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

function current_filename() {
basename "${BASH_SOURCE[1]}"
}
18 changes: 9 additions & 9 deletions tests/unit/directory_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

function test_successful_assert_directory_exists() {
local a_directory
a_directory="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
a_directory="$(current_dir)"

assert_empty "$(assert_directory_exists "$a_directory")"
}
Expand All @@ -18,7 +18,7 @@ function test_unsuccessful_assert_directory_exists() {

function test_assert_directory_exists_should_not_work_with_files() {
local a_file
a_file="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
a_file="$(current_dir)/$(current_filename)"

assert_same\
"$(console_results::print_failed_test \
Expand All @@ -34,7 +34,7 @@ function test_successful_assert_directory_not_exists() {

function test_unsuccessful_assert_directory_not_exists() {
local a_directory
a_directory="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
a_directory="$(current_dir)"

assert_same\
"$(console_results::print_failed_test \
Expand All @@ -44,7 +44,7 @@ function test_unsuccessful_assert_directory_not_exists() {

function test_successful_assert_is_directory() {
local a_directory
a_directory="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
a_directory="$(current_dir)"

assert_empty "$(assert_is_directory "$a_directory")"
}
Expand All @@ -60,7 +60,7 @@ function test_unsuccessful_assert_is_directory() {

function test_unsuccessful_assert_is_directory_when_a_file_is_given() {
local a_file
a_file="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
a_file="$(current_dir)/$(current_filename)"

assert_same\
"$(console_results::print_failed_test\
Expand All @@ -79,7 +79,7 @@ function test_successful_assert_is_directory_empty() {

function test_unsuccessful_assert_is_directory_empty() {
local a_directory
a_directory="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
a_directory="$(current_dir)"

assert_same\
"$(console_results::print_failed_test \
Expand All @@ -89,7 +89,7 @@ function test_unsuccessful_assert_is_directory_empty() {

function test_successful_assert_is_directory_not_empty() {
local a_directory
a_directory="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
a_directory="$(current_dir)"

assert_empty "$(assert_is_directory_not_empty "$a_directory")"
}
Expand Down Expand Up @@ -117,7 +117,7 @@ function test_successful_assert_is_directory_readable() {

function test_unsuccessful_assert_is_directory_readable_when_a_file_is_given() {
local a_file
a_file="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
a_file="$(current_dir)/$(current_filename)"

assert_same\
"$(console_results::print_failed_test\
Expand Down Expand Up @@ -230,7 +230,7 @@ function test_unsuccessful_assert_is_directory_writable() {

function test_unsuccessful_assert_is_directory_writable_when_a_file_is_given() {
local a_file
a_file="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
a_file="$(current_dir)/$(current_filename)"

assert_same\
"$(console_results::print_failed_test\
Expand Down
25 changes: 10 additions & 15 deletions tests/unit/file_test.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/bin/bash

# shellcheck disable=SC2155

function test_successful_assert_file_exists() {
local a_file
a_file="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
local a_file="$(current_dir)/$(current_filename)"

assert_empty "$(assert_file_exists "$a_file")"
}
Expand All @@ -16,8 +17,7 @@ function test_unsuccessful_assert_file_exists() {
}

function test_assert_file_exists_should_not_work_with_folders() {
local a_dir
a_dir="$(dirname "${BASH_SOURCE[0]}")"
local a_dir="$(current_dir)"

assert_same\
"$(console_results::print_failed_test \
Expand All @@ -32,8 +32,7 @@ function test_successful_assert_file_not_exists() {
}

function test_unsuccessful_assert_file_not_exists() {
local a_file
a_file="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
local a_file="$(current_dir)/$(current_filename)"

assert_same\
"$(console_results::print_failed_test\
Expand All @@ -42,8 +41,7 @@ function test_unsuccessful_assert_file_not_exists() {
}

function test_successful_assert_is_file() {
local a_file
a_file="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
local a_file="$(current_dir)/$(current_filename)"

assert_empty "$(assert_is_file "$a_file")"
}
Expand All @@ -57,8 +55,7 @@ function test_unsuccessful_assert_is_file() {
}

function test_unsuccessful_assert_is_file_when_a_folder_is_given() {
local a_folder
a_folder="$(dirname "${BASH_SOURCE[0]}")"
local a_folder="$(current_dir)"

assert_same\
"$(console_results::print_failed_test\
Expand All @@ -76,8 +73,7 @@ function test_successful_assert_is_file_empty() {
}

function test_unsuccessful_assert_is_file_empty() {
local a_file
a_file="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
local a_file="$(current_dir)/$(current_filename)"

assert_same\
"$(console_results::print_failed_test\
Expand Down Expand Up @@ -107,9 +103,8 @@ function test_successful_assert_files_equals() {

# shellcheck disable=SC2155
function test_fails_assert_files_equals() {
datetime=$(date +%s)
local expected="/tmp/a_random_file_${datetime}_1"
local actual="/tmp/a_random_file_${datetime}_2"
local expected="/tmp/test_fails_assert_files_equals_1"
local actual="/tmp/test_fails_assert_files_equals_2"

echo -e "same\noriginal content" > "$expected"
echo -e "same\ndifferent content" > "$actual"
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/helpers_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ function test_successful_unset_if_exists() {

function test_check_duplicate_functions_with_duplicates() {
local file
file="$(dirname "${BASH_SOURCE[0]}")/fixtures/duplicate_functions.sh"
file="$(current_dir)/fixtures/duplicate_functions.sh"

assert_general_error "$(helper::check_duplicate_functions "$file")"
}

function test_check_duplicate_functions_without_duplicates() {
local file
file="$(dirname "${BASH_SOURCE[0]}")/fixtures/no_duplicate_functions.sh"
file="$(current_dir)/fixtures/no_duplicate_functions.sh"

assert_successful_code "$(helper::check_duplicate_functions "$file")"
}
Expand Down Expand Up @@ -161,7 +161,7 @@ function test_trim() {

function test_find_files_recursive_given_file() {
local path
path="$(dirname "${BASH_SOURCE[0]}")/fixtures/tests/example1_test.sh"
path="$(current_dir)/fixtures/tests/example1_test.sh"

local result
result=$(helper::find_files_recursive "$path")
Expand All @@ -171,7 +171,7 @@ function test_find_files_recursive_given_file() {

function test_find_files_recursive_given_dir() {
local path
path="$(dirname "${BASH_SOURCE[0]}")/fixtures/tests"
path="$(current_dir)/fixtures/tests"

local result
result=$(helper::find_files_recursive "$path")
Expand All @@ -183,7 +183,7 @@ tests/unit/fixtures/tests/example2_test.sh"\

function test_find_files_recursive_given_wildcard() {
local path
path="$(dirname "${BASH_SOURCE[0]}")/fixtures/tests/*2_test.sh"
path="$(current_dir)/fixtures/tests/*2_test.sh"

local result
result=$(helper::find_files_recursive "$path")
Expand Down