-
Notifications
You must be signed in to change notification settings - Fork 13
/
parse.test.js
156 lines (144 loc) · 5.56 KB
/
parse.test.js
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
const {parse} = require('./parse');
describe('Environment file parser', function () {
it('should parse single line payload (no end-line)', function () {
const payload = 'key1=value1';
const result = parse(payload);
expect(result).toEqual([
{key: 'key1', value: 'value1', isEnvVar: true},
]);
});
it('should parse single line payload (with end-line)', function () {
const payload = 'key1=value1\n';
const result = parse(payload);
expect(result).toEqual([
{key: 'key1', value: 'value1', isEnvVar: true},
{key: null, value: '', isEnvVar: false},
]);
});
it('should parse single line payload (with multiple end-lines)', function () {
const payload = 'key1=value1\n\n\n\nkey2=value2';
const result = parse(payload);
expect(result).toEqual([
{key: 'key1', value: 'value1', isEnvVar: true},
{key: null, value: '', isEnvVar: false},
{key: null, value: '', isEnvVar: false},
{key: null, value: '', isEnvVar: false},
{key: 'key2', value: 'value2', isEnvVar: true},
]);
});
it('should parse string(lowercase) payload', function () {
const payload = 'key1=value1\nkey2=value2';
const result = parse(payload);
expect(result).toEqual([
{key: 'key1', value: 'value1', isEnvVar: true},
{key: 'key2', value: 'value2', isEnvVar: true}
]);
});
it('should parse string(uppercase) payload', function () {
const payload = 'KEY1=value1\nKEY2=value2';
const result = parse(payload);
expect(result).toEqual([
{key: 'KEY1', value: 'value1', isEnvVar: true},
{key: 'KEY2', value: 'value2', isEnvVar: true}
]);
});
it('should parse string(mixed case) payload', function () {
const payload = 'KEY1=value1\nKeY2=value2\nkEy=ValuE3';
const result = parse(payload);
expect(result).toEqual([
{key: 'KEY1', value: 'value1', isEnvVar: true},
{key: 'KeY2', value: 'value2', isEnvVar: true},
{key: 'kEy', value: 'ValuE3', isEnvVar: true},
]);
});
it('should parse string(with comment) payload', function () {
const payload = 'KEY1=value1\n\n# Second value is for app port\nPORT=3000';
const result = parse(payload);
expect(result).toEqual([
{key: 'KEY1', value: 'value1', isEnvVar: true},
{key: null, value: '', isEnvVar: false}, // Blank line
{key: null, value: '# Second value is for app port', isEnvVar: false}, // Comment
{key: 'PORT', value: '3000', isEnvVar: true},
]);
});
it('should parse string(with quotes&indentation) payload', function () {
// Template string already has end-lines and indentations from the left
const payload = `
ORIGINS="http://127.0.0.1:8100,http://127.0.0.1:8200"
APP_PORT=8100
# Redis config
REDIS_URL="redis://127.0.0.1:6379"
# or
REDIS_HOST= "127.0.0.1"
REDIS_PORT=6379
REDIS_NAMESPACE='sse'
REDIS_USER="'Redis-User'"
REDIS_PASS=
# SSE config
SSE_TTL=20m
SSE_TTC=10m
# Keycloak config
KC_URL="http://127.0.0.1:8200/auth"
KC_REALM="master"
KC_CLIENT_ID='sse-local'
KC_CLIENT_SECRET=
# RabbitMQ config
RABBITMQ_URL="amqp://127.0.0.1:5672"
# or
RABBITMQ_HOST="127.0.0.1"
RABBITMQ_VHOST="/"
RABBITMQ_PORT=5672
RABBITMQ_USER=
RABBITMQ_PW=
`;
const result = parse(payload);
expect(result).toEqual([
{ key: null, value: '', isEnvVar: false },
{
key: 'ORIGINS',
value: 'http://127.0.0.1:8100,http://127.0.0.1:8200',
isEnvVar: true
},
{ key: 'APP_PORT', value: '8100', isEnvVar: true },
{ key: null, value: '', isEnvVar: false },
{ key: null, value: '# Redis config', isEnvVar: false },
{ key: 'REDIS_URL', value: 'redis://127.0.0.1:6379', isEnvVar: true },
{ key: null, value: '# or', isEnvVar: false },
{ key: 'REDIS_HOST', value: '127.0.0.1', isEnvVar: true },
{ key: 'REDIS_PORT', value: '6379', isEnvVar: true },
{ key: 'REDIS_NAMESPACE', value: 'sse', isEnvVar: true },
// 'Redis-User' because it only removed outer quotes
{ key: 'REDIS_USER', value: "'Redis-User'", isEnvVar: true },
{ key: 'REDIS_PASS', value: '', isEnvVar: true },
{ key: null, value: '', isEnvVar: false },
{ key: null, value: '# SSE config', isEnvVar: false },
{ key: 'SSE_TTL', value: '20m', isEnvVar: true },
{ key: 'SSE_TTC', value: '10m', isEnvVar: true },
{ key: null, value: '', isEnvVar: false },
{ key: null, value: '# Keycloak config', isEnvVar: false },
{
key: 'KC_URL',
value: 'http://127.0.0.1:8200/auth',
isEnvVar: true
},
{ key: 'KC_REALM', value: 'master', isEnvVar: true },
{ key: 'KC_CLIENT_ID', value: 'sse-local', isEnvVar: true },
{ key: 'KC_CLIENT_SECRET', value: '', isEnvVar: true },
{ key: null, value: '', isEnvVar: false },
{ key: null, value: '# RabbitMQ config', isEnvVar: false },
{
key: 'RABBITMQ_URL',
value: 'amqp://127.0.0.1:5672',
isEnvVar: true
},
{ key: null, value: '# or', isEnvVar: false },
{ key: 'RABBITMQ_HOST', value: '127.0.0.1', isEnvVar: true },
{ key: 'RABBITMQ_VHOST', value: '/', isEnvVar: true },
{ key: 'RABBITMQ_PORT', value: '5672', isEnvVar: true },
{ key: 'RABBITMQ_USER', value: '', isEnvVar: true },
{ key: 'RABBITMQ_PW', value: '', isEnvVar: true },
{ key: null, value: '', isEnvVar: false }
]
);
});
});