Skip to content

Commit aba7fa1

Browse files
committed
[utils/incrparse] Introduce testWithParserLib() helper function
This is to allow swift-syntax to run its lit tests without needing to execute swift-syntax-test for incremental reparsing.
1 parent b55c1e2 commit aba7fa1

File tree

2 files changed

+101
-25
lines changed

2 files changed

+101
-25
lines changed

utils/incrparse/incr_transfer_round_trip.py

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,62 @@
88
import sys
99

1010
from test_util import TestFailedError, run_command, \
11-
serializeIncrParseMarkupFile
11+
serializeIncrParseMarkupFile, prepareForIncrParse
12+
13+
14+
def testWithParserLib(test_file, test_case, pre_edit_file, post_edit_file,
15+
after_roundtrip_file, swiftsyntax_lit_test_helper):
16+
# =========================================================================
17+
# First generate the pre-edit and post-edit Swift file and gather the edits
18+
# and expected reparse regions. This is the parser for the special edit
19+
# markup for testing incremental parsing
20+
# =========================================================================
21+
22+
# Gather command line arguments for swift-syntax-test specifiying the
23+
# performed edits in this list
24+
incremental_edit_args = []
25+
reparse_args = []
26+
try:
27+
prepareForIncrParse(test_file=test_file, test_case=test_case,
28+
pre_edit_file=pre_edit_file,
29+
post_edit_file=post_edit_file,
30+
incremental_edit_args=incremental_edit_args,
31+
reparse_args=reparse_args)
32+
except TestFailedError as e:
33+
print('Test case "%s" of %s FAILed' % (test_case, test_file),
34+
file=sys.stderr)
35+
print(e.message, file=sys.stderr)
36+
sys.exit(1)
37+
38+
try:
39+
run_command([swiftsyntax_lit_test_helper, '-parse-incremental'] +
40+
['-old-source-file', pre_edit_file] +
41+
['-source-file', post_edit_file] +
42+
incremental_edit_args + reparse_args +
43+
['-out', after_roundtrip_file])
44+
except subprocess.CalledProcessError as e:
45+
print('Test case "%s" of %s FAILed' % (test_case, test_file),
46+
file=sys.stderr)
47+
print('Parsing the swift file failed:\n', file=sys.stderr)
48+
print(e.output, file=sys.stderr)
49+
sys.exit(1)
50+
51+
# Check if the two syntax trees are the same
52+
try:
53+
run_command(
54+
[
55+
'diff', '-u',
56+
post_edit_file,
57+
after_roundtrip_file
58+
])
59+
except subprocess.CalledProcessError as e:
60+
print('Test case "%s" of %s FAILed' % (test_case, test_file),
61+
file=sys.stderr)
62+
print('Source file after incrementally parsing '
63+
'does not match post-edit source file:\n\n',
64+
file=sys.stderr)
65+
print(e.output, file=sys.stderr)
66+
sys.exit(1)
1267

1368

1469
def main():
@@ -43,7 +98,7 @@ def main():
4398
help='A temporary directory where pre-edit and post-edit files can be \
4499
saved')
45100
parser.add_argument(
46-
'--swift-syntax-test', required=True,
101+
'--swift-syntax-test', required=False,
47102
help='The path to swift-syntax-test')
48103
parser.add_argument(
49104
'--swiftsyntax-lit-test-helper', required=True,
@@ -67,6 +122,21 @@ def main():
67122
if not os.path.exists(temp_dir):
68123
os.makedirs(temp_dir)
69124

125+
if not swift_syntax_test:
126+
pre_edit_file = temp_dir + '/' + test_file_name + '.' + test_case + \
127+
'.pre.swift'
128+
post_edit_file = temp_dir + '/' + test_file_name + '.' + test_case + \
129+
'.post.swift'
130+
after_roundtrip_file = temp_dir + '/' + test_file_name + '.' \
131+
+ test_case + '.post_after_roundtrip.swift'
132+
return testWithParserLib(test_file=test_file,
133+
test_case=test_case,
134+
pre_edit_file=pre_edit_file,
135+
post_edit_file=post_edit_file,
136+
after_roundtrip_file=after_roundtrip_file,
137+
swiftsyntax_lit_test_helper=swiftsyntax_lit_test_helper)
138+
139+
70140
treeFileExtension = serialization_format
71141

72142
pre_edit_tree_file = temp_dir + '/' + test_file_name + '.' \

utils/incrparse/test_util.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -105,33 +105,12 @@ def parseLine(line, line_no, test_case, incremental_edit_args, reparse_args,
105105
return (pre_edit_line, post_edit_line, current_reparse_start)
106106

107107

108-
def serializeIncrParseMarkupFile(test_file, test_case, mode,
109-
serialization_mode, serialization_format,
110-
omit_node_ids, output_file, temp_dir,
111-
swift_syntax_test, print_visual_reuse_info):
112-
test_file_name = os.path.basename(test_file)
113-
pre_edit_file = temp_dir + '/' + test_file_name + '.' + test_case + \
114-
'.pre.swift'
115-
post_edit_file = temp_dir + '/' + test_file_name + '.' + test_case + \
116-
'.post.swift'
117-
118-
if not os.path.exists(temp_dir):
119-
os.makedirs(temp_dir)
120-
121-
# =========================================================================
122-
# First generate the pre-edit and post-edit Swift file and gather the edits
123-
# and expected reparse regions. This is the parser for the special edit
124-
# markup for testing incremental parsing
125-
# =========================================================================
126-
108+
def prepareForIncrParse(test_file, test_case, pre_edit_file, post_edit_file,
109+
incremental_edit_args, reparse_args):
127110
with open(test_file, mode='r') as test_file_handle, \
128111
open(pre_edit_file, mode='w+') as pre_edit_file_handle, \
129112
open(post_edit_file, mode='w+') as post_edit_file_handle:
130113

131-
# Gather command line arguments for swift-syntax-test specifiying the
132-
# performed edits in this list
133-
incremental_edit_args = []
134-
reparse_args = []
135114
current_reparse_start = None
136115

137116
line_no = 1
@@ -151,6 +130,33 @@ def serializeIncrParseMarkupFile(test_file, test_case, mode,
151130
raise TestFailedError('Unclosed reparse tag for test case %s' %
152131
test_case)
153132

133+
134+
def serializeIncrParseMarkupFile(test_file, test_case, mode,
135+
serialization_mode, serialization_format,
136+
omit_node_ids, output_file, temp_dir,
137+
swift_syntax_test, print_visual_reuse_info):
138+
test_file_name = os.path.basename(test_file)
139+
pre_edit_file = temp_dir + '/' + test_file_name + '.' + test_case + \
140+
'.pre.swift'
141+
post_edit_file = temp_dir + '/' + test_file_name + '.' + test_case + \
142+
'.post.swift'
143+
144+
if not os.path.exists(temp_dir):
145+
os.makedirs(temp_dir)
146+
147+
# =========================================================================
148+
# First generate the pre-edit and post-edit Swift file and gather the edits
149+
# and expected reparse regions. This is the parser for the special edit
150+
# markup for testing incremental parsing
151+
# =========================================================================
152+
153+
# Gather command line arguments for swift-syntax-test specifiying the
154+
# performed edits in this list
155+
incremental_edit_args = []
156+
reparse_args = []
157+
prepareForIncrParse(test_file, test_case, pre_edit_file, post_edit_file,
158+
incremental_edit_args, reparse_args)
159+
154160
# =========================================================================
155161
# Now generate the requested serialized file
156162
# =========================================================================

0 commit comments

Comments
 (0)