• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /bin/sh
2# vim:et:ft=sh:sts=2:sw=2
3#
4# This script runs the provided unit tests and sends the output to the
5# appropriate file.
6
7# Treat unset variables as an error.
8set -u
9
10die() {
11  [ $# -gt 0 ] && echo "error: $@" >&2
12  exit 1
13}
14
15BASE_DIR="`dirname $0`/.."
16LIB_DIR="${BASE_DIR}/lib"
17
18# Load libraries.
19. ${LIB_DIR}/shflags || die 'unable to load shflags library'
20. ${LIB_DIR}/shlib || die 'unable to load shlib library'
21. ${LIB_DIR}/versions || die 'unable to load versions library'
22
23# Redefining BASE_DIR now that we have the shlib functions.
24BASE_DIR=`shlib_relToAbsPath "${BASE_DIR}"`
25BIN_DIR="${BASE_DIR}/bin"
26SRC_DIR="${BASE_DIR}/src"
27
28os_name=`versions_osName |sed 's/ /_/g'`
29os_version=`versions_osVersion`
30
31# Load external flags.
32. ${BIN_DIR}/gen_test_results.flags
33
34# Define flags.
35DEFINE_boolean force false 'force overwrite' f
36DEFINE_string output_dir "`pwd`" 'output dir' d
37DEFINE_string output_file "${os_name}-${os_version}.txt" 'output file' o
38DEFINE_boolean dry_run false "supress logging to a file" n
39
40main() {
41  # Determine output filename.
42  output="${FLAGS_output_dir:+${FLAGS_output_dir}/}${FLAGS_output_file}"
43  output=`shlib_relToAbsPath "${output}"`
44
45  # Checks.
46  [ -n "${FLAGS_suite:-}" ] || die 'suite flag missing'
47
48  if [ ${FLAGS_dry_run} -eq ${FLAGS_FALSE} -a -f "${output}" ]; then
49    if [ ${FLAGS_force} -eq ${FLAGS_TRUE} ]; then
50      rm -f "${output}"
51    else
52      echo "not overwriting '${output}'" >&2
53      exit ${FLAGS_ERROR}
54    fi
55  fi
56  if [ ${FLAGS_dry_run} -eq ${FLAGS_FALSE} ]; then
57    touch "${output}" 2>/dev/null || die "unable to write to '${output}'"
58  fi
59
60  # Run tests.
61  (
62    cd "${SRC_DIR}";
63    if [ ${FLAGS_dry_run} -eq ${FLAGS_FALSE} ]; then
64      ./${FLAGS_suite} |tee "${output}"
65    else
66      ./${FLAGS_suite}
67    fi
68  )
69
70  if [ ! ${FLAGS_dry_run} ]; then
71    echo >&2
72    echo "output written to '${output}'" >&2
73  fi
74}
75
76FLAGS "$@" || exit $?
77[ ${FLAGS_help} -eq ${FALSE} ] || exit
78eval set -- "${FLAGS_ARGV}"
79main "$@"
80