Skip to content

Commit

Permalink
feat: add logs on dev_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Chemaclass committed Oct 11, 2024
1 parent 0f452a8 commit 155e694
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion bashunit
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ 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/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/check_os.sh"
source "$BASHUNIT_ROOT_DIR/src/clock.sh"
source "$BASHUNIT_ROOT_DIR/src/state.sh"
source "$BASHUNIT_ROOT_DIR/src/colors.sh"
Expand Down
2 changes: 1 addition & 1 deletion src/env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function env::find_terminal_width() {
echo "${cols:-$_DEFAULT_TERMINAL_WIDTH}"
}

TEMP_DIR_PARALLEL_TEST_SUITE="/tmp/bashunit/parallel"
TEMP_DIR_PARALLEL_TEST_SUITE="/tmp/bashunit/parallel/${_OS:-Unknown}"
TERMINAL_WIDTH="$(env::find_terminal_width)"
FAILURES_OUTPUT_PATH=$(mktemp)
CAT="$(which cat)"
19 changes: 14 additions & 5 deletions src/runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ function runner::run_test() {
# Closes FD 3, which was used temporarily to hold the original stdout.
exec 3>&-

runner::parse_result "$function_name" "$test_execution_result" "$@"

local subshell_output=$(\
echo "$test_execution_result" |\
tail -n 1 |\
Expand All @@ -185,13 +183,15 @@ function runner::run_test() {
"division by 0" "cannot allocate memory" "bad file descriptor" \
"segmentation fault" "illegal option" "argument list too long" \
"readonly variable" "missing keyword" "killed" \
"cannot execute binary file"; do
"cannot execute binary file" "invalid arithmetic operator"; do
if [[ "$runtime_output" == *"$error"* ]]; then
runtime_error=$(echo "${runtime_output#*: }" | tr -d '\n')
break
fi
done

runner::parse_result "$function_name" "$test_execution_result" "$@"

local total_assertions="$(state::calculate_total_assertions "$test_execution_result")"

local end_time=$(clock::now)
Expand Down Expand Up @@ -282,6 +282,15 @@ function runner::parse_result_parallel() {
count=$((count + 1))
done

if env::is_dev_mode_enabled; then
local test_id=$(\
echo "$execution_result" |\
tail -n 1 |\
sed -E -e 's/.*##TEST_ID=([a-zA-Z0-9]*)##.*/\1/g'\
)
log "debug" "[PARA] test_id:$test_id" "function_name:$function_name" "execution_result:$execution_result"
fi

echo "$execution_result" > "$unique_test_result_file"
}

Expand Down Expand Up @@ -324,9 +333,9 @@ function runner::parse_result_sync() {
local test_id=$(\
echo "$execution_result" |\
tail -n 1 |\
sed -E -e 's/.*##TEST_ID=([0-9]*)##.*/\1/g'\
sed -E -e 's/.*##TEST_ID=([a-zA-Z0-9]*)##.*/\1/g'\
)
log "$test_id" "$function_name" "$execution_result"
log "debug" "[SYNC] test_id:$test_id" "function_name:$function_name" "execution_result:$execution_result"
fi

((_ASSERTIONS_PASSED += assertions_passed)) || true
Expand Down
4 changes: 2 additions & 2 deletions src/state.sh
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function state::export_subshell_context() {
encoded_test_output=$(echo -n "$_TEST_OUTPUT" | base64)
fi

echo "##TEST_ID=$(random_str)\
echo "##TEST_ID=$(LC_ALL=C tr -dc A-Za-z0-9 </dev/urandom | head -c 8)\
##ASSERTIONS_FAILED=$_ASSERTIONS_FAILED\
##ASSERTIONS_PASSED=$_ASSERTIONS_PASSED\
##ASSERTIONS_SKIPPED=$_ASSERTIONS_SKIPPED\
Expand Down Expand Up @@ -196,7 +196,7 @@ function state::print_line() {
incomplete) char="${_COLOR_INCOMPLETE}I${_COLOR_DEFAULT}" ;;
snapshot) char="${_COLOR_SNAPSHOT}N${_COLOR_DEFAULT}" ;;
error) char="${_COLOR_FAILED}E${_COLOR_DEFAULT}" ;;
*) char="?" ;;
*) char="?" && log "warning" "unknown test type '$type'" ;;
esac

if env::is_parallel_run_enabled; then
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/state_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ function test_set_duplicated_functions_merged() {
}

function test_initialize_assertions_count() {
mock random_str echo "abc123"
mock tr echo "abc123"

local export_assertions_count
export_assertions_count=$(
Expand All @@ -261,7 +261,7 @@ function test_initialize_assertions_count() {
}

function test_export_assertions_count() {
mock random_str echo "abc123"
mock tr echo "abc123"

local export_assertions_count
export_assertions_count=$(
Expand Down

0 comments on commit 155e694

Please sign in to comment.