Skip to content

Add support for Alpine (Linux Distro) #331

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

Merged
merged 34 commits into from
Sep 22, 2024
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
58de4e2
fix: add test automation for Alpine
skinner-m-c Sep 16, 2024
8424d5e
Update .github/CONTRIBUTING.md
Chemaclass Sep 21, 2024
70499d1
docs: improve BASHUNIT_LOAD_FILE documentation
Chemaclass Sep 18, 2024
0055475
feat: add optional second arg to --debug option
Chemaclass Sep 21, 2024
a8ad97e
docs: upgrade changelog
Chemaclass Sep 21, 2024
c8f8848
refactor: rename runtime_in_milliseconds to total_runtime_in_millisec…
Chemaclass Sep 21, 2024
673d1bc
refactor: move duration logic from logger to runner test scope
Chemaclass Sep 21, 2024
291fdb7
feat: add str::rpad()
Chemaclass Sep 21, 2024
5b79121
feat: add time per test if BASHUNIT_SHOW_EXECUTION_TIME=true
Chemaclass Sep 21, 2024
50061f8
fix: static-analysis
Chemaclass Sep 21, 2024
878a55b
refactor: simplify total tests display
Chemaclass Sep 21, 2024
ae2e529
fix: str::rpad()
Chemaclass Sep 21, 2024
4c05947
fix: TERMINAL_WIDTH when TERM is not define, use a default
Chemaclass Sep 21, 2024
dde0369
docs: upgrade changelog
Chemaclass Sep 21, 2024
cc23fa1
test: print_successful_test_output time with and without args
Chemaclass Sep 21, 2024
9874c17
fix: load check_os before clock
Chemaclass Sep 21, 2024
1d66eaf
fix: TERMINAL_WIDTH using stty instead of tput to get the cols
Chemaclass Sep 21, 2024
e70da26
Merge branch 'main' into fix/add-alpine-test-automation
Chemaclass Sep 21, 2024
194853f
feat: distinguish Linux - Ubuntu,Alpine,Other
Chemaclass Sep 21, 2024
cab23f7
fix: filtered_lines in check_duplicate_functions()
Chemaclass Sep 21, 2024
0cd4af1
refactor: _OS split into _OS and _DISTRO
Chemaclass Sep 21, 2024
f6be730
refactor: remove unnecesary test
Chemaclass Sep 21, 2024
9463fb7
test: ignore directory tests for Alpine
Chemaclass Sep 21, 2024
dec200e
fix: alpine compabible filtered_lines grep on check_duplicate_functions
Chemaclass Sep 21, 2024
e4fe41c
fix: add missing deps on teset-alpine make command
Chemaclass Sep 21, 2024
07d44bc
fix: time on linux alpine
Chemaclass Sep 21, 2024
939abfb
docs: update changelog
Chemaclass Sep 21, 2024
c5be076
fix: time per test adding perl to test-alpine
Chemaclass Sep 21, 2024
f144546
refactor: clean ups
Chemaclass Sep 21, 2024
23763f1
docs: testing different OS in contributing
Chemaclass Sep 22, 2024
ee31360
test: add alpine-latest to CI strategy matrix
Chemaclass Sep 22, 2024
9e6d625
fix: CI alpine tests in a separate job
Chemaclass Sep 22, 2024
5426cc5
refactor: remove BASHUNIT_ALPINE_TEST_VERSION from .env.example
Chemaclass Sep 22, 2024
141b208
refactor: make test/alpine for local and CI test alpine with custom d…
Chemaclass Sep 22, 2024
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
27 changes: 27 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -67,6 +67,33 @@ this will require to have installed [fswatcher](https://github.com/emcrisostomo/
make test/watch
```

### Testing different OS

- Linux
- Ubuntu latest
- Alpine latest
- Windows latest
- MacOS latest

#### Docker

##### Ubuntu latest

```bash
docker run --rm -it -v "$(pwd)":/project -w /project ubuntu:latest \
sh -c "apt-get update && \
apt-get install -y bash make shellcheck git curl perl && make test"
```

##### Alpine latest

```bash
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"
```

## Coding Guidelines

### ShellCheck
21 changes: 19 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -19,10 +19,10 @@ jobs:
include:
- os: windows-latest
script_name: 'bash -c "./bashunit -e tests/globals.sh tests/**/*_test.sh"'
- os: ubuntu-latest
script_name: 'make test'
- os: macos-latest
script_name: 'make test'
- os: ubuntu-latest
script_name: 'make test'
steps:
- name: Checkout
uses: actions/checkout@v4
@@ -32,3 +32,20 @@ jobs:
- name: Run Tests
run: ${{ matrix.script_name }}

alpine:
name: "Run tests on alpine-latest"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- 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 && \
adduser -D builder && \
chown -R builder /project && \
su - builder -c 'cd /project; make test';"
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Changelog

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

- Add optional file-path as 2nd arg to `--debug` option
- Add runtime per test
- Simplify total tests display on header
- Rename `BASHUNIT_TESTS_ENV` to `BASHUNIT_LOAD_FILE`
- Added support for Alpine (Linux Distro)
- Added optional file-path as 2nd arg to `--debug` option
- Added runtime per test
- Simplified total tests display on header
- Renamed `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

5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -43,6 +43,7 @@ help:
@echo " test Run the tests"
@echo " test/list List all tests under the tests directory"
@echo " test/watch Automatically run tests every second"
@echo " test/alpine Run the tests in a Linux/Alpine:latest image"
@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"
@@ -67,6 +68,10 @@ 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)

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

env/example:
@echo "Copying variables without the values from .env into .env.example"
@sed 's/=.*/=/' .env > .env.example
2 changes: 1 addition & 1 deletion src/assert.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

function fail() {
local message=$1
local message="${1:-${FUNCNAME[1]}}"

local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
13 changes: 12 additions & 1 deletion src/check_os.sh
Original file line number Diff line number Diff line change
@@ -2,11 +2,22 @@

# shellcheck disable=SC2034
_OS="Unknown"
_DISTRO="Unknown"

if [[ "$(uname)" == "Linux" ]]; then
_OS="Linux"
if command -v apt > /dev/null; then
_DISTRO="Ubuntu"
elif command -v apk > /dev/null; then
_DISTRO="Alpine"
else
_DISTRO="Other"
fi
elif [[ "$(uname)" == "Darwin" ]]; then
_OS="OSX"
elif [[ $(uname) == *"MINGW"* ]]; then
elif [[ "$(uname)" == *"MINGW"* ]]; then
_OS="Windows"
fi

export _OS
export _DISTRO
2 changes: 1 addition & 1 deletion src/clock.sh
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

function clock::now() {
if perl -MTime::HiRes -e "" > /dev/null 2>&1; then
perl -MTime::HiRes -e 'printf("%.0f\n",Time::HiRes::time()*1000)'
perl -MTime::HiRes -e 'printf("%.0f\n", Time::HiRes::time() * 1000)'
elif [[ "${_OS:-}" != "OSX" ]]; then
date +%s%N
else
25 changes: 24 additions & 1 deletion src/default_env_config.sh
Original file line number Diff line number Diff line change
@@ -14,4 +14,27 @@ _DEFAULT_BASHUNIT_LOAD_FILE=
_DEFAULT_TERMINAL_WIDTH=150

CAT="$(which cat)"
TERMINAL_WIDTH=${TERM:+$(tput cols)} || TERMINAL_WIDTH=$_DEFAULT_TERMINAL_WIDTH

function find_terminal_width() {
if [[ -n "$TERM" && $(command -v tput) ]]; then
_cols=$(tput cols 2>/dev/null) || _cols=""

# If tput fails, fallback to stty if available
if [[ -z "$_cols" ]] && command -v stty > /dev/null; then
_cols=$(stty size | cut -d' ' -f2)
fi
else
# Fallback to stty if TERM is not set and stty is available
if command -v stty > /dev/null; then
_cols=$(stty size | cut -d' ' -f2)
fi
fi

if [[ -z "$_cols" ]]; then
_cols=$_DEFAULT_TERMINAL_WIDTH
fi

echo "$_cols"
}

TERMINAL_WIDTH="$(find_terminal_width)"
8 changes: 3 additions & 5 deletions src/helpers.sh
Original file line number Diff line number Diff line change
@@ -23,20 +23,18 @@ function helper::normalize_test_function_name() {
echo "$result"
}


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

local filtered_lines
filtered_lines=$(grep -E '^\s*(function)?\s*test[a-zA-Z_][a-zA-Z_0-9]*\s*\(\)?\s*{' "$script")
filtered_lines=$(grep -E '^[[:space:]]*(function[[:space:]]+)?test[a-zA-Z_][a-zA-Z0-9_]*\s*\(\)\s*\{' "$script")

local function_names
function_names=$(echo "$filtered_lines" | awk '{gsub(/\(|\)/, ""); print $2}')

local sorted_names
sorted_names=$(echo "$function_names" | sort)

local duplicates
duplicates=$(echo "$sorted_names" | uniq -d)
duplicates=$(echo "$function_names" | sort | uniq -d)
if [ -n "$duplicates" ]; then
state::set_duplicated_functions_merged "$script" "$duplicates"
return 1
1 change: 1 addition & 0 deletions tests/acceptance/fixtures/.env.default
Original file line number Diff line number Diff line change
@@ -6,3 +6,4 @@ BASHUNIT_SHOW_EXECUTION_TIME=false
BASHUNIT_DEFAULT_PATH=
BASHUNIT_LOG_JUNIT=
BASHUNIT_REPORT_HTML=
BASHUNIT_LOAD_FILE=
1 change: 1 addition & 0 deletions tests/acceptance/install_test.sh
Original file line number Diff line number Diff line change
@@ -56,6 +56,7 @@ function test_install_downloads_the_given_version() {

function test_install_downloads_the_non_stable_beta_version() {
mock date echo "2023-11-13"
mock tput echo ""
local installed_bashunit="./deps/bashunit"
local output

36 changes: 0 additions & 36 deletions tests/functional/provider_test.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
#!/bin/bash
set -euo pipefail

_TEST_GET_DATA_FROM_PROVIDER_ITERATION_FILE=""

function set_up_before_script() {
_TEST_GET_DATA_FROM_PROVIDER_ITERATION_FILE=$(mktemp)
echo 0 > "$_TEST_GET_DATA_FROM_PROVIDER_ITERATION_FILE"
}

function tear_down_after_script() {
rm "$_TEST_GET_DATA_FROM_PROVIDER_ITERATION_FILE"
}

function set_up() {
_GLOBAL="aa-bb"
}
@@ -29,31 +18,6 @@ function provide_multiples_values() {
echo "aa" "bb"
}

# data_provider provide_single_values
function test_single_values_from_data_provider() {
local current_data="$1"
local current_iteration=0

echo "$(awk 'BEGIN{FS=OFS=""} {$1++} {print $1}' "$_TEST_GET_DATA_FROM_PROVIDER_ITERATION_FILE")"\
> "$_TEST_GET_DATA_FROM_PROVIDER_ITERATION_FILE"
current_iteration=$(cat "$_TEST_GET_DATA_FROM_PROVIDER_ITERATION_FILE")

case $current_iteration in
1)
assert_same "one" "$current_data"
;;
2)
assert_same "two" "$current_data"
;;
3)
assert_same "three" "$current_data"
;;
*)
fail
;;
esac
}

function provide_single_values() {
echo "one"
echo "two"
12 changes: 11 additions & 1 deletion tests/unit/clock_test.sh
Original file line number Diff line number Diff line change
@@ -20,13 +20,23 @@ function test_now_with_perl() {
assert_same "1720705883457" "$(clock::now)"
}

function test_now_on_linux_without_perl() {
function test_now_on_linux_unknown() {
export _OS="Linux"
export _DISTRO="Unknown"
mock perl mock_non_existing_fn
mock date echo "1720705883457"

assert_same "1720705883457" "$(clock::now)"
}

function test_now_on_linux_alpine() {
export _OS="Linux"
export _DISTRO="Alpine"
mock perl echo "1720705883457"

assert_same "1720705883457" "$(clock::now)"
}

function test_now_on_windows_without_perl() {
export _OS="Windows"
mock perl mock_non_existing_fn
12 changes: 6 additions & 6 deletions tests/unit/directory_test.sh
Original file line number Diff line number Diff line change
@@ -127,7 +127,7 @@ function test_unsuccessful_assert_is_directory_readable_when_a_file_is_given() {
}

function test_unsuccessful_assert_is_directory_readable_without_execution_permission() {
if [[ "$_OS" == "Windows" ]]; then
if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then
Copy link
Member

@Chemaclass Chemaclass Sep 22, 2024 β€’

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got these tests/unit/directory_test.sh failing on Alpine for some reason (directory permissions), but I don't know how to solve them now, so I'd better skip them for now, and work on them in another iteration/PR

# Running these tests without ` || $_DISTRO = "Alpine"`:
docker run --rm -it -v "$(pwd)":/project -w /project alpine:latest \
    sh -c  "apk add bash make shellcheck git curl perl && ./bashunit tests/unit/directory_test.sh"
Screenshot 2024-09-22 at 14 57 09

return
fi

@@ -145,7 +145,7 @@ function test_unsuccessful_assert_is_directory_readable_without_execution_permis
}

function test_unsuccessful_assert_is_directory_readable_without_read_permission() {
if [[ "$_OS" == "Windows" ]]; then
if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then
return
fi

@@ -163,7 +163,7 @@ function test_unsuccessful_assert_is_directory_readable_without_read_permission(
}

function test_successful_assert_is_directory_not_readable_without_read_permission() {
if [[ "$_OS" == "Windows" ]]; then
if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then
return
fi

@@ -177,7 +177,7 @@ function test_successful_assert_is_directory_not_readable_without_read_permissio
}

function test_successful_assert_is_directory_not_readable_without_execution_permission() {
if [[ "$_OS" == "Windows" ]]; then
if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then
return
fi

@@ -212,7 +212,7 @@ function test_successful_assert_is_directory_writable() {
}

function test_unsuccessful_assert_is_directory_writable() {
if [[ "$_OS" == "Windows" ]]; then
if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then
return
fi

@@ -240,7 +240,7 @@ function test_unsuccessful_assert_is_directory_writable_when_a_file_is_given() {
}

function test_successful_assert_is_directory_not_writable() {
if [[ "$_OS" == "Windows" ]]; then
if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then
return
fi

Loading