-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathcli.test.js
204 lines (179 loc) Β· 4.16 KB
/
cli.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
const os = require('os');
const {git} = require('@commitlint/test');
const test = require('ava');
const execa = require('execa');
const which = require('which');
const NODE_BIN = which.sync('node');
const BIN = require.resolve('../lib/cli.js');
const TRAVIS_COMMITLINT_BIN = require.resolve('../fixtures/commitlint');
const TRAVIS_COMMITLINT_GIT_BIN = require.resolve('../fixtures/git');
const TRAVIS_BRANCH = 'TRAVIS_BRANCH';
const TRAVIS_COMMIT = 'TRAVIS_COMMIT';
const bin = async (config = {}) => {
try {
return await execa(BIN, Object.assign({extendEnv: false}, config));
} catch (err) {
throw new Error([err.stdout, err.stderr].join('\n'));
}
};
test('should throw when not on travis ci', async t => {
const env = {
CI: false,
TRAVIS: false
};
await t.throws(
bin({env}),
/@commitlint\/travis-cli is inteded to be used on Travis CI/
);
});
test('should throw when on travis ci, but env vars are missing', async t => {
const env = {
TRAVIS: true,
CI: true
};
await t.throws(bin({env}), /TRAVIS_COMMIT, TRAVIS_BRANCH/);
});
test('should throw when on travis ci, but TRAVIS_COMMIT is missing', async t => {
const env = {
TRAVIS: true,
CI: true
};
await t.throws(bin({env}), /TRAVIS_COMMIT/);
});
test('should throw when on travis ci, but TRAVIS_BRANCH is missing', async t => {
const env = {
TRAVIS: true,
CI: true
};
await t.throws(bin({env}), /TRAVIS_BRANCH/);
});
test('should call git with expected args on shallow repo', async t => {
if (os.platform() === 'win32') {
t.pass();
return;
}
const cwd = await git.clone('https://github.com/marionebl/commitlint.git', [
'--depth=10'
]);
const env = {
TRAVIS: true,
CI: true,
TRAVIS_BRANCH,
TRAVIS_COMMIT,
TRAVIS_COMMITLINT_BIN,
TRAVIS_COMMITLINT_GIT_BIN
};
const result = await bin({cwd, env});
const invocations = await getInvocations(result.stdout);
t.is(invocations.length, 7);
const [
stash,
branches,
unshallow,
checkout,
back,
pop,
commilint
] = invocations;
t.deepEqual(stash, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash']);
t.deepEqual(branches, [
NODE_BIN,
TRAVIS_COMMITLINT_GIT_BIN,
'remote',
'set-branches',
'origin',
TRAVIS_BRANCH
]);
t.deepEqual(unshallow, [
NODE_BIN,
TRAVIS_COMMITLINT_GIT_BIN,
'fetch',
'--unshallow',
'--quiet'
]);
t.deepEqual(checkout, [
NODE_BIN,
TRAVIS_COMMITLINT_GIT_BIN,
'checkout',
TRAVIS_BRANCH,
'--quiet'
]);
t.deepEqual(back, [
NODE_BIN,
TRAVIS_COMMITLINT_GIT_BIN,
'checkout',
'-',
'--quiet'
]);
t.deepEqual(pop, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash', 'pop']);
t.deepEqual(commilint, [
NODE_BIN,
TRAVIS_COMMITLINT_BIN,
'--from',
TRAVIS_BRANCH,
'--to',
TRAVIS_COMMIT
]);
});
test('should call git with expected args on unshallow repo', async t => {
if (os.platform() === 'win32') {
t.pass();
return;
}
const cwd = await git.clone('https://github.com/marionebl/commitlint.git');
const env = {
TRAVIS: true,
CI: true,
TRAVIS_BRANCH,
TRAVIS_COMMIT,
TRAVIS_COMMITLINT_BIN,
TRAVIS_COMMITLINT_GIT_BIN
};
const result = await bin({cwd, env});
const invocations = await getInvocations(result.stdout);
t.is(invocations.length, 6);
const [stash, branches, checkout, back, pop, commilint] = invocations;
t.deepEqual(stash, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash']);
t.deepEqual(branches, [
NODE_BIN,
TRAVIS_COMMITLINT_GIT_BIN,
'remote',
'set-branches',
'origin',
TRAVIS_BRANCH
]);
t.deepEqual(checkout, [
NODE_BIN,
TRAVIS_COMMITLINT_GIT_BIN,
'checkout',
TRAVIS_BRANCH,
'--quiet'
]);
t.deepEqual(back, [
NODE_BIN,
TRAVIS_COMMITLINT_GIT_BIN,
'checkout',
'-',
'--quiet'
]);
t.deepEqual(pop, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash', 'pop']);
t.deepEqual(commilint, [
NODE_BIN,
TRAVIS_COMMITLINT_BIN,
'--from',
TRAVIS_BRANCH,
'--to',
TRAVIS_COMMIT
]);
});
function getInvocations(stdout) {
const matches = stdout.match(/[^[\]]+/g);
const raw = Array.isArray(matches) ? matches : [];
return raw.filter(invocation => invocation !== '\n').map(invocation =>
invocation
.split(',')
.map(fragment => fragment.trim())
.map(fragment => fragment.substring(1, fragment.length - 1))
.filter(Boolean)
);
}