Skip to content

Commit eec5760

Browse files
committed
fixed http tests and changed some flags
1 parent 1d446f4 commit eec5760

File tree

6 files changed

+73
-54
lines changed

6 files changed

+73
-54
lines changed

commands/pull.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ const merge = require('../util/merge')
66
const file = require('../util/file')
77

88
function * pull (context, heroku) {
9-
let fname = context.flags.env // this gets defaulted in read
9+
let fname = context.flags.file // this gets defaulted in read
1010
let config = yield {
1111
remote: heroku.get(`/apps/${context.app}/config-vars`),
1212
local: file.read(fname)
1313
}
1414
// cli.debug(config)
1515
let res = merge(config.remote, config.local, context.flags)
1616
yield file.write(res, fname)
17+
// write handles success message
1718
}
1819

1920
module.exports = {

commands/push.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@ const merge = require('../util/merge')
66
const file = require('../util/file')
77

88
function * pull (context, heroku) {
9-
let fname = context.flags.env // this gets defaulted in read
9+
let fname = context.flags.file // this gets defaulted in read
1010
let config = yield {
1111
remote: heroku.get(`/apps/${context.app}/config-vars`),
1212
local: file.read(fname)
1313
}
1414
// cli.debug(config)
1515
let res = merge(config.local, config.remote, context.flags)
16-
try {
17-
yield heroku.patch(`/apps/${context.app}/config-vars`, res)
18-
} catch (e) {
19-
cli.error(`There was a problem saving to heroku: ${e.message}`)
20-
}
16+
yield heroku.patch(`/apps/${context.app}/config-vars`, res)
2117
cli.log('Successuflly wrote settings to heroku!')
2218
}
2319

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333
"mock-fs": "^3.9.0",
3434
"nock": "^8.0.0",
3535
"rewire": "^2.5.1",
36-
"standard": "^6.0.8"
36+
"standard": "^7.0.0"
3737
}
3838
}

test.js

+56-32
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,20 @@ const header = file.__get__('header')
2323
const fs = require('fs')
2424

2525
// HELPERS
26-
function fetch_cmd (name) {
26+
function fetchCMD (name) {
2727
return commands.find((c) => c.command === name)
2828
}
2929

3030
function setup () {
3131
cli.raiseErrors = true
3232
}
3333

34-
function default_fs () {
34+
function defaultFS () {
3535
// this is so I can setup without affecting other tests
3636
return {
3737
'.env': fixtures.local_file,
3838
'other.txt': fixtures.local_file,
39-
'dnt': mock.file({mode: '000'}),
40-
totally: {}
39+
'dnt': mock.file({mode: '000'})
4140
}
4241
}
4342

@@ -62,12 +61,14 @@ let fixtures = {
6261
DB_STRING: 'mongo://blah@thing.mongo.thing.com:4567',
6362
NAME: 'david'
6463
},
64+
6565
local_file: '#comment\nNODE_ENV=test\nSOURCE=local\n\nDB_STRING=mongo://blah@thing.mongo.thing.com:4567\n',
6666
merged_local_file: header + 'DB_STRING="mongo://blah@thing.mongo.thing.com:4567"\nNAME="david"\nNODE_ENV="test"\nSOURCE="local"\n',
67+
6768
// test both quote styles
68-
sample_file: header + 'NAME="david"\n\n#this is a comment!\nCITY=boulder\n\n\n',
69-
clean_sample_file: header + 'CITY="boulder"\nNAME="david"\n',
70-
sample_obj: { NAME: 'david', CITY: 'boulder' }
69+
sample_file: header + 'PIZZA="Abo\'s"\nNAME="david"\n\n#this is a comment!\nCITY=boulder\n\n\n',
70+
clean_sample_file: header + 'CITY="boulder"\nNAME="david"\nPIZZA="Abo\'s"\n',
71+
sample_obj: { NAME: 'david', CITY: 'boulder', PIZZA: "Abo's" }
7172
}
7273

7374
// TESTS
@@ -92,7 +93,7 @@ describe('Merging', () => {
9293

9394
describe('Reading', () => {
9495
beforeEach(() => {
95-
mock(default_fs())
96+
mock(defaultFS())
9697
})
9798

9899
it('should return empty object from non-existant file', () => {
@@ -111,20 +112,19 @@ describe('Reading', () => {
111112
describe('Writing', () => {
112113
beforeEach(() => {
113114
cli.mockConsole()
114-
mock(default_fs())
115+
mock(defaultFS())
115116
})
116117

117118
it('should fail to read from a file it lacks permissions for', () => {
118-
return file.write({}, 'dnt').then(() => {
119-
expect(cli.stderr).to.contain('EACCES, permission denied')
120-
})
119+
return expect(file.write({}, 'dnt')).to.be.rejectedWith(Error)
121120
})
122121

123-
// it('should successfully write a file', () => {
124-
// return file.write(fixtures.local_obj).then(() => {
125-
// let res = require('fs').readFileSync('.env')
126-
// })
127-
// })
122+
it('should successfully write a file', () => {
123+
return file.write(fixtures.sample_obj).then(() => {
124+
let res = fs.readFileSync('.env', 'utf-8')
125+
expect(res).to.equal(fixtures.clean_sample_file)
126+
})
127+
})
128128

129129
afterEach(() => {
130130
mock.restore()
@@ -133,40 +133,64 @@ describe('Writing', () => {
133133

134134
describe('Transforming', () => {
135135
it('should decode files correctly', () => {
136-
expect(file.__get__('obj_from_file_format')(fixtures.sample_file)).to.deep.equal(fixtures.sample_obj)
136+
expect(file.__get__('objFromFileFormat')(fixtures.sample_file)).to.deep.equal(fixtures.sample_obj)
137137
})
138138

139139
it('should encode file correctly', () => {
140-
expect(file.__get__('obj_to_file_format')(fixtures.sample_obj)).to.equal(fixtures.clean_sample_file)
140+
expect(file.__get__('objToFileFormat')(fixtures.sample_obj)).to.equal(fixtures.clean_sample_file)
141141
})
142142
})
143143

144-
// describe('Pushing', {skip: true}, (t) => {
145-
// let api_get = nock('https://api.heroku.com:443')
146-
// .get('/apps/test/config-vars')
147-
// .reply(200, remote)
144+
describe('Pushing', () => {
145+
beforeEach(() => {
146+
cli.mockConsole()
147+
mock(defaultFS())
148+
})
149+
150+
it('should correctly push configs w/ flags', () => {
151+
nock('https://api.heroku.com:443')
152+
.get('/apps/test/config-vars')
153+
.reply(200, fixtures.remote_obj)
154+
155+
// this will fail if we don't pass the correct body, as intended
156+
nock('https://api.heroku.com:443')
157+
.patch('/apps/test/config-vars', fixtures.remote_win_obj)
158+
.reply(200, fixtures.remote_win_obj)
148159

149-
// let api_patch = nock('https://api.heroku.com:443')
150-
// .patch('/apps/test/config-vars')
151-
// .reply(200, remote_win_obj)
160+
// fetch the updated value
161+
nock('https://api.heroku.com:443')
162+
.get('/apps/test/config-vars')
163+
.reply(200, fixtures.remote_win_obj)
164+
165+
let cmd = fetchCMD('mypush')
166+
let fname = 'other.txt'
167+
return cmd.run({flags: {file: fname}, app: 'test'}).then(() => {
168+
return cli.got('https://api.heroku.com:443/apps/test/config-vars')
169+
}).then((res) => {
170+
expect(JSON.parse(res.body)).to.deep.equal(fixtures.remote_win_obj)
171+
})
172+
})
152173

153-
// let cmd = fetch_cmd('mypush')
154-
// })
174+
afterEach(() => {
175+
mock.restore()
176+
})
177+
})
155178

156179
describe('Pulling', () => {
157180
beforeEach(() => {
158181
cli.mockConsole()
159-
mock(default_fs())
182+
mock(defaultFS())
160183
})
161184

162185
it('should correctly pull configs', () => {
163186
nock('https://api.heroku.com:443')
164187
.get('/apps/test/config-vars')
165188
.reply(200, fixtures.remote_obj)
166189

167-
let cmd = fetch_cmd('mypull')
168-
return cmd.run({flags: {}, app: 'test'}).then(() => {
169-
let res = fs.readFileSync('.env', 'utf-8')
190+
let cmd = fetchCMD('mypull')
191+
let fname = 'other.txt'
192+
return cmd.run({flags: {file: fname}, app: 'test'}).then(() => {
193+
let res = fs.readFileSync(fname, 'utf-8')
170194
expect(res).to.include(fixtures.merged_local_file)
171195
})
172196
})

util/file.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const cli = require('heroku-cli-util')
77
const DEFAULT_FNAME = '.env'
88
const header = '# this file was creatd automatically by heroku-config\n\n'
99

10-
function obj_to_file_format (obj) {
10+
function objToFileFormat (obj) {
1111
let res = `${header}`
1212
// always write keys alphabetically
1313
// makes file writing deterministic
@@ -19,7 +19,7 @@ function obj_to_file_format (obj) {
1919
return res
2020
}
2121

22-
function obj_from_file_format (s) {
22+
function objFromFileFormat (s) {
2323
let res = {}
2424
let data = s.split('\n')
2525
data.forEach(function (v) {
@@ -40,7 +40,7 @@ module.exports = {
4040
fname = fname || DEFAULT_FNAME
4141
// let data = fs.readFileSync(fname, 'utf-8')
4242
return fs.readFile(fname, 'utf-8').then((data) => {
43-
return Promise.resolve(obj_from_file_format(data))
43+
return Promise.resolve(objFromFileFormat(data))
4444
}).catch(() => {
4545
// cli.warning(`WARN - Unable to read from ${fname}`)
4646
// if it doesn't exist or we can't read, just start from scratch
@@ -49,13 +49,10 @@ module.exports = {
4949
},
5050
write: (obj, fname) => {
5151
fname = fname || DEFAULT_FNAME
52-
return fs.writeFile(fname, obj_to_file_format(obj)).then(() => {
52+
return fs.writeFile(fname, objToFileFormat(obj)).then(() => {
5353
cli.log(`Successfully wrote config to ${fname}!`)
5454
}).catch((err) => {
55-
cli.error(`Error writing to file ${fname} (${err.message})`)
56-
// return Promise.reject()
57-
// dunno if i actually need to throw this
58-
// throw new Error()
55+
throw new Error(`Error writing to file ${fname} (${err.message})`)
5956
})
6057
}
6158
}

util/flags.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
// command line args
22

33
module.exports = [{
4-
name: 'env',
5-
char: 'e',
4+
name: 'file',
5+
char: 'f',
66
hasValue: true,
77
description: 'specify target filename'
88
}, {
9-
name: 'interactive',
10-
char: 'i',
11-
description: 'prompt whether to overwrite each config var'
12-
}, {
9+
// i'll do this later
10+
// name: 'interactive',
11+
// char: 'i',
12+
// description: 'prompt whether to overwrite each config var'
13+
// }, {
1314
name: 'overwrite',
1415
char: 'o',
1516
description: 'overwrite existing config vars'

0 commit comments

Comments
 (0)