mirror of
https://github.com/github/codeql.git
synced 2026-01-06 11:10:23 +01:00
A slightly complicated test setup. I wanted to both make sure I captured the semantics of Python and also the fact that the kinds of global flow we expect to see are indeed present. The code is executable, and prints out both when the execution reaches certain files, and also what values are assigned to the various attributes that are referenced throughout the program. These values are validated in the test as well. My original version used introspection to avoid referencing attributes directly (thus enabling better error diagnostics), but unfortunately that made it so that the model couldn't follow what was going on. The current setup is a bit clunky (and Python's scoping rules makes it especially so -- cf. the explicit calls to `globals` and `locals`), but I think it does the job okay.
50 lines
1.4 KiB
Plaintext
50 lines
1.4 KiB
Plaintext
import python
|
|
import TestUtilities.InlineExpectationsTest
|
|
import semmle.python.dataflow.new.DataFlow
|
|
import semmle.python.dataflow.new.internal.ImportResolution
|
|
|
|
private class ImmediateModuleRef extends DataFlow::Node {
|
|
Module mod;
|
|
string alias;
|
|
|
|
ImmediateModuleRef() {
|
|
this = ImportResolution::getImmediateModuleReference(mod) and
|
|
not mod.getName() in ["__future__", "trace"] and
|
|
this.asExpr() = any(Alias a | alias = a.getAsname().(Name).getId()).getAsname()
|
|
}
|
|
|
|
Module getModule() { result = mod }
|
|
|
|
string getAsname() { result = alias }
|
|
}
|
|
|
|
class ImportTest extends InlineExpectationsTest {
|
|
ImportTest() { this = "ImportTest" }
|
|
|
|
override string getARelevantTag() { result = "imports" }
|
|
|
|
override predicate hasActualResult(Location location, string element, string tag, string value) {
|
|
exists(ImmediateModuleRef ref |
|
|
tag = "imports" and
|
|
location = ref.getLocation() and
|
|
value = ref.getModule().getName() and
|
|
element = ref.toString()
|
|
)
|
|
}
|
|
}
|
|
|
|
class AliasTest extends InlineExpectationsTest {
|
|
AliasTest() { this = "AliasTest" }
|
|
|
|
override string getARelevantTag() { result = "as" }
|
|
|
|
override predicate hasActualResult(Location location, string element, string tag, string value) {
|
|
exists(ImmediateModuleRef ref |
|
|
tag = "as" and
|
|
location = ref.getLocation() and
|
|
value = ref.getAsname() and
|
|
element = ref.toString()
|
|
)
|
|
}
|
|
}
|