summaryrefslogtreecommitdiff
path: root/src/test/lib/test_to_junit.sh
blob: 71e908bd812d903296a392930e2abe2c5ec25483 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env bash
set -e

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'


pass=0
fail=0

XML=""

XML_FILE=tests.xml

function green {
    (( pass += 1 ))
    printf "$1: ${GREEN}$2${NC}\n"
    XML+="    <testcase name=\"$1\"/>\n"
}

function yellow {
    (( fail += 1 ))
    printf "$1: ${YELLOW}$2${NC}\n"
    XML+="    <testcase name=\"$1\">\n      <error message=\"$2\">$2</error>\n    </testcase>\n"
}

function red {
    (( fail += 1 ))
    printf "$1: ${RED}$2${NC}\n"
    XML+="    <testcase name=\"$1\">\n      <error message=\"$2\">$2</error>\n    </testcase>\n"
}

function finish_suite {
    printf "$1: Passed ${pass} out of $(( pass + fail ))\n"
    XML="  <testsuite name=\"$1\" tests=\"$(( pass + fail ))\" failures=\"${fail}\" timestamp=\"$(date)\">\n$XML  </testsuite>\n"
    printf "$XML" >> $XML_FILE
    XML=""
    pass=0
    fail=0
}

test_regex="^\"*([^\"]*)\"*: (pass|fail)$"
echo "<testsuites>" > $XML_FILE
for result_file in $@; do
    while read line; do
        if [[ $line =~ $test_regex ]] ; then
            test_name=${BASH_REMATCH[1]}
            test_status=${BASH_REMATCH[2]}
            if [[ $test_status == pass ]] ; then
                green $test_name $test_status
            else
                red $test_name $test_status
            fi
        else
            echo $line
        fi
    done < "$result_file"
    finish_suite $result_file
done
echo "</testsuites>" >> $XML_FILE