-
Notifications
You must be signed in to change notification settings - Fork 68
/
test.js
265 lines (231 loc) · 6.11 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
/* eslint-env mocha */
'use strict';
const assert = require('assert').strict;
const {PassThrough} = require('stream');
const {join} = require('path');
const {randomBytes} = require('crypto');
const {readFile} = require('fs').promises;
const File = require('vinyl');
const ghPages = require('.');
const github = require('octonode');
const loadGhToken = require('load-gh-token');
const noop = require('lodash/noop');
const readRemoveFile = require('read-remove-file');
const rimraf = require('rimraf');
const tmpDir = '.publish';
const tmpStr = randomBytes(32).toString('hex');
const tmpFile = new File({
path: tmpStr,
contents: Buffer.from(tmpStr)
});
const files = [
new File({
path: '.gitignore',
contents: Buffer.from('ignored-path')
}),
new File({
path: 'ignored-path',
contents: Buffer.from('ignored')
}),
tmpFile
];
const tmpRepoName = 'shinnn/css-wide-keywords';
let remoteUrl;
let accessToken;
let client;
before(async () => {
accessToken = await loadGhToken();
client = github.client(accessToken);
remoteUrl = `https://${accessToken}@github.com/${tmpRepoName}.git`;
});
describe('gulp-gh-pages', () => {
before(done => {
rimraf.sync(tmpDir);
client.del(`/repos/${tmpRepoName}/git/refs/heads/tmp`, {}, err => {
if (err && err.message !== 'Reference does not exist') {
done(err);
return;
}
client.post(`/repos/${tmpRepoName}/git/refs`, {
ref: 'refs/heads/tmp',
sha: '8d6c241faa1246137a57b9f3cefcafbf30f14966'
}, done);
});
});
it('should emit an error when it takes a non-Vinyl value', done => {
ghPages()
.on('error', ({message}) => {
assert.equal(
message,
'Expected a stream created by gulp-gh-pages to receive Vinyl objects https://github.com/gulpjs/vinyl, but got <Buffer 3f>.'
);
done();
})
.end(Buffer.from('?'));
});
it('should emit an error when it takes an older version of Vinyl object', done => {
ghPages()
.on('error', ({message}) => {
assert.equal(
message,
'gulp-gh-pages doesn\'t support gulp <= v3.x. Update your project to use gulp >= v4.0.0.'
);
done();
})
.end({isNull: noop});
});
it('should ignore empty vinyl file objects', done => {
ghPages()
.on('error', done)
.on('data', file => {
assert(file.isNull());
done();
})
.end(new File());
});
it('should emit an error when it takes files with stream contents', done => {
ghPages()
.on('error', err => {
assert.equal(err.message, 'Stream content is not supported');
done();
})
.end(new File({contents: new PassThrough({objectMode: true})}));
});
it('should push commits to an existing gh-pages branch', done => {
const stream = ghPages({
remoteUrl,
branch: 'tmp',
message: '[ci skip] temporary commit ("tmp" branch)'
})
.on('error', done)
.on('data', file => assert(file.isBuffer()))
.on('end', async () => {
assert.equal(await readFile(join(tmpDir, tmpStr), 'utf8'), tmpStr);
done();
});
for (const file of files) {
stream.write(file);
}
stream.end();
});
it('should not push any commits when no files has been changed', done => {
const stream = ghPages({
remoteUrl,
branch: 'tmp'
})
.on('error', done)
.on('data', file => assert(file.isBuffer()))
.on('end', done);
for (const file of files) {
stream.write(file);
}
stream.end();
});
it('should create and checkout a branch', done => {
client.del(`/repos/${tmpRepoName}/git/refs/heads/new`, {}, err => {
if (err && err.message !== 'Reference does not exist') {
done(err);
return;
}
const stream = ghPages({
remoteUrl,
branch: 'new',
message: '[ci skip] temporary commit ("new" branch)',
push: true,
force: true
})
.on('error', done)
.on('data', file => assert(file.isBuffer()))
.on('end', () => {
client.del(`/repos/${tmpRepoName}/git/refs/heads/new`, {}, reqErr => {
if (reqErr && reqErr.message !== 'Reference does not exist') {
done(reqErr);
return;
}
done();
});
});
stream.write(new File({
path: '.gitignore',
contents: Buffer.from('node_modules\nfoo')
}));
stream.write(new File({
path: 'foo',
contents: Buffer.from('hi')
}));
stream.end(new File({
path: 'node_modules/bar.txt',
contents: Buffer.from('hello\n')
}));
});
});
it('should specify the cache directory path with `cacheDir` option', done => {
ghPages({
remoteUrl,
cacheDir: '__cache__',
push: false
})
.on('error', done)
.on('data', file => {
assert(file.isBuffer());
})
.on('end', async () => {
assert.equal(
await readRemoveFile(join(__dirname, '__cache__', tmpStr), 'utf8'),
tmpStr
);
done();
})
.end(tmpFile);
});
it('should emit an error when it fails to create a cache directory', done => {
ghPages({
remoteUrl,
cacheDir: join(__filename, 'dir')
})
.on('error', err => {
assert(err.code);
done();
})
.on('end', () => done(new Error('Expected an error.')))
.end(tmpFile);
});
it('should emit an error when the repository doesn\'t exist', done => {
ghPages({remoteUrl: 'https://_/_this_/_repo_/_does_/_not_/_exist_/_.git'})
.on('error', err => {
assert(err);
done();
})
.on('end', () => done(new Error('Expected an error.')))
.end(tmpFile);
});
it('should emit an error when the user has no permission to push the repo', done => {
ghPages({remoteUrl: `https://${accessToken}@github.com/z/dotfiles.git`})
.on('error', ({message}) => {
assert(message.includes('Permission to'));
done();
})
.on('end', () => done(new Error('Expected an error.')))
.end(tmpFile);
});
it('should emit an error when the remote URL is not a git repository\'s URL', done => {
ghPages({remoteUrl: 'https://example.org/'})
.on('error', ({message}) => {
assert(message.includes('ENOENT'));
done();
})
.on('end', () => done(new Error('Expected an error.')))
.end(tmpFile);
});
it('should emit an error when the current directory is not a git repository', done => {
process.chdir('/');
ghPages()
.on('error', err => {
assert(err);
process.chdir(join(__dirname, '..'));
done();
})
.on('end', () => done(new Error('Expected an error.')))
.end(tmpFile);
});
});