Debian Codegraph @

tests/test_loader.py

raw symbols SCIP
1 """Tests for cyclopts.loader module."""
2
3 import sys
4 import textwrap
5 from pathlib import Path
6 from unittest.mock import patch
7
8 import pytest
9
10 from cyclopts.loader import load_app_from_script
11
12
13 @pytest.fixture
14 def sample_app_script(tmp_path):
15 """Create a sample script with a Cyclopts app."""
16 script = tmp_path / "sample_app.py"
17 script.write_text(
18 """\
19 from cyclopts import App
20
21 app = App(name="sample")
22
23 @app.default
24 def main(value: str = "default"):
25 return value
26 """
27 )
28 return script
29
30
31 @pytest.fixture
32 def multi_app_script(tmp_path):
33 """Create a script with multiple Cyclopts apps."""
34 script = tmp_path / "multi_app.py"
35 script.write_text(
36 """\
37 from cyclopts import App
38
39 app1 = App(name="first")
40 app2 = App(name="second")
41
42 @app1.default
43 def main1():
44 return "app1"
45
46 @app2.default
47 def main2():
48 return "app2"
49 """
50 )
51 return script
52
53
54 def test_load_basic_script(sample_app_script):
55 """Test loading a basic script with one app."""
56 app, name = load_app_from_script(sample_app_script)
57 assert name == "app"
58 assert "sample" in app.name
59
60
61 def test_load_with_explicit_app_name(multi_app_script):
62 """Test loading with explicit app name using : notation."""
63 app, name = load_app_from_script(f"{multi_app_script}:app1")
64 assert name == "app1"
65 assert "first" in app.name
66
67 app, name = load_app_from_script(f"{multi_app_script}:app2")
68 assert name == "app2"
69 assert "second" in app.name
70
71
72 def test_load_with_path_object(sample_app_script):
73 """Test loading with Path object."""
74 app, name = load_app_from_script(Path(sample_app_script))
75 assert name == "app"
76 assert "sample" in app.name
77
78
79 def test_load_with_path_object_and_app_name(multi_app_script):
80 """Test loading with Path object using : notation."""
81 # Construct a Path object that contains the :app_name notation
82 path_with_app = Path(f"{multi_app_script}:app1")
83 app, name = load_app_from_script(path_with_app)
84 assert name == "app1"
85 assert "first" in app.name
86
87 path_with_app = Path(f"{multi_app_script}:app2")
88 app, name = load_app_from_script(path_with_app)
89 assert name == "app2"
90 assert "second" in app.name
91
92
93 def test_nonexistent_file(tmp_path):
94 """Test error handling for nonexistent file."""
95 nonexistent = tmp_path / "nonexistent.py"
96 with pytest.raises(SystemExit):
97 load_app_from_script(nonexistent)
98
99
100 def test_non_python_file(tmp_path):
101 """Test error handling for non-Python file."""
102 txt_file = tmp_path / "not_python.txt"
103 txt_file.write_text("This is not Python")
104 with pytest.raises(SystemExit):
105 load_app_from_script(txt_file)
106
107
108 def test_script_without_app(tmp_path):
109 """Test error handling when script has no App objects."""
110 script = tmp_path / "no_app.py"
111 script.write_text("x = 42")
112 with pytest.raises(SystemExit):
113 load_app_from_script(script)
114
115
116 def test_invalid_app_name(sample_app_script):
117 """Test error handling for invalid app name."""
118 with pytest.raises(SystemExit):
119 load_app_from_script(f"{sample_app_script}:nonexistent")
120
121
122 def test_not_an_app_object(tmp_path):
123 """Test error when specified object is not an App."""
124 script = tmp_path / "not_app.py"
125 script.write_text(
126 """\
127 not_an_app = "I am a string"
128 """
129 )
130 with pytest.raises(SystemExit):
131 load_app_from_script(f"{script}:not_an_app")
132
133
134 def test_multiple_apps_without_specification(multi_app_script):
135 """Test error when multiple apps exist without specification."""
136 with pytest.raises(SystemExit):
137 load_app_from_script(multi_app_script)
138
139
140 def test_app_execution_suppressed(tmp_path):
141 """Test that app() calls are suppressed during loading."""
142 script = tmp_path / "executes.py"
143 script.write_text(
144 """\
145 from cyclopts import App
146
147 app = App()
148 executed = False
149
150 @app.default
151 def main():
152 global executed
153 executed = True
154 return "executed"
155
156 # This call should be suppressed
157 result = app()
158 """
159 )
160 app, _ = load_app_from_script(script)
161 assert app is not None
162
163
164 @pytest.mark.skipif(sys.platform != "win32", reason="Windows-specific test")
165 def test_windows_path_with_backslashes(tmp_path):
166 """Test loading script with Windows-style backslashes."""
167 script = tmp_path / "windows_app.py"
168 script.write_text(
169 """\
170 from cyclopts import App
171 app = App(name="windows")
172 """
173 )
174 windows_path = str(script).replace("/", "\\")
175 app, name = load_app_from_script(windows_path)
176 assert name == "app"
177 assert "windows" in app.name
178
179
180 @pytest.mark.skipif(sys.platform != "win32", reason="Windows-specific test")
181 def test_windows_path_with_drive_letter(tmp_path):
182 """Test loading script with Windows drive letter."""
183 script = tmp_path / "drive_app.py"
184 script.write_text(
185 """\
186 from cyclopts import App
187 app = App(name="drive")
188 """
189 )
190 absolute_path = script.resolve()
191 app, name = load_app_from_script(str(absolute_path))
192 assert name == "app"
193 assert "drive" in app.name
194
195
196 @pytest.mark.skipif(sys.platform != "win32", reason="Windows-specific test")
197 def test_windows_mixed_path_separators(tmp_path):
198 """Test loading script with mixed forward/backslashes."""
199 subdir = tmp_path / "subdir"
200 subdir.mkdir()
201 script = subdir / "mixed_app.py"
202 script.write_text(
203 """\
204 from cyclopts import App
205 app = App(name="mixed")
206 """
207 )
208 mixed_path = str(tmp_path).replace("/", "\\") + "/subdir/mixed_app.py"
209 app, name = load_app_from_script(mixed_path)
210 assert name == "app"
211 assert "mixed" in app.name
212
213
214 @pytest.mark.skipif(sys.platform != "win32", reason="Windows-specific test")
215 def test_windows_path_with_app_name_notation(tmp_path):
216 r"""Test Windows path with :app_name notation (e.g., ``C:\path\to\file.py:app``)."""
217 script = tmp_path / "windows_multi_app.py"
218 script.write_text(
219 textwrap.dedent(
220 """\
221 from cyclopts import App
222
223 app1 = App(name="first")
224 app2 = App(name="second")
225
226 @app1.default
227 def main1():
228 return "app1"
229
230 @app2.default
231 def main2():
232 return "app2"
233 """
234 )
235 )
236 # Test with absolute Windows path including drive letter and :app_name
237 absolute_path = script.resolve()
238 app, name = load_app_from_script(f"{absolute_path}:app1")
239 assert name == "app1"
240 assert "first" in app.name
241
242 app, name = load_app_from_script(f"{absolute_path}:app2")
243 assert name == "app2"
244 assert "second" in app.name
245
246
247 @pytest.mark.skipif(sys.platform == "win32", reason="POSIX-specific test")
248 def test_posix_path_resolution(tmp_path):
249 """Test POSIX path resolution."""
250 script = tmp_path / "posix_app.py"
251 script.write_text(
252 """\
253 from cyclopts import App
254 app = App(name="posix")
255 """
256 )
257 app, name = load_app_from_script(script)
258 assert name == "app"
259 assert "posix" in app.name
260
261
262 def test_path_resolution_with_symlinks(tmp_path):
263 """Test loading script through symlink."""
264 script = tmp_path / "original.py"
265 script.write_text(
266 """\
267 from cyclopts import App
268 app = App(name="original")
269 """
270 )
271 link = tmp_path / "link.py"
272 try:
273 link.symlink_to(script)
274 app, name = load_app_from_script(link)
275 assert name == "app"
276 assert "original" in app.name
277 except (OSError, NotImplementedError):
278 pytest.skip("Symlinks not supported on this platform")
279
280
281 def test_relative_path_resolution(tmp_path, monkeypatch):
282 """Test loading script with relative path."""
283 script = tmp_path / "relative_app.py"
284 script.write_text(
285 """\
286 from cyclopts import App
287 app = App(name="relative")
288 """
289 )
290 monkeypatch.chdir(tmp_path)
291 app, name = load_app_from_script("relative_app.py")
292 assert name == "app"
293 assert "relative" in app.name
294
295
296 def test_path_with_spaces(tmp_path):
297 """Test loading script from path with spaces."""
298 subdir = tmp_path / "path with spaces"
299 subdir.mkdir()
300 script = subdir / "spaces app.py"
301 script.write_text(
302 """\
303 from cyclopts import App
304 app = App(name="spaces")
305 """
306 )
307 app, name = load_app_from_script(script)
308 assert name == "app"
309 assert "spaces" in app.name
310
311
312 @patch.dict("os.environ", {"USERPROFILE": "/fake/windows/home"}, clear=False)
313 def test_windows_userprofile_in_path(tmp_path):
314 """Test that Windows USERPROFILE environment variable doesn't break path resolution."""
315 script = tmp_path / "userprofile_app.py"
316 script.write_text(
317 """\
318 from cyclopts import App
319 app = App(name="userprofile")
320 """
321 )
322 app, name = load_app_from_script(script)
323 assert name == "app"
324 assert "userprofile" in app.name
325
326
327 def test_script_with_syntax_error(tmp_path):
328 """Test handling of script with syntax errors."""
329 script = tmp_path / "syntax_error.py"
330 script.write_text("this is not valid python @@@@")
331 with pytest.raises(SyntaxError):
332 load_app_from_script(script)
333
334
335 def test_script_with_import_error(tmp_path):
336 """Test handling of script with import errors."""
337 script = tmp_path / "import_error.py"
338 script.write_text(
339 """\
340 from cyclopts import App
341 import nonexistent_module
342
343 app = App()
344 """
345 )
346 with pytest.raises(ModuleNotFoundError):
347 load_app_from_script(script)
348
349
350 def test_unicode_in_path(tmp_path):
351 """Test loading script from path with Unicode characters."""
352 subdir = tmp_path / "ディレクトリ"
353 subdir.mkdir()
354 script = subdir / "アプリ.py"
355 script.write_text(
356 """\
357 from cyclopts import App
358 app = App(name="unicode")
359 """
360 )
361 app, name = load_app_from_script(script)
362 assert name == "app"
363 assert "unicode" in app.name