forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenv.test.ts
More file actions
188 lines (171 loc) · 6.15 KB
/
Copy pathenv.test.ts
File metadata and controls
188 lines (171 loc) · 6.15 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { getEnvPath, getEnvVar, readEnvFile, setEnvVar, writeEnvFile } from '../env.js';
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
describe('getEnvPath', () => {
it('joins configRoot with .env.local', () => {
const result = getEnvPath('/some/root');
expect(result).toBe('/some/root/.env.local');
});
it('throws when no configRoot and no project found', () => {
// With no configRoot, it calls findConfigRoot() which may fail
// In a tmpdir with no agentcore/ directory, it should throw
const isolated = mkdtempSync(join(tmpdir(), 'env-test-noroot-'));
const origCwd = process.cwd();
const origInitCwd = process.env.INIT_CWD;
try {
process.chdir(isolated);
delete process.env.INIT_CWD;
expect(() => getEnvPath()).toThrow();
} finally {
process.chdir(origCwd);
if (origInitCwd !== undefined) process.env.INIT_CWD = origInitCwd;
else delete process.env.INIT_CWD;
rmSync(isolated, { recursive: true, force: true });
}
});
});
describe('readEnvFile + writeEnvFile', () => {
let root: string;
beforeAll(() => {
root = mkdtempSync(join(tmpdir(), 'env-test-'));
});
afterAll(() => {
rmSync(root, { recursive: true, force: true });
});
it('returns empty record when .env.local does not exist', async () => {
const emptyRoot = mkdtempSync(join(tmpdir(), 'env-test-empty-'));
try {
const result = await readEnvFile(emptyRoot);
expect(result).toEqual({});
} finally {
rmSync(emptyRoot, { recursive: true, force: true });
}
});
it('reads existing .env.local file', async () => {
writeFileSync(join(root, '.env.local'), 'MY_KEY="my_value"\nANOTHER="val2"\n');
const result = await readEnvFile(root);
expect(result.MY_KEY).toBe('my_value');
expect(result.ANOTHER).toBe('val2');
});
it('writes env file and reads it back', async () => {
const dir = mkdtempSync(join(tmpdir(), 'env-test-write-'));
try {
await writeEnvFile({ FOO: 'bar', BAZ: 'qux' }, dir, false);
const result = await readEnvFile(dir);
expect(result.FOO).toBe('bar');
expect(result.BAZ).toBe('qux');
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it('merges with existing values by default', async () => {
const dir = mkdtempSync(join(tmpdir(), 'env-test-merge-'));
try {
await writeEnvFile({ EXISTING: 'old' }, dir, false);
await writeEnvFile({ NEW_KEY: 'new' }, dir); // merge = true by default
const result = await readEnvFile(dir);
expect(result.EXISTING).toBe('old');
expect(result.NEW_KEY).toBe('new');
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it('overwrites when merge is false', async () => {
const dir = mkdtempSync(join(tmpdir(), 'env-test-overwrite-'));
try {
await writeEnvFile({ OLD: 'value' }, dir, false);
await writeEnvFile({ NEW: 'value' }, dir, false);
const result = await readEnvFile(dir);
expect(result.NEW).toBe('value');
expect(result.OLD).toBeUndefined();
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it('escapes special characters in values', async () => {
const dir = mkdtempSync(join(tmpdir(), 'env-test-escape-'));
try {
await writeEnvFile({ KEY: 'value with "quotes" and \\ backslash' }, dir, false);
const raw = readFileSync(join(dir, '.env.local'), 'utf-8');
expect(raw).toContain('\\\\'); // escaped backslash
expect(raw).toContain('\\"'); // escaped quote
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it('throws for invalid env key', async () => {
const dir = mkdtempSync(join(tmpdir(), 'env-test-invalid-'));
try {
await expect(writeEnvFile({ 'invalid-key': 'value' }, dir, false)).rejects.toThrow('Invalid env key');
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it('skips null and undefined values', async () => {
const dir = mkdtempSync(join(tmpdir(), 'env-test-nulls-'));
try {
await writeEnvFile(
{
KEEP: 'value',
SKIP_NULL: null as unknown as string,
SKIP_UNDEF: undefined as unknown as string,
},
dir,
false
);
const result = await readEnvFile(dir);
expect(result.KEEP).toBe('value');
expect(result.SKIP_NULL).toBeUndefined();
expect(result.SKIP_UNDEF).toBeUndefined();
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it('escapes newlines and carriage returns', async () => {
const dir = mkdtempSync(join(tmpdir(), 'env-test-newlines-'));
try {
await writeEnvFile({ KEY: 'line1\nline2\rline3' }, dir, false);
const raw = readFileSync(join(dir, '.env.local'), 'utf-8');
expect(raw).toContain('\\n');
expect(raw).toContain('\\r');
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});
describe('getEnvVar', () => {
it('returns a specific env var value', async () => {
const dir = mkdtempSync(join(tmpdir(), 'env-test-getvar-'));
try {
writeFileSync(join(dir, '.env.local'), 'TARGET="found"\n');
const result = await getEnvVar('TARGET', dir);
expect(result).toBe('found');
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it('returns undefined for missing key', async () => {
const dir = mkdtempSync(join(tmpdir(), 'env-test-missing-'));
try {
writeFileSync(join(dir, '.env.local'), 'OTHER="val"\n');
const result = await getEnvVar('MISSING', dir);
expect(result).toBeUndefined();
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});
describe('setEnvVar', () => {
it('sets a single env var', async () => {
const dir = mkdtempSync(join(tmpdir(), 'env-test-setvar-'));
try {
await setEnvVar('SINGLE', 'value', dir);
const result = await readEnvFile(dir);
expect(result.SINGLE).toBe('value');
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});