-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenv.test.ts
More file actions
144 lines (112 loc) · 4.27 KB
/
Copy pathenv.test.ts
File metadata and controls
144 lines (112 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import fs from "fs";
import os from "os";
import path from "path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { parseEnv, parseEnvString, writeEnv } from "./env.js";
let tmpDir: string;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "seamless-env-test-"));
});
afterEach(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
describe("parseEnv", () => {
it("parses simple key=value lines from a file", () => {
const file = path.join(tmpDir, ".env");
fs.writeFileSync(file, "FOO=bar\nBAZ=qux\n");
expect(parseEnv(file)).toEqual({ FOO: "bar", BAZ: "qux" });
});
it("skips blank lines and comment lines", () => {
const file = path.join(tmpDir, ".env");
fs.writeFileSync(file, "\n# a comment\nFOO=bar\n\n#another\nBAZ=qux\n");
expect(parseEnv(file)).toEqual({ FOO: "bar", BAZ: "qux" });
});
it("skips lines with no key", () => {
const file = path.join(tmpDir, ".env");
fs.writeFileSync(file, "=novalue\nFOO=bar\n");
expect(parseEnv(file)).toEqual({ FOO: "bar" });
});
it("trims whitespace around key and value", () => {
const file = path.join(tmpDir, ".env");
fs.writeFileSync(file, " FOO = bar \n");
expect(parseEnv(file)).toEqual({ FOO: "bar" });
});
it("rejoins values that contain an equals sign", () => {
const file = path.join(tmpDir, ".env");
fs.writeFileSync(file, "URL=https://example.com?a=1&b=2\n");
expect(parseEnv(file)).toEqual({ URL: "https://example.com?a=1&b=2" });
});
it("unquotes double-quoted values and unescapes them", () => {
const file = path.join(tmpDir, ".env");
fs.writeFileSync(file, 'FOO="bar baz"\nMULTI="a\\nb"\n');
expect(parseEnv(file)).toEqual({ FOO: "bar baz", MULTI: "a\nb" });
});
it("treats single-quoted values as literal", () => {
const file = path.join(tmpDir, ".env");
fs.writeFileSync(file, "FOO='a\\nb'\n");
expect(parseEnv(file)).toEqual({ FOO: "a\\nb" });
});
});
describe("parseEnvString", () => {
it("parses simple key=value lines from a string", () => {
expect(parseEnvString("FOO=bar\nBAZ=qux\n")).toEqual({
FOO: "bar",
BAZ: "qux",
});
});
it("skips blank lines and comment lines", () => {
expect(parseEnvString("\n# a comment\nFOO=bar\n\n#another\n")).toEqual({
FOO: "bar",
});
});
it("skips lines with no key", () => {
expect(parseEnvString("=novalue\nFOO=bar\n")).toEqual({ FOO: "bar" });
});
it("trims whitespace around key and value", () => {
expect(parseEnvString(" FOO = bar ")).toEqual({ FOO: "bar" });
});
it("rejoins values that contain an equals sign", () => {
expect(parseEnvString("A=1=2=3")).toEqual({ A: "1=2=3" });
});
it("returns an empty object for empty content", () => {
expect(parseEnvString("")).toEqual({});
});
});
describe("writeEnv", () => {
it("writes key=value pairs, one per line, with a trailing newline", () => {
const file = path.join(tmpDir, "out.env");
writeEnv(file, { FOO: "bar", BAZ: "qux" });
expect(fs.readFileSync(file, "utf-8")).toBe("FOO=bar\nBAZ=qux\n");
});
it("writes just a trailing newline for an empty env object", () => {
const file = path.join(tmpDir, "empty.env");
writeEnv(file, {});
expect(fs.readFileSync(file, "utf-8")).toBe("\n");
});
it("round-trips through parseEnv", () => {
const file = path.join(tmpDir, "roundtrip.env");
const original = { A: "1", B: "two", C: "" };
writeEnv(file, original);
expect(parseEnv(file)).toEqual(original);
});
it("quotes values with characters dotenv would misread, and round-trips them", () => {
const file = path.join(tmpDir, "special.env");
const original = {
HASH: "abc#notacomment",
SPACED: "with spaces",
QUOTED: 'a"b',
NEWLINE: "line1\nline2",
BACKSLASH: "a\\b",
LEADING: " padded ",
PLAIN: "simple-token_123",
};
writeEnv(file, original);
const raw = fs.readFileSync(file, "utf-8");
// The `#` value must be quoted so a dotenv parser doesn't treat it as a comment,
// and a simple token must stay bare.
expect(raw).toContain('HASH="abc#notacomment"');
expect(raw).toContain("PLAIN=simple-token_123");
expect(raw).not.toContain("PLAIN=\"");
expect(parseEnv(file)).toEqual(original);
});
});