fix(config): treat empty .env values with inline # comments as empty
All checks were successful
Publish Python Package to Gitea / publish (push) Successful in 32s
All checks were successful
Publish Python Package to Gitea / publish (push) Successful in 32s
This commit is contained in:
@@ -81,6 +81,49 @@ class TestConfig(unittest.TestCase):
|
||||
self.assertEqual(config.get_float("BAD", 9.9), 9.9)
|
||||
self.assertEqual(config.get_int("BAD", 7), 7)
|
||||
|
||||
def test_parse_empty_value_with_inline_comment(self) -> None:
|
||||
"""Regression: ``KEY= # comment`` must be empty, not ``# comment``."""
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
example = os.path.join(tmp, ".env.example")
|
||||
with open(example, "w", encoding="utf-8") as f:
|
||||
f.write(
|
||||
"DEFAULT_ACCOUNT_ID= # 可填账号管理中的编号\n"
|
||||
"DEFAULT_LOGIN_ID=# 只填登录名\n"
|
||||
"HAS_VALUE=real # trailing note\n"
|
||||
"URL=https://example.com/path#frag\n"
|
||||
"QUOTED=\"keep # hash\"\n"
|
||||
"EMPTY=\n"
|
||||
)
|
||||
|
||||
os.environ["JIANGCHANG_DATA_ROOT"] = tmp
|
||||
os.environ["JIANGCHANG_USER_ID"] = "u1"
|
||||
# Clear process overrides for these keys if any
|
||||
for k in (
|
||||
"DEFAULT_ACCOUNT_ID",
|
||||
"DEFAULT_LOGIN_ID",
|
||||
"HAS_VALUE",
|
||||
"URL",
|
||||
"QUOTED",
|
||||
"EMPTY",
|
||||
):
|
||||
os.environ.pop(k, None)
|
||||
|
||||
dest = config.ensure_env_file("sk-comment", example)
|
||||
config.reset_cache()
|
||||
|
||||
parsed = config._parse_env_file(dest)
|
||||
self.assertEqual(parsed.get("DEFAULT_ACCOUNT_ID"), "")
|
||||
self.assertEqual(parsed.get("DEFAULT_LOGIN_ID"), "")
|
||||
self.assertEqual(parsed.get("HAS_VALUE"), "real")
|
||||
self.assertEqual(parsed.get("URL"), "https://example.com/path#frag")
|
||||
self.assertEqual(parsed.get("QUOTED"), "keep # hash")
|
||||
self.assertEqual(parsed.get("EMPTY"), "")
|
||||
|
||||
# Empty example/user values must not surface as fake non-empty defaults.
|
||||
self.assertIsNone(config.get("DEFAULT_ACCOUNT_ID"))
|
||||
self.assertIsNone(config.get("DEFAULT_LOGIN_ID"))
|
||||
self.assertEqual(config.get("HAS_VALUE"), "real")
|
||||
|
||||
def test_merge_missing_env_keys_appends_only_new(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
example = os.path.join(tmp, ".env.example")
|
||||
|
||||
Reference in New Issue
Block a user