[12] | 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 | |
---|
| 7 | import difflib |
---|
| 8 | import os |
---|
| 9 | import re |
---|
| 10 | import shutil |
---|
| 11 | import string |
---|
| 12 | import sys |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | def scan_for_test_cases(): |
---|
| 17 | return [ os.path.join( "test-cases", x ) for x in os.listdir( "test-cases" ) if x != "CVS" ] |
---|
| 18 | |
---|
| 19 | def clean_dir( dir ): |
---|
| 20 | if os.path.exists( dir ): |
---|
| 21 | shutil.rmtree( dir ) |
---|
| 22 | os.makedirs( dir ) |
---|
| 23 | |
---|
| 24 | def system( commands ): |
---|
| 25 | if sys.platform == 'win32': |
---|
| 26 | f = open( 'tmp.cmd', 'w' ) |
---|
| 27 | f.write( string.join( commands, '\n' ) ) |
---|
| 28 | f.close() |
---|
| 29 | rc = os.system( 'tmp.cmd' ) |
---|
| 30 | os.unlink( 'tmp.cmd' ) |
---|
| 31 | return rc |
---|
| 32 | else: |
---|
| 33 | rc = os.system( '&&'.join( commands ) ) |
---|
| 34 | return rc |
---|
| 35 | |
---|
| 36 | def checked_system( commands, valid_return_codes = [ 0 ] ): |
---|
| 37 | rc = system( commands ) |
---|
| 38 | if rc not in [ 0 ] + valid_return_codes: |
---|
| 39 | raise Exception( 'Command sequence "%s" failed with return code %d' % ( commands, rc ) ) |
---|
| 40 | return rc |
---|
| 41 | |
---|
| 42 | def list_recursively( dir ): |
---|
| 43 | r = [] |
---|
| 44 | for root, dirs, files in os.walk( dir, topdown=False ): |
---|
| 45 | root = root[ len( dir ) + 1 : ] |
---|
| 46 | r.extend( [ os.path.join( root, x ) for x in dirs ] ) |
---|
| 47 | r.extend( [ os.path.join( root, x ) for x in files ] ) |
---|
| 48 | |
---|
| 49 | return r |
---|
| 50 | |
---|
| 51 | def find_process_jam_log(): |
---|
| 52 | root = "../../../" |
---|
| 53 | |
---|
| 54 | for root, dirs, files in os.walk( os.path.join( root, "bin" ), topdown=False ): |
---|
| 55 | if "process_jam_log.exe" in files: |
---|
| 56 | return os.path.abspath( os.path.normpath( os.path.join( root, "process_jam_log.exe" ) ) ) |
---|
| 57 | if "process_jam_log" in files: |
---|
| 58 | return os.path.abspath( os.path.normpath( os.path.join( root, "process_jam_log" ) ) ) |
---|
| 59 | return None |
---|
| 60 | |
---|
| 61 | def process_jam_log( executable, file, locate_root, results_dir ): |
---|
| 62 | args = [] |
---|
| 63 | args.append( executable ) |
---|
| 64 | args.append( "--results-dir" ) |
---|
| 65 | args.append( results_dir ) |
---|
| 66 | args.append( locate_root ) |
---|
| 67 | args.append( file ) |
---|
| 68 | |
---|
| 69 | cmd = " ".join( args ) |
---|
| 70 | print "Running process_jam_log (%s)" % cmd |
---|
| 71 | checked_system( [ cmd ] ) |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | def read_file( file_path ): |
---|
| 75 | f = open( file_path ) |
---|
| 76 | try: |
---|
| 77 | return f.read() |
---|
| 78 | finally: |
---|
| 79 | f.close() |
---|
| 80 | |
---|
| 81 | def remove_timestamps( log_lines ): |
---|
| 82 | return [ re.sub( "timestamp=\"[^\"]+\"", "timestamp=\"\"", x ) for x in log_lines ] |
---|
| 83 | |
---|
| 84 | def run_test_cases( test_cases ): |
---|
| 85 | process_jam_log_executable = find_process_jam_log() |
---|
| 86 | for test_case in test_cases: |
---|
| 87 | print 'Running test case "%s"' % test_case |
---|
| 88 | os.chdir( test_case ) |
---|
| 89 | if os.path.exists( "expected" ): |
---|
| 90 | actual_results_dir = os.path.join( test_case, "actual" ) |
---|
| 91 | f = open( "locate_root.txt" ) |
---|
| 92 | try: |
---|
| 93 | locate_root = f.read().splitlines()[0] |
---|
| 94 | finally: |
---|
| 95 | f.close() |
---|
| 96 | |
---|
| 97 | clean_dir( "actual" ) |
---|
| 98 | os.chdir( "actual" ) |
---|
| 99 | process_jam_log( executable = process_jam_log_executable |
---|
| 100 | , results_dir = "." |
---|
| 101 | , locate_root = locate_root |
---|
| 102 | , file="..\\bjam.log" ) |
---|
| 103 | os.chdir( ".." ) |
---|
| 104 | |
---|
| 105 | actual_content = list_recursively( "actual" ) |
---|
| 106 | actual_content.sort() |
---|
| 107 | |
---|
| 108 | expected_content = list_recursively( "expected" ) |
---|
| 109 | expected_content.sort() |
---|
| 110 | |
---|
| 111 | structure_diffs = list( difflib.unified_diff( actual_content, expected_content ) ) |
---|
| 112 | if ( len( structure_diffs ) > 0 ): |
---|
| 113 | raise "Actual results are different from expected \n %s" % "\n".join( structure_diffs ) |
---|
| 114 | else: |
---|
| 115 | for i in range( 0, len( actual_content ) ): |
---|
| 116 | expected_file = os.path.join( "expected", expected_content[ i ] ) |
---|
| 117 | actual_file = os.path.join( "actual", actual_content[ i ] ) |
---|
| 118 | |
---|
| 119 | print "Comparing %s to %s" % ( expected_file, actual_file ) |
---|
| 120 | if ( not os.path.isdir( expected_file ) and not os.path.isdir( actual_file ) ): |
---|
| 121 | expected = remove_timestamps( read_file( expected_file ).splitlines() ) |
---|
| 122 | actual = remove_timestamps( read_file( actual_file ).splitlines() ) |
---|
| 123 | content_diff = list( difflib.unified_diff( expected, actual ) ) |
---|
| 124 | if ( len( content_diff ) > 0 ): |
---|
| 125 | raise "difference \n%s" % "\n".join( content_diff ) |
---|
| 126 | |
---|
| 127 | else: |
---|
| 128 | raise ' Test case "%s" doesn\'t contain the expected results directory ("expected" )' % ( test_case ) |
---|
| 129 | |
---|
| 130 | run_test_cases( scan_for_test_cases() ) |
---|
| 131 | # print find_process_jam_log() |
---|