/
parse_config.js
250 lines (213 loc) · 6.3 KB
/
parse_config.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
'use strict';
var should = require('chai').should(); // eslint-disable-line
describe('parse config', () => {
const parseConfig = require('../lib/parse_config');
it('single repo, no branch', () => {
const result = parseConfig({
repo: 'https://example.com/path/to/repo.git'
});
result.should.eql([
{url: 'https://example.com/path/to/repo.git', branch: 'master'}
]);
});
it('single repo, with branch', () => {
const result = parseConfig({
repo: 'https://example.com/path/to/repo.git',
branch: 'develop'
});
result.should.eql([
{url: 'https://example.com/path/to/repo.git', branch: 'develop'}
]);
});
it('single repo, branch after url', () => {
const result = parseConfig({
repo: 'https://example.com/path/to/repo.git,develop'
});
result.should.eql([
{url: 'https://example.com/path/to/repo.git', branch: 'develop'}
]);
});
it('multiple repo', () => {
const result = parseConfig({
repo: {
foo: 'https://example.com/path/to/repo.git',
bar: 'https://example.com/path/to/repo2.git,custom'
}
});
result.should.eql([
{url: 'https://example.com/path/to/repo.git', branch: 'master'},
{url: 'https://example.com/path/to/repo2.git', branch: 'custom'}
]);
});
it('github repo, master branch', () => {
// https
parseConfig({
repo: 'https://github.com/hexojs/hexojs.github.io.git'
})[0].branch.should.eql('master');
// git
parseConfig({
repo: 'git://github.com/hexojs/hexojs.github.io.git'
})[0].branch.should.eql('master');
// ssh
parseConfig({
repo: 'git@github.com:hexojs/hexojs.github.io.git'
})[0].branch.should.eql('master');
});
it('github repo, gh-pages branch', () => {
// https
parseConfig({
repo: 'https://github.com/hexojs/hexo.git'
})[0].branch.should.eql('gh-pages');
// git
parseConfig({
repo: 'git://github.com/hexojs/hexo.git'
})[0].branch.should.eql('gh-pages');
// ssh
parseConfig({
repo: 'git@github.com:hexojs/hexo.git'
})[0].branch.should.eql('gh-pages');
});
it('github repo, custom branch', () => {
parseConfig({
repo: 'https://github.com/hexojs/hexojs.github.io.git',
branch: 'site'
})[0].branch.should.eql('site');
});
it('coding repo, coding-pages branch', () => {
// https
parseConfig({
repo: 'https://coding.net/hexojs/hexojs.git'
})[0].branch.should.eql('coding-pages');
// git
parseConfig({
repo: 'git://coding.net/hexojs/hexojs.git'
})[0].branch.should.eql('coding-pages');
// ssh
parseConfig({
repo: 'git@coding.net/hexojs/hexojs.git'
})[0].branch.should.eql('coding-pages');
});
it('coding, custom branch', () => {
parseConfig({
repo: 'https://coding.net/hexojs/hexojs.git',
branch: 'site'
})[0].branch.should.eql('site');
});
it('repo is required', () => {
try {
parseConfig({});
} catch (err) {
err.should.have.property('message', 'repo is required!');
}
});
it('single repo with plain text token', () => {
// http
parseConfig({
repo: {
url: 'http://github.com/hexojs/hexojs.github.io.git',
token: 'plain_text_token'
}
})[0].url.should.eql('http://plain_text_token@github.com/hexojs/hexojs.github.io.git');
// token config for git scheme should be ignored
parseConfig({
repo: {
url: 'git://github.com/hexojs/hexojs.github.io.git',
token: 'plain_text_token'
}
})[0].url.should.eql('git://github.com/hexojs/hexojs.github.io.git');
});
it('single repo with env var token', () => {
process.env.GIT_TOKEN = 'env_token';
// http
parseConfig({
repo: {
url: 'http://github.com/hexojs/hexojs.github.io.git',
token: '$GIT_TOKEN'
}
})[0].url.should.eql('http://env_token@github.com/hexojs/hexojs.github.io.git');
// token config for git scheme should be ignored
parseConfig({
repo: {
url: 'git://github.com/hexojs/hexojs.github.io.git',
token: '$GIT_TOKEN'
}
})[0].url.should.eql('git://github.com/hexojs/hexojs.github.io.git');
delete process.env.GIT_TOKEN;
});
it('Structured single repo setting', () => {
parseConfig({
repo: {
url: 'https://coding.net/hexojs/hexojs.git',
branch: 'site'
}
})[0].branch.should.eql('site');
});
it('Single repo setting with name', () => {
parseConfig({
repo: {
my_repo: 'https://coding.net/hexojs/hexojs.git,site'
}
})[0].branch.should.eql('site');
});
it('Single structured repo setting with name', () => {
parseConfig({
repo: {
my_repo: {
url: 'https://coding.net/hexojs/hexojs.git',
branch: 'site'
}
}
})[0].branch.should.eql('site');
});
it('Structured multiple repo settings', () => {
process.env.GIT_TOKEN = 'env_token';
const result = parseConfig({
repo: {
coding: {
url: 'https://coding.net/hexojs/hexojs.git',
branch: 'site'
},
github: {
url: 'https://github.com/hexojs/hexojs.github.io.git',
token: 'plain_token'
},
other: {
url: 'https://example.com/path/to/repo.git',
token: '$GIT_TOKEN',
branch: 'page'
}
}
});
result.should.eql([
{url: 'https://coding.net/hexojs/hexojs.git', branch: 'site'},
{url: 'https://plain_token@github.com/hexojs/hexojs.github.io.git', branch: 'master'},
{url: 'https://env_token@example.com/path/to/repo.git', branch: 'page'}
]);
delete process.env.GIT_TOKEN;
});
it('fail to read env var token', () => {
// http
try {
parseConfig({
repo: {
url: 'http://github.com/hexojs/hexojs.github.io.git',
token: '$GIT_TOKEN'
}
});
} catch (err) {
err.should.have.property('message', 'Fail to read environment varable: $GIT_TOKEN, check your config!');
}
});
it('invalid url', () => {
try {
parseConfig({
repo: {
url: 'http:///hexojs/hexojs.github.io.git',
token: '$GIT_TOKEN'
}
});
} catch (err) {
err.should.have.property('message', 'Fail to parse your repo url, check your config!');
}
});
});