Skip to content

Commit 6798825

Browse files
committed
Shared: Post-processing query for inline test expectations
1 parent eaaf50c commit 6798825

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

shared/util/codeql/util/test/InlineExpectationsTest.qll

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,3 +584,69 @@ private string expectationPattern() {
584584
result = tags + "(?:=" + value + ")?"
585585
)
586586
}
587+
588+
/**
589+
* Provides logic for creating a `@kind test-postprocess` query that checks
590+
* inline test expectations using `$ BAD` markers.
591+
*/
592+
module TestPostProcessing {
593+
external predicate queryResults(string relation, int row, int column, string data);
594+
595+
external predicate queryRelations(string relation);
596+
597+
signature module InputSig<InlineExpectationsTestSig Input> {
598+
string getRelativeUrl(Input::Location location);
599+
}
600+
601+
module Make<InlineExpectationsTestSig Input, InputSig<Input> Input2> {
602+
private import InlineExpectationsTest as InlineExpectationsTest
603+
private import InlineExpectationsTest::Make<Input>
604+
605+
private module TestInput implements TestSig {
606+
string getARelevantTag() { result = "BAD" }
607+
608+
predicate hasActualResult(Input::Location location, string element, string tag, string value) {
609+
exists(int row, string loc |
610+
queryResults("#select", row, 0, loc) and
611+
element = "Unexpected result" and
612+
tag = "BAD" and
613+
value = "" and
614+
Input2::getRelativeUrl(location) = loc
615+
)
616+
}
617+
}
618+
619+
private module Test = MakeTest<TestInput>;
620+
621+
private predicate rankedTestFailures(int i, Test::FailureLocatable f) {
622+
f =
623+
rank[i](Test::FailureLocatable f0, string filename, int startLine, int startColumn,
624+
int endLine, int endColumn |
625+
Test::testFailures(f0, _) and
626+
f0.getLocation().hasLocationInfo(filename, startLine, startColumn, endLine, endColumn)
627+
|
628+
f0 order by filename, startLine, startColumn, endLine, endColumn
629+
)
630+
}
631+
632+
query predicate results(string relation, int row, int column, string data) {
633+
queryResults(relation, row, column, data)
634+
or
635+
exists(Test::FailureLocatable f |
636+
relation = "testFailures" and
637+
rankedTestFailures(row, f)
638+
|
639+
column = 0 and data = Input2::getRelativeUrl(f.getLocation())
640+
or
641+
column = 1 and data = f.toString()
642+
)
643+
}
644+
645+
query predicate resultRelations(string relation) {
646+
queryRelations(relation)
647+
or
648+
Test::testFailures(_, _) and
649+
relation = "testFailures"
650+
}
651+
}
652+
}

0 commit comments

Comments
 (0)