<commit_before># Copyright (C) 2011 Google Inc.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

import os
import shutil
import tempfile
from webkitpy.common.checkout.scm.detection import detect_scm_system
from webkitpy.common.system.executive import ScriptError

PASS_MESSAGE = 'All tests PASS!'
FAIL_MESSAGE = """Some tests FAIL!
To update the reference files, execute:
    run-bindings-tests --reset-results

If the failures are not due to your changes, test results may be out of sync;
please rebaseline them in a separate CL, after checking that tests fail in ToT.
In CL, please set:
NOTRY=true
TBR=(someone in Source/bindings/OWNERS or WATCHLISTS:bindings)
"""

# Python compiler is incomplete; skip IDLs with unimplemented features
SKIP_PYTHON = set([
    'TestActiveDOMObject.idl',
    'TestCustomAccessors.idl',
    'TestEvent.idl',
    'TestEventConstructor.idl',
    'TestEventTarget.idl',
    'TestException.idl',
    'TestExtendedEvent.idl',
    'TestImplements.idl',
    'TestInterface.idl',
    'TestInterfaceImplementedAs.idl',
    'TestMediaQueryListListener.idl',
    'TestNamedConstructor.idl',
    'TestNode.idl',
    'TestObject.idl',
    'TestOverloadedConstructors.idl',
    'TestPartialInterface.idl',
    'TestSerializedScriptValueInterface.idl',
    'TestTypedArray.idl',
    'TestTypedefs.idl',
])

input_directory = os.path.join('bindings', 'tests', 'idls')
support_input_directory = os.path.join('bindings', 'tests', 'idls', 'testing')
reference_directory = os.path.join('bindings', 'tests', 'results')
reference_event_names_filename = os.path.join(reference_directory, 'EventInterfaces.in')


class ScopedTempFileProvider(object):
    def __init__(self):
        self.files = []
        self.directories = []

    def __del__(self):
        for filename in self.files:
            os.remove(filename)
        for directory in self.directories:
            shutil.rmtree(directory)

    def newtempfile(self):
        file_handle, path = tempfile.mkstemp()
        self.files.append(path)
        return file_handle, path

    def newtempdir(self):
        path = tempfile.mkdtemp()
        self.directories.append(path)
        return path

provider = ScopedTempFileProvider()


class BindingsTests(object):
    def __init__(self, reset_results, test_python, verbose, executive):
        self.reset_results = reset_results
        self.test_python = test_python
        self.verbose = verbose
        self.executive = executive
        _, self.interface_dependencies_filename = provider.newtempfile()
        _, self.derived_sources_list_filename = provider.newtempfile()
        if reset_results:
            self.event_names_filename = os.path.join(reference_directory, 'EventInterfaces.in')
        else:
            _, self.event_names_filename = provider.newtempfile()

    def run_command(self, cmd):
        return self.executive.run_command(cmd)

    def generate_from_idl_pl(self, idl_file, output_directory):
        cmd = ['perl', '-w',
               '-Ibindings/scripts',
               '-Ibuild/scripts',
               '-Icore/scripts',
               '-I../../JSON/out/lib/perl5',
               'bindings/scripts/generate_bindings.pl',
               # idl include directories (path relative to generate-bindings.pl)
               '--include', '.',
               '--outputDir', output_directory,
               '--interfaceDependenciesFile', self.interface_dependencies_filename,
               '--idlAttributesFile', 'bindings/scripts/IDLAttributes.txt',
               idl_file]
        try:
            output = self.run_command(cmd)
        except ScriptError, e:
            print e.output
            return e.exit_code
        if output:
            print output
        return 0

    def generate_from_idl_py(self, idl_file, output_directory):
        cmd = ['python',
               'bindings/scripts/unstable/idl_compiler.py',
               '--output-dir', output_directory,
               '--idl-attributes-file', 'bindings/scripts/IDLAttributes.txt',
               '--include', '.',
               '--interface-dependencies-file',
               self.interface_dependencies_filename,
               idl_file]
        try:
            output = self.run_command(cmd)
        except ScriptError, e:
            print e.output
            return e.exit_code
        if output:
            print output
        return 0

    def generate_interface_dependencies(self):
        idl_files_list_file, main_idl_files_list_filename = provider.newtempfile()
        idl_paths = [os.path.join(input_directory, input_file)
                     for input_file in os.listdir(input_directory)
                     if input_file.endswith('.idl')]
        idl_files_list_contents = ''.join(idl_path + '\n'
                                          for idl_path in idl_paths)
        os.write(idl_files_list_file, idl_files_list_contents)
        support_idl_files_list_file, support_idl_files_list_filename = provider.newtempfile()
        support_idl_paths = [os.path.join(support_input_directory, input_file)
                     for input_file in os.listdir(support_input_directory)
                     if input_file.endswith('.idl')]
        support_idl_files_list_contents = ''.join(idl_path + '\n'
                                          for idl_path in support_idl_paths)
        os.write(support_idl_files_list_file, support_idl_files_list_contents)

        # Dummy files, required by compute_dependencies but not checked
        _, window_constructors_file = provider.newtempfile()
        _, workerglobalscope_constructors_file = provider.newtempfile()
        _, sharedworkerglobalscope_constructors_file = provider.newtempfile()
        _, dedicatedworkerglobalscope_constructors_file = provider.newtempfile()
        cmd = ['python',
               'bindings/scripts/compute_dependencies.py',
               '--main-idl-files-list', main_idl_files_list_filename,
               '--support-idl-files-list', support_idl_files_list_filename,
               '--interface-dependencies-file', self.interface_dependencies_filename,
               '--bindings-derived-sources-file', self.derived_sources_list_filename,
               '--window-constructors-file', window_constructors_file,
               '--workerglobalscope-constructors-file', workerglobalscope_constructors_file,
               '--sharedworkerglobalscope-constructors-file', sharedworkerglobalscope_constructors_file,
               '--dedicatedworkerglobalscope-constructors-file', dedicatedworkerglobalscope_constructors_file,
               '--event-names-file', self.event_names_filename,
               '--write-file-only-if-changed', '0']

        if self.reset_results:
            print 'Reset results: EventInterfaces.in'
        try:
            output = self.run_command(cmd)
        except ScriptError, e:
            print e.output
            return e.exit_code
        if output:
            print output
        return 0

    def identical_file(self, reference_filename, work_filename):
        cmd = ['diff',<commit_msg>Make run-bindings-test reset results for EventInterfaces.in quiet too<commit_after># Copyright (C) 2011 Google Inc.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

import os
import shutil
import tempfile
from webkitpy.common.checkout.scm.detection import detect_scm_system
from webkitpy.common.system.executive import ScriptError

PASS_MESSAGE = 'All tests PASS!'
FAIL_MESSAGE = """Some tests FAIL!
To update the reference files, execute:
    run-bindings-tests --reset-results

If the failures are not due to your changes, test results may be out of sync;
please rebaseline them in a separate CL, after checking that tests fail in ToT.
In CL, please set:
NOTRY=true
TBR=(someone in Source/bindings/OWNERS or WATCHLISTS:bindings)
"""

# Python compiler is incomplete; skip IDLs with unimplemented features
SKIP_PYTHON = set([
    'TestActiveDOMObject.idl',
    'TestCustomAccessors.idl',
    'TestEvent.idl',
    'TestEventConstructor.idl',
    'TestEventTarget.idl',
    'TestException.idl',
    'TestExtendedEvent.idl',
    'TestImplements.idl',
    'TestInterface.idl',
    'TestInterfaceImplementedAs.idl',
    'TestMediaQueryListListener.idl',
    'TestNamedConstructor.idl',
    'TestNode.idl',
    'TestObject.idl',
    'TestOverloadedConstructors.idl',
    'TestPartialInterface.idl',
    'TestSerializedScriptValueInterface.idl',
    'TestTypedArray.idl',
    'TestTypedefs.idl',
])

input_directory = os.path.join('bindings', 'tests', 'idls')
support_input_directory = os.path.join('bindings', 'tests', 'idls', 'testing')
reference_directory = os.path.join('bindings', 'tests', 'results')
reference_event_names_filename = os.path.join(reference_directory, 'EventInterfaces.in')


class ScopedTempFileProvider(object):
    def __init__(self):
        self.files = []
        self.directories = []

    def __del__(self):
        for filename in self.files:
            os.remove(filename)
        for directory in self.directories:
            shutil.rmtree(directory)

    def newtempfile(self):
        file_handle, path = tempfile.mkstemp()
        self.files.append(path)
        return file_handle, path

    def newtempdir(self):
        path = tempfile.mkdtemp()
        self.directories.append(path)
        return path

provider = ScopedTempFileProvider()


class BindingsTests(object):
    def __init__(self, reset_results, test_python, verbose, executive):
        self.reset_results = reset_results
        self.test_python = test_python
        self.verbose = verbose
        self.executive = executive
        _, self.interface_dependencies_filename = provider.newtempfile()
        _, self.derived_sources_list_filename = provider.newtempfile()
        if reset_results:
            self.event_names_filename = os.path.join(reference_directory, 'EventInterfaces.in')
        else:
            _, self.event_names_filename = provider.newtempfile()

    def run_command(self, cmd):
        return self.executive.run_command(cmd)

    def generate_from_idl_pl(self, idl_file, output_directory):
        cmd = ['perl', '-w',
               '-Ibindings/scripts',
               '-Ibuild/scripts',
               '-Icore/scripts',
               '-I../../JSON/out/lib/perl5',
               'bindings/scripts/generate_bindings.pl',
               # idl include directories (path relative to generate-bindings.pl)
               '--include', '.',
               '--outputDir', output_directory,
               '--interfaceDependenciesFile', self.interface_dependencies_filename,
               '--idlAttributesFile', 'bindings/scripts/IDLAttributes.txt',
               idl_file]
        try:
            output = self.run_command(cmd)
        except ScriptError, e:
            print e.output
            return e.exit_code
        if output:
            print output
        return 0

    def generate_from_idl_py(self, idl_file, output_directory):
        cmd = ['python',
               'bindings/scripts/unstable/idl_compiler.py',
               '--output-dir', output_directory,
               '--idl-attributes-file', 'bindings/scripts/IDLAttributes.txt',
               '--include', '.',
               '--interface-dependencies-file',
               self.interface_dependencies_filename,
               idl_file]
        try:
            output = self.run_command(cmd)
        except ScriptError, e:
            print e.output
            return e.exit_code
        if output:
            print output
        return 0

    def generate_interface_dependencies(self):
        idl_files_list_file, main_idl_files_list_filename = provider.newtempfile()
        idl_paths = [os.path.join(input_directory, input_file)
                     for input_file in os.listdir(input_directory)
                     if input_file.endswith('.idl')]
        idl_files_list_contents = ''.join(idl_path + '\n'
                                          for idl_path in idl_paths)
        os.write(idl_files_list_file, idl_files_list_contents)
        support_idl_files_list_file, support_idl_files_list_filename = provider.newtempfile()
        support_idl_paths = [os.path.join(support_input_directory, input_file)
                     for input_file in os.listdir(support_input_directory)
                     if input_file.endswith('.idl')]
        support_idl_files_list_contents = ''.join(idl_path + '\n'
                                          for idl_path in support_idl_paths)
        os.write(support_idl_files_list_file, support_idl_files_list_contents)

        # Dummy files, required by compute_dependencies but not checked
        _, window_constructors_file = provider.newtempfile()
        _, workerglobalscope_constructors_file = provider.newtempfile()
        _, sharedworkerglobalscope_constructors_file = provider.newtempfile()
        _, dedicatedworkerglobalscope_constructors_file = provider.newtempfile()
        cmd = ['python',
               'bindings/scripts/compute_dependencies.py',
               '--main-idl-files-list', main_idl_files_list_filename,
               '--support-idl-files-list', support_idl_files_list_filename,
               '--interface-dependencies-file', self.interface_dependencies_filename,
               '--bindings-derived-sources-file', self.derived_sources_list_filename,
               '--window-constructors-file', window_constructors_file,
               '--workerglobalscope-constructors-file', workerglobalscope_constructors_file,
               '--sharedworkerglobalscope-constructors-file', sharedworkerglobalscope_constructors_file,
               '--dedicatedworkerglobalscope-constructors-file', dedicatedworkerglobalscope_constructors_file,
               '--event-names-file', self.event_names_filename,
               '--write-file-only-if-changed', '0']

        if self.reset_results and self.verbose:
            print 'Reset results: EventInterfaces.in'
        try:
            output = self.run_command(cmd)
        except ScriptError, e:
            print e.output
            return e.exit_code
        if output:
            print output
        return 0

    def identical_file(self, reference_filename, work_filename):
        cmd = ['diff',
{'fraction_non_alphanumeric': 0.08919259126056464, 'fraction_numerical': 0.0013187076664868428, 'mean_word_length': 2.7610459873760145, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3939008', 'n_tokens_mistral': 4739, 'n_tokens_neox': 4616, 'n_words': 1132}