| 1 | import pytest |
| 2 | |
| 3 | from lib import env |
| 4 | |
| 5 | |
| 6 | def _load(tmp_path, text): |
| 7 | env_path = tmp_path / ".env" |
| 8 | env_path.write_text(text, encoding="utf-8") |
| 9 | env_path.chmod(0o600) |
| 10 | return env.load_env_file(env_path) |
| 11 | |
| 12 | |
| 13 | def test_trailing_inline_comment_is_stripped(tmp_path): |
| 14 | loaded = _load(tmp_path, "FOO=bar # trailing comment\n") |
| 15 | assert loaded["FOO"] == "bar" |
| 16 | |
| 17 | |
| 18 | def test_tab_before_hash_also_opens_comment(tmp_path): |
| 19 | loaded = _load(tmp_path, "FOO=bar\t# trailing comment\n") |
| 20 | assert loaded["FOO"] == "bar" |
| 21 | |
| 22 | |
| 23 | def test_hash_without_preceding_whitespace_is_literal(tmp_path): |
| 24 | loaded = _load(tmp_path, "BAZ=value#nothash\n") |
| 25 | assert loaded["BAZ"] == "value#nothash" |
| 26 | |
| 27 | |
| 28 | @pytest.mark.parametrize("quote", ['"', "'"]) |
| 29 | def test_hash_inside_quotes_is_kept(tmp_path, quote): |
| 30 | loaded = _load(tmp_path, f"QUX={quote}value # not a comment{quote}\n") |
| 31 | assert loaded["QUX"] == "value # not a comment" |
| 32 | |
| 33 | |
| 34 | def test_comment_after_closing_quote_is_stripped(tmp_path): |
| 35 | loaded = _load(tmp_path, 'QUX="value # kept" # dropped\n') |
| 36 | assert loaded["QUX"] == "value # kept" |
| 37 | |
| 38 | |
| 39 | def test_leading_whitespace_before_value_is_trimmed(tmp_path): |
| 40 | loaded = _load(tmp_path, "KEY= spaced\n") |
| 41 | assert loaded["KEY"] == "spaced" |
| 42 | |
| 43 | |
| 44 | def test_whole_line_comment_and_empty_value_are_skipped(tmp_path): |
| 45 | loaded = _load(tmp_path, "# whole line comment\nEMPTY=\nEMPTY2= # only a comment\n") |
| 46 | assert loaded == {} |
| 47 | |
| 48 | |
| 49 | def test_documented_configuration_example_round_trips(tmp_path): |
| 50 | # CONFIGURATION.md shows this shape (a path, run-in whitespace, then an |
| 51 | # annotation); uncommenting it must not leak the annotation into the value. |
| 52 | # The path is a stand-in: test_version_consistency forbids repeating the |
| 53 | # real default outside the lines that document it. |
| 54 | loaded = _load( |
| 55 | tmp_path, |
| 56 | "LAST30DAYS_MEMORY_DIR=~/Archive/Briefings # POSIX\n" |
| 57 | "LAST30DAYS_REDDIT_KEYLESS_RATE=1 # keyless reddit.com req/sec\n", |
| 58 | ) |
| 59 | assert loaded["LAST30DAYS_MEMORY_DIR"] == "~/Archive/Briefings" |
| 60 | assert loaded["LAST30DAYS_REDDIT_KEYLESS_RATE"] == "1" |
| 61 | |
| 62 | |
| 63 | def test_unterminated_quote_is_left_verbatim(tmp_path): |
| 64 | loaded = _load(tmp_path, 'RAW="abc # def\n') |
| 65 | assert loaded["RAW"] == '"abc # def' |
| 66 |