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

Rename logger.sh to reports.sh #361

Merged
merged 3 commits into from
Oct 12, 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 bashunit
Original file line number Diff line number Diff line change
Expand Up @@ -25,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 Down
4 changes: 2 additions & 2 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ function main::exec_tests() {
exit_code=$?

if [[ -n "$BASHUNIT_LOG_JUNIT" ]]; then
logger::generate_junit_xml "$BASHUNIT_LOG_JUNIT"
reports::generate_junit_xml "$BASHUNIT_LOG_JUNIT"
fi

if [[ -n "$BASHUNIT_REPORT_HTML" ]]; then
logger::generate_report_html "$BASHUNIT_REPORT_HTML"
reports::generate_report_html "$BASHUNIT_REPORT_HTML"
fi

cleanup_temp_files
Expand Down
113 changes: 52 additions & 61 deletions src/logger.sh → src/reports.sh
Original file line number Diff line number Diff line change
@@ -1,74 +1,70 @@
#!/bin/bash
# shellcheck disable=SC2155

TEST_FILES=()
TEST_NAMES=()
TEST_STATUSES=()
TEST_DURATIONS=()
TEST_ASSERTIONS=()
_REPORTS_TEST_FILES=()
_REPORTS_TEST_NAMES=()
_REPORTS_TEST_STATUSES=()
_REPORTS_TEST_DURATIONS=()
_REPORTS_TEST_ASSERTIONS=()

function logger::test_snapshot() {
logger::log "$1" "$2" "$3" "$4" "snapshot"
function reports::add_test_snapshot() {
reports::add_test "$1" "$2" "$3" "$4" "snapshot"
}

function logger::test_incomplete() {
logger::log "$1" "$2" "$3" "$4" "incomplete"
function reports::add_test_incomplete() {
reports::add_test "$1" "$2" "$3" "$4" "incomplete"
}

function logger::test_skipped() {
logger::log "$1" "$2" "$3" "$4" "skipped"
function reports::add_test_skipped() {
reports::add_test "$1" "$2" "$3" "$4" "skipped"
}

function logger::test_passed() {
logger::log "$1" "$2" "$3" "$4" "passed"
function reports::add_test_passed() {
reports::add_test "$1" "$2" "$3" "$4" "passed"
}

function logger::test_failed() {
logger::log "$1" "$2" "$3" "$4" "failed"
function reports::add_test_failed() {
reports::add_test "$1" "$2" "$3" "$4" "failed"
}

function logger::log() {
function reports::add_test() {
local file="$1"
local test_name="$2"
local duration="$3"
local assertions="$4"
local status="$5"

TEST_FILES+=("$file")
TEST_NAMES+=("$test_name")
TEST_STATUSES+=("$status")
TEST_ASSERTIONS+=("$assertions")
TEST_DURATIONS+=("$duration")
_REPORTS_TEST_FILES+=("$file")
_REPORTS_TEST_NAMES+=("$test_name")
_REPORTS_TEST_STATUSES+=("$status")
_REPORTS_TEST_ASSERTIONS+=("$assertions")
_REPORTS_TEST_DURATIONS+=("$duration")
}

function logger::generate_junit_xml() {
function reports::generate_junit_xml() {
local output_file="$1"
local test_passed
test_passed=$(state::get_tests_passed)
local tests_skipped
tests_skipped=$(state::get_tests_skipped)
local tests_incomplete
tests_incomplete=$(state::get_tests_incomplete)
local tests_snapshot
tests_snapshot=$(state::get_tests_snapshot)
local tests_failed
tests_failed=$(state::get_tests_failed)
local time
time=$(clock::total_runtime_in_milliseconds)

local test_passed=$(state::get_tests_passed)
local tests_skipped=$(state::get_tests_skipped)
local tests_incomplete=$(state::get_tests_incomplete)
local tests_snapshot=$(state::get_tests_snapshot)
local tests_failed=$(state::get_tests_failed)
local time=$(clock::total_runtime_in_milliseconds)

{
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
echo "<testsuites>"
echo " <testsuite name=\"bashunit\" tests=\"${#TEST_NAMES[@]}\""
echo " <testsuite name=\"bashunit\" tests=\"${#_REPORTS_TEST_NAMES[@]}\""
echo " passed=\"$test_passed\" failures=\"$tests_failed\" incomplete=\"$tests_incomplete\""
echo " skipped=\"$tests_skipped\" snapshot=\"$tests_snapshot\""
echo " time=\"$time\">"

for i in "${!TEST_NAMES[@]}"; do
local file="${TEST_FILES[$i]}"
local name="${TEST_NAMES[$i]}"
local assertions="${TEST_ASSERTIONS[$i]}"
local status="${TEST_STATUSES[$i]}"
local test_time="${TEST_DURATIONS[$i]}"
for i in "${!_REPORTS_TEST_NAMES[@]}"; do
local file="${_REPORTS_TEST_FILES[$i]}"
local name="${_REPORTS_TEST_NAMES[$i]}"
local assertions="${_REPORTS_TEST_ASSERTIONS[$i]}"
local status="${_REPORTS_TEST_STATUSES[$i]}"
local test_time="${_REPORTS_TEST_DURATIONS[$i]}"

echo " <testcase file=\"$file\""
echo " name=\"$name\""
Expand All @@ -83,31 +79,26 @@ function logger::generate_junit_xml() {
} > "$output_file"
}

function logger::generate_report_html() {
function reports::generate_report_html() {
local output_file="$1"
local test_passed
test_passed=$(state::get_tests_passed)
local tests_skipped
tests_skipped=$(state::get_tests_skipped)
local tests_incomplete
tests_incomplete=$(state::get_tests_incomplete)
local tests_snapshot
tests_snapshot=$(state::get_tests_snapshot)
local tests_failed
tests_failed=$(state::get_tests_failed)
local time
time=$(clock::total_runtime_in_milliseconds)

local test_passed=$(state::get_tests_passed)
local tests_skipped=$(state::get_tests_skipped)
local tests_incomplete=$(state::get_tests_incomplete)
local tests_snapshot=$(state::get_tests_snapshot)
local tests_failed=$(state::get_tests_failed)
local time=$(clock::total_runtime_in_milliseconds)

# Temporary file to store test cases by file
local temp_file="temp_test_cases.txt"

# Collect test cases by file
: > "$temp_file" # Clear temp file if it exists
for i in "${!TEST_NAMES[@]}"; do
local file="${TEST_FILES[$i]}"
local name="${TEST_NAMES[$i]}"
local status="${TEST_STATUSES[$i]}"
local test_time="${TEST_DURATIONS[$i]}"
for i in "${!_REPORTS_TEST_NAMES[@]}"; do
local file="${_REPORTS_TEST_FILES[$i]}"
local name="${_REPORTS_TEST_NAMES[$i]}"
local status="${_REPORTS_TEST_STATUSES[$i]}"
local test_time="${_REPORTS_TEST_DURATIONS[$i]}"
local test_case="$file|$name|$status|$test_time"

echo "$test_case" >> "$temp_file"
Expand Down Expand Up @@ -148,13 +139,13 @@ function logger::generate_report_html() {
echo " </thead>"
echo " <tbody>"
echo " <tr>"
echo " <td>${#TEST_NAMES[@]}</td>"
echo " <td>${#_REPORTS_TEST_NAMES[@]}</td>"
echo " <td>$test_passed</td>"
echo " <td>$tests_failed</td>"
echo " <td>$tests_incomplete</td>"
echo " <td>$tests_skipped</td>"
echo " <td>$tests_snapshot</td>"
echo " <td>${time}</td>"
echo " <td>$time</td>"
echo " </tr>"
echo " </tbody>"
echo " </table>"
Expand Down
12 changes: 6 additions & 6 deletions src/runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,14 @@ function runner::run_test() {
if [[ -n $runtime_error ]]; then
state::add_tests_failed
console_results::print_error_test "$function_name" "$runtime_error"
logger::test_failed "$test_file" "$function_name" "$duration" "$total_assertions"
reports::add_test_failed "$test_file" "$function_name" "$duration" "$total_assertions"
runner::write_failure_result_output "$test_file" "$runtime_error"
return
fi

if [[ "$current_assertions_failed" != "$(state::get_assertions_failed)" ]]; then
state::add_tests_failed
logger::test_failed "$test_file" "$function_name" "$duration" "$total_assertions"
reports::add_test_failed "$test_file" "$function_name" "$duration" "$total_assertions"
runner::write_failure_result_output "$test_file" "$subshell_output"
if env::is_stop_on_failure_enabled; then
exit 1
Expand All @@ -223,27 +223,27 @@ function runner::run_test() {
if [[ "$current_assertions_snapshot" != "$(state::get_assertions_snapshot)" ]]; then
state::add_tests_snapshot
console_results::print_snapshot_test "$function_name"
logger::test_snapshot "$test_file" "$function_name" "$duration" "$total_assertions"
reports::add_test_snapshot "$test_file" "$function_name" "$duration" "$total_assertions"
return
fi

if [[ "$current_assertions_incomplete" != "$(state::get_assertions_incomplete)" ]]; then
state::add_tests_incomplete
logger::test_incomplete "$test_file" "$function_name" "$duration" "$total_assertions"
reports::add_test_incomplete "$test_file" "$function_name" "$duration" "$total_assertions"
return
fi

if [[ "$current_assertions_skipped" != "$(state::get_assertions_skipped)" ]]; then
state::add_tests_skipped
logger::test_skipped "$test_file" "$function_name" "$duration" "$total_assertions"
reports::add_test_skipped "$test_file" "$function_name" "$duration" "$total_assertions"
return
fi

local label="$(helper::normalize_test_function_name "$function_name")"

console_results::print_successful_test "${label}" "$duration" "$@"
state::add_tests_passed
logger::test_passed "$test_file" "$function_name" "$duration" "$total_assertions"
reports::add_test_passed "$test_file" "$function_name" "$duration" "$total_assertions"
}

function runner::parse_result() {
Expand Down
Loading