Skip to content

Commit 0ea2d56

Browse files
committed
Python tests: Move helper predicate into TestUtils.qll
1 parent 5dd0123 commit 0ea2d56

File tree

4 files changed

+15
-24
lines changed

4 files changed

+15
-24
lines changed

python/ql/src/semmle/python/TestUtils.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ string remove_prefix_before_substring(string str, string sub) {
2222
string remove_library_prefix(Location loc) {
2323
result = remove_prefix_before_substring(loc.toString(), "resources/lib")
2424
}
25+
26+
/** Returns the location of an AST node in compact form: `basename:line:column` */
27+
string compact_location(AstNode a) {
28+
exists(Location l |
29+
l = a.getLocation() |
30+
result = l.getFile().getBaseName() + ":" + l.getStartLine() + ":" + l.getStartColumn()
31+
)
32+
}
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import python
2-
3-
string loc(ControlFlowNode f) {
4-
exists(Location l |
5-
l = f.getLocation() |
6-
result = l.getFile().getBaseName() + ":" + l.getStartLine() + ":" + l.getStartColumn()
7-
)
8-
}
2+
import semmle.python.TestUtils
93

104
from ControlFlowNode p, ControlFlowNode s, string what
115
where
@@ -20,4 +14,5 @@ or
2014
// Add fake edges for node that raise out of scope
2115
p.isExceptionalExit(_) and s = p.getScope().getEntryNode() and what = "exit"
2216

23-
select loc(p), p.getNode().toString(), loc(s), s.getNode().toString(), what
17+
select compact_location(p.getNode()), p.getNode().toString(),
18+
compact_location(s.getNode()), s.getNode().toString(), what
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import python
2-
3-
4-
string loc(AstNode a) {
5-
exists(Location l |
6-
l = a.getLocation() |
7-
result = l.getFile().getBaseName() + ":" + l.getStartLine() + ":" + l.getStartColumn()
8-
)
9-
}
2+
import semmle.python.TestUtils
103

114
from AstNode p, AstNode c
125
where p.getAChildNode() = c
13-
select loc(p), p.toString(), loc(c), c.toString()
6+
select compact_location(p), p.toString(), compact_location(c), c.toString()
147

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11

22
import python
3+
import semmle.python.TestUtils
34

4-
string loc(AstNode f) {
5-
exists(Location l |
6-
l = f.getLocation() |
7-
result = l.getFile().getBaseName() + ":" + l.getStartLine() + ":" + l.getStartColumn()
8-
)
9-
}
105

116
from Compare comp, Expr left, Expr right, Cmpop op
127
where comp.compares(left, op, right)
13-
select loc(comp), comp.toString(), left.toString(), op.toString(), right.toString()
8+
select compact_location(comp), comp.toString(), left.toString(), op.toString(), right.toString()

0 commit comments

Comments
 (0)