-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathenv.test.ts
More file actions
100 lines (90 loc) · 4.12 KB
/
Copy pathenv.test.ts
File metadata and controls
100 lines (90 loc) · 4.12 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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { afterEach, describe, expect, it, vi } from 'vitest';
import { _resetEnvDeprecationWarnings, readEnvWithDeprecation } from './env.js';
describe('readEnvWithDeprecation', () => {
const originalPreferred = process.env.OS_TEST_FOO;
const originalLegacy = process.env.TEST_FOO;
afterEach(() => {
if (originalPreferred === undefined) delete process.env.OS_TEST_FOO;
else process.env.OS_TEST_FOO = originalPreferred;
if (originalLegacy === undefined) delete process.env.TEST_FOO;
else process.env.TEST_FOO = originalLegacy;
_resetEnvDeprecationWarnings();
vi.restoreAllMocks();
});
it('returns the preferred OS_ value when set and stays silent', () => {
process.env.OS_TEST_FOO = 'os-value';
process.env.TEST_FOO = 'legacy-value';
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
expect(readEnvWithDeprecation('OS_TEST_FOO', 'TEST_FOO')).toBe('os-value');
expect(warn).not.toHaveBeenCalled();
});
it('falls back to the legacy alias and warns exactly once per process', () => {
delete process.env.OS_TEST_FOO;
process.env.TEST_FOO = 'legacy-value';
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
expect(readEnvWithDeprecation('OS_TEST_FOO', 'TEST_FOO')).toBe('legacy-value');
expect(readEnvWithDeprecation('OS_TEST_FOO', 'TEST_FOO')).toBe('legacy-value');
expect(warn).toHaveBeenCalledTimes(1);
expect(String(warn.mock.calls[0][0])).toContain('TEST_FOO');
expect(String(warn.mock.calls[0][0])).toContain('OS_TEST_FOO');
expect(String(warn.mock.calls[0][0])).toContain('deprecated');
});
it('returns the legacy value without warning when silent is set', () => {
delete process.env.OS_TEST_FOO;
process.env.TEST_FOO = 'legacy-value';
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
expect(
readEnvWithDeprecation('OS_TEST_FOO', 'TEST_FOO', { silent: true }),
).toBe('legacy-value');
expect(warn).not.toHaveBeenCalled();
});
it('returns undefined and does not warn when neither var is set', () => {
delete process.env.OS_TEST_FOO;
delete process.env.TEST_FOO;
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
expect(readEnvWithDeprecation('OS_TEST_FOO', 'TEST_FOO')).toBeUndefined();
expect(warn).not.toHaveBeenCalled();
});
it('treats empty string as set (operator opt-in to blank value)', () => {
process.env.OS_TEST_FOO = '';
process.env.TEST_FOO = 'legacy-value';
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
expect(readEnvWithDeprecation('OS_TEST_FOO', 'TEST_FOO')).toBe('');
expect(warn).not.toHaveBeenCalled();
});
it('checks legacy aliases in order and warns for the matched one', () => {
const originalAlt = process.env.ALT_TEST_FOO;
try {
delete process.env.OS_TEST_FOO;
delete process.env.TEST_FOO;
process.env.ALT_TEST_FOO = 'alt-value';
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
expect(
readEnvWithDeprecation('OS_TEST_FOO', ['TEST_FOO', 'ALT_TEST_FOO']),
).toBe('alt-value');
expect(warn).toHaveBeenCalledTimes(1);
expect(String(warn.mock.calls[0][0])).toContain('ALT_TEST_FOO');
} finally {
if (originalAlt === undefined) delete process.env.ALT_TEST_FOO;
else process.env.ALT_TEST_FOO = originalAlt;
}
});
it('first legacy alias wins when multiple are set', () => {
const originalAlt = process.env.ALT_TEST_FOO;
try {
delete process.env.OS_TEST_FOO;
process.env.TEST_FOO = 'first-legacy';
process.env.ALT_TEST_FOO = 'second-legacy';
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
expect(
readEnvWithDeprecation('OS_TEST_FOO', ['TEST_FOO', 'ALT_TEST_FOO']),
).toBe('first-legacy');
expect(warn).toHaveBeenCalledTimes(1);
expect(String(warn.mock.calls[0][0])).toContain('TEST_FOO');
} finally {
if (originalAlt === undefined) delete process.env.ALT_TEST_FOO;
else process.env.ALT_TEST_FOO = originalAlt;
}
});
});