-
Notifications
You must be signed in to change notification settings - Fork 3
/
commands.spec.js
191 lines (162 loc) · 5.38 KB
/
commands.spec.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
/* eslint-env jest */
it('help', () => {
const mockLog = jest.spyOn(console, 'log').mockImplementation(() => {})
const { help } = require('./commands')
help()
expect(mockLog).toHaveBeenCalled()
})
it('newDeploy', () => {
jest.resetModules() // this is important - it clears the cache
jest.doMock('os', () => ({
homedir: () => '$HOME'
}))
const execSync = jest.fn((cmd, opt) => {
if (cmd.match(/heroku config:get MONGODB_URI/)) {
return 'mongodb://user:[email protected]/db'
}
if (cmd.match(/heroku container:release/)) {
return 'been released'
}
if (cmd.match(/heroku create/)) {
return 'https://tomato-sauce-54.herokuapps.com'
}
return 'stdout'
})
jest.doMock('child_process', () => ({
execSync
}))
const writeFileSync = jest.fn()
jest.doMock('fs', () => ({
readFileSync: (path) => {
if (path === '.meteor/release') {
return '[email protected]\r\n'
}
},
writeFileSync
}))
const tarXMock = jest.fn()
jest.doMock('tar', () => ({
x: tarXMock
}))
const clearBuildFolder = jest.fn()
jest.doMock('./helpers', () => ({
clearBuildFolder,
meteorBuildDir: '$HOME/.meteor-hero/builds',
explain: e => console.log(e),
logger: {
info: () => {},
error: (e) => console.log(e),
success: () => {}
},
objToEnvStr: () => 'E=t'
}))
const spy = jest.spyOn(process, 'cwd')
spy.mockReturnValue('cwd')
const dockerFile = require('./dockerfile')
const herokuCreate = jest.fn(() => '$APPNAME')
jest.doMock('./commandHelpers', () => ({
herokuCreate
}))
const { newDeploy } = require('./commands')
newDeploy({})
expect(clearBuildFolder).toHaveBeenCalledWith('$HOME/.meteor-hero/builds')
// buildMeteorApp
expect(execSync).toHaveBeenCalledWith('meteor build $HOME/.meteor-hero/builds --server-only --architecture=os.linux.x86_64')
// unzipMeteorBundle
expect(tarXMock)
.toHaveBeenCalledWith({
file: '$HOME/.meteor-hero/builds/cwd.tar.gz',
cwd: '$HOME/.meteor-hero/builds',
sync: true
})
// writeDockerFile
expect(writeFileSync).toHaveBeenCalledWith('$HOME/.meteor-hero/builds/bundle/Dockerfile', dockerFile)
// herokuCreate
expect(herokuCreate).toHaveBeenCalled()
// writeAppName
expect(writeFileSync).toHaveBeenCalledWith('.heroku_app_name', '$APPNAME')
// herokuLogin
expect(execSync).toHaveBeenCalledWith('heroku container:login')
// getFinalEnvVars (also looks for MONGO_URL)
expect(execSync).toHaveBeenCalledWith('heroku addons:create mongolab --app $APPNAME')
const execOpts = { cwd: '$HOME/.meteor-hero/builds/bundle' }
expect(execSync).toHaveBeenCalledWith(`heroku config:set --app $APPNAME E=t`, execOpts)
expect(execSync).toHaveBeenCalledWith('heroku container:push web -a $APPNAME', execOpts)
expect(execSync).toHaveBeenCalledWith('heroku container:release web -a $APPNAME', execOpts)
})
it('update', () => {
jest.resetModules() // this is important - it clears the cache
jest.doMock('os', () => ({
homedir: () => '$HOME'
}))
const execSync = jest.fn((cmd, opt) => {
if (cmd.match(/heroku config:get MONGODB_URI/)) {
return 'mongodb://user:[email protected]/db'
}
if (cmd.match(/heroku container:release/)) {
return 'been released'
}
if (cmd.match(/heroku create/)) {
return 'https://tomato-sauce-54.herokuapps.com'
}
return 'stdout'
})
jest.doMock('child_process', () => ({
execSync
}))
const writeFileSync = jest.fn()
jest.doMock('fs', () => ({
readFileSync: (path) => {
if (path === '.meteor/release') {
return '[email protected]\r\n'
}
if (path === '.heroku_app_name') {
return 'UPDATE_APP_NAME'
}
},
writeFileSync
}))
const tarXMock = jest.fn()
jest.doMock('tar', () => ({
x: tarXMock
}))
const clearBuildFolder = jest.fn()
jest.doMock('./helpers', () => ({
clearBuildFolder,
meteorBuildDir: '$HOME/.meteor-hero/builds',
explain: e => console.log(e),
logger: {
info: () => {},
error: (e) => console.log(e),
success: () => {}
},
objToEnvStr: () => 'nv=smthn'
}))
const spy = jest.spyOn(process, 'cwd')
spy.mockReturnValue('cwd')
const dockerFile = require('./dockerfile')
const herokuCreate = jest.fn(() => '$APPNAME')
jest.doMock('./commandHelpers', () => ({
herokuCreate
}))
const { newDeploy } = require('./commands')
newDeploy({ 'u': true, 'e': ['nv=smthn'] })
expect(clearBuildFolder).toHaveBeenCalledWith('$HOME/.meteor-hero/builds')
// buildMeteorApp
expect(execSync).toHaveBeenCalledWith('meteor build $HOME/.meteor-hero/builds --server-only --architecture=os.linux.x86_64')
// unzipMeteorBundle
expect(tarXMock)
.toHaveBeenCalledWith({
file: '$HOME/.meteor-hero/builds/cwd.tar.gz',
cwd: '$HOME/.meteor-hero/builds',
sync: true
})
// writeDockerFile
expect(writeFileSync).toHaveBeenCalledWith('$HOME/.meteor-hero/builds/bundle/Dockerfile', dockerFile)
// herokuLogin
expect(execSync).toHaveBeenCalledWith('heroku container:login')
const execOpts = { cwd: '$HOME/.meteor-hero/builds/bundle' }
expect(execSync).toHaveBeenCalledWith('heroku config:set --app UPDATE_APP_NAME nv=smthn', execOpts)
expect(execSync).toHaveBeenCalledWith('heroku container:push web -a UPDATE_APP_NAME', execOpts)
expect(execSync).toHaveBeenCalledWith('heroku container:release web -a UPDATE_APP_NAME', execOpts)
})