test.sh revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
1#!/bin/bash
2#
3# Copyright 2014 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 the llvm build dir, usually Release+Asserts>"
19  echo ""
20  echo "  Runs all the libBlinkGCPlugin unit tests"
21  echo ""
22}
23
24# Runs a single test case.
25do_testcase() {
26  local flags=""
27  if [ -e "${3}" ]; then
28    flags="$(cat "${3}")"
29  fi
30  local output="$("${CLANG_DIR}"/bin/clang -c -Wno-c++11-extensions \
31      -Xclang -load -Xclang "${CLANG_DIR}"/lib/lib${LIBNAME}.${LIB} \
32      -Xclang -add-plugin -Xclang blink-gc-plugin ${flags} ${1} 2>&1)"
33  local diffout="$(echo "${output}" | diff - "${2}")"
34  if [ "${diffout}" = "" ]; then
35    echo "PASS: ${1}"
36  else
37    failed_any_test=yes
38    echo "FAIL: ${1}"
39    echo "Output of compiler:"
40    echo "${output}"
41    echo "Expected output:"
42    cat "${2}"
43    echo
44  fi
45}
46
47# Validate input to the script.
48if [[ -z "${1}" ]]; then
49  usage
50  exit ${E_BADARGS}
51elif [[ ! -d "${1}" ]]; then
52  echo "${1} is not a directory."
53  usage
54  exit ${E_BADARGS}
55else
56  export CLANG_DIR="${PWD}/${1}"
57  echo "Using clang directory ${CLANG_DIR}..."
58
59  # The golden files assume that the cwd is this directory. To make the script
60  # work no matter what the cwd is, explicitly cd to there.
61  cd "$(dirname "${0}")"
62
63  export LIBNAME=$(grep LIBRARYNAME ../Makefile | cut -d ' ' -f 3)
64  if [ "$(uname -s)" = "Linux" ]; then
65    export LIB=so
66  elif [ "$(uname -s)" = "Darwin" ]; then
67    export LIB=dylib
68  fi
69fi
70
71for input in *.cpp; do
72  do_testcase "${input}" "${input%cpp}txt" "${input%cpp}flags"
73done
74
75if [[ "${failed_any_test}" ]]; then
76  exit ${E_FAILEDTEST}
77fi
78