120120repo_root = Path (__file__ ).parent .parent .parent
121121rule_packages_file_path = repo_root .joinpath ("rule_packages" )
122122env = Environment (loader = FileSystemLoader (Path (__file__ ).parent .joinpath (
123- "templates" )), trim_blocks = True , lstrip_blocks = True )
123+ "templates" )), trim_blocks = True , lstrip_blocks = True , keep_trailing_newline = True )
124124
125125def write_shared_implementation (package_name , rule_id , query , language_name , ql_language_name , common_src_pack_dir , common_test_pack_dir , test_src_dir , skip_tests = False ):
126126
@@ -164,6 +164,7 @@ def write_shared_implementation(package_name, rule_id, query, language_name, ql_
164164 # Write out the test. Test are always stored under the `language_name`
165165 # directory.
166166 if not skip_tests :
167+
167168 shared_impl_test_dir = common_test_pack_dir .joinpath (
168169 "rules" ,
169170 shared_impl_dir_name
@@ -176,31 +177,17 @@ def write_shared_implementation(package_name, rule_id, query, language_name, ql_
176177 f"{ query ['shared_implementation_short_name' ]} .ql"
177178 )
178179
179- with open (shared_impl_test_query_path , "w" , newline = "\n " ) as f :
180- f .write ("// GENERATED FILE - DO NOT MODIFY\n " )
181- f .write (
182- "import "
183- + str (shared_impl_query_library_path .relative_to (common_src_pack_dir ).with_suffix ('' ))
184- .replace ("\\ " , "/" )
185- .replace ("/" , "." )
186- + "\n "
180+ shared_library_test_template = env .get_template (
181+ "shared_library_test.ql.template"
182+ )
183+
184+ if not shared_impl_test_query_path .exists ():
185+ write_template (
186+ shared_library_test_template ,
187+ query ,
188+ package_name ,
189+ shared_impl_test_query_path
187190 )
188- f .write ("\n " )
189- class_name = str (query ["shared_implementation_short_name" ]) + "SharedQuery"
190- f .write ("class TestFileQuery extends " + class_name + "," )
191- # ql formatting of this line depends on the line length
192- if len (class_name ) > 61 :
193- # Line break required after comma
194- f .write ("\n TestQuery\n { }\n " )
195- elif len (class_name ) == 61 :
196- # Line break required before `{`
197- f .write (" TestQuery\n { }\n " )
198- elif len (class_name ) > 57 :
199- # Line break required after `{`
200- f .write (" TestQuery {\n }\n " )
201- else :
202- # Under 100 characters, can be formatted on the same line
203- f .write (" TestQuery { }\n " )
204191
205192 # Create an empty test file, if one doesn't already exist
206193 shared_impl_test_dir .joinpath (
0 commit comments