45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
import subprocess, sys, os
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
SCRIPT = os.path.join(HERE, "shadow_lint.py")
|
|
|
|
def run(src):
|
|
p = "/tmp/scratchpad/test_shadow.py"
|
|
os.makedirs("/tmp/scratchpad", exist_ok=True)
|
|
open(p,"w").write(src)
|
|
r = subprocess.run([sys.executable, SCRIPT, p], capture_output=True, text=True)
|
|
return r.returncode, r.stdout + r.stderr
|
|
|
|
def test_param_shadow():
|
|
rc, out = run("x = 1\ndef f(x):\n return x\n")
|
|
assert rc == 1
|
|
assert "function-param" in out and "'x'" in out
|
|
|
|
def test_no_shadow():
|
|
rc, out = run("x = 1\ndef f(y):\n return x + y\n")
|
|
assert rc == 0, out
|
|
|
|
def test_import_shadow():
|
|
rc, out = run("import os\ndef f():\n import os\n return os\n")
|
|
assert rc == 1 and "import" in out
|
|
|
|
def test_loop_target_shadow():
|
|
rc, out = run("def outer():\n i = 0\n def inner():\n for i in range(3):\n pass\n")
|
|
assert rc == 1 and "loop-target" in out
|
|
|
|
def test_comprehension_shadow():
|
|
rc, out = run("x = 1\ny = [x for x in range(3)]\n")
|
|
assert rc == 1 and "comprehension" in out
|
|
|
|
def test_with_as_shadow():
|
|
rc, out = run("def outer():\n f = 1\n def inner():\n with open('/dev/null') as f:\n pass\n")
|
|
assert rc == 1 and "with-as" in out
|
|
|
|
if __name__ == "__main__":
|
|
for name in [n for n in globals() if n.startswith("test_")]:
|
|
globals()[name]()
|
|
print("PASS", name)
|
|
print("ALL PASS")
|
|
|
|
if __name__ == "__main__":
|
|
pass
|