返回 last30days-skill
test_env_config_read_errors.py
根目录 / tests / test_env_config_read_errors.py
1 """Config file read errors: ordinary loaders keep swallowing them, opted-in callers can retry."""
2
3 import errno
4 import os
5
6 import pytest
7
8 from lib import env
9
10
11 def _write(tmp_path, text="LAST30DAYS_SEARXNG_URL=https://searx.example\n"):
12 path = tmp_path / "config.env"
13 path.write_text(text, encoding="utf-8")
14 return path
15
16
17 def _fail_open(monkeypatch, path, error):
18 real_open = os.open
19
20 def flaky(target, *args, **kwargs):
21 if os.fspath(target) == os.fspath(path):
22 raise error
23 return real_open(target, *args, **kwargs)
24
25 monkeypatch.setattr(os, "open", flaky)
26
27
28 def _fail_read(monkeypatch, error):
29 def flaky(fd, *args, **kwargs):
30 raise error
31
32 monkeypatch.setattr(os, "fdopen", flaky)
33
34
35 @pytest.mark.parametrize("site", ["open", "read"])
36 def test_default_loader_still_swallows_a_read_error(site, tmp_path, monkeypatch):
37 path = _write(tmp_path)
38 if site == "open":
39 _fail_open(monkeypatch, path, PermissionError(errno.EACCES, "in use"))
40 else:
41 _fail_read(monkeypatch, OSError(errno.EIO, "input/output error"))
42
43 assert env.load_env_file(path) == {}
44
45
46 @pytest.mark.parametrize("site", ["open", "read"])
47 def test_opted_in_loader_raises_a_retryable_error_without_the_path(site, tmp_path, monkeypatch):
48 path = _write(tmp_path)
49 cause = PermissionError(errno.EACCES, "in use") if site == "open" else OSError(errno.EIO, "io")
50 if site == "open":
51 _fail_open(monkeypatch, path, cause)
52 else:
53 _fail_read(monkeypatch, cause)
54
55 with pytest.raises(env.ConfigReadError) as caught:
56 env.load_env_file(path, raise_read_errors=True)
57
58 assert caught.value.__cause__ is cause
59 assert os.fspath(path) not in str(caught.value) and "searx" not in str(caught.value)
60
61
62 @pytest.mark.parametrize("error", [
63 FileNotFoundError(errno.ENOENT, "missing"),
64 NotADirectoryError(errno.ENOTDIR, "not a directory"),
65 OSError(errno.ELOOP, "symlink refused"),
66 IsADirectoryError(errno.EISDIR, "is a directory"),
67 ])
68 def test_opted_in_loader_treats_absent_or_unsafe_files_as_empty(error, tmp_path, monkeypatch):
69 path = _write(tmp_path)
70 _fail_open(monkeypatch, path, error)
71
72 assert env.load_env_file(path, raise_read_errors=True) == {}
73
74
75 def test_opted_in_loader_still_reads_a_good_file(tmp_path):
76 path = _write(tmp_path)
77
78 assert env.load_env_file(path, raise_read_errors=True) == {
79 "LAST30DAYS_SEARXNG_URL": "https://searx.example",
80 }
81
82
83 @pytest.mark.parametrize("opted_in", [False, True], ids=["default", "opted-in"])
84 def test_project_layer_follows_the_same_read_error_policy(opted_in, tmp_path, monkeypatch):
85 project = tmp_path / "last30days.env"
86 project.write_text("LAST30DAYS_SEARXNG_URL=https://project.example\n", encoding="utf-8")
87 monkeypatch.setattr(env, "CONFIG_FILE", None)
88 monkeypatch.setattr(env, "_find_project_env", lambda: project)
89 _fail_open(monkeypatch, project, PermissionError(errno.EACCES, "in use"))
90 policy = env.ConfigLoadPolicy(allow_project_config=True)
91
92 if opted_in:
93 with pytest.raises(env.ConfigReadError):
94 env._load_config_files(policy, raise_read_errors=True)
95 else:
96 assert env._load_config_files(policy)[1] == {}
97
98
99 def test_project_layer_still_refuses_a_symlinked_file(tmp_path, monkeypatch):
100 project = tmp_path / "last30days.env"
101 project.write_text("LAST30DAYS_SEARXNG_URL=https://project.example\n", encoding="utf-8")
102 monkeypatch.setattr(env, "CONFIG_FILE", None)
103 monkeypatch.setattr(env, "_find_project_env", lambda: project)
104 monkeypatch.setattr(env, "_is_symlink_or_junction", lambda path: path == project)
105 policy = env.ConfigLoadPolicy(allow_project_config=True)
106
107 assert env._load_config_files(policy)[1] == {}
108 assert env._load_config_files(policy, raise_read_errors=True)[1] == {}
109
109 lines PYTHON