File size: 2,779 Bytes
4bc63c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""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()