返回 last30days-skill
test_env_export_prefix.py
根目录 / tests / test_env_export_prefix.py
1 """``export KEY=value`` lines in a .env file set KEY (#930).
2
3 The loader and the setup wizard's writers must agree on which key a line
4 names: if only the loader understood ``export``, the wizard would miss a
5 hand-set ``export KEY=...`` line and append a duplicate that overrides it.
6 """
7
8 import pytest
9
10 from lib import env, setup_wizard
11
12
13 def _write(tmp_path, text):
14 env_path = tmp_path / ".env"
15 env_path.write_text(text, encoding="utf-8")
16 env_path.chmod(0o600)
17 return env_path
18
19
20 def _load(tmp_path, text):
21 return env.load_env_file(_write(tmp_path, text))
22
23
24 @pytest.mark.parametrize("sep", [" ", "\t", " "])
25 def test_export_prefix_is_dropped_from_key(tmp_path, sep):
26 assert _load(tmp_path, f"export{sep}FOO=bar\n") == {"FOO": "bar"}
27
28
29 def test_export_prefix_combines_with_quotes_and_inline_comment(tmp_path):
30 loaded = _load(tmp_path, 'export NAME="Jane # Doe" # annotation\n')
31 assert loaded == {"NAME": "Jane # Doe"}
32
33
34 def test_exported_settings_reach_get_config(tmp_path, monkeypatch):
35 env_path = _write(tmp_path, "export FUN_LEVEL=high\n")
36 monkeypatch.setenv("LAST30DAYS_CONFIG_DIR", str(tmp_path))
37 monkeypatch.delenv("FUN_LEVEL", raising=False)
38 monkeypatch.setattr(env, "CONFIG_DIR", tmp_path)
39 monkeypatch.setattr(env, "CONFIG_FILE", env_path)
40 monkeypatch.setattr(env, "_load_keychain", lambda *args, **kwargs: {})
41 monkeypatch.setattr(env, "_load_pass", lambda *args, **kwargs: {})
42 monkeypatch.chdir(tmp_path)
43
44 assert env.get_config()["FUN_LEVEL"] == "high"
45
46
47 @pytest.mark.parametrize("value", ["$HOME", "$(printf sentinel)", "`printf sentinel`"])
48 def test_export_does_not_evaluate_shell_syntax(tmp_path, value):
49 assert _load(tmp_path, f"export RAW={value}\n") == {"RAW": value}
50
51
52 def test_export_preserves_empty_disable_values_but_ignores_empty_secrets(tmp_path):
53 loaded = _load(
54 tmp_path,
55 'export LAST30DAYS_YT_PLAYER_CLIENT= # disabled\n'
56 'export LAST30DAYS_MEMORY_DIR=""\n'
57 'export XAI_API_KEY= # not configured\n',
58 )
59 assert loaded == {"LAST30DAYS_YT_PLAYER_CLIENT": "", "LAST30DAYS_MEMORY_DIR": ""}
60
61
62 @pytest.mark.parametrize("line, key", [
63 ("export=literal\n", "export"),
64 ("export =literal\n", "export"),
65 ("exporter=1\n", "exporter"),
66 ("EXPORT FOO=1\n", "EXPORT FOO"),
67 ("set FOO=1\n", "set FOO"),
68 ])
69 def test_export_is_only_a_prefix_when_a_key_follows(tmp_path, line, key):
70 assert list(_load(tmp_path, line)) == [key]
71
72
73 def test_write_api_key_sees_export_line_as_present(tmp_path):
74 env_path = _write(tmp_path, "export SCRAPECREATORS_API_KEY=hand_set\n")
75
76 assert setup_wizard.write_api_key(env_path, "sc_new_value") is True
77
78 assert env_path.read_text() == "export SCRAPECREATORS_API_KEY=hand_set\n"
79 assert env.load_env_file(env_path)["SCRAPECREATORS_API_KEY"] == "hand_set"
80
81
82 def test_replace_rewrites_export_line_and_keeps_the_prefix(tmp_path):
83 env_path = _write(tmp_path, "export X_BEARER_TOKEN=stale\nOTHER=1\n")
84
85 setup_wizard.write_api_key(env_path, "fresh", key_name="X_BEARER_TOKEN", replace=True)
86
87 # Rewritten in place, not appended, so the stale secret is gone; the
88 # prefix stays so a shell that sources the file still exports the key.
89 assert env_path.read_text() == "export X_BEARER_TOKEN=fresh\nOTHER=1\n"
90 assert env.load_env_file(env_path) == {"X_BEARER_TOKEN": "fresh", "OTHER": "1"}
91
92
93 @pytest.mark.parametrize("text", [
94 "X_BEARER_TOKEN=old\nexport X_BEARER_TOKEN=older\n",
95 "export X_BEARER_TOKEN=old\nX_BEARER_TOKEN=older\n",
96 ])
97 def test_replace_keeps_the_prefix_when_any_duplicate_is_exported(tmp_path, text):
98 env_path = _write(tmp_path, text)
99
100 setup_wizard.write_api_key(env_path, "fresh", key_name="X_BEARER_TOKEN", replace=True)
101
102 # The duplicates collapse to one line; dropping the prefix would stop a
103 # shell that sources the file from exporting the key it used to export.
104 assert env_path.read_text() == "export X_BEARER_TOKEN=fresh\n"
105
106
107 def test_replace_does_not_add_the_prefix_to_a_plain_line(tmp_path):
108 env_path = _write(tmp_path, "X_BEARER_TOKEN =stale\n")
109
110 setup_wizard.write_api_key(env_path, "fresh", key_name="X_BEARER_TOKEN", replace=True)
111
112 assert env_path.read_text() == "X_BEARER_TOKEN=fresh\n"
113
114
115 def test_write_setup_config_sees_export_line_as_present(tmp_path):
116 env_path = _write(tmp_path, "export SETUP_COMPLETE=true\n")
117
118 assert setup_wizard.write_setup_config(env_path) is True
119
120 assert env_path.read_text() == "export SETUP_COMPLETE=true\n"
121
122
123 def test_write_setup_config_rotates_exported_consent_and_preserves_browser(tmp_path):
124 env_path = _write(
125 tmp_path,
126 "export SETUP_COMPLETE=true\n"
127 "export FROM_BROWSER=firefox\n"
128 "export BROWSER_CONSENT=true\n"
129 "BROWSER_CONSENT=true\n",
130 )
131
132 assert setup_wizard.write_setup_config(
133 env_path, from_browser="chrome", browser_consent=False,
134 ) is True
135
136 assert env_path.read_text() == (
137 "export SETUP_COMPLETE=true\n"
138 "export FROM_BROWSER=firefox\n"
139 "export BROWSER_CONSENT=false\n"
140 )
141 assert env.load_env_file(env_path) == {
142 "SETUP_COMPLETE": "true", "FROM_BROWSER": "firefox", "BROWSER_CONSENT": "false",
143 }
144
144 lines PYTHON