Commit a0de0476 authored by Daniel Cheng's avatar Daniel Cheng

Rewrite blink_gc_plugin test script in Python.

.sh tests don't work on Windows.

BUG=486571
R=thakis@chromium.org

Review URL: https://codereview.chromium.org/1135363003

Cr-Commit-Position: refs/heads/master@{#329758}
parent 9f78c24e
......@@ -27,7 +27,8 @@ else()
cr_install(TARGETS "lib${LIBRARYNAME}" LIBRARY DESTINATION lib)
cr_add_test(blink_gc_plugin_test
${CMAKE_CURRENT_SOURCE_DIR}/tests/test.sh
python
${CMAKE_CURRENT_SOURCE_DIR}/tests/test.py
${CMAKE_BINARY_DIR}/bin/clang
$<TARGET_FILE:lib${LIBRARYNAME}>
)
......
#!/usr/bin/env python
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import glob
import os
import subprocess
import sys
def run_test(test_base_name, cmd):
"""Run a test case.
Args:
test_base_name: The name for the test C++ source file without the extension.
cmd: The actual command to run for the test.
Returns:
None on pass, or a str with the description of the failure.
"""
try:
actual = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
# Some of the Blink GC plugin tests intentionally trigger compile errors, so
# just ignore an exit code that indicates failure.
actual = e.output
except Exception as e:
return 'could not execute %s (%s)' % (cmd, e)
# Some Blink GC plugins dump a JSON representation of the object graph, and
# use the processed results as the actual results of the test.
if os.path.exists('%s.graph.json' % test_base_name):
try:
actual = subprocess.check_output(
['python', '../process-graph.py', '-c',
'%s.graph.json' % test_base_name],
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError, e:
# The graph processing script returns a failure exit code if the graph is
# 'bad' (e.g. it has a cycle). The output still needs to be captured in
# that case, since the expected results capture the errors.
actual = e.output
# TODO(dcheng): Remove the rstrip() and just rebaseline the tests to match.
actual = actual.rstrip()
try:
expected = open('%s.txt' % test_base_name).read().rstrip()
except IOError:
open('%s.txt' % test_base_name, 'w').write(actual)
return 'no expected file found'
if expected != actual:
open('%s.txt' % test_base_name, 'w').write(actual)
return 'expected and actual differed'
def run_tests(clang_path, plugin_path):
"""Runs the tests.
Args:
clang_path: The path to the clang binary to be tested.
plugin_path: An optional path to the plugin to test. This may be None, if
plugin is built directly into clang, like on Windows.
Returns:
(passing, failing): Two lists containing the base names of the passing and
failing tests respectively.
"""
passing = []
failing = []
base_cmd = [clang_path, '-fsyntax-only', '-std=c++11']
base_cmd.extend(['-Wno-inaccessible-base'])
if plugin_path:
base_cmd.extend(['-Xclang', '-load', '-Xclang', plugin_path])
base_cmd.extend(['-Xclang', '-add-plugin', '-Xclang', 'blink-gc-plugin'])
tests = glob.glob('*.cpp')
for test in tests:
sys.stdout.write('Testing %s... ' % test)
test_base_name, _ = os.path.splitext(test)
cmd = base_cmd[:]
try:
cmd.extend(file('%s.flags' % test_base_name).read().split())
except IOError:
pass
cmd.append(test)
failure_message = run_test(test_base_name, cmd)
if failure_message:
print 'failed: %s' % failure_message
failing.append(test_base_name)
else:
print 'passed!'
passing.append(test_base_name)
return passing, failing
def main():
if len(sys.argv) < 2:
print 'Usage: <path to clang>[ <path to plugin>]'
return -1
os.chdir(os.path.dirname(os.path.realpath(__file__)))
clang_path = sys.argv[1]
plugin_path = sys.argv[2] if len(sys.argv) > 2 else None
print 'Using clang %s...' % clang_path
print 'Using plugin %s...' % plugin_path
passing, failing = run_tests(clang_path, plugin_path)
print 'Ran %d tests: %d succeeded, %d failed' % (
len(passing) + len(failing), len(passing), len(failing))
for test in failing:
print ' %s' % test
if __name__ == '__main__':
sys.exit(main())
#!/bin/bash
#
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Hacky, primitive testing: This runs the style plugin for a set of input files
# and compares the output with golden result files.
E_BADARGS=65
E_FAILEDTEST=1
failed_any_test=
# Prints usage information.
usage() {
echo "Usage: $(basename "${0}")" \
"<path to clang>" \
"<path to plugin>"
echo ""
echo " Runs all the libBlinkGCPlugin unit tests"
echo ""
}
# Runs a single test case.
do_testcase() {
local flags=""
if [ -e "${3}" ]; then
flags="$(cat "${3}")"
fi
local output="$("${CLANG_PATH}" -c -Wno-c++11-extensions \
-Wno-inaccessible-base \
-Xclang -load -Xclang "${PLUGIN_PATH}" \
-Xclang -add-plugin -Xclang blink-gc-plugin ${flags} ${1} 2>&1)"
local json="${input%cpp}graph.json"
if [ -f "$json" ]; then
output="$(python ../process-graph.py -c ${json} 2>&1)"
fi
local diffout="$(echo "${output}" | diff - "${2}")"
if [ "${diffout}" = "" ]; then
echo "PASS: ${1}"
else
failed_any_test=yes
echo "FAIL: ${1}"
echo "Output of compiler:"
echo "${output}"
echo "Expected output:"
cat "${2}"
echo
fi
}
# Validate input to the script.
if [[ -z "${1}" ]]; then
usage
exit ${E_BADARGS}
elif [[ -z "${2}" ]]; then
usage
exit ${E_BADARGS}
elif [[ ! -x "${1}" ]]; then
echo "${1} is not an executable"
usage
exit ${E_BADARGS}
elif [[ ! -f "${2}" ]]; then
echo "${2} could not be found"
usage
exit ${E_BADARGS}
else
export CLANG_PATH="${1}"
export PLUGIN_PATH="${2}"
echo "Using clang ${CLANG_PATH}..."
echo "Using plugin ${PLUGIN_PATH}..."
# The golden files assume that the cwd is this directory. To make the script
# work no matter what the cwd is, explicitly cd to there.
cd "$(dirname "${0}")"
fi
for input in *.cpp; do
do_testcase "${input}" "${input%cpp}txt" "${input%cpp}flags"
done
if [[ "${failed_any_test}" ]]; then
exit ${E_FAILEDTEST}
fi
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment