|
| 1 | +#!/usr/bin/env bash |
| 2 | +# --------------------------------------------------------------------------- |
| 3 | +# verify.sh – Verify that JUNIT.XSL preserves whitespace in failure messages |
| 4 | +# |
| 5 | +# Works on Linux, macOS, and Windows Subsystem for Linux (WSL). |
| 6 | +# |
| 7 | +# Usage: |
| 8 | +# cd products/eclipse-junit-tests/src/main/scripts/verify |
| 9 | +# ./verify.sh |
| 10 | +# |
| 11 | +# The script transforms sample-results.xml with the parent JUNIT.XSL stylesheet |
| 12 | +# and opens the resulting HTML file in a browser. |
| 13 | +# |
| 14 | +# What to look for in the output: |
| 15 | +# In the "Type" column of the testFormatAll01 row you should see: |
| 16 | +# |
| 17 | +# contents was not formatted. Actual: |
| 18 | +# package test1; |
| 19 | +# public class E1 { |
| 20 | +# public void foo( Object o ) { |
| 21 | +# String s = (String)o; <-- four spaces preserved |
| 22 | +# } |
| 23 | +# } |
| 24 | +# |
| 25 | +# WITHOUT the fix, the browser collapses all whitespace sequences to a single |
| 26 | +# space, making "String s = (String)o;" look like "String s = (String)o;" |
| 27 | +# and the multiline message appears on one line. |
| 28 | +# |
| 29 | +# Requirements (one of the following): |
| 30 | +# • xsltproc – sudo apt-get install xsltproc (Ubuntu/WSL) |
| 31 | +# brew install libxslt (macOS) |
| 32 | +# • python3 – python3 -m pip install lxml |
| 33 | +# --------------------------------------------------------------------------- |
| 34 | + |
| 35 | +set -euo pipefail |
| 36 | + |
| 37 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 38 | +XSL_FILE="$SCRIPT_DIR/../JUNIT.XSL" |
| 39 | +XML_FILE="$SCRIPT_DIR/sample-results.xml" |
| 40 | +OUT_FILE="$SCRIPT_DIR/report.html" |
| 41 | + |
| 42 | +# Resolve the stylesheet path to an absolute URI (required by xsltproc) |
| 43 | +XSL_ABS="$(cd "$(dirname "$XSL_FILE")" && pwd)/$(basename "$XSL_FILE")" |
| 44 | + |
| 45 | +echo "Input XML : $XML_FILE" |
| 46 | +echo "Stylesheet: $XSL_ABS" |
| 47 | +echo "Output : $OUT_FILE" |
| 48 | +echo "" |
| 49 | + |
| 50 | +# --------------------------------------------------------------------------- |
| 51 | +# 1. Transform |
| 52 | +# --------------------------------------------------------------------------- |
| 53 | +if command -v xsltproc &>/dev/null; then |
| 54 | + echo "Using xsltproc..." |
| 55 | + xsltproc --novalid -o "$OUT_FILE" "$XSL_ABS" "$XML_FILE" |
| 56 | + |
| 57 | +elif python3 -c "from lxml import etree" &>/dev/null 2>&1; then |
| 58 | + echo "Using python3 + lxml..." |
| 59 | + python3 - <<PYEOF |
| 60 | +from lxml import etree |
| 61 | +xml = etree.parse("$XML_FILE") |
| 62 | +xsl = etree.parse("$XSL_ABS") |
| 63 | +result = etree.XSLT(xsl)(xml) |
| 64 | +with open("$OUT_FILE", "w", encoding="utf-8") as f: |
| 65 | + f.write(str(result)) |
| 66 | +PYEOF |
| 67 | + |
| 68 | +else |
| 69 | + echo "ERROR: No XSLT processor found." >&2 |
| 70 | + echo "" >&2 |
| 71 | + echo "Install one of the following and re-run:" >&2 |
| 72 | + echo " Ubuntu/WSL : sudo apt-get install xsltproc" >&2 |
| 73 | + echo " macOS : brew install libxslt" >&2 |
| 74 | + echo " Python : python3 -m pip install lxml" >&2 |
| 75 | + exit 1 |
| 76 | +fi |
| 77 | + |
| 78 | +echo "HTML report written to: $OUT_FILE" |
| 79 | +echo "" |
| 80 | + |
| 81 | +# --------------------------------------------------------------------------- |
| 82 | +# 2. Open in browser |
| 83 | +# --------------------------------------------------------------------------- |
| 84 | +open_in_browser() { |
| 85 | + local file="$1" |
| 86 | + |
| 87 | + # WSL: use Windows explorer / default browser via wslview or explorer.exe |
| 88 | + if grep -qi microsoft /proc/version 2>/dev/null || \ |
| 89 | + grep -qi wsl /proc/version 2>/dev/null; then |
| 90 | + # Convert the Linux path to a Windows path |
| 91 | + local win_path |
| 92 | + win_path="$(wslpath -w "$file" 2>/dev/null || echo "")" |
| 93 | + if [ -n "$win_path" ] && command -v explorer.exe &>/dev/null; then |
| 94 | + echo "Opening in Windows default browser via explorer.exe..." |
| 95 | + explorer.exe "$win_path" |
| 96 | + return |
| 97 | + fi |
| 98 | + fi |
| 99 | + |
| 100 | + # macOS |
| 101 | + if command -v open &>/dev/null; then |
| 102 | + open "$file" |
| 103 | + return |
| 104 | + fi |
| 105 | + |
| 106 | + # Linux (X11/Wayland) |
| 107 | + if command -v xdg-open &>/dev/null; then |
| 108 | + xdg-open "$file" |
| 109 | + return |
| 110 | + fi |
| 111 | + |
| 112 | + echo "Could not open the browser automatically." |
| 113 | + echo "Open the following file manually in your browser:" |
| 114 | + echo " $file" |
| 115 | +} |
| 116 | + |
| 117 | +open_in_browser "$OUT_FILE" || true |
0 commit comments