2424
2525If --force is present, existing files may be overwritten.
2626
27- Requirements: `mvn` and `codeql` should both appear on your path.
27+ Requirements:
28+ - `mvn` and `codeql` should both appear on your path.
29+ - `--additional-packs /path/to/semmle-code/ql` should be added to your `.config/codeql/config` file.
2830
2931After test generation completes, any lines in specsToTest.csv that didn't produce tests are output.
3032If this happens, check the spelling of class and method names, and the syntax of input and output specifications.
5254
5355resultJava = os .path .join (sys .argv [3 ], "Test.java" )
5456resultQl = os .path .join (sys .argv [3 ], "test.ql" )
57+ resultYml = os .path .join (sys .argv [3 ], "test.model.yml" )
58+ resultPack = os .path .join (sys .argv [3 ], "qlpack.yml" )
5559
56- if not force and (os .path .exists (resultJava ) or os .path .exists (resultQl )):
57- print ("Won't overwrite existing files '%s' or '%s'" %
58- (resultJava , resultQl ), file = sys .stderr )
60+ if not force and (os .path .exists (resultJava ) or os .path .exists (resultQl ) or os . path . exists ( resultYml ) or os . path . exists ( resultPack ) ):
61+ print ("Won't overwrite existing files '%s', '%s', '%s' or '%s'. " %
62+ (resultJava , resultQl , resultYml , resultPack ), file = sys .stderr )
5963 sys .exit (1 )
6064
6165workDir = tempfile .mkdtemp ()
@@ -127,7 +131,13 @@ def qualifiedOuterNameFromCsvRow(row):
127131os .makedirs (queryDir )
128132qlFile = os .path .join (queryDir , "gen.ql" )
129133with open (os .path .join (queryDir , "qlpack.yml" ), "w" ) as f :
130- f .write ("name: test-generation-query\n version: 0.0.0\n libraryPathDependencies: codeql/java-queries" )
134+ f .write ("""name: test-generation-query
135+ version: 0.0.0
136+ dependencies:
137+ codeql/java-all: '*'
138+ codeql/java-queries: '*'
139+ """ )
140+
131141with open (qlFile , "w" ) as f :
132142 f .write (
133143 "import java\n import utils.flowtestcasegenerator.GenerateFlowTestCase\n \n class GenRow extends TargetSummaryModelCsv {\n \n \t override predicate row(string r) {\n \t \t r = [\n " )
@@ -163,9 +173,9 @@ def getTuples(queryName, jsonResult, fname):
163173with open (generatedJson , "r" ) as f :
164174 generateOutput = json .load (f )
165175 expectedTables = ("getTestCase" , "getASupportMethodModel" ,
166- "missingSummaryModelCsv " , "getAParseFailure" , "noTestCaseGenerated" )
176+ "missingSummaryModel " , "getAParseFailure" , "noTestCaseGenerated" )
167177
168- testCaseRows , supportModelRows , missingSummaryModelCsvRows , parseFailureRows , noTestCaseGeneratedRows = \
178+ testCaseRows , supportModelRows , missingSummaryModelRows , parseFailureRows , noTestCaseGeneratedRows = \
169179 tuple ([getTuples (k , generateOutput , generatedJson )
170180 for k in expectedTables ])
171181
@@ -182,9 +192,9 @@ def getTuples(queryName, jsonResult, fname):
182192 print ("Expected exactly one column in noTestCaseGenerated relation (got: %s)" %
183193 json .dumps (noTestCaseGeneratedRows ), file = sys .stderr )
184194
185- if len (missingSummaryModelCsvRows ) != 0 :
186- print ("Tests for some CSV rows were requested that were not in scope (SummaryModelCsv.row does not hold ):\n " +
187- "\n " .join (r [0 ] for r in missingSummaryModelCsvRows ))
195+ if len (missingSummaryModelRows ) != 0 :
196+ print ("Tests for some CSV rows were requested that were not in scope (a summary doesn't already exist ):\n " +
197+ "\n " .join (r [0 ] for r in missingSummaryModelRows ))
188198 sys .exit (1 )
189199 if len (parseFailureRows ) != 0 :
190200 print ("The following rows failed to generate any test case. Check package, class and method name spelling, and argument and result specifications:\n %s" %
@@ -207,11 +217,32 @@ def copyfile(fromName, toFileHandle):
207217
208218with open (resultQl , "w" ) as f :
209219 copyfile ("testHeader.qlfrag" , f )
210- if len (supportModelRows ) != 0 :
211- copyfile ("testModelsHeader.qlfrag" , f )
212- f .write (", " .join ('"%s"' %
213- modelSpecRow [0 ].strip () for modelSpecRow in supportModelRows ))
214- copyfile ("testModelsFooter.qlfrag" , f )
220+
221+ if len (supportModelRows ) != 0 :
222+ # Make a test extension file
223+ with open (resultYml , "w" ) as f :
224+ models = "\n " .join (' - [%s]' %
225+ modelSpecRow [0 ].strip () for modelSpecRow in supportModelRows )
226+ dataextensions = f"""extensions:
227+ - addsTo:
228+ pack: codeql/java-tests
229+ extensible: extSummaryModel
230+ data:
231+ { models }
232+ """
233+ f .write (dataextensions )
234+ # Make a qlpack file such that the extension will be picked up
235+ with open (resultPack , "w" ) as f :
236+ f .write ("""name: example-test-pack
237+ version: 0.0.0
238+ extractor: java
239+ dependencies:
240+ codeql/java-all: '*'
241+ codeql/java-queries: '*'
242+ codeql/java-tests: '*'
243+ dataExtensions:
244+ - test.model.yml
245+ """ )
215246
216247# Make an empty .expected file, since this is an inline-exectations test
217248with open (os .path .join (sys .argv [3 ], "test.expected" ), "w" ):
0 commit comments