main.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # Copyright (C) 2011 Google Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions
  5. # are met:
  6. # 1. Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # 2. Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. #
  12. # THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  13. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  15. # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  16. # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  17. # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19. # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  20. # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  22. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. #
  24. import os
  25. import os.path
  26. import shutil
  27. import subprocess
  28. import sys
  29. import tempfile
  30. from webkitpy.common.checkout.scm.detection import detect_scm_system
  31. from webkitpy.common.system.executive import ScriptError
  32. class BindingsTests:
  33. def __init__(self, reset_results, generators, executive):
  34. self.reset_results = reset_results
  35. self.generators = generators
  36. self.executive = executive
  37. def generate_from_idl(self, generator, idl_file, output_directory, supplemental_dependency_file):
  38. cmd = ['perl', '-w',
  39. '-IWebCore/bindings/scripts',
  40. 'WebCore/bindings/scripts/generate-bindings.pl',
  41. # idl include directories (path relative to generate-bindings.pl)
  42. '--include', '.',
  43. '--defines', 'TESTING_%s' % generator,
  44. '--generator', generator,
  45. '--outputDir', output_directory,
  46. '--supplementalDependencyFile', supplemental_dependency_file,
  47. idl_file]
  48. exit_code = 0
  49. try:
  50. output = self.executive.run_command(cmd)
  51. if output:
  52. print output
  53. except ScriptError, e:
  54. print e.output
  55. exit_code = e.exit_code
  56. return exit_code
  57. def generate_supplemental_dependency(self, input_directory, supplemental_dependency_file, window_constructors_file, workercontext_constructors_file):
  58. idl_files_list = tempfile.mkstemp()
  59. for input_file in os.listdir(input_directory):
  60. (name, extension) = os.path.splitext(input_file)
  61. if extension != '.idl':
  62. continue
  63. os.write(idl_files_list[0], os.path.join(input_directory, input_file) + "\n")
  64. os.close(idl_files_list[0])
  65. cmd = ['perl', '-w',
  66. '-IWebCore/bindings/scripts',
  67. 'WebCore/bindings/scripts/preprocess-idls.pl',
  68. '--idlFilesList', idl_files_list[1],
  69. '--defines', '',
  70. '--supplementalDependencyFile', supplemental_dependency_file,
  71. '--windowConstructorsFile', window_constructors_file,
  72. '--workerContextConstructorsFile', workercontext_constructors_file]
  73. exit_code = 0
  74. try:
  75. output = self.executive.run_command(cmd)
  76. if output:
  77. print output
  78. except ScriptError, e:
  79. print e.output
  80. exit_code = e.exit_code
  81. os.remove(idl_files_list[1])
  82. return exit_code
  83. def detect_changes(self, generator, work_directory, reference_directory):
  84. changes_found = False
  85. for output_file in os.listdir(work_directory):
  86. cmd = ['diff',
  87. '-u',
  88. '-N',
  89. os.path.join(reference_directory, output_file),
  90. os.path.join(work_directory, output_file)]
  91. exit_code = 0
  92. try:
  93. output = self.executive.run_command(cmd)
  94. except ScriptError, e:
  95. output = e.output
  96. exit_code = e.exit_code
  97. if exit_code or output:
  98. print 'FAIL: (%s) %s' % (generator, output_file)
  99. print output
  100. changes_found = True
  101. else:
  102. print 'PASS: (%s) %s' % (generator, output_file)
  103. return changes_found
  104. def run_tests(self, generator, input_directory, reference_directory, supplemental_dependency_file):
  105. work_directory = reference_directory
  106. passed = True
  107. for input_file in os.listdir(input_directory):
  108. (name, extension) = os.path.splitext(input_file)
  109. if extension != '.idl':
  110. continue
  111. # Generate output into the work directory (either the given one or a
  112. # temp one if not reset_results is performed)
  113. if not self.reset_results:
  114. work_directory = tempfile.mkdtemp()
  115. if self.generate_from_idl(generator,
  116. os.path.join(input_directory, input_file),
  117. work_directory,
  118. supplemental_dependency_file):
  119. passed = False
  120. if self.reset_results:
  121. print "Reset results: (%s) %s" % (generator, input_file)
  122. continue
  123. # Detect changes
  124. if self.detect_changes(generator, work_directory, reference_directory):
  125. passed = False
  126. shutil.rmtree(work_directory)
  127. return passed
  128. def main(self):
  129. current_scm = detect_scm_system(os.curdir)
  130. os.chdir(os.path.join(current_scm.checkout_root, 'Source'))
  131. all_tests_passed = True
  132. input_directory = os.path.join('WebCore', 'bindings', 'scripts', 'test')
  133. supplemental_dependency_file = tempfile.mkstemp()[1]
  134. window_constructors_file = tempfile.mkstemp()[1]
  135. workercontext_constructors_file = tempfile.mkstemp()[1]
  136. if self.generate_supplemental_dependency(input_directory, supplemental_dependency_file, window_constructors_file, workercontext_constructors_file):
  137. print 'Failed to generate a supplemental dependency file.'
  138. os.remove(supplemental_dependency_file)
  139. return -1
  140. for generator in self.generators:
  141. input_directory = os.path.join('WebCore', 'bindings', 'scripts', 'test')
  142. reference_directory = os.path.join('WebCore', 'bindings', 'scripts', 'test', generator)
  143. if not self.run_tests(generator, input_directory, reference_directory, supplemental_dependency_file):
  144. all_tests_passed = False
  145. os.remove(supplemental_dependency_file)
  146. print ''
  147. if all_tests_passed:
  148. print 'All tests PASS!'
  149. return 0
  150. else:
  151. print 'Some tests FAIL! (To update the reference files, execute "run-bindings-tests --reset-results")'
  152. return -1