1#!/usr/local/bin/bash
2
3# Stop in case of error
4set -e
5
6enable_color() {
7  ENABLECOLOR=''
8  ANSI_RED="\033[31m"
9  ANSI_GREEN="\033[32m"
10  ANSI_YELLOW="\033[33m"
11  ANSI_BLUE="\033[34m"
12  ANSI_MAGENTA="\033[35m"
13  ANSI_GRAY="\033[90m"
14  ANSI_CYAN="\033[36;1m"
15  ANSI_DARKCYAN="\033[36m"
16  ANSI_NOCOLOR="\033[0m"
17}
18
19disable_color() { unset ENABLECOLOR ANSI_RED ANSI_GREEN ANSI_YELLOW ANSI_BLUE ANSI_MAGENTA ANSI_CYAN ANSI_DARKCYAN ANSI_NOCOLOR; }
20enable_color
21
22print_start() {
23  COL="$ANSI_YELLOW"
24  if [ "x$2" != "x" ]; then
25    COL="$2"
26  fi
27  printf "${COL}${1}$ANSI_NOCOLOR\n"
28}
29
30gstart () {
31  print_start "$@"
32}
33gend () {
34  :
35}
36
37if [ -n "$CI" ]; then
38  echo "INFO: set 'gstart' and 'gend' for CI"
39  gstart () {
40    printf '::group::'
41    print_start "$@"
42    SECONDS=0
43  }
44
45  gend () {
46    duration=$SECONDS
47    echo '::endgroup::'
48    printf "${ANSI_GRAY}took $((duration / 60)) min $((duration % 60)) sec.${ANSI_NOCOLOR}\n"
49  }
50fi
51
52#---
53
54# The VESTS testsuite: compliance testsuite, from: https://github.com/nickg/vests.git 388250486a
55_vests () {
56  gstart "[GHDL - test] vests"
57  cd vests
58
59  if ./testsuite.sh > vests.log 2>&1 ; then
60    printf "${ANSI_GREEN}Vests is OK$ANSI_NOCOLOR\n"
61    wc -l vests.log
62  else
63    cat vests.log
64    printf "${ANSI_RED}Vests failure$ANSI_NOCOLOR\n"
65    failures=vests
66  fi
67
68  cd ..
69  gend
70  [ "$failures" = "" ] || exit 1
71}
72
73#---
74
75if [ "x$GHDL" = "x" ]; then
76  if [ "x$prefix" != "x" ]; then
77    export GHDL="$prefix/bin/ghdl"
78  elif [ "x$(command -v which)" != "x" ]; then
79    export GHDL="$(which ghdl)"
80  else
81    printf "${ANSI_RED}error: GHDL environment variable is not defined${ANSI_NOCOLOR}\n"
82    exit 1
83  fi
84fi
85
86cd $(dirname "$0")
87rm -f test_ok
88failures=""
89tests=
90
91for opt; do
92  shift
93  case "$opt" in
94      [a-z]*) tests="$tests $opt" ;;
95      --) break ;;
96      *) echo "$0: unknown option $opt"; exit 2 ;;
97  esac
98done
99
100if [ "x$tests" = "x" ]; then tests="sanity gna vests synth vpi"; fi
101
102echo "> tests: $tests"
103echo "> args: $@"
104
105# Run a testsuite
106do_test() {
107  case $1 in
108    sanity|gna|synth|vpi)
109      gstart "[GHDL - test] $1"
110      cd "$1"
111      ../suite_driver.sh $@
112      cd ..
113      gend
114      [ "$failures" = "" ] || exit 1
115    ;;
116
117    pyunit)
118      # The Python Unit testsuite: regression testsuite for Python bindings to libghdl
119      gstart "[GHDL - test] pyunit"
120      PYTHONPATH=$(pwd)/.. ${PYTHON:-python3} -m pytest -rA pyunit
121      gend
122    ;;
123
124    vests)
125      _vests
126    ;;
127    *)
128      printf "${ANSI_RED}$0: test name '$1' is unknown${ANSI_NOCOLOR}\n"
129      exit 1;;
130  esac
131}
132
133gstart "GHDL is: $GHDL"
134$GHDL version
135echo "REF: $($GHDL version ref)"
136echo "HASH: $($GHDL version hash)"
137gend
138
139gstart "GHDL help"
140$GHDL help
141gend
142
143for t in $tests; do do_test "$t"; done
144
145printf "${ANSI_GREEN}[GHDL - test] SUCCESSFUL${ANSI_NOCOLOR}\n"
146touch test_ok
147