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

Log info into file #348

Merged
merged 6 commits into from
Oct 3, 2024
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -18,3 +18,4 @@ report.html

# internal
local/
out.log
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@

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

- Added `assert_file_contains` and `assert_file_not_contains`
- Added `BASHUNIT_LOG_PATH`
- Added global util functions
- current_dir
- current_filename
50 changes: 50 additions & 0 deletions docs/assertions.md
Original file line number Diff line number Diff line change
@@ -456,6 +456,31 @@ function test_failure() {
```
:::

## assert_file_contains
> `assert_file_contains "file" "search"`

Reports an error if `file` does not contains the search string.

[assert_file_not_contains](#assert-file-not-contains) is the inverse of this assertion and takes the same arguments.

::: code-group
```bash [Example]
function test_success() {
local file="/tmp/file-path.txt"
echo -e "original content" > "$file"

assert_file_contains "$file" "content"
}

function test_failure() {
local file="/tmp/file-path.txt"
echo -e "original content" > "$file"

assert_file_contains "$file" "non existing"
}
```
:::

## assert_is_file
> `assert_is_file "file"`

@@ -826,6 +851,31 @@ function test_failed() {
```
:::

## assert_file_not_contains
> `assert_file_not_contains "file" "search"`

Reports an error if `file` contains the search string.

[assert_file_contains](#assert-file-contains) is the inverse of this assertion and takes the same arguments.

::: code-group
```bash [Example]
function test_success() {
local file="/tmp/file-path.txt"
echo -e "original content" > "$file"

assert_file_not_contains "$file" "non existing"
}

function test_failure() {
local file="/tmp/file-path.txt"
echo -e "original content" > "$file"

assert_file_not_contains "$file" "content"
}
```
:::

## assert_directory_not_exists
> `assert_directory_not_exists "directory"`

22 changes: 22 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
@@ -193,6 +193,28 @@ BASHUNIT_LOAD_FILE="tests/globals.sh"
```
:::

## Log path

> `BASHUNIT_LOG_PATH=file`

> See: [Globals > log](/globals#log)

::: code-group
```bash [Setup]
BASHUNIT_LOG_PATH="out.log"
```
```bash [Usage]
log "I am tracing something..."
log "error" "an" "error" "message"
log "warning" "different log level messages!"
```
```bash [Output: out.log]
2024-10-03 21:27:23 [INFO]: I am tracing something...
2024-10-03 21:27:23 [ERROR]: an error message
2024-10-03 21:27:23 [WARNING]: different log level messages!
```
:::

<script setup>
import pkg from '../package.json'
</script>
23 changes: 9 additions & 14 deletions docs/globals.md
Original file line number Diff line number Diff line change
@@ -73,22 +73,17 @@ function test_globals_temp_dir() {
}
```

## log_info
## log

```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")"
}
```
Write into the `BASHUNIT_LOG_PATH` a log message.

## log_error
> See: [Log path](/configuration#log-path)

```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)"
}
log "hello" "world" # default level: info
log "info" "hello" "world"
log "debug" "hello" "world"
log "warning" "hello" "world"
log "critical" "hello" "world"
log "error" "hello" "world"
```
32 changes: 32 additions & 0 deletions src/assert_files.sh
Original file line number Diff line number Diff line change
@@ -85,3 +85,35 @@ function assert_files_not_equals() {

state::add_assertions_passed
}

function assert_file_contains() {
local file="$1"
local string="$2"

if ! grep -F -q "$string" "$file"; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed

console_results::print_failed_test "${label}" "${file}" "to contain" "${string}"
return
fi

state::add_assertions_passed
}

function assert_file_not_contains() {
local file="$1"
local string="$2"

if grep -q "$string" "$file"; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed

console_results::print_failed_test "${label}" "${file}" "to not contain" "${string}"
return
fi

state::add_assertions_passed
}
26 changes: 15 additions & 11 deletions src/env.sh
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
#!/bin/bash

# shellcheck disable=SC2034
_DEFAULT_PARALLEL_RUN=false
_DEFAULT_SHOW_HEADER=true
_DEFAULT_HEADER_ASCII_ART=false
_DEFAULT_SIMPLE_OUTPUT=false
_DEFAULT_STOP_ON_FAILURE=false
_DEFAULT_SHOW_EXECUTION_TIME=true
_DEFAULT_DEFAULT_PATH=
_DEFAULT_LOG_JUNIT=
_DEFAULT_REPORT_HTML=
_DEFAULT_BASHUNIT_LOAD_FILE=
_DEFAULT_TERMINAL_WIDTH=100

set -o allexport
# shellcheck source=/dev/null
[[ -f ".env" ]] && source .env set
set +o allexport

_DEFAULT_DEFAULT_PATH=
_DEFAULT_LOG_JUNIT=
_DEFAULT_LOG_PATH=out.log
_DEFAULT_REPORT_HTML=
_DEFAULT_BASHUNIT_LOAD_FILE=
_DEFAULT_TERMINAL_WIDTH=100

: "${BASHUNIT_DEFAULT_PATH:=${DEFAULT_PATH:=$_DEFAULT_DEFAULT_PATH}}"
: "${BASHUNIT_LOG_JUNIT:=${LOG_JUNIT:=$_DEFAULT_LOG_JUNIT}}"
: "${BASHUNIT_LOG_PATH:=${LOG_PATH:=$_DEFAULT_LOG_PATH}}"
: "${BASHUNIT_REPORT_HTML:=${REPORT_HTML:=$_DEFAULT_REPORT_HTML}}"
: "${BASHUNIT_LOAD_FILE:=${LOAD_FILE:=$_DEFAULT_BASHUNIT_LOAD_FILE}}"

# Booleans
_DEFAULT_PARALLEL_RUN=false
_DEFAULT_SHOW_HEADER=true
_DEFAULT_HEADER_ASCII_ART=false
_DEFAULT_SIMPLE_OUTPUT=false
_DEFAULT_STOP_ON_FAILURE=false
_DEFAULT_SHOW_EXECUTION_TIME=true

: "${BASHUNIT_PARALLEL_RUN:=${PARALLEL_RUN:=$_DEFAULT_PARALLEL_RUN}}"
: "${BASHUNIT_SHOW_HEADER:=${SHOW_HEADER:=$_DEFAULT_SHOW_HEADER}}"
: "${BASHUNIT_HEADER_ASCII_ART:=${HEADER_ASCII_ART:=$_DEFAULT_HEADER_ASCII_ART}}"
23 changes: 15 additions & 8 deletions src/globals.sh
Original file line number Diff line number Diff line change
@@ -42,12 +42,19 @@ 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
# shellcheck disable=SC2145
function log() {
local level="$1"
shift

case "$level" in
info|INFO) level="INFO" ;;
debug|DEBUG) level="DEBUG" ;;
warning|WARNING) level="WARNING" ;;
critical|CRITICAL) level="CRITICAL" ;;
error|ERROR) level="ERROR" ;;
*) set -- "$level $@"; level="INFO" ;;
esac

echo "$(current_timestamp) [$level]: $@" >> "$BASHUNIT_LOG_PATH"
}
42 changes: 42 additions & 0 deletions tests/unit/file_test.sh
Original file line number Diff line number Diff line change
@@ -147,3 +147,45 @@ function test_fails_assert_files_not_equals() {
rm "$expected"
rm "$actual"
}

function test_successful_assert_file_contains() {
local file="/tmp/test_successful_assert_file_contains"
echo -e "original content" > "$file"

assert_successful_code "$(assert_file_contains "$file" "original content")"

rm "$file"
}

function test_fails_assert_file_contains() {
local file="/tmp/test_fail_assert_file_contains"
echo -e "original content" > "$file"

assert_contains \
"$(console_results::print_failed_test\
"Fails assert file contains" "${file}" "to contain" "non-existing-str")" \
"$(assert_file_contains "$file" "non-existing-str")"

rm "$file"
}

function test_successful_assert_file_not_contains() {
local file="/tmp/test_successful_assert_file_not_contains"
echo -e "original content" > "$file"

assert_successful_code "$(assert_file_not_contains "$file" "non-existing-str")"

rm "$file"
}

function test_fails_assert_file_not_contains() {
local file="/tmp/test_fails_assert_file_not_contains"
echo -e "original content" > "$file"

assert_contains \
"$(console_results::print_failed_test\
"Fails assert file not contains" "${file}" "to not contain" "original content")" \
"$(assert_file_not_contains "$file" "original content")"

rm "$file"
}
49 changes: 41 additions & 8 deletions tests/unit/globals_test.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
#!/bin/bash
set -euo pipefail

function set_up_before_script() {
BASHUNIT_LOG_PATH=$(temp_file)
export BASHUNIT_LOG_PATH
}

function tear_down_after_script() {
rm "$BASHUNIT_LOG_PATH"
}

function test_globals_current_dir() {
assert_same "tests/unit" "$(current_dir)"
}
@@ -47,14 +56,38 @@ function test_globals_temp_dir() {
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_level_error() {
log "error" "hello," "error"

assert_file_contains "$BASHUNIT_LOG_PATH" "[ERROR]: hello, error"
}

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)"
function test_globals_log_level_warning() {
log "warning" "hello," "warning"

assert_file_contains "$BASHUNIT_LOG_PATH" "[WARNING]: hello, warning"
}

function test_globals_log_level_debug() {
log "debug" "hello," "debug"

assert_file_contains "$BASHUNIT_LOG_PATH" "[DEBUG]: hello, debug"
}

function test_globals_log_level_critical() {
log "critical" "hello," "critical"

assert_file_contains "$BASHUNIT_LOG_PATH" "[CRITICAL]: hello, critical"
}

function test_globals_log_level_info() {
log "info" "hello," "info"

assert_file_contains "$BASHUNIT_LOG_PATH" "[INFO]: hello, info"
}

function test_globals_log_level_default() {
log "hello," "info"

assert_file_contains "$BASHUNIT_LOG_PATH" "[INFO]: hello, info"
}
Loading