forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub-pages-deploy.spec.js
263 lines (227 loc) · 9.28 KB
/
github-pages-deploy.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
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
/*eslint-disable no-console */
'use strict';
var ng = require('../helpers/ng');
var tmp = require('../helpers/tmp');
var conf = require('ember-cli/tests/helpers/conf');
var Promise = require('ember-cli/lib/ext/promise');
var fs = require('fs');
var path = require('path');
var chai = require('chai');
var sinon = require('sinon');
var ExecStub = require('../helpers/exec-stub');
var https = require('https');
var SilentError = require('silent-error');
const expect = chai.expect;
const fsReadFile = Promise.denodeify(fs.readFile);
const fsWriteFile = Promise.denodeify(fs.writeFile);
const fsMkdir = Promise.denodeify(fs.mkdir);
describe('Acceptance: ng github-pages:deploy', function() {
let execStub;
let project = 'foo',
initialBranch = 'master',
ghPagesBranch = 'gh-pages',
message = 'new gh-pages version',
remote = 'origin [email protected]:username/project.git (fetch)';
function setupDist() {
return fsMkdir('./dist')
.then(() => {
let indexHtml = path.join(process.cwd(), 'dist', 'index.html');
let indexData = `<title>project</title>\n<base href="/">`;
return fsWriteFile(indexHtml, indexData, 'utf8');
});
}
before(conf.setup);
after(conf.restore);
beforeEach(function() {
this.timeout(10000);
return tmp.setup('./tmp')
.then(() => process.chdir('./tmp'))
.then(() => ng(['new', project, '--skip-npm', '--skip-bower']))
.then(() => setupDist())
.finally(() => execStub = new ExecStub());
});
afterEach(function() {
this.timeout(10000);
return tmp.teardown('./tmp')
.then(() => expect(execStub.hasFailed()).to.be.false)
.then(() => expect(execStub.hasEmptyStack()).to.be.true)
.finally(() => execStub.restore());
});
it('should fail with uncommited changes', function() {
execStub.addExecSuccess('git status --porcelain', 'M dir/file.ext');
return ng(['github-pages:deploy', '--skip-build'])
.then(() => {
throw new SilentError('Should fail with uncommited changes but passing.');
}, (ret) => {
expect(ret.name).to.equal('SilentError');
expect(ret.isSilentError).to.equal(true);
});
});
it('should deploy with defaults to existing remote', function() {
execStub.addExecSuccess('git status --porcelain')
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
.addExecSuccess('git remote -v', remote)
.addExecSuccess(`git checkout ${ghPagesBranch}`)
.addExecSuccess('git add .')
.addExecSuccess(`git commit -m "${message}"`)
.addExecSuccess(`git checkout ${initialBranch}`)
.addExecSuccess(`git push origin ${ghPagesBranch}:${ghPagesBranch}`)
.addExecSuccess('git remote -v', remote);
return ng(['github-pages:deploy', '--skip-build'])
.then(() => {
let indexHtml = path.join(process.cwd(), 'index.html');
return fsReadFile(indexHtml, 'utf8');
})
.then((data) => expect(data.search(`<base href="/${project}/">`)).to.not.equal(-1));
});
it('should deploy with changed defaults', function() {
let userPageBranch = 'master',
message = 'not new gh-pages version';
execStub.addExecSuccess('git status --porcelain')
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
.addExecSuccess('git remote -v', remote)
.addExecSuccess(`git checkout ${ghPagesBranch}`)
.addExecSuccess('git add .')
.addExecSuccess(`git commit -m "${message}"`)
.addExecSuccess(`git checkout ${initialBranch}`)
.addExecSuccess(`git push origin ${ghPagesBranch}:${userPageBranch}`)
.addExecSuccess('git remote -v', remote);
return ng(['github-pages:deploy', '--skip-build', `--message=${message}`,
'--user-page'])
.then(() => {
let indexHtml = path.join(process.cwd(), 'index.html');
return fsReadFile(indexHtml, 'utf8');
})
.then((data) => expect(data.search('<base href="/">')).to.not.equal(-1));
});
it('should create branch if needed', function() {
execStub.addExecSuccess('git status --porcelain')
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
.addExecSuccess('git remote -v', remote)
.addExecError(`git checkout ${ghPagesBranch}`)
.addExecSuccess(`git checkout --orphan ${ghPagesBranch}`)
.addExecSuccess('git rm --cached -r .')
.addExecSuccess('git add .gitignore')
.addExecSuccess('git clean -f -d')
.addExecSuccess(`git commit -m \"initial ${ghPagesBranch} commit\"`)
.addExecSuccess('git add .')
.addExecSuccess(`git commit -m "${message}"`)
.addExecSuccess(`git checkout ${initialBranch}`)
.addExecSuccess(`git push origin ${ghPagesBranch}:${ghPagesBranch}`)
.addExecSuccess('git remote -v', remote);
return ng(['github-pages:deploy', '--skip-build'])
.then(() => {
let indexHtml = path.join(process.cwd(), 'index.html');
return fsReadFile(indexHtml, 'utf8');
})
.then((data) => expect(data.search(`<base href="/${project}/">`)).to.not.equal(-1));
});
it('should create repo if needed', function() {
let noRemote = '',
token = 'token',
username = 'username';
execStub.addExecSuccess('git status --porcelain')
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
.addExecSuccess('git remote -v', noRemote)
.addExecSuccess(`git remote add origin [email protected]:${username}/${project}.git`)
.addExecSuccess(`git push -u origin ${initialBranch}`)
.addExecSuccess(`git checkout ${ghPagesBranch}`)
.addExecSuccess('git add .')
.addExecSuccess(`git commit -m "${message}"`)
.addExecSuccess(`git checkout ${initialBranch}`)
.addExecSuccess(`git push origin ${ghPagesBranch}:${ghPagesBranch}`)
.addExecSuccess('git remote -v', remote);
var httpsStub = sinon.stub(https, 'request', httpsRequestStubFunc);
function httpsRequestStubFunc(req) {
let responseCb;
let expectedPostData = JSON.stringify({
'name': project
});
let expectedReq = {
hostname: 'api.github.com',
port: 443,
path: '/user/repos',
method: 'POST',
headers: {
'Authorization': `token ${token}`,
'Content-Type': 'application/json',
'Content-Length': expectedPostData.length,
'User-Agent': 'angular-cli-github-pages'
}
}
expect(req).to.eql(expectedReq);
return {
on: (event, cb) => responseCb = cb,
write: (postData) => expect(postData).to.eql(expectedPostData),
end: () => responseCb({ statusCode: 201 })
}
}
return ng(['github-pages:deploy', '--skip-build', `--gh-token=${token}`,
`--gh-username=${username}`])
.then(() => {
let indexHtml = path.join(process.cwd(), 'index.html');
return fsReadFile(indexHtml, 'utf8');
})
.then((data) => expect(data.search(`<base href="/${project}/">`)).to.not.equal(-1))
.then(() => httpsStub.restore());
});
it('should stop deploy if create branch fails', function() {
let noRemote = '',
token = 'token',
username = 'username';
execStub.addExecSuccess('git status --porcelain')
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
.addExecSuccess('git remote -v', noRemote);
var httpsStub = sinon.stub(https, 'request', httpsRequestStubFunc);
function httpsRequestStubFunc(req) {
let responseCb;
let expectedPostData = JSON.stringify({
'name': project
});
let expectedReq = {
hostname: 'api.github.com',
port: 443,
path: '/user/repos',
method: 'POST',
headers: {
'Authorization': `token ${token}`,
'Content-Type': 'application/json',
'Content-Length': expectedPostData.length,
'User-Agent': 'angular-cli-github-pages'
}
}
expect(req).to.eql(expectedReq);
return {
on: (event, cb) => responseCb = cb,
write: (postData) => expect(postData).to.eql(expectedPostData),
end: () => responseCb({ statusCode: 401, statusMessage: 'Unauthorized' })
}
}
return ng(['github-pages:deploy', '--skip-build', `--gh-token=${token}`,
`--gh-username=${username}`])
.then(() => {
throw new SilentError('Should not pass the deploy.');
}, (ret) => {
expect(ret.name).to.equal('SilentError');
expect(ret.isSilentError).to.equal(true);
})
.then(() => httpsStub.restore());
});
it('should fail gracefully when checkout has permissions failure', function() {
execStub.addExecSuccess('git status --porcelain')
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
.addExecSuccess('git remote -v', remote)
.addExecSuccess(`git checkout ${ghPagesBranch}`)
.addExecSuccess('git add .')
.addExecSuccess(`git commit -m "${message}"`)
.addExecError(`git checkout ${initialBranch}`, 'error: cannot stat \'src/client\': Permission denied');
return ng(['github-pages:deploy', '--skip-build'])
.then(() => {
throw new SilentError('Should not pass the deploy.');
}, (ret) => {
expect(ret.name).to.equal('SilentError');
expect(ret.isSilentError).to.equal(true);
expect(ret.message).to.contain('There was a permissions error');
});
});
});