Swift: Convert .qlref security query tests to inline expectation tests#21973
Swift: Convert .qlref security query tests to inline expectation tests#21973Copilot wants to merge 2 commits into
Conversation
|
@copilot In |
Fixed in the latest commit. For each of the three folders:
|
This was stopping trailing comments, as in `// $ Alert // some comment`, from working.
c9a0410 to
85e1ae2
Compare
Converts 19 Swift security query
.qlreftests from.expected-file-based testing to inline expectation tests by adding// $ Alert,// $ Source, and// $ Sinkcomments directly in the Swift source files.What changed
.qlreffiles updated to addpostprocess: utils/test/InlineExpectationsTestQuery.ql(compact single-line form).expectedfiles:@kind problemqueries:// $ Alertat each result's end-line@kind path-problemqueries:// $ Alertat alert location,// $ Sourceat source (when on different line),// $ Sinkat sink (when on different line from alert).expectedfiles were modifiedBefore/after example (
CWE-760/ConstantSalt):Lines with pre-existing
// BAD, descriptive textcomments were restructured to// $ Alert // BAD, descriptive textso the$appears first in the comment content (required by the inline expectation parser).Skipped tests (12)
PrintAst.qlreflibrary-tests/ast/Errors.qlref@kindmetadataAlertSuppression.qlref@kind alert-suppressionDiagnostics/ExtractedFiles.qlref@kind diagnosticDiagnostics/SuccessfullyExtractedLines.qlref@kind metricCWE-757/InsecureTLS.qlreffile://synthetic alert locations in#select(9 rows)CWE-321/HardcodedEncryptionKey.qlreffile://synthetic alert locationCWE-078/CommandInjection.qlreffile://synthetic alert locationsCWE-135/StringLengthConflation.qlreffile://synthetic source locations (13 rows)Original prompt
Follow these instructions for Swift.
qlref conversion instructions
Here’s the repeatable recipe for converting a CodeQL language’s
.qlreftests to inline expectation tests.Overall Workflow
cdinto the language folder.Run
grep -rL "InlineExpectationsTestQuery" --include="*.qlref".Ignore generated
.actualfiles,.testprojcopies, and other generated artifacts.For each
.qlref, resolve the referenced query and read its metadata.Classify each test before editing:
-
convert nowfor standard@kind problemand@kind path-problem-
skipfor anything else, with a short concrete reason.expectedfile before converting:- If it is empty and the query is a standard
problemorpath-problemquery, usually convert by adding the postprocessor only. No source comments are needed.- If it is non-empty, add inline expectation comments to the source files at the expected result locations, then add the postprocessor to the
.qlref.Keep the existing
.expectedfile. Do not edit generated expected files by hand, even when converting.Edit only source files and
.qlreffiles.Maintain a skip list for tests not converted, with exact paths and short reasons.
Do not make a PR.
.qlrefFormattingFor a single postprocessor, use the compact form:
If the
.qlrefalready has other postprocessors, use list form:If the original
.qlrefwas a plain single-line query reference, convert it toquery:form only when addingpostprocess:.Always preserve existing postprocessors.
Expectation Comments
Use the language’s supported inline comment syntax from its inline expectation implementation. Typical tags are:
For example, in Java or Kotlin:
For
problemqueries, addAlertat each expected result location.For
path-problemqueries, add:Alertat the alert location.Sourceat the source location, unless the source is on the same line as the alert.Sinkat the sink location when it is a distinct relevant location.Multi-line result locations should be annotated on the end line.
If more than one result is expected on the same line for the same query, just use a single tag in the comment, for example:
Only add comments to real source files that belong to the test. Do not annotate generated copies, shared vendored fixtures, or shared stubs unless that is clearly the right ownership boundary for the test.
Empty
.expectedFilesIf a
problemorpath-problemtest already has an empty.expectedfile, the default conversion is:Leave
.expecteduntouched.Add
postprocess: utils/test/InlineExpectationsTestQuery.qlto the.qlref.Add no source comments unless the test actually has expected results that are currently represented elsewhere.
This is common in zero-result tests.
Query Ids
Do not add
[query-id]by default.Only qualify tags when needed, usually when more than one inline expectation test in the same effective test folder or database can see the same source comments. Then use the query’s metadata id:
Apply this to
Alert,Source, andSinkonly for the comments that need disambiguation.If you add query ids in a shared folder, validate the owner queries for those comments as well, not just the query you were editing.
Good Skip Reasons
Skip, or revert to non-inline, when conversion would make the test misleading, brittle, or noisy. Common reasons:
The query is not
@kind problemor@kind path-problem.The query is a telemetry, metrics, table, definitions, diagnostic, extractor-information, stub-generation, or tool-specific query.
The test is an AST dump or similar structural output test, such as
PrintAst.The selected result text is inside a string literal, raw string, heredoc, text block, XML literal, or multiline literal and adding a comment changes the selected text.
The expected location is inside documentation syntax where adding a comment changes what the query reports, such as Javadoc tags.
The
.expectedoutput is nonstandard enough that mechanical conversion is risky.Results are in shared stubs, generated copies, vendored files, or synthetic/test-project paths where inline comments would pollute shared fixtures.
The alert, source, or sink locations are too dense or ambiguous to make the source understandable.
The language’s inline expectation implementation does not support the source file or comment style needed.
The query uses a special result kind or framework behavior, such as alert-...
Created from VS Code.