test.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # Copyright (c) MetaCommunications, Inc. 2003-2005
  2. #
  3. # Distributed under the Boost Software License, Version 1.0.
  4. # (See accompanying file LICENSE_1_0.txt or copy at
  5. # http://www.boost.org/LICENSE_1_0.txt)
  6. import difflib
  7. import os
  8. import re
  9. import shutil
  10. import string
  11. import sys
  12. def scan_for_test_cases():
  13. return [ os.path.join( "test-cases", x ) for x in os.listdir( "test-cases" ) if x != "CVS" ]
  14. def clean_dir( dir ):
  15. if os.path.exists( dir ):
  16. shutil.rmtree( dir )
  17. os.makedirs( dir )
  18. def system( commands ):
  19. if sys.platform == 'win32':
  20. f = open( 'tmp.cmd', 'w' )
  21. f.write( string.join( commands, '\n' ) )
  22. f.close()
  23. rc = os.system( 'tmp.cmd' )
  24. os.unlink( 'tmp.cmd' )
  25. return rc
  26. else:
  27. rc = os.system( '&&'.join( commands ) )
  28. return rc
  29. def checked_system( commands, valid_return_codes = [ 0 ] ):
  30. rc = system( commands )
  31. if rc not in [ 0 ] + valid_return_codes:
  32. raise Exception( 'Command sequence "%s" failed with return code %d' % ( commands, rc ) )
  33. return rc
  34. def list_recursively( dir ):
  35. r = []
  36. for root, dirs, files in os.walk( dir, topdown=False ):
  37. root = root[ len( dir ) + 1 : ]
  38. r.extend( [ os.path.join( root, x ) for x in dirs ] )
  39. r.extend( [ os.path.join( root, x ) for x in files ] )
  40. return r
  41. def find_process_jam_log():
  42. root = "../../../"
  43. for root, dirs, files in os.walk( os.path.join( root, "bin.v2" ), topdown=False ):
  44. if "process_jam_log.exe" in files:
  45. return os.path.abspath( os.path.normpath( os.path.join( root, "process_jam_log.exe" ) ) )
  46. if "process_jam_log" in files:
  47. return os.path.abspath( os.path.normpath( os.path.join( root, "process_jam_log" ) ) )
  48. return None
  49. def process_jam_log( executable, file, locate_root, results_dir ):
  50. args = []
  51. args.append( executable )
  52. # args.append( '--echo' )
  53. args.append( '--create-directories' )
  54. args.append( '--v2' )
  55. args.append( locate_root )
  56. args.append( '<' )
  57. args.append( file )
  58. cmd = " ".join( args )
  59. print "Running process_jam_log (%s)" % cmd
  60. checked_system( [ cmd ] )
  61. def read_file( file_path ):
  62. f = open( file_path )
  63. try:
  64. return f.read()
  65. finally:
  66. f.close()
  67. def remove_timestamps( log_lines ):
  68. return [ re.sub( "timestamp=\"[^\"]+\"", "timestamp=\"\"", x ) for x in log_lines ]
  69. def determine_locate_root( bjam_log ):
  70. locate_root = None
  71. f = open( 'bjam.log' )
  72. try:
  73. locate_root_re = re.compile( r'locate-root\s+"(.*)"' )
  74. for l in f.readlines():
  75. m = locate_root_re.match( l )
  76. if m:
  77. locate_root = m.group(1)
  78. break
  79. finally:
  80. f.close()
  81. return locate_root
  82. def read_file( path ):
  83. f = open( path )
  84. try:
  85. return f.read()
  86. finally:
  87. f.close()
  88. def read_file_lines( path ):
  89. f = open( path )
  90. try:
  91. return f.readlines()
  92. finally:
  93. f.close()
  94. def write_file( path, content ):
  95. f = open( path, 'w' )
  96. try:
  97. return f.write( content )
  98. finally:
  99. f.close()
  100. def write_file_lines( path, content ):
  101. f = open( path, 'w' )
  102. try:
  103. return f.writelines( content )
  104. finally:
  105. f.close()
  106. def run_test_cases( test_cases ):
  107. process_jam_log_executable = find_process_jam_log()
  108. print 'Found process_jam_log: %s' % process_jam_log_executable
  109. initial_dir = os.getcwd()
  110. for test_case in test_cases:
  111. os.chdir( initial_dir )
  112. print 'Running test case "%s"' % test_case
  113. os.chdir( test_case )
  114. if os.path.exists( "expected" ):
  115. locate_root = determine_locate_root( 'bjam.log' )
  116. print 'locate_root: %s' % locate_root
  117. actual_results_dir = os.path.join( test_case, "actual" )
  118. clean_dir( "actual" )
  119. os.chdir( "actual" )
  120. root = os.getcwd()
  121. i = 0
  122. while 1:
  123. if i == 0:
  124. bjam_log_file = 'bjam.log'
  125. else:
  126. bjam_log_file = 'bjam.log.%0d' % i
  127. i += 1
  128. print 'Looking for %s' % bjam_log_file
  129. if not os.path.exists( os.path.join( '..', bjam_log_file ) ):
  130. print ' does not exists'
  131. break
  132. print ' found'
  133. write_file_lines(bjam_log_file.replace( 'bjam', 'bjam_' ),
  134. [ x.replace( locate_root, root ) for x in read_file_lines( os.path.join( '..', bjam_log_file ) ) ] )
  135. process_jam_log( executable = process_jam_log_executable
  136. , results_dir = "."
  137. , locate_root = root
  138. , file=bjam_log_file.replace( 'bjam', 'bjam_' ) )
  139. actual_content = list_recursively( "." )
  140. actual_content.sort()
  141. result_xml = []
  142. for test_log in [ x for x in actual_content if os.path.splitext( x )[1] == '.xml' ]:
  143. print 'reading %s' % test_log
  144. result = [ re.sub( r'timestamp="(.*)"', 'timestamp="xxx"', x ) for x in read_file_lines( test_log ) ]
  145. result_xml.extend( result )
  146. write_file_lines( 'results.xml', result_xml )
  147. os.chdir( '..' )
  148. assert read_file( 'expected/results.xml' ) == read_file( 'actual/results.xml' )
  149. os.chdir( '..' )
  150. else:
  151. raise ' Test case "%s" doesn\'t contain the expected results directory ("expected" )' % ( test_case )
  152. run_test_cases( scan_for_test_cases() )
  153. # print find_process_jam_log()
粤ICP备19079148号