Skip to content

Commit 2dae961

Browse files
Merge pull request #13 from barecheck/cursor/missing-test-coverage-b5dc
test: cover dotenv parse, remove, and append helpers
2 parents b77acae + f2eefa3 commit 2dae961

1 file changed

Lines changed: 85 additions & 0 deletions

File tree

src/lib/dotenv-parse.test.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { describe, expect, it } from "vitest";
2+
import { appendDotenvKey, parseDotenv, removeDotenvKey } from "./dotenv-parse";
3+
4+
describe("parseDotenv", () => {
5+
it("ignores blanks and # comments", () => {
6+
expect(parseDotenv("\n \n# x=1\nFOO=bar\n")).toEqual([
7+
{ key: "FOO", value: "bar" },
8+
]);
9+
});
10+
11+
it("parses unquoted and quoted values", () => {
12+
expect(parseDotenv(`A=1\nB='2'\nC="3"`)).toEqual([
13+
{ key: "A", value: "1" },
14+
{ key: "B", value: "2" },
15+
{ key: "C", value: "3" },
16+
]);
17+
});
18+
19+
it("splits on CRLF and skips malformed lines", () => {
20+
expect(parseDotenv("OK=yes\r\n=bad\nNOEQ\nALSO=ok")).toEqual([
21+
{ key: "OK", value: "yes" },
22+
{ key: "ALSO", value: "ok" },
23+
]);
24+
});
25+
26+
it("trims keys and values around =", () => {
27+
expect(parseDotenv(" KEY = val ")).toEqual([
28+
{ key: "KEY", value: "val" },
29+
]);
30+
});
31+
});
32+
33+
describe("removeDotenvKey", () => {
34+
it("drops assignment lines for the key and keeps comments and spacing lines", () => {
35+
const src = "# keep\n\nSECRET=x\nOTHER=1\n";
36+
expect(removeDotenvKey(src, "SECRET")).toBe("# keep\n\nOTHER=1\n");
37+
});
38+
39+
it("removes every line parseDotenv would attribute to that key", () => {
40+
const src = "DUP=a\nDUP=b\n";
41+
expect(removeDotenvKey(src, "DUP")).toBe("");
42+
});
43+
});
44+
45+
describe("appendDotenvKey", () => {
46+
it("appends a line to empty content", () => {
47+
expect(appendDotenvKey("", "K", "v")).toEqual({
48+
ok: true,
49+
content: "K=v",
50+
});
51+
});
52+
53+
it("trims trailing whitespace and joins with a single newline", () => {
54+
expect(appendDotenvKey("A=1\n\n \t", "B", "2")).toEqual({
55+
ok: true,
56+
content: "A=1\nB=2",
57+
});
58+
});
59+
60+
it("quotes values that need escaping", () => {
61+
expect(appendDotenvKey("", "K", `say "hi"`)).toEqual({
62+
ok: true,
63+
content: `K="say \\"hi\\""`,
64+
});
65+
});
66+
67+
it("rejects invalid keys and newlines in the value", () => {
68+
expect(appendDotenvKey("", "", "x").ok).toBe(false);
69+
expect(appendDotenvKey("", "a=b", "x").ok).toBe(false);
70+
expect(appendDotenvKey("", "#x", "1").ok).toBe(false);
71+
expect(appendDotenvKey("", "bad\nkey", "1").ok).toBe(false);
72+
expect(appendDotenvKey("", "K", "a\nb").ok).toBe(false);
73+
});
74+
75+
it("rejects duplicate keys visible to parseDotenv", () => {
76+
const r = appendDotenvKey("FOO=1", "FOO", "2");
77+
expect(r.ok).toBe(false);
78+
if (!r.ok) expect(r.error).toContain("FOO");
79+
});
80+
81+
it("treats trimmed key as duplicate of existing assignment", () => {
82+
const r = appendDotenvKey("FOO=1", " FOO ", "2");
83+
expect(r.ok).toBe(false);
84+
});
85+
});

0 commit comments

Comments
 (0)