Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""Unit tests for the PASS0 loader.
 
Uses temporary directories with inline fixtures.
"""
 
from __future__ import annotations
 
import sys
import tempfile
import unittest
from pathlib import Path
 
# Make the wgsl_template package importable regardless of cwd.
_THIS_DIR = Path(__file__).resolve().parent
_PARENT_DIR = _THIS_DIR.parent.parent
if str(_PARENT_DIR) not in sys.path:
    sys.path.insert(0, str(_PARENT_DIR))
 
from wgsl_template.errors import WgslTemplateLoadError  # noqa: E402
from wgsl_template.loader import (  # noqa: E402
    load_from_directories,
    load_from_directory,
)
from wgsl_template.types import SourceDir, TemplatePass0  # noqa: E402
 
 
def _write(path: Path, content: str) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    # Use binary write so callers control line endings exactly.
    path.write_bytes(content.encode("utf-8"))
 
 
class LoaderBasicTest(unittest.TestCase):
    def test_loads_single_file(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            _write(
                root / "basic.wgsl.template",
                "fn a() {\n    var x = 5;\n    var y = 10;\n}\n",
            )
 
            repo = load_from_directory(root)
 
            self.assertEqual(set(repo.templates), {"basic.wgsl.template"})
            tpl = repo.templates["basic.wgsl.template"]
            assert isinstance(tpl, TemplatePass0)
            # Trailing \n means split produces an empty trailing entry.
            self.assertEqual(
                tpl.raw,
                ["fn a() {", "    var x = 5;", "    var y = 10;", "}", ""],
            )
 
    def test_empty_directory_returns_empty_repo(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            repo = load_from_directory(tmp)
            self.assertEqual(repo.templates, {})
 
    def test_recurses_into_subdirectories(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            _write(root / "main.wgsl.template", "main\n")
            _write(root / "shaders" / "vertex.wgsl.template", "vertex\n")
            _write(root / "shaders" / "compute" / "matrix.wgsl.template", "matrix\n")
 
            repo = load_from_directory(root)
 
            self.assertEqual(
                set(repo.templates),
                {
                    "main.wgsl.template",
                    "shaders/vertex.wgsl.template",
                    "shaders/compute/matrix.wgsl.template",
                },
            )
 
    def test_template_keys_are_sorted(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            for name in ["zeta.wgsl.template", "alpha.wgsl.template", "mu.wgsl.template"]:
                _write(root / name, "x\n")
 
            repo = load_from_directory(root)
 
            self.assertEqual(
                list(repo.templates),
                ["alpha.wgsl.template", "mu.wgsl.template", "zeta.wgsl.template"],
            )
 
    def test_skips_files_with_other_extensions(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            _write(root / "keep.wgsl.template", "keep\n")
            _write(root / "drop.txt", "drop\n")
            _write(root / "drop.wgsl", "drop\n")
 
            repo = load_from_directory(root)
 
            self.assertEqual(set(repo.templates), {"keep.wgsl.template"})
 
    def test_uses_posix_paths_in_keys(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            _write(root / "a" / "b" / "c.wgsl.template", "x\n")
 
            repo = load_from_directory(root)
 
            self.assertEqual(set(repo.templates), {"a/b/c.wgsl.template"})
 
 
class LoaderLineEndingsTest(unittest.TestCase):
    def test_lf(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            _write(root / "lf.wgsl.template", "line1\nline2\n")
 
            repo = load_from_directory(root)
            self.assertEqual(repo.templates["lf.wgsl.template"].raw, ["line1", "line2", ""])
 
    def test_crlf(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            _write(root / "crlf.wgsl.template", "line1\r\nline2\r\n")
 
            repo = load_from_directory(root)
            self.assertEqual(repo.templates["crlf.wgsl.template"].raw, ["line1", "line2", ""])
 
    def test_mixed(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            _write(root / "mixed.wgsl.template", "a\r\nb\nc\r\n")
 
            repo = load_from_directory(root)
            self.assertEqual(repo.templates["mixed.wgsl.template"].raw, ["a", "b", "c", ""])
 
 
class LoaderExtensionTest(unittest.TestCase):
    def test_custom_extension(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            _write(root / "shader.wgsl.custom", "x\n")
            _write(root / "ignored.wgsl.template", "x\n")
 
            repo = load_from_directory(root, ext=".wgsl.custom")
            self.assertEqual(set(repo.templates), {"shader.wgsl.custom"})
 
 
class LoaderMultipleDirectoriesTest(unittest.TestCase):
    def test_loads_from_multiple_dirs_without_aliases(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            d1, d2 = root / "d1", root / "d2"
            _write(d1 / "a.wgsl.template", "a\n")
            _write(d2 / "b.wgsl.template", "b\n")
 
            repo = load_from_directories([d1, d2])
            self.assertEqual(set(repo.templates), {"a.wgsl.template", "b.wgsl.template"})
 
    def test_loads_with_aliases(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            d1, d2 = root / "templates", root / "shaders"
            _write(d1 / "base.wgsl.template", "base\n")
            _write(d2 / "vertex.wgsl.template", "vertex\n")
 
            repo = load_from_directories(
                [
                    SourceDir(path=str(d1), alias="@templates"),
                    SourceDir(path=str(d2), alias="@shaders"),
                ]
            )
 
            self.assertEqual(
                set(repo.templates),
                {
                    "@templates/base.wgsl.template",
                    "@shaders/vertex.wgsl.template",
                },
            )
 
    def test_template_name_conflict_raises(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            d1, d2 = root / "d1", root / "d2"
            _write(d1 / "shared.wgsl.template", "v1\n")
            _write(d2 / "shared.wgsl.template", "v2\n")
 
            with self.assertRaises(WgslTemplateLoadError) as ctx:
                load_from_directories([d1, d2])
            self.assertIn("Template name conflict", str(ctx.exception))
 
 
class LoaderErrorsTest(unittest.TestCase):
    def test_missing_directory_raises(self) -> None:
        with self.assertRaises(WgslTemplateLoadError) as ctx:
            load_from_directory("/no/such/path/does/not/exist/abc123")
        self.assertIn("Cannot access directory", str(ctx.exception))
 
    def test_path_is_a_file_raises(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            file_path = root / "notadir"
            file_path.write_text("x")
            with self.assertRaises(WgslTemplateLoadError) as ctx:
                load_from_directory(file_path)
            self.assertIn("not a directory", str(ctx.exception))
 
    def test_no_directories_raises(self) -> None:
        with self.assertRaises(WgslTemplateLoadError):
            load_from_directories([])
 
 
if __name__ == "__main__":  # pragma: no cover
    unittest.main(verbosity=2)