Skip to content

Commit 4289e35

Browse files
committed
Python: Add module import test case
This one will require some explanation... First, the file structure. This commit adds a test consisting representing a few different kinds of imports. - Absolute imports, from `module.py` to `main.py` when the latter is executed directly. - A package (contained in the `package` folder) - A namespace package (contained in the `namespace_package` folder) All of these are inside a folder called `code` for reasons I will detail later. The file `main.py` is identified as a script, by the presence of the `!#` comment in its first line. The files themselves are executable, and `python3 main.py` will print out all modules in the order they are imported. The test itself is very simple. It simply lists all modules and their corresponding names. As is plainly visible, without modification we only pick up `package` and its component modules as having names. This is the bit that needs to be fixed. Convincing the test runner to extract this test in a way that mimics reality is, unfortunately, a bit complicated. By default, the test runner itself includes any Python files in the test directory as modules in the invocation of the extractor, and so we must hide everything in the `code` subdirectory. Secondly, a `--path` argument (set to the test directory) is automatically added, and this would also interfere with extraction, and hence we must prevent this. Luckily, if we supply our own `--path` argument -- even if it doesn't make any sense -- then the other argument is left out. Finally, we must actually tell the extractor to extract the files (or it would just happily pass the test with zero files extracted), so the `-R .` argument ensures that we recurse over the files in the test directory after all.
1 parent 119872d commit 4289e35

File tree

10 files changed

+26
-0
lines changed

10 files changed

+26
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#! /usr/bin/python3
2+
print(__file__)
3+
import module
4+
import package
5+
import namespace_package
6+
import namespace_package.namespace_package_main
7+
print(module.message)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
print(__file__.split("entry_point")[1])
2+
message = "Hello world!"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
print(__file__.split("entry_point")[1])
2+
import namespace_package.namespace_package_module
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print(__file__.split("entry_point")[1])
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
print(__file__.split("entry_point")[1])
2+
from . import package_main
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
print(__file__.split("entry_point")[1])
2+
from . import package_module
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print(__file__.split("entry_point")[1])
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| package | code/package:0:0:0:0 | Package package |
2+
| package.__init__ | code/package/__init__.py:0:0:0:0 | Module package.__init__ |
3+
| package.package_main | code/package/package_main.py:0:0:0:0 | Module package.package_main |
4+
| package.package_module | code/package/package_module.py:0:0:0:0 | Module package.package_module |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import python
2+
3+
from Module m
4+
select m.getName(), m
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle-extractor-options: --lang=3 --path bogus -R .

0 commit comments

Comments
 (0)