Skip to content

Commit 42e0e53

Browse files
authored
Merge pull request #281 from TypedDevs/feat/generate-report-html
Generate report html
2 parents 7d635c9 + 29799dd commit 42e0e53

20 files changed

+276
-42
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ STOP_ON_FAILURE=
55
SHOW_EXECUTION_TIME=
66
DEFAULT_PATH=
77
LOG_JUNIT=
8+
REPORT_HTML=

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ lib/bashunit
1414

1515
# logger
1616
log-junit.xml
17+
report.html

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Document how to verify the `sha256sum` of the final executable
88
- Enable display execution time on MacOS with `SHOW_EXECUTION_TIME`
99
- Add `-l|--log-junit <output.xml>` option
10+
- Add `-r|--report-html <report.html>` option
1011

1112
## [0.13.0](https://github.com/TypedDevs/bashunit/compare/0.12.0...0.13.0) - 2024-06-23
1213

bashunit

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ while [[ $# -gt 0 ]]; do
6262
shift
6363
shift
6464
;;
65+
-r|--report-html)
66+
REPORT_HTML="$2";
67+
shift
68+
shift
69+
;;
6570
--version)
6671
console_header::print_version
6772
trap '' EXIT && exit 0

docs/command-line.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Filters the tests to be run based on the `test name`.
7171
:::
7272

7373
## Logging
74-
s
74+
7575
> `bashunit -l|--log-junit log-junit.xml`
7676
7777
Create a report XML file that follows the JUnit XML format and contains information about the test results of your bashunit tests.
@@ -82,7 +82,6 @@ Create a report XML file that follows the JUnit XML format and contains informat
8282
```
8383
:::
8484

85-
8685
## Output
8786

8887
> `bashunit -s|--simple`
@@ -124,6 +123,18 @@ Running tests/functional/logic_test.sh
124123
```
125124
:::
126125

126+
## Report
127+
128+
> `bashunit -r|--report-html report.html`
129+
130+
Create a report HTML file that contains information about the test results of your bashunit tests.
131+
132+
::: code-group
133+
```bash [Example]
134+
./bashunit ./tests --report-html report.html
135+
```
136+
:::
137+
127138
## Stop on failure
128139

129140
> `bashunit -S|--stop-on-failure`

docs/configuration.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Running tests/functional/logic_test.sh
6161
SIMPLE_OUTPUT=false
6262
```
6363
:::
64+
6465
## Stop on failure
6566

6667
> `STOP_ON_FAILURE=true|false`
@@ -75,7 +76,7 @@ Similar as using `-S|--stop-on-failure` option on the [command line](/command-li
7576
>
7677
> `HEADER_ASCII_ART=true|false`
7778
78-
Specifies if you want to show the bashunit header. `true` by default.
79+
Specify if you want to show the bashunit header. `true` by default.
7980

8081
Additionally, you can use the env-var `HEADER_ASCII_ART` to display bashunit in ASCII. `false` by default.
8182

@@ -153,6 +154,18 @@ SHOW_EXECUTION_TIMER=false
153154
```
154155
:::
155156

157+
## Log JUnit
158+
159+
> `LOG_JUNIT=log-junit.xml`
160+
161+
Create a report XML file that follows the JUnit XML format and contains information about the test results of your bashunit tests.
162+
163+
## Report HTML
164+
165+
> `REPORT_HTML=report.html`
166+
167+
Create a report HTML file that contains information about the test results of your bashunit tests.
168+
156169
<script setup>
157170
import pkg from '../package.json'
158171
</script>

src/default_env_config.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ _DEFAULT_STOP_ON_FAILURE=false
99
_DEFAULT_SHOW_EXECUTION_TIME=true
1010
_DEFAULT_DEFAULT_PATH=
1111
_DEFAULT_LOG_JUNIT=
12+
_DEFAULT_REPORT_HTML=
1213
CAT="$(which cat)"

src/env_configuration.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,7 @@ fi
3636
if [[ -z "$LOG_JUNIT" ]]; then
3737
LOG_JUNIT=$_DEFAULT_LOG_JUNIT
3838
fi
39+
40+
if [[ -z "$REPORT_HTML" ]]; then
41+
REPORT_HTML=$_DEFAULT_REPORT_HTML
42+
fi

src/logger.sh

Lines changed: 132 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,41 @@
33
TEST_NAMES=()
44
TEST_STATUSES=()
55
TEST_DURATIONS=()
6-
TEST_ERRORS=()
76

87
function logger::test_snapshot() {
9-
logger::log "$1" "$2" "snapshot" "$3"
8+
logger::log "$1" "$2" "$3" "snapshot"
109
}
1110

1211
function logger::test_incomplete() {
13-
logger::log "$1" "$2" "incomplete" "$3"
12+
logger::log "$1" "$2" "$3" "incomplete"
1413
}
1514

1615
function logger::test_skipped() {
17-
logger::log "$1" "$2" "skipped" "$3"
16+
logger::log "$1" "$2" "$3" "skipped"
1817
}
1918

2019
function logger::test_passed() {
21-
logger::log "$1" "$2" "passed" "$3"
20+
logger::log "$1" "$2" "$3" "passed"
2221
}
2322

2423
function logger::test_failed() {
25-
logger::log "$1" "$2" "failed" "$3"
24+
logger::log "$1" "$2" "$3" "failed"
2625
}
2726

2827
function logger::log() {
29-
local test_name="$1"
30-
local start_time="$2"
31-
local status="$3"
32-
local error_msg="${4:-}"
28+
local file="$1"
29+
local test_name="$2"
30+
local start_time="$3"
31+
local status="$4"
3332

3433
local end_time
3534
end_time=$(clock::now)
3635
local duration=$((end_time - start_time))
3736

37+
TEST_FILES+=("$file")
3838
TEST_NAMES+=("$test_name")
3939
TEST_STATUSES+=("$status")
4040
TEST_DURATIONS+=("$duration")
41-
TEST_ERRORS+=("$error_msg")
4241
}
4342

4443
function logger::generate_junit_xml() {
@@ -64,19 +63,136 @@ function logger::generate_junit_xml() {
6463
echo " skipped=\"$tests_skipped\" snapshot=\"$tests_snapshot\">"
6564

6665
for i in "${!TEST_NAMES[@]}"; do
66+
local file="${TEST_FILES[$i]}"
6767
local name="${TEST_NAMES[$i]}"
6868
local status="${TEST_STATUSES[$i]}"
6969
local test_time="${TEST_DURATIONS[$i]}"
70-
local msg="${TEST_ERRORS[$i]}"
7170

72-
echo " <testcase name=\"$name\" time=\"$test_time\" status=\"$status\">"
73-
if [[ -n $msg ]]; then
74-
echo " <message>$msg<message/>"
75-
fi
71+
echo " <testcase file=\"$file\""
72+
echo " name=\"$name\""
73+
echo " status=\"$status\" time=\"$test_time\">"
7674
echo " </testcase>"
7775
done
7876

7977
echo " </testsuite>"
8078
echo "</testsuites>"
8179
} > "$output_file"
8280
}
81+
82+
function logger::generate_report_html() {
83+
local output_file="$1"
84+
local test_passed
85+
test_passed=$(state::get_tests_passed)
86+
local tests_skipped
87+
tests_skipped=$(state::get_tests_skipped)
88+
local tests_incomplete
89+
tests_incomplete=$(state::get_tests_incomplete)
90+
local tests_snapshot
91+
tests_snapshot=$(state::get_tests_snapshot)
92+
local tests_failed
93+
tests_failed=$(state::get_tests_failed)
94+
local time
95+
time=$(clock::runtime_in_milliseconds)
96+
97+
# Temporary file to store test cases by file
98+
local temp_file="temp_test_cases.txt"
99+
100+
# Collect test cases by file
101+
: > "$temp_file" # Clear temp file if it exists
102+
for i in "${!TEST_NAMES[@]}"; do
103+
local file="${TEST_FILES[$i]}"
104+
local name="${TEST_NAMES[$i]}"
105+
local status="${TEST_STATUSES[$i]}"
106+
local test_time="${TEST_DURATIONS[$i]}"
107+
local test_case="$file|$name|$status|$test_time"
108+
109+
echo "$test_case" >> "$temp_file"
110+
done
111+
112+
{
113+
echo "<!DOCTYPE html>"
114+
echo "<html lang=\"en\">"
115+
echo "<head>"
116+
echo " <meta charset=\"UTF-8\">"
117+
echo " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"
118+
echo " <title>Test Report</title>"
119+
echo " <style>"
120+
echo " body { font-family: Arial, sans-serif; }"
121+
echo " table { width: 100%; border-collapse: collapse; }"
122+
echo " th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }"
123+
echo " th { background-color: #f2f2f2; }"
124+
echo " .passed { background-color: #dff0d8; }"
125+
echo " .failed { background-color: #f2dede; }"
126+
echo " .skipped { background-color: #fcf8e3; }"
127+
echo " .incomplete { background-color: #d9edf7; }"
128+
echo " .snapshot { background-color: #dfe6e9; }"
129+
echo " </style>"
130+
echo "</head>"
131+
echo "<body>"
132+
echo " <h1>Test Report</h1>"
133+
echo " <table>"
134+
echo " <thead>"
135+
echo " <tr>"
136+
echo " <th>Total Tests</th>"
137+
echo " <th>Passed</th>"
138+
echo " <th>Failed</th>"
139+
echo " <th>Incomplete</th>"
140+
echo " <th>Skipped</th>"
141+
echo " <th>Snapshot</th>"
142+
echo " <th>Time (ms)</th>"
143+
echo " </tr>"
144+
echo " </thead>"
145+
echo " <tbody>"
146+
echo " <tr>"
147+
echo " <td>${#TEST_NAMES[@]}</td>"
148+
echo " <td>$test_passed</td>"
149+
echo " <td>$tests_failed</td>"
150+
echo " <td>$tests_incomplete</td>"
151+
echo " <td>$tests_skipped</td>"
152+
echo " <td>$tests_snapshot</td>"
153+
echo " <td>${time}</td>"
154+
echo " </tr>"
155+
echo " </tbody>"
156+
echo " </table>"
157+
echo " <p>Time: $time ms</p>"
158+
159+
# Read the temporary file and group by file
160+
local current_file=""
161+
while IFS='|' read -r file name status test_time; do
162+
if [ "$file" != "$current_file" ]; then
163+
if [ -n "$current_file" ]; then
164+
echo " </tbody>"
165+
echo " </table>"
166+
fi
167+
echo " <h2>File: $file</h2>"
168+
echo " <table>"
169+
echo " <thead>"
170+
echo " <tr>"
171+
echo " <th>Test Name</th>"
172+
echo " <th>Status</th>"
173+
echo " <th>Time (ms)</th>"
174+
echo " </tr>"
175+
echo " </thead>"
176+
echo " <tbody>"
177+
current_file="$file"
178+
fi
179+
echo " <tr class=\"$status\">"
180+
echo " <td>$name</td>"
181+
echo " <td>$status</td>"
182+
echo " <td>$test_time</td>"
183+
echo " </tr>"
184+
done < "$temp_file"
185+
186+
# Close the last table
187+
if [ -n "$current_file" ]; then
188+
echo " </tbody>"
189+
echo " </table>"
190+
fi
191+
192+
echo "</body>"
193+
echo "</html>"
194+
} > "$output_file"
195+
196+
# Clean up temporary file
197+
rm -f "$temp_file"
198+
}

src/main.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ function main::exec_tests() {
1313
logger::generate_junit_xml "$LOG_JUNIT"
1414
fi
1515

16+
if [[ -n "$REPORT_HTML" ]]; then
17+
logger::generate_report_html "$REPORT_HTML"
18+
fi
19+
1620
exit $exit_code
1721
}
1822

0 commit comments

Comments
 (0)