31 lines
1.4 KiB
Python
31 lines
1.4 KiB
Python
import ast
|
|
import os
|
|
import unittest
|
|
|
|
from _common import SRC
|
|
|
|
# Ограничение проверки: ast.parse(feature_version=(3, 9)) ловит синтаксис 3.10+ (match и т. п.) и сторонние
|
|
# импорты; API и конструкции новее 3.9 (например, `X | Y` — синтаксис 3.10: аннотации функций
|
|
# вычисляются при импорте и их ловит прогон на /usr/bin/python3 3.9.6, аннотации локальных переменных не вычисляются), не проверяет.
|
|
|
|
|
|
class Py39Test(unittest.TestCase):
|
|
def test_agent_parses_as_python_39(self):
|
|
with open(os.path.join(SRC, "agent.py"), encoding="utf-8") as f:
|
|
ast.parse(f.read(), feature_version=(3, 9))
|
|
|
|
def test_no_third_party_imports(self):
|
|
with open(os.path.join(SRC, "agent.py"), encoding="utf-8") as f:
|
|
tree = ast.parse(f.read())
|
|
allowed = {"argparse", "base64", "json", "os", "signal", "ssl", "http", "sys", "time", "urllib", "datetime", "typing", "uuid", "fcntl"}
|
|
for node in ast.walk(tree):
|
|
if isinstance(node, ast.Import):
|
|
for a in node.names:
|
|
self.assertIn(a.name.split(".")[0], allowed, a.name)
|
|
elif isinstance(node, ast.ImportFrom):
|
|
self.assertIn((node.module or "").split(".")[0], allowed, node.module)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|