1#!/bin/bash
2#
3# Copyright (c) 2011 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6#
7# Hacky, primitive testing: This runs the style plugin for a set of input files
8# and compares the output with golden result files.
9
10E_BADARGS=65
11E_FAILEDTEST=1
12
13failed_any_test=
14
15# Prints usage information.
16usage() {
17  echo "Usage: $(basename "${0}")" \
18    "<path to clang>" \
19    "<path to plugin>"
20  echo ""
21  echo "  Runs all the libFindBadConstructs unit tests"
22  echo ""
23}
24
25# Runs a single test case.
26do_testcase() {
27  local flags=""
28  if [ -e "${3}" ]; then
29    flags="$(cat "${3}")"
30  fi
31
32  if [ "$(uname -s)" = "Darwin" ]; then
33    flags="${flags} -isysroot $(xcrun --show-sdk-path) -stdlib=libstdc++"
34  fi
35
36  local output="$("${CLANG_PATH}" -c -Wno-c++11-extensions \
37      -Xclang -load -Xclang "${PLUGIN_PATH}" \
38      -Xclang -add-plugin -Xclang find-bad-constructs ${flags} ${1} 2>&1)"
39  local diffout="$(echo "${output}" | diff - "${2}")"
40  if [ "${diffout}" = "" ]; then
41    echo "PASS: ${1}"
42  else
43    failed_any_test=yes
44    echo "FAIL: ${1}"
45    echo "Output of compiler:"
46    echo "${output}"
47    echo "Expected output:"
48    cat "${2}"
49    echo
50  fi
51}
52
53# Validate input to the script.
54if [[ -z "${1}" ]]; then
55  usage
56  exit ${E_BADARGS}
57elif [[ -z "${2}" ]]; then
58  usage
59  exit ${E_BADARGS}
60elif [[ ! -x "${1}" ]]; then
61  echo "${1} is not an executable"
62  usage
63  exit ${E_BADARGS}
64elif [[ ! -f "${2}" ]]; then
65  echo "${2} could not be found"
66  usage
67  exit ${E_BADARGS}
68else
69  export CLANG_PATH="${1}"
70  export PLUGIN_PATH="${2}"
71  echo "Using clang ${CLANG_PATH}..."
72  echo "Using plugin ${PLUGIN_PATH}..."
73
74  # The golden files assume that the cwd is this directory. To make the script
75  # work no matter what the cwd is, explicitly cd to there.
76  cd "$(dirname "${0}")"
77fi
78
79for input in *.cpp; do
80  do_testcase "${input}" "${input%cpp}txt" "${input%cpp}flags"
81done
82
83for input in *.c; do
84  do_testcase "${input}" "${input%c}txt" "${input%c}flags"
85done
86
87if [[ "${failed_any_test}" ]]; then
88  exit ${E_FAILEDTEST}
89fi
90