Skip to content

Commit

Permalink
Merge branch 'main' into feat/339-multiple-asserts-on-standalone
Browse files Browse the repository at this point in the history
  • Loading branch information
Chemaclass committed Oct 12, 2024
2 parents 715ca90 + 98c326b commit 0b912df
Show file tree
Hide file tree
Showing 85 changed files with 2,277 additions and 714 deletions.
10 changes: 7 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
BASHUNIT_DEFAULT_PATH=
BASHUNIT_LOG_JUNIT=
BASHUNIT_REPORT_HTML=
BASHUNIT_LOAD_FILE=

# Booleans
BASHUNIT_PARALLEL_RUN=
BASHUNIT_SHOW_HEADER=
BASHUNIT_HEADER_ASCII_ART=
BASHUNIT_SIMPLE_OUTPUT=
BASHUNIT_STOP_ON_FAILURE=
BASHUNIT_SHOW_EXECUTION_TIME=
BASHUNIT_LOG_JUNIT=
BASHUNIT_REPORT_HTML=
BASHUNIT_LOAD_FILE=
BASHUNIT_DEV_MODE=
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ docker run --rm -it -v "$(pwd)":/project -w /project ubuntu:latest \
make test/alpine
# or
docker run --rm -it -v "$(pwd)":/project -w /project alpine:latest \
sh -c "apk add bash make shellcheck git curl perl && make test"
sh -c "apk add bash make shellcheck git && make test"
```

## Coding Guidelines
Expand Down
1 change: 1 addition & 0 deletions .github/RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
This is a guide to know the steps to create a new release.

1. Update the version in [BASHUNIT_VERSION](../bashunit)
1. Update the version in [LATEST_BASHUNIT_VERSION](../install.sh)
1. Update the version in [CHANGELOG.md](../CHANGELOG.md)
1. Update the version in [package.json](../package.json)
1. Build the project `./build.sh bin`
Expand Down
81 changes: 66 additions & 15 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,96 @@ on:
- main

jobs:
tests:
name: "Run tests on ${{ matrix.os }}"
ubuntu-macos:
name: "On ${{ matrix.os }}"
timeout-minutes: 10
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
- macos-latest
- windows-latest
include:
- os: windows-latest
script_name: 'bash -c "./bashunit -e tests/globals.sh tests/**/*_test.sh"'
- os: macos-latest
script_name: 'make test'
- os: ubuntu-latest
script_name: 'make test'
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-depth: 1

- name: Run Tests
run: ${{ matrix.script_name }}
run: make test

windows:
name: "On windows (${{ matrix.test_chunk }})"
timeout-minutes: 10
runs-on: windows-latest
strategy:
matrix:
test_chunk: [acceptance, functional, unit]
fail-fast: false
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 1

- name: Run tests
shell: bash
run: |
./bashunit tests/${{ matrix.test_chunk }}/*_test.sh
alpine:
name: "Run tests on alpine-latest"
name: "On alpine-latest"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-depth: 1

- name: Run Tests
run: |
docker run --rm -v "$(pwd)":/project alpine:latest /bin/sh -c " \
apk update && \
apk add --no-cache bash make git curl perl coreutils && \
apk add --no-cache bash make git && \
adduser -D builder && \
chown -R builder /project && \
su - builder -c 'cd /project; make test';"
simple-output:
name: "Simple output"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 1

- name: Run Tests
run: |
./bashunit --simple tests/
simple-output-parallel:
name: "Simple output in parallel"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 1

- name: Run Tests
run: |
./bashunit --parallel --simple tests/
extended-output-parallel:
name: "Extended output in parallel"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 1

- name: Run Tests
run: |
./bashunit --parallel tests/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ report.html

# internal
local/
out.log
44 changes: 41 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,53 @@
# Changelog

## [Unreleased](https://github.com/TypedDevs/bashunit/compare/0.16.0...main)

## [Unreleased](https://github.com/TypedDevs/bashunit/compare/0.17.0...main)

- Added `-p|--parallel` to enable running tests in parallel
- Added `assert_file_contains` and `assert_file_not_contains`
- Added `assert_true` and `assert_false`
- Added `BASHUNIT_LOG_PATH`
- Added global util functions
- current_dir
- current_filename
- current_timestamp
- is_command_available
- random_str
- temp_file
- temp_dir
- cleanup_temp_files
- log
- Add default env values:
- `DEFAULT_PATH="tests"`
- `LOG_PATH="out.log"`
- `LOAD_FILE="tests/bootstrap.sh"`
- Add check that git is installed to `install.sh`
- Fixed `-S|--stop-on-failure` behaviour
- Improved time taken display
- Improved clean up temporal files and directories
- Improved CI test speed by running them in parallel
- Removed git dependency for stable installations

## [0.17.0](https://github.com/TypedDevs/bashunit/compare/0.16.0...0.17.0) - 2024-10-01

- Fixed simple output for non-successful states
- Added support for Alpine (Linux Distro)
- Added optional file-path as 2nd arg to `--debug` option
- Added runtime per test
- Added runtime duration per test
- Added defer expressions with when using standalone assertions
- Added failing tests after running the entire suite
- Improved runtime errors handling
- Simplified total tests display on the header
- Renamed `BASHUNIT_TESTS_ENV` to `BASHUNIT_LOAD_FILE`
- Better handler runtime errors
- Display failing tests after running the entire suite
- Added defer expressions with `eval` when using standalone assertions
- Fixed simple output for non-successful states
- Remove deprecated assertions
- Some required dependencies now optional: perl, coreutils
- Upgrade and install script can now use `wget` if `curl` is not installed
- Tests can be also be timed by making use of `EPOCHREALTIME` on supported system
- Switch to testing the environment of capabilities
- rather than assuming various operating systems and Linux distributions have programs installed

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

Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ test/list:
@echo $(TEST_SCRIPTS) | tr ' ' '\n'

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

test/watch: $(TEST_SCRIPTS)
@./bashunit $(TEST_SCRIPTS)
@fswatch -m poll_monitor -or $(SRC_SCRIPTS_DIR) $(TEST_SCRIPTS_DIR) .env Makefile | xargs -n1 ./bashunit $(TEST_SCRIPTS)

docker/alpine:
@docker run --rm -it -v "$(shell pwd)":/project -w /project alpine:latest \
sh -c "apk add bash make shellcheck git curl perl coreutils && bash"
sh -c "apk add bash make shellcheck git && bash"

env/example:
@echo "Copying variables without the values from .env into .env.example"
Expand Down
53 changes: 53 additions & 0 deletions adrs/adr-002-using-booleans.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Title: Using native bash booleans

* Status: accepted
* Authors: @Chemaclass
* Date: 2024-10-03

Technical Story:
- Pull Request: [TypedDevs/bashunit#345](https://github.com/TypedDevs/bashunit/pull/345#discussion_r1782226289)

## Context and Problem Statement

We are using booleans with different syntax in different parts of the project.

## Considered Options

* Use true and false as `0`, `1` native shell booleans
* Use true and false as strings: `"true"`, `"false"`
* Use true and false as native programs: `true`, `false`

## Decision Outcome

To keep consistency in the project, we want to use the standard and best practices of booleans while
keeping a great DX.

When using return, we must use a number:
- `return 0` # for success
- `return 1` # for failure

When using variables, we must use `true` and `false` as commands (not strings!):
- `true` is a command that always returns a successful exit code (0)
- `false` is a command that always returns a failure exit code (1)

When possible, extract a condition into a function. For example:
```bash
function env::is_show_header_enabled() {
# this is a string comparison because it is coming from the .env
[[ "$BASHUNIT_SHOW_HEADER" == "true" ]]
}
```
Usage
```bash
if env::is_show_header_enabled; then
# ...
fi
```

### Positive Consequences

We keep the native shell boolean syntax in conditions.

### Negative Consequences

Not that I am aware of.
50 changes: 50 additions & 0 deletions adrs/adr-003-parallel-testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Title: Parallel testing

* Status: accepted
* Authors: @Chemaclass
* Date: 2024-10-11

Technical Story:
- Pull Request: [TypedDevs/bashunit#358](https://github.com/TypedDevs/bashunit/pull/358)

## Context and Problem Statement

We aim to enhance testing performance by running tests in parallel processes while capturing and aggregating results effectively.

## Considered Options

- Implement parallel execution using subprocesses.
- Aggregate test results from temporary files.
- Use a spinner for user feedback during result aggregation.

## Decision Outcome

- Implemented parallel test execution using subprocesses.
- Each test creates a temporary directory to store results, later aggregated.

### Positive Consequences

- Reduced test execution time considerably.
- Clear feedback via a spinner during aggregation.

### Negative Consequences

- Potential complexity
- with handling temporary files during interruptions.
- in handling temporary files and managing subprocesses.

## Technical Details

When the `--parallel` flag is used, each test is run in its own subprocess by calling:

> runner::call_test_functions "$test_file" "$filter" 2>/dev/null &
Each test script creates a temporary directory and stores individual test results in temp files.
After all tests finish, the results are aggregated by traversing these directories and files.
This approach ensures isolation of test execution while improving performance by running tests concurrently.

The aggregation (which collects all test outcomes into a final result set) is handled by the function:

> parallel::aggregate_test_results "$TEMP_DIR_PARALLEL_TEST_SUITE"

23 changes: 18 additions & 5 deletions bashunit
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
set -euo pipefail

# shellcheck disable=SC2034
declare -r BASHUNIT_VERSION="0.16.0"
declare -r BASHUNIT_VERSION="0.17.0"

# shellcheck disable=SC2155
declare -r BASHUNIT_ROOT_DIR="$(dirname "${BASH_SOURCE[0]}")"
export BASHUNIT_ROOT_DIR

source "$BASHUNIT_ROOT_DIR/src/dev/debug.sh"
source "$BASHUNIT_ROOT_DIR/src/str.sh"
source "$BASHUNIT_ROOT_DIR/src/default_env_config.sh"
source "$BASHUNIT_ROOT_DIR/src/env_configuration.sh"
source "$BASHUNIT_ROOT_DIR/src/check_os.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"
source "$BASHUNIT_ROOT_DIR/src/parallel.sh"
source "$BASHUNIT_ROOT_DIR/src/env.sh"
source "$BASHUNIT_ROOT_DIR/src/clock.sh"
source "$BASHUNIT_ROOT_DIR/src/state.sh"
source "$BASHUNIT_ROOT_DIR/src/colors.sh"
Expand All @@ -21,7 +25,7 @@ source "$BASHUNIT_ROOT_DIR/src/console_results.sh"
source "$BASHUNIT_ROOT_DIR/src/helpers.sh"
source "$BASHUNIT_ROOT_DIR/src/upgrade.sh"
source "$BASHUNIT_ROOT_DIR/src/assertions.sh"
source "$BASHUNIT_ROOT_DIR/src/logger.sh"
source "$BASHUNIT_ROOT_DIR/src/reports.sh"
source "$BASHUNIT_ROOT_DIR/src/runner.sh"
source "$BASHUNIT_ROOT_DIR/src/bashunit.sh"
source "$BASHUNIT_ROOT_DIR/src/main.sh"
Expand All @@ -31,6 +35,9 @@ _ASSERT_FN_VALUES=()
_FILTER=""
_ARGS=()

check_os::init
clock::init

while [[ $# -gt 0 ]]; do
argument="$1"
case $argument in
Expand Down Expand Up @@ -59,6 +66,12 @@ while [[ $# -gt 0 ]]; do
-S|--stop-on-failure)
export BASHUNIT_STOP_ON_FAILURE=true
;;
-p|--parallel)
export BASHUNIT_PARALLEL_RUN=true
;;
--no-parallel)
export BASHUNIT_PARALLEL_RUN=false
;;
-e|--env|--load)
# shellcheck disable=SC1090
source "$2"
Expand Down
Loading

0 comments on commit 0b912df

Please sign in to comment.