lingologos-thinkingpython / tests /test_preflight.py
tailot's picture
thinking-python: reference implementation, corpus and gates
4bc63c7 verified
Raw History Blame Contribute Delete
2.78 kB
"""Workspace admission: the reason code is the deliverable."""
import os
import shutil
import sys
import tempfile
import unittest
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ROOT)
from lingologos_thinkingpython import preflight # noqa: E402
def _write(root, name, text):
path = os.path.join(root, name)
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8", newline="") as handle:
handle.write(text)
class Admission(unittest.TestCase):
def setUp(self):
self.temp = tempfile.mkdtemp(prefix="admit_")
def tearDown(self):
shutil.rmtree(self.temp, ignore_errors=True)
def test_no_test_file_refuses(self):
_write(self.temp, "app.py", "x = 1\n")
report = preflight.admit(self.temp)
self.assertFalse(report["admitted"])
self.assertEqual(report["reasons"], ["NO_TEST_FILE"])
def test_tabs_refuse(self):
_write(self.temp, "tests/test_a.py", "def test_a():\n assert True\n")
_write(self.temp, "app.py", "def f():\n\treturn 1\n")
report = preflight.admit(self.temp)
self.assertIn("TAB_INDENTED_TARGET", report["reasons"])
def test_crlf_refuses(self):
_write(self.temp, "tests/test_a.py", "def test_a():\n assert True\n")
_write(self.temp, "app.py", "x = 1\r\ny = 2\r\n")
report = preflight.admit(self.temp)
self.assertIn("CRLF_TARGET", report["reasons"])
def test_non_ascii_refuses(self):
_write(self.temp, "tests/test_a.py", "def test_a():\n assert True\n")
_write(self.temp, "app.py", "note = 'caf\xc3\xa9'\n")
report = preflight.admit(self.temp)
self.assertIn("NOT_ASCII_TARGET", report["reasons"])
def test_dynamic_reach_refuses(self):
_write(self.temp, "tests/test_a.py", "def test_a():\n assert True\n")
_write(self.temp, "app.py", "def f(obj, key):\n return getattr(obj, key)\n")
report = preflight.admit(self.temp)
self.assertIn("DYNAMIC_REACH", report["reasons"])
def test_generated_target_refuses(self):
_write(self.temp, "tests/test_a.py", "def test_a():\n assert True\n")
_write(self.temp, "wire_pb2.py", "x = 1\n")
_write(self.temp, "app.py", "x = 1\n")
report = preflight.admit(self.temp)
self.assertIn("GENERATED_TARGET", report["reasons"])
def test_clean_workspace_is_admitted(self):
_write(self.temp, "tests/test_a.py", "def test_a():\n assert True\n")
_write(self.temp, "app.py", "def f(value: int):\n return value + 1\n")
report = preflight.admit(self.temp)
self.assertTrue(report["admitted"], report)
if __name__ == "__main__":
unittest.main()