Skip to content

Commit

Permalink
refactor: rename log_junit.sh to reports.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
Chemaclass committed Oct 12, 2024
1 parent f553d59 commit b15838a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 47 deletions.
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/log_junit.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
log_junit::generate_junit_xml "$BASHUNIT_LOG_JUNIT"
reports::generate_junit_xml "$BASHUNIT_LOG_JUNIT"
fi

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

cleanup_temp_files
Expand Down
76 changes: 38 additions & 38 deletions src/log_junit.sh → src/reports.sh
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
#!/bin/bash

TEST_FILES=()
TEST_NAMES=()
TEST_STATUSES=()
TEST_DURATIONS=()
TEST_ASSERTIONS=()

function log_junit::test_snapshot() {
log_junit::log "$1" "$2" "$3" "$4" "snapshot"
_REPORTS_TEST_FILES=()
_REPORTS_TEST_NAMES=()
_REPORTS_TEST_STATUSES=()
_REPORTS_TEST_DURATIONS=()
_REPORTS_TEST_ASSERTIONS=()

function reports::add_snapshot() {
reports::log_test "$1" "$2" "$3" "$4" "snapshot"
}

function log_junit::test_incomplete() {
log_junit::log "$1" "$2" "$3" "$4" "incomplete"
function reports::add_incomplete() {
reports::log_test "$1" "$2" "$3" "$4" "incomplete"
}

function log_junit::test_skipped() {
log_junit::log "$1" "$2" "$3" "$4" "skipped"
function reports::add_skipped() {
reports::log_test "$1" "$2" "$3" "$4" "skipped"
}

function log_junit::test_passed() {
log_junit::log "$1" "$2" "$3" "$4" "passed"
function reports::add_passed() {
reports::log_test "$1" "$2" "$3" "$4" "passed"
}

function log_junit::test_failed() {
log_junit::log "$1" "$2" "$3" "$4" "failed"
function reports::add_failed() {
reports::log_test "$1" "$2" "$3" "$4" "failed"
}

function log_junit::log() {
function reports::log_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 log_junit::generate_junit_xml() {
function reports::generate_junit_xml() {
local output_file="$1"
local test_passed
test_passed=$(state::get_tests_passed)
Expand All @@ -58,17 +58,17 @@ function log_junit::generate_junit_xml() {
{
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,7 +83,7 @@ function log_junit::generate_junit_xml() {
} > "$output_file"
}

function log_junit::generate_report_html() {
function reports::generate_report_html() {
local output_file="$1"
local test_passed
test_passed=$(state::get_tests_passed)
Expand All @@ -103,11 +103,11 @@ function log_junit::generate_report_html() {

# 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 +148,13 @@ function log_junit::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"
log_junit::test_failed "$test_file" "$function_name" "$duration" "$total_assertions"
reports::add_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
log_junit::test_failed "$test_file" "$function_name" "$duration" "$total_assertions"
reports::add_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"
log_junit::test_snapshot "$test_file" "$function_name" "$duration" "$total_assertions"
reports::add_snapshot "$test_file" "$function_name" "$duration" "$total_assertions"
return
fi

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

if [[ "$current_assertions_skipped" != "$(state::get_assertions_skipped)" ]]; then
state::add_tests_skipped
log_junit::test_skipped "$test_file" "$function_name" "$duration" "$total_assertions"
reports::add_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
log_junit::test_passed "$test_file" "$function_name" "$duration" "$total_assertions"
reports::add_passed "$test_file" "$function_name" "$duration" "$total_assertions"
}

function runner::parse_result() {
Expand Down

0 comments on commit b15838a

Please sign in to comment.