1#!/usr/bin/python
2# Copyright (C) 2010 Google Inc.  All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7# 1. Redistributions of source code must retain the above copyright
8#    notice, this list of conditions and the following disclaimer.
9# 2. Redistributions in binary form must reproduce the above copyright
10#    notice, this list of conditions and the following disclaimer in the
11#    documentation and/or other materials provided with the distribution.
12#
13# THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24#
25
26# This script generates h and cpp file for TestObj.idl using the V8 code
27# generator. Please execute the script whenever changes are made to
28# CodeGeneratorV8.pm, and submit the changes in V8TestObj.h/cpp in the same
29# patch. This makes it easier to track and review changes in generated code.
30# To execute, invoke: 'python run_tests.py'
31
32import os
33import os.path
34import subprocess
35import sys
36import tempfile
37from webkitpy.common.checkout import scm
38
39
40def generate_from_idl(generator, idl_file, output_directory):
41    cmd = ['perl', '-w',
42           '-ISource/WebCore/bindings/scripts',
43           'Source/WebCore/bindings/scripts/generate-bindings.pl',
44           # idl include directories (path relative to generate-bindings.pl)
45           '--include', '.',
46           '--defines', 'TESTING_%s' % generator,
47           '--generator', generator,
48           '--outputDir', output_directory,
49           idl_file]
50    return subprocess.call(cmd) == 0
51
52
53def detect_changes(work_directory, reference_directory):
54    changes_found = False
55    for output_file in os.listdir(work_directory):
56        print 'Detecting changes in %s...' % output_file
57        cmd = ['diff',
58               '-u',
59               os.path.join(reference_directory, output_file),
60               os.path.join(work_directory, output_file)]
61        if subprocess.call(cmd) != 0:
62            print 'Detected changes in %s (see above)' % output_file
63            changes_found = True
64        else:
65            print 'No changes found.'
66
67    return changes_found
68
69
70def run_tests(generator, input_directory, reference_directory, reset_results):
71    work_directory = reference_directory
72
73    passed = True
74    for input_file in os.listdir(input_directory):
75        (name, extension) = os.path.splitext(input_file)
76        if extension != '.idl':
77            continue
78        print 'Testing the %s generator on %s' % (generator, input_file)
79        # Generate output into the work directory (either the given one or a 
80        # temp one if not reset_results is performed)
81        if not reset_results:
82            work_directory = tempfile.mkdtemp()
83        if not generate_from_idl(generator, os.path.join(input_directory,
84                                                         input_file),
85                                 work_directory):
86            passed = False
87        if reset_results:
88            print "Overwrote reference files"
89            continue
90        # Detect changes
91        if detect_changes(work_directory, reference_directory):
92            passed = False
93
94    if not passed:
95        print '%s generator failed.' % generator
96    return passed
97
98
99def main(argv):
100    """Runs WebCore bindings code generators on test IDL files and compares 
101    the results with reference files.
102
103    Options:
104       --reset-results: Overwrites the reference files with the generated results.
105
106    """
107    reset_results = "--reset-results" in argv
108
109    current_scm = scm.detect_scm_system(os.curdir)
110    os.chdir(current_scm.checkout_root)
111
112    all_tests_passed = True
113
114    generators = [
115        'JS',
116        'V8',
117        'ObjC',
118        'GObject',
119        'CPP'
120    ]
121
122    for generator in generators:
123        input_directory = os.path.join('Source', 'WebCore', 'bindings', 'scripts', 'test')
124        reference_directory = os.path.join('Source', 'WebCore', 'bindings', 'scripts', 'test', generator)
125        if not run_tests(generator, input_directory, reference_directory, reset_results):
126            all_tests_passed = False
127
128    if all_tests_passed:
129        print 'All tests passed!'
130        return 0
131    else:
132        print '(To update the reference files, execute "run-bindings-tests --reset-results")'
133        return -1
134
135
136if __name__ == '__main__':
137    sys.exit(main(sys.argv))
138