aboutsummaryrefslogtreecommitdiff
path: root/gen_test_results.sh
blob: 421bb7aeb280909ee334885c18d863724ed9da0c (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#! /bin/sh
# vim:et:ft=sh:sts=2:sw=2
#
# This script runs the provided unit tests and sends the output to the
# appropriate file.
#
# Copyright 2016-2017 Kate Ward. All Rights Reserved.
# Released under the Apache 2.0 license.
#
# Author: kate.ward@forestent.com (Kate Ward)
# https://github.com/kward/shflags

# Treat unset variables as an error.
set -u

die() {
  [ $# -gt 0 ] && echo "error: $@" >&2
  exit 1
}

BASE_DIR="`dirname $0`"
LIB_DIR="${BASE_DIR}/lib"

# Load libraries.
. ${LIB_DIR}/shflags || die 'unable to load shflags library'
. ${LIB_DIR}/shlib || die 'unable to load shlib library'
. ${LIB_DIR}/versions || die 'unable to load versions library'

# Redefining BASE_DIR now that we have the shlib functions.
BASE_DIR=`shlib_relToAbsPath "${BASE_DIR}"`
SRC_DIR="${BASE_DIR}/src"

__GEN_TEST_RESULTS_BACKUP_EXT='~'

os_name=`versions_osName |sed 's/ /_/g'`
os_version=`versions_osVersion`

# Load external flags.
. ${BASE_DIR}/gen_test_results.flags

# Define flags.
DEFINE_boolean force false 'force overwrite' f
DEFINE_boolean backup false 'backup old output file' b
DEFINE_string output_dir "${TMPDIR}" 'output dir' d
DEFINE_string output_file "${os_name}-${os_version}.txt" 'output file' o
DEFINE_boolean dry_run false "suppress logging to a file" n

main() {
  # Determine output filename.
  output="${FLAGS_output_dir:+${FLAGS_output_dir}/}${FLAGS_output_file}"
  output=`shlib_relToAbsPath "${output}"`

  # Checks.
  [ -n "${FLAGS_suite:-}" ] || die 'suite flag missing'

  if [ ${FLAGS_dry_run} -eq ${FLAGS_FALSE} ]; then
    if [ -f "${output}" -a ${FLAGS_backup} -eq ${FLAGS_TRUE} ]; then
      old="${output}" new="${output}${__GEN_TEST_RESULTS_BACKUP_EXT}"
      echo "backing up '${old}' to '${new}'"
      mv "${old}" "${new}"
    fi
    if [ -f "${output}" -a ${FLAGS_force} -eq ${FLAGS_TRUE} ]; then
      rm -f "${output}"
    fi
    if [ -f "${output}" ]; then
      echo "not overwriting '${output}'" >&2
      exit ${FLAGS_ERROR}
    fi
    touch "${output}" 2>/dev/null || die "unable to write to '${output}'"
  fi

  # Run tests.
  (
    cd "${SRC_DIR}";
    if [ ${FLAGS_dry_run} -eq ${FLAGS_FALSE} ]; then
      ./${FLAGS_suite} |tee "${output}"
    else
      ./${FLAGS_suite}
    fi
  )

  if [ ! ${FLAGS_dry_run} ]; then
    echo >&2
    echo "output written to '${output}'" >&2
  fi
}

FLAGS "$@" || exit $?
[ ${FLAGS_help} -eq ${FALSE} ] || exit
eval set -- "${FLAGS_ARGV}"
main "$@"