-
Notifications
You must be signed in to change notification settings - Fork 21
/
test.js
444 lines (376 loc) · 12.3 KB
/
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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
'use strict'
/* globals describe beforeEach it afterEach */
const chai = require('chai')
chai.use(require('chai-as-promised'))
const expect = chai.expect
const nock = require('nock')
nock.disableNetConnect()
const mock = require('mock-fs')
const rewire = require('rewire')
const fs = require('fs')
const path = require('path')
const co = require('co')
const stdin = require('mock-stdin').stdin()
// heroku stuff
const cli = require('heroku-cli-util')
const commands = require('./index').commands
// things I'm testing
const merge = require('./util/merge')
const file = rewire('./util/file')
const header = file.__get__('header')
// HELPERS
const fetchCMD = name => {
return commands.find(c => c.command === name)
}
// heroku-cli err output depends on terminal width, so we have to standardize
const cleanStdErr = s => {
return s.replace(/\n|▸| {2,}/g, '').trim()
}
const setup = () => {
cli.raiseErrors = true
cli.exit.mock()
}
const defaultFS = () => {
// this is so I can setup without affecting other tests
return {
'.env': fixtures.local_file,
windows: fixtures.windows_file,
'other.txt': fixtures.local_file,
dnt: mock.file({ mode: '000' }),
multiline: fixtures.multiline_file,
tricky_multiline: fixtures.tricky_multiline_file,
expanded: fixtures.expanded_file
}
}
// https://glebbahmutov.com/blog/unit-testing-cli-programs/
const mockInput = i => {
process.nextTick(() => {
stdin.send(i)
})
}
// val must be true or false
const mockSettingsFile = val => {
const location = path.join(process.env.HOME, '.heroku_config_settings.json')
fs.writeFileSync(location, fixtures[`settings_obj_${val}`])
}
const nockFetchConfig = appName => {
appName = appName || 'test'
nock('https://api.heroku.com:443')
.get(`/apps/${appName}/config-vars`)
.reply(200, fixtures.remote_obj)
}
// FIXTURES
const fixtures = {
remote_obj: {
NODE_ENV: 'pRoDuction',
NAME: 'david',
SOURCE: 'remote'
},
local_obj: {
NODE_ENV: 'test',
SOURCE: 'local',
DB_STRING: 'mongo://blah@thing.mongo.thing.com:4567'
},
remote_win_obj: {
NODE_ENV: 'pRoDuction',
SOURCE: 'remote',
DB_STRING: 'mongo://blah@thing.mongo.thing.com:4567',
NAME: 'david'
},
local_win_obj: {
NODE_ENV: 'test',
SOURCE: 'local',
DB_STRING: 'mongo://blah@thing.mongo.thing.com:4567',
NAME: 'david'
},
remote_cleaned_obj: {
NODE_ENV: 'pRoDuction',
SOURCE: 'remote',
DB_STRING: 'mongo://blah@thing.mongo.thing.com:4567'
},
multiline_obj: {
SECRET_KEY:
'-----BEGIN RSA PRIVATE KEY-----\nMIIrandomtext\nmorerandomtext\n-----END RSA PRIVATE KEY-----',
OTHER_KEY: 'blahblah',
ITS_GONNA_BE: 'legend wait for it...\ndary'
},
tricky_multiline_obj: {
SECRET_KEY:
'-----BEGIN RSA PRIVATE KEY-----\nMIIrandomtext\nmorerandomtext==\n-----END RSA PRIVATE KEY-----',
OTHER_KEY: 'blahblah',
ITS_GONNA_BE: 'legend wait for it...\ndary'
},
expanded_obj: {
'na-me': 'david',
'integration.url': 'https://google.com'
},
local_file:
'#comment\nNODE_ENV= test\nSOURCE =local\nSOURCE = local\nDB_STRING="mongo://blah@thing.mongo.thing.com:4567"\n',
windows_file:
'#comment\r\nNODE_ENV= test\r\nSOURCE =local\r\nSOURCE = local\r\nDB_STRING=mongo://blah@thing.mongo.thing.com:4567\r\n',
merged_local_file:
header +
'DB_STRING="mongo://blah@thing.mongo.thing.com:4567"\nNAME="david"\nNODE_ENV="test"\nSOURCE="local"\n',
multiline_file:
'SECRET_KEY="-----BEGIN RSA PRIVATE KEY-----\nMIIrandomtext\n\nmorerandomtext\n-----END RSA PRIVATE KEY-----"\nOTHER_KEY=blahblah\nITS_GONNA_BE="legend wait for it...\n# you better not be lactose intolerant cause it\'s\ndary"',
tricky_multiline_file:
'SECRET_KEY="-----BEGIN RSA PRIVATE KEY-----\nMIIrandomtext\n\nmorerandomtext==\n-----END RSA PRIVATE KEY-----"\nOTHER_KEY=blahblah\nITS_GONNA_BE="legend wait for it...\n# you better not be lactose intolerant cause it\'s\ndary"',
expanded_file: 'na-me=david\nintegration.url=https://google.com\n',
// test both quote styles
sample_file:
header +
'export PIZZA="Abo\'s"\nNAME="david"\n\n#this is a comment!\nCITY=boulder\n\n\n',
clean_sample_file: header + 'CITY="boulder"\nNAME="david"\nPIZZA="Abo\'s"\n',
naked_clean_sample_file: header + "CITY=boulder\nNAME=david\nPIZZA=Abo's\n",
sample_obj: { NAME: 'david', CITY: 'boulder', PIZZA: "Abo's" },
settings_obj_true: JSON.stringify({ blah: true }),
settings_obj_false: JSON.stringify({ blah: false })
}
// TESTS
setup()
describe('Merging', () => {
it('should overwrite local with remote', () => {
expect(merge(fixtures.local_obj, fixtures.remote_obj, {})).to.deep.equal(
fixtures.remote_win_obj
)
})
it('should overwrite remote with local', () => {
expect(merge(fixtures.remote_obj, fixtures.local_obj, {})).to.deep.equal(
fixtures.local_win_obj
)
})
it('should overwrite local with remote w/override', () => {
expect(
merge(fixtures.remote_obj, fixtures.local_obj, { overwrite: true })
).to.deep.equal(fixtures.remote_win_obj)
})
it('should overwrite remote with local w/override', () => {
expect(
merge(fixtures.local_obj, fixtures.remote_obj, { overwrite: true })
).to.deep.equal(fixtures.local_win_obj)
})
})
describe('Reading', () => {
beforeEach(() => {
mock(defaultFS())
cli.mockConsole()
})
it('should return empty object from non-existant file', () => {
return expect(file.read('asdf')).to.eventually.deep.equal({})
})
it('should read a local file', () => {
return expect(file.read('.env')).to.eventually.deep.equal(
fixtures.local_obj
)
})
it('should read a local windows file', () => {
return expect(file.read('windows')).to.eventually.deep.equal(
fixtures.local_obj
)
})
it('should read multiline values (and multi-multiline values)', () => {
return expect(file.read('multiline')).to.eventually.deep.equal(
fixtures.multiline_obj
)
})
it('should read multiline values (and multi-multiline values)', () => {
return expect(file.read('tricky_multiline')).to.eventually.deep.equal(
fixtures.tricky_multiline_obj
)
})
it('should warn about duplicate keys', () => {
return file.read('.env').then(() => {
expect(cleanStdErr(cli.stderr)).to.include(
'"SOURCE" is in env file twice'
)
})
})
it('should skip warnings in quiet mode', () => {
return file.read('.env', { quiet: true }).then(() => {
expect(cli.stderr).to.equal('')
})
})
it('should warn when it hits a malformed line', () => {
return file.read('expanded').then(() => {
expect(cleanStdErr(cli.stderr)).to.include('unable to parse line')
})
})
it('should read expanded files', () => {
return expect(
file.read('expanded', { expanded: true })
).to.eventually.deep.equal(fixtures.expanded_obj)
})
afterEach(() => {
mock.restore()
cli.mockConsole(false)
})
})
describe('Writing', () => {
beforeEach(() => {
cli.mockConsole()
mock(defaultFS())
})
it('should fail to write to a file it lacks permissions for', () => {
return expect(file.write({}, 'dnt')).to.be.rejectedWith(Error)
})
const fname = '.env'
it('should successfully write a file, louldly', () => {
return file.write(fixtures.sample_obj).then(() => {
const res = fs.readFileSync(fname, 'utf-8')
expect(res).to.equal(fixtures.clean_sample_file)
expect(cli.stdout).to.not.equal('')
})
})
it('should successfully write a file, quietly', () => {
// need to pass all params if i'm passing quiet mode
return file.write(fixtures.sample_obj, fname, { quiet: true }).then(() => {
const res = fs.readFileSync(fname, 'utf-8')
expect(res).to.equal(fixtures.clean_sample_file)
expect(cli.stdout).to.equal('')
})
})
afterEach(() => {
mock.restore()
cli.mockConsole(false)
})
})
describe('Transforming', () => {
it('should decode files correctly', () => {
expect(
file.__get__('objFromFileFormat')(fixtures.sample_file)
).to.deep.equal(fixtures.sample_obj)
})
it('should encode file correctly', () => {
expect(file.__get__('objToFileFormat')(fixtures.sample_obj)).to.equal(
fixtures.clean_sample_file
)
})
it('should read unquoted files', () => {
expect(
file.__get__('objFromFileFormat')(fixtures.naked_clean_sample_file)
).to.deep.equal(fixtures.sample_obj)
})
it('should write unquoted files', () => {
expect(
file.__get__('objToFileFormat')(fixtures.sample_obj, { unquoted: true })
).to.equal(fixtures.naked_clean_sample_file)
})
})
describe('Checking for Prod', () => {
beforeEach(() => {
cli.mockConsole()
})
it('should delete prod when prompted', () => {
mockInput('d')
return co(file.shouldDeleteProd({ app: 'test' })).then(shouldDelete => {
expect(shouldDelete).to.equal(true)
})
})
it('should ignore prod when prompted', () => {
mockInput('i')
return co(file.shouldDeleteProd({ app: 'test' })).then(shouldDelete => {
expect(shouldDelete).to.equal(false)
})
})
it('should read a true settings file if able', () => {
const testMode = true
mock()
mockSettingsFile(testMode)
return co(file.shouldDeleteProd({ app: 'blah' })).then(shouldDelete => {
expect(shouldDelete).to.equal(testMode)
})
})
it('should read a false settings file if able', () => {
const testMode = false
mock()
mockSettingsFile(testMode)
return co(file.shouldDeleteProd({ app: 'blah' })).then(shouldDelete => {
expect(shouldDelete).to.equal(testMode)
})
})
afterEach(() => {
cli.mockConsole(false)
mock.restore()
})
})
describe.skip('Pushing', () => {
beforeEach(() => {
cli.mockConsole()
mock(defaultFS())
})
const cmd = fetchCMD('push')
const fname = 'other.txt'
it('should correctly push configs w/ flags', () => {
nockFetchConfig()
// this will fail if we don't pass the correct body, as intended
nock('https://api.heroku.com:443')
.patch('/apps/test/config-vars', fixtures.remote_win_obj)
.reply(200, fixtures.remote_win_obj)
return cmd
.run({ flags: { file: fname, quiet: true }, app: 'test' })
.then(() => {
expect(nock.isDone()).to.equal(true)
expect(cli.stdout).to.equal('')
})
})
it('should correctly push null values for unused configs', () => {
nockFetchConfig()
// this will fail if we don't pass the correct body, as intended
nock('https://api.heroku.com:443')
.patch('/apps/test/config-vars', fixtures.remote_win_obj)
.reply(200, fixtures.remote_win_obj)
// delete call
nock('https://api.heroku.com:443')
.patch('/apps/test/config-vars', { NAME: null })
.reply(200, fixtures.remote_cleaned_obj)
return cmd.run({ flags: { clean: true }, app: 'test' }).then(() => {
expect(nock.isDone()).to.equal(true)
})
})
afterEach(() => {
mock.restore()
cli.mockConsole(false)
})
})
// the following tests are broken because mock-fs v4 doesn't support lazy
// requires and mock-fs v3 only supports up to node 7. What I need is a mocked
// filesystem that uses the real fs for requires, even when mocked
describe.skip('Pulling', () => {
beforeEach(() => {
cli.mockConsole()
mock(defaultFS())
})
const cmd = fetchCMD('pull')
it('should correctly pull configs', () => {
const fname = 'other.txt'
nockFetchConfig()
return cmd.run({ flags: { file: fname }, app: 'test' }).then(() => {
const res = fs.readFileSync(fname, 'utf-8')
expect(res).to.include(fixtures.merged_local_file)
})
})
describe('skipping production value', () => {
const fname = '.env'
const appName = 'blah'
it('should delete value', () => {
mockSettingsFile(true)
nockFetchConfig(appName)
return cmd.run({ flags: { overwrite: true }, app: appName }).then(() => {
const res = fs.readFileSync(fname, 'utf-8')
expect(res).not.to.include('pRoDuction')
})
})
it('should not delete value', () => {
mockSettingsFile(false)
nockFetchConfig(appName)
return cmd.run({ flags: { overwrite: true }, app: appName }).then(() => {
const res = fs.readFileSync(fname, 'utf-8')
expect(res).to.include('pRoDuction')
})
})
})
afterEach(() => {
mock.restore()
cli.mockConsole(false)
})
})