Skip to content

Commit b844e68

Browse files
committed
Python: Post-processing query for inline test expectations
1 parent 75c8409 commit b844e68

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

python/ql/test/TestUtilities/InlineExpectationsTest.qll

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,5 @@
55

66
private import python as PY
77
private import codeql.util.test.InlineExpectationsTest
8-
9-
private module Impl implements InlineExpectationsTestSig {
10-
/**
11-
* A class representing line comments in Python. As this is the only form of comment Python
12-
* permits, we simply reuse the `Comment` class.
13-
*/
14-
class ExpectationComment = PY::Comment;
15-
16-
class Location = PY::Location;
17-
}
18-
8+
private import internal.InlineExpectationsTestImpl
199
import Make<Impl>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @kind test-postprocess
3+
*/
4+
5+
private import python
6+
private import codeql.util.test.InlineExpectationsTest as T
7+
private import internal.InlineExpectationsTestImpl
8+
import T::TestPostProcessing
9+
import T::TestPostProcessing::Make<Impl, Input>
10+
11+
private module Input implements T::TestPostProcessing::InputSig<Impl> {
12+
string getRelativeUrl(Location location) {
13+
exists(File f, int startline, int startcolumn, int endline, int endcolumn |
14+
location.hasLocationInfo(_, startline, startcolumn, endline, endcolumn) and
15+
f = location.getFile()
16+
|
17+
result =
18+
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
19+
)
20+
}
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
private import python as PY
2+
private import codeql.util.test.InlineExpectationsTest
3+
4+
module Impl implements InlineExpectationsTestSig {
5+
/**
6+
* A class representing line comments in Python. As this is the only form of comment Python
7+
* permits, we simply reuse the `Comment` class.
8+
*/
9+
class ExpectationComment = PY::Comment;
10+
11+
class Location = PY::Location;
12+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Numerics/Pythagorean.ql
1+
query: Numerics/Pythagorean.ql
2+
postprocess: TestUtilities/InlineExpectationsTestQuery.ql

python/ql/test/query-tests/Numerics/pythagorean_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
from math import sqrt
44

55
def withPow(a, b):
6-
return sqrt(a**2 + b**2)
6+
return sqrt(a**2 + b**2) # $ Alert
77

88
def withMul(a, b):
9-
return sqrt(a*a + b*b)
9+
return sqrt(a*a + b*b) # $ Alert
1010

1111
def withRef(a, b):
1212
a2 = a**2
1313
b2 = b*b
14-
return sqrt(a2 + b2)
14+
return sqrt(a2 + b2) # $ Alert

0 commit comments

Comments
 (0)