Skip to content

Commit 19f6cc0

Browse files
committed
Python: rewrite import time test
1 parent 5aee715 commit 19f6cc0

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

python/ql/test/experimental/dataflow/module-initialization/localFlow.ql

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,17 @@ import experimental.dataflow.TestUtil.FlowTest
44
private import semmle.python.dataflow.new.internal.PrintNode
55
private import semmle.python.dataflow.new.internal.DataFlowPrivate as DP
66

7-
class ImportTimeLocalFlowTest extends InlineExpectationsTest {
7+
class ImportTimeLocalFlowTest extends FlowTest {
88
ImportTimeLocalFlowTest() { this = "ImportTimeLocalFlowTest" }
99

10-
override string getARelevantTag() { result = "importTimeFlow" }
10+
override string flowTag() { result = "importTimeFlow" }
1111

12-
override predicate hasActualResult(Location location, string element, string tag, string value) {
13-
exists(DataFlow::Node nodeFrom, DataFlow::ModuleVariableNode nodeTo |
14-
DP::importTimeLocalFlowStep(nodeFrom, nodeTo)
15-
|
16-
nodeFrom.getLocation().getFile().getBaseName() = "multiphase.py" and
17-
location = nodeFrom.getLocation() and
18-
tag = "importTimeFlow" and
19-
value = "\"" + prettyNode(nodeTo).replaceAll("\"", "'") + "\"" and
20-
element = nodeTo.toString()
21-
)
12+
override predicate relevantFlow(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
13+
nodeFrom.getLocation().getFile().getBaseName() = "multiphase.py" and
14+
// results are displayed next to `nodeTo`, so we need a line to write on
15+
nodeTo.getLocation().getStartLine() > 0 and
16+
nodeTo.asVar() instanceof GlobalSsaVariable and
17+
DP::importTimeLocalFlowStep(nodeFrom, nodeTo)
2218
}
2319
}
2420

python/ql/test/experimental/dataflow/module-initialization/multiphase.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
import sys
2-
import os
1+
import sys #$ importTimeFlow="ImportExpr -> GSSA Variable sys"
2+
import os #$ importTimeFlow="ImportExpr -> GSSA Variable os"
33

44
sys.path.append(os.path.dirname(os.path.dirname((__file__))))
55
from testlib import *
66

77
# These are defined so that we can evaluate the test code.
8-
NONSOURCE = "not a source"
9-
SOURCE = "source"
8+
NONSOURCE = "not a source" #$ importTimeFlow="'not a source' -> GSSA Variable NONSOURCE"
9+
SOURCE = "source" #$ importTimeFlow="'source' -> GSSA Variable SOURCE"
1010

1111

12-
def is_source(x):
12+
def is_source(x): #$ importTimeFlow="FunctionExpr -> GSSA Variable is_source"
1313
return x == "source" or x == b"source" or x == 42 or x == 42.0 or x == 42j
1414

1515

16-
def SINK(x):
16+
def SINK(x): #$ importTimeFlow="FunctionExpr -> GSSA Variable SINK"
1717
if is_source(x): #$ runtimeFlow="ModuleVariableNode for multiphase.is_source, l:-17 -> is_source"
1818
print("OK") #$ runtimeFlow="ModuleVariableNode for multiphase.print, l:-18 -> print"
1919
else:
2020
print("Unexpected flow", x) #$ runtimeFlow="ModuleVariableNode for multiphase.print, l:-20 -> print"
2121

2222

23-
def SINK_F(x):
23+
def SINK_F(x): #$ importTimeFlow="FunctionExpr -> GSSA Variable SINK_F"
2424
if is_source(x): #$ runtimeFlow="ModuleVariableNode for multiphase.is_source, l:-24 -> is_source"
2525
print("Unexpected flow", x) #$ runtimeFlow="ModuleVariableNode for multiphase.print, l:-25 -> print"
2626
else:
2727
print("OK") #$ runtimeFlow="ModuleVariableNode for multiphase.print, l:-27 -> print"
2828

29-
def set_foo():
29+
def set_foo(): #$ importTimeFlow="FunctionExpr -> GSSA Variable set_foo"
3030
global foo
31-
foo = SOURCE #$ runtimeFlow="ModuleVariableNode for multiphase.SOURCE, l:-31 -> SOURCE" MISSING:importTimeFlow="ModuleVariableNode for multiphase.foo"
31+
foo = SOURCE #$ runtimeFlow="ModuleVariableNode for multiphase.SOURCE, l:-31 -> SOURCE" # missing final definition of foo
3232

33-
foo = NONSOURCE
33+
foo = NONSOURCE #$ importTimeFlow="NONSOURCE -> GSSA Variable foo"
3434
set_foo()
3535

3636
@expects(2)
37-
def test_phases():
37+
def test_phases(): #$ importTimeFlow="expects(..)(..), l:-1 -> GSSA Variable test_phases"
3838
global foo
3939
SINK(foo) #$ runtimeFlow="ModuleVariableNode for multiphase.SINK, l:-39 -> SINK" runtimeFlow="ModuleVariableNode for multiphase.foo, l:-39 -> foo"
4040
foo = NONSOURCE #$ runtimeFlow="ModuleVariableNode for multiphase.NONSOURCE, l:-40 -> NONSOURCE"

0 commit comments

Comments
 (0)