|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | + |
| 8 | +from test_util import TestFailedError, prepareForIncrParse, run_command |
| 9 | + |
| 10 | + |
| 11 | +def main(): |
| 12 | + parser = argparse.ArgumentParser( |
| 13 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 14 | + description='Utility for testing incremental syntax parsing', |
| 15 | + epilog=''' |
| 16 | + Based of a single template the utility generates a pre-edit and a post-edit |
| 17 | + file. It then verifies that incrementally parsing the post-edit file base |
| 18 | + on the pre-edit file results in the same syntax tree as reparsing the |
| 19 | + post-edit file from scratch. |
| 20 | +
|
| 21 | + To generate the pre-edit and the post-edit file from the template, it |
| 22 | + operates on markers of the form: |
| 23 | +
|
| 24 | + <<test_case<pre|||post>>> |
| 25 | +
|
| 26 | + These placeholders are replaced by: |
| 27 | + - 'pre' if a different test case than 'test_case' is run |
| 28 | + - 'pre' for the pre-edit version of 'test_case' |
| 29 | + - 'post' for the post-edit version of 'test_case' |
| 30 | + ''') |
| 31 | + parser.add_argument( |
| 32 | + 'file', type=argparse.FileType(), |
| 33 | + help='The template file to test') |
| 34 | + parser.add_argument( |
| 35 | + '--test-case', default='', |
| 36 | + help='The test case to execute. If no test case is specified all \ |
| 37 | + unnamed substitutions are applied') |
| 38 | + parser.add_argument( |
| 39 | + '--temp-dir', required=True, |
| 40 | + help='A temporary directory where pre-edit and post-edit files can be \ |
| 41 | + saved') |
| 42 | + parser.add_argument( |
| 43 | + '--swiftsyntax-lit-test-helper', required=True, |
| 44 | + help='The path to the lit-test-helper binary of SwiftSyntax') |
| 45 | + |
| 46 | + args = parser.parse_args(sys.argv[1:]) |
| 47 | + |
| 48 | + test_file = args.file.name |
| 49 | + test_file_name = os.path.basename(test_file) |
| 50 | + test_case = args.test_case |
| 51 | + temp_dir = args.temp_dir |
| 52 | + swiftsyntax_lit_test_helper = args.swiftsyntax_lit_test_helper |
| 53 | + |
| 54 | + if not os.path.exists(temp_dir): |
| 55 | + os.makedirs(temp_dir) |
| 56 | + |
| 57 | + pre_edit_file = temp_dir + '/' + test_file_name + '.' + test_case + \ |
| 58 | + '.pre.swift' |
| 59 | + post_edit_file = temp_dir + '/' + test_file_name + '.' + test_case + \ |
| 60 | + '.post.swift' |
| 61 | + after_roundtrip_file = temp_dir + '/' + test_file_name + '.' \ |
| 62 | + + test_case + '.post_after_roundtrip.swift' |
| 63 | + |
| 64 | + # ========================================================================= |
| 65 | + # First generate the pre-edit and post-edit Swift file and gather the edits |
| 66 | + # and expected reparse regions. This is the parser for the special edit |
| 67 | + # markup for testing incremental parsing |
| 68 | + # ========================================================================= |
| 69 | + |
| 70 | + # Gather command line arguments for swift-syntax-test specifying the |
| 71 | + # performed edits in this list |
| 72 | + incremental_edit_args = [] |
| 73 | + reparse_args = [] |
| 74 | + try: |
| 75 | + prepareForIncrParse(test_file=test_file, test_case=test_case, |
| 76 | + pre_edit_file=pre_edit_file, |
| 77 | + post_edit_file=post_edit_file, |
| 78 | + incremental_edit_args=incremental_edit_args, |
| 79 | + reparse_args=reparse_args) |
| 80 | + except TestFailedError as e: |
| 81 | + print('Test case "%s" of %s FAILed' % (test_case, test_file), |
| 82 | + file=sys.stderr) |
| 83 | + print(e.message, file=sys.stderr) |
| 84 | + sys.exit(1) |
| 85 | + |
| 86 | + try: |
| 87 | + run_command([swiftsyntax_lit_test_helper, '-parse-incremental'] + |
| 88 | + ['-old-source-file', pre_edit_file] + |
| 89 | + ['-source-file', post_edit_file] + |
| 90 | + incremental_edit_args + reparse_args + |
| 91 | + ['-out', after_roundtrip_file]) |
| 92 | + except subprocess.CalledProcessError as e: |
| 93 | + print('Test case "%s" of %s FAILed' % (test_case, test_file), |
| 94 | + file=sys.stderr) |
| 95 | + print('Parsing the swift file failed:\n', file=sys.stderr) |
| 96 | + print(e.output.decode('UTF-8'), file=sys.stderr) |
| 97 | + sys.exit(1) |
| 98 | + |
| 99 | + # Check if the two syntax trees are the same |
| 100 | + try: |
| 101 | + run_command( |
| 102 | + [ |
| 103 | + 'diff', '-u', |
| 104 | + post_edit_file, |
| 105 | + after_roundtrip_file |
| 106 | + ]) |
| 107 | + except subprocess.CalledProcessError as e: |
| 108 | + print('Test case "%s" of %s FAILed' % (test_case, test_file), |
| 109 | + file=sys.stderr) |
| 110 | + print('Source file after incrementally parsing ' |
| 111 | + 'does not match post-edit source file:\n\n', |
| 112 | + file=sys.stderr) |
| 113 | + print(e.output, file=sys.stderr) |
| 114 | + sys.exit(1) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == '__main__': |
| 118 | + main() |
0 commit comments