#! /bin/bash
#
# @file         junit-ansi.bash
# @author       Wes Garland, wes@kingsds.network
# @date         May 2024
#
#               Test that ensures ANSI control sequences are correctly identified and removed
#               from the exported JUnit xml file. Needed in order to not corrupt certain forms
#               of CI output when viewed on GitLab  

set -u
source $(dirname "$0")/helper-functions.incl

junitFile="${TMPDIR:-/tmp}/peter-junit-csi-$$.xml"
outputFile="${TMPDIR:-/tmp}/peter-junit-csi-$$.output"

trap 'rm -f "${junitFile}" "${outputFile}"' EXIT

peter -J "${junitFile}" ../../tests-libexec/console-yellow.simple >"${outputFile}"
sed "s/^/:    /" < "${outputFile}"

if ! grep $'\033' "${outputFile}" >/dev/null; then
  echo "Test failed, did not find control sequence in output!" >&2
  exit 1
fi

if ! grep 123456 "${outputFile}" >/dev/null; then
  echo "Test failed, did not find 123456 in output!" >&2
  exit 1
fi

if grep '\\u001b' "${junitFile}" >/dev/null; then
  echo "Test failed, found XML entity corresponding to control sequence in junit.xml file!" >&2
  exit 2
else
  echo "Test passed"
fi

cp ${junitFile} /tmp/test.xml
