|
1 |
| -// Disable ftb |
2 |
| -// const os = require('os'); |
3 |
| -// const {git} = require('@commitlint/test'); |
4 |
| -import test from 'ava'; |
5 | 1 | import execa from 'execa';
|
6 |
| -// Disable ftb |
7 |
| -// const which = require('which'); |
8 |
| - |
9 |
| -// Disable ftb |
10 |
| -// const NODE_BIN = which.sync('node'); |
11 |
| -const BIN = require.resolve('../lib/cli.js'); |
12 |
| - |
13 |
| -// Disable ftb |
14 |
| -// const TRAVIS_COMMITLINT_BIN = require.resolve('../fixtures/commitlint'); |
15 |
| -// const TRAVIS_COMMITLINT_GIT_BIN = require.resolve('../fixtures/git'); |
16 |
| -// const TRAVIS_BRANCH = 'TRAVIS_BRANCH'; |
17 |
| -// const TRAVIS_COMMIT = 'TRAVIS_COMMIT'; |
| 2 | +import {git} from '@commitlint/test'; |
| 3 | + |
| 4 | +const bin = require.resolve('../lib/cli.js'); |
| 5 | + |
| 6 | +const TRAVIS_COMMITLINT_BIN = require.resolve('../fixtures/commitlint'); |
| 7 | +const TRAVIS_COMMITLINT_GIT_BIN = require.resolve('../fixtures/git'); |
| 8 | + |
| 9 | +const validBaseEnv = { |
| 10 | + TRAVIS: true, |
| 11 | + CI: true, |
| 12 | + TRAVIS_COMMIT: 'TRAVIS_COMMIT', |
| 13 | + TRAVIS_COMMITLINT_BIN: TRAVIS_COMMITLINT_BIN, |
| 14 | + TRAVIS_COMMITLINT_GIT_BIN: TRAVIS_COMMITLINT_GIT_BIN, |
| 15 | + TRAVIS_COMMIT_RANGE: 'TRAVIS_COMMIT_A.TRAVIS_COMMIT_B', |
| 16 | + TRAVIS_EVENT_TYPE: 'TRAVIS_EVENT_TYPE', |
| 17 | + TRAVIS_REPO_SLUG: 'TRAVIS_REPO_SLUG', |
| 18 | + TRAVIS_PULL_REQUEST_SLUG: 'TRAVIS_PULL_REQUEST_SLUG' |
| 19 | +}; |
18 | 20 |
|
19 |
| -const bin = async (config = {}) => { |
| 21 | +const cli = async (config = {}) => { |
20 | 22 | try {
|
21 |
| - return await execa(BIN, Object.assign({extendEnv: false}, config)); |
| 23 | + return await execa(bin, [], config); |
22 | 24 | } catch (err) {
|
23 | 25 | throw new Error([err.stdout, err.stderr].join('\n'));
|
24 | 26 | }
|
25 | 27 | };
|
26 | 28 |
|
27 |
| -test('should throw when not on travis ci', async t => { |
| 29 | +test('should throw when not on travis ci', async () => { |
28 | 30 | const env = {
|
29 | 31 | CI: false,
|
30 | 32 | TRAVIS: false
|
31 | 33 | };
|
32 | 34 |
|
33 |
| - await t.throwsAsync( |
34 |
| - bin({env}), |
35 |
| - /@commitlint\/travis-cli is intended to be used on Travis CI/ |
| 35 | + await expect(cli({env})).rejects.toThrow( |
| 36 | + '@commitlint/travis-cli is intended to be used on Travis CI' |
36 | 37 | );
|
37 | 38 | });
|
38 | 39 |
|
39 |
| -/* Test.failing( |
40 |
| - 'should throw when on travis ci, but env vars are missing', |
41 |
| - async t => { |
42 |
| - const env = { |
43 |
| - TRAVIS: true, |
44 |
| - CI: true |
45 |
| - }; |
46 |
| -
|
47 |
| - await t.throwsAsync(bin({env}), /TRAVIS_COMMIT, TRAVIS_BRANCH/); |
48 |
| - } |
49 |
| -); */ |
50 |
| - |
51 |
| -test('should throw when on travis ci, but TRAVIS_COMMIT is missing', async t => { |
| 40 | +test('should throw when on travis ci, but env vars are missing', async () => { |
52 | 41 | const env = {
|
53 | 42 | TRAVIS: true,
|
54 | 43 | CI: true
|
55 | 44 | };
|
56 | 45 |
|
57 |
| - await t.throwsAsync(bin({env}), /TRAVIS_COMMIT/); |
| 46 | + await expect(cli({env})).rejects.toThrow( |
| 47 | + 'TRAVIS_COMMIT, TRAVIS_COMMIT_RANGE, TRAVIS_EVENT_TYPE, TRAVIS_REPO_SLUG, TRAVIS_PULL_REQUEST_SLUG' |
| 48 | + ); |
58 | 49 | });
|
59 | 50 |
|
60 |
| -/* Test.failing( |
61 |
| - 'should throw when on travis ci, but TRAVIS_BRANCH is missing', |
62 |
| - async t => { |
63 |
| - const env = { |
64 |
| - TRAVIS: true, |
65 |
| - CI: true |
66 |
| - }; |
| 51 | +test('should call git with expected args', async () => { |
| 52 | + const cwd = await git.clone( |
| 53 | + 'https://github.com/conventional-changelog/commitlint.git', |
| 54 | + ['--depth=10'], |
| 55 | + __dirname, |
| 56 | + TRAVIS_COMMITLINT_GIT_BIN |
| 57 | + ); |
67 | 58 |
|
68 |
| - await t.throwsAsync(bin({env}), /TRAVIS_BRANCH/); |
69 |
| - } |
70 |
| -); |
| 59 | + const result = await cli({ |
| 60 | + cwd, |
| 61 | + env: validBaseEnv |
| 62 | + }); |
| 63 | + const invocations = await getInvocations(result.stdout); |
| 64 | + expect(invocations.length).toBe(3); |
71 | 65 |
|
72 |
| -test.failing('should call git with expected args on shallow repo', async t => { |
73 |
| - if (os.platform() === 'win32') { |
74 |
| - t.pass(); |
75 |
| - return; |
76 |
| - } |
| 66 | + const [stash, branches, commilint] = invocations; |
77 | 67 |
|
78 |
| - const cwd = await git.clone('https://github.com/conventional-changelog/commitlint.git', [ |
79 |
| - '--depth=10' |
80 |
| - ]); |
| 68 | + expect(stash).toEqual(['git', 'stash', '-k', '-u', '--quiet']); |
| 69 | + expect(branches).toEqual(['git', 'stash', 'pop', '--quiet']); |
| 70 | + expect(commilint).toEqual(['commitlint']); |
| 71 | +}); |
81 | 72 |
|
82 |
| - const env = { |
83 |
| - TRAVIS: true, |
84 |
| - CI: true, |
85 |
| - TRAVIS_BRANCH, |
86 |
| - TRAVIS_COMMIT, |
87 |
| - TRAVIS_COMMITLINT_BIN, |
| 73 | +test('should call git with expected args on pull_request', async () => { |
| 74 | + const cwd = await git.clone( |
| 75 | + 'https://github.com/conventional-changelog/commitlint.git', |
| 76 | + ['--depth=10'], |
| 77 | + __dirname, |
88 | 78 | TRAVIS_COMMITLINT_GIT_BIN
|
89 |
| - }; |
| 79 | + ); |
90 | 80 |
|
91 |
| - const result = await bin({cwd, env}); |
| 81 | + const result = await cli({ |
| 82 | + cwd, |
| 83 | + env: {...validBaseEnv, TRAVIS_EVENT_TYPE: 'pull_request'} |
| 84 | + }); |
92 | 85 | const invocations = await getInvocations(result.stdout);
|
93 |
| - t.is(invocations.length, 7); |
94 |
| -
|
95 |
| - const [ |
96 |
| - stash, |
97 |
| - branches, |
98 |
| - unshallow, |
99 |
| - checkout, |
100 |
| - back, |
101 |
| - pop, |
102 |
| - commilint |
103 |
| - ] = invocations; |
104 |
| -
|
105 |
| - t.deepEqual(stash, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash']); |
106 |
| - t.deepEqual(branches, [ |
107 |
| - NODE_BIN, |
108 |
| - TRAVIS_COMMITLINT_GIT_BIN, |
109 |
| - 'remote', |
110 |
| - 'set-branches', |
111 |
| - 'origin', |
112 |
| - TRAVIS_BRANCH |
113 |
| - ]); |
114 |
| - t.deepEqual(unshallow, [ |
115 |
| - NODE_BIN, |
116 |
| - TRAVIS_COMMITLINT_GIT_BIN, |
117 |
| - 'fetch', |
118 |
| - '--unshallow', |
119 |
| - '--quiet' |
120 |
| - ]); |
121 |
| - t.deepEqual(checkout, [ |
122 |
| - NODE_BIN, |
123 |
| - TRAVIS_COMMITLINT_GIT_BIN, |
124 |
| - 'checkout', |
125 |
| - TRAVIS_BRANCH, |
126 |
| - '--quiet' |
127 |
| - ]); |
128 |
| - t.deepEqual(back, [ |
129 |
| - NODE_BIN, |
130 |
| - TRAVIS_COMMITLINT_GIT_BIN, |
131 |
| - 'checkout', |
132 |
| - '-', |
133 |
| - '--quiet' |
134 |
| - ]); |
135 |
| - t.deepEqual(pop, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash', 'pop']); |
136 |
| - t.deepEqual(commilint, [ |
137 |
| - NODE_BIN, |
138 |
| - TRAVIS_COMMITLINT_BIN, |
| 86 | + expect(invocations.length).toBe(3); |
| 87 | + |
| 88 | + const [stash, branches, commilint] = invocations; |
| 89 | + |
| 90 | + expect(stash).toEqual(['git', 'stash', '-k', '-u', '--quiet']); |
| 91 | + expect(branches).toEqual(['git', 'stash', 'pop', '--quiet']); |
| 92 | + expect(commilint).toEqual([ |
| 93 | + 'commitlint', |
139 | 94 | '--from',
|
140 |
| - TRAVIS_BRANCH, |
| 95 | + 'TRAVIS_COMMIT_A', |
141 | 96 | '--to',
|
142 |
| - TRAVIS_COMMIT |
| 97 | + 'TRAVIS_COMMIT_B' |
143 | 98 | ]);
|
144 | 99 | });
|
145 | 100 |
|
146 |
| -test.failing( |
147 |
| - 'should call git with expected args on unshallow repo', |
148 |
| - async t => { |
149 |
| - if (os.platform() === 'win32') { |
150 |
| - t.pass(); |
151 |
| - return; |
152 |
| - } |
153 |
| -
|
154 |
| - const cwd = await git.clone('https://github.com/conventional-changelog/commitlint.git'); |
155 |
| -
|
156 |
| - const env = { |
157 |
| - TRAVIS: true, |
158 |
| - CI: true, |
159 |
| - TRAVIS_BRANCH, |
160 |
| - TRAVIS_COMMIT, |
161 |
| - TRAVIS_COMMITLINT_BIN, |
162 |
| - TRAVIS_COMMITLINT_GIT_BIN |
163 |
| - }; |
164 |
| -
|
165 |
| - const result = await bin({cwd, env}); |
166 |
| - const invocations = await getInvocations(result.stdout); |
167 |
| - t.is(invocations.length, 6); |
168 |
| -
|
169 |
| - const [stash, branches, checkout, back, pop, commilint] = invocations; |
170 |
| -
|
171 |
| - t.deepEqual(stash, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash']); |
172 |
| - t.deepEqual(branches, [ |
173 |
| - NODE_BIN, |
174 |
| - TRAVIS_COMMITLINT_GIT_BIN, |
175 |
| - 'remote', |
176 |
| - 'set-branches', |
177 |
| - 'origin', |
178 |
| - TRAVIS_BRANCH |
179 |
| - ]); |
180 |
| - t.deepEqual(checkout, [ |
181 |
| - NODE_BIN, |
182 |
| - TRAVIS_COMMITLINT_GIT_BIN, |
183 |
| - 'checkout', |
184 |
| - TRAVIS_BRANCH, |
185 |
| - '--quiet' |
186 |
| - ]); |
187 |
| - t.deepEqual(back, [ |
188 |
| - NODE_BIN, |
189 |
| - TRAVIS_COMMITLINT_GIT_BIN, |
190 |
| - 'checkout', |
191 |
| - '-', |
192 |
| - '--quiet' |
193 |
| - ]); |
194 |
| - t.deepEqual(pop, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash', 'pop']); |
195 |
| - t.deepEqual(commilint, [ |
196 |
| - NODE_BIN, |
197 |
| - TRAVIS_COMMITLINT_BIN, |
198 |
| - '--from', |
199 |
| - TRAVIS_BRANCH, |
200 |
| - '--to', |
201 |
| - TRAVIS_COMMIT |
202 |
| - ]); |
203 |
| - } |
204 |
| -); |
205 |
| -
|
206 | 101 | function getInvocations(stdout) {
|
207 | 102 | const matches = stdout.match(/[^[\]]+/g);
|
208 | 103 | const raw = Array.isArray(matches) ? matches : [];
|
209 | 104 |
|
210 |
| - return raw.filter(invocation => invocation !== '\n').map(invocation => |
211 |
| - invocation |
212 |
| - .split(',') |
213 |
| - .map(fragment => fragment.trim()) |
214 |
| - .map(fragment => fragment.substring(1, fragment.length - 1)) |
215 |
| - .filter(Boolean) |
216 |
| - ); |
| 105 | + return raw |
| 106 | + .filter(invocation => invocation !== '\n') |
| 107 | + .map(invocation => |
| 108 | + invocation |
| 109 | + .split(',') |
| 110 | + .map(fragment => fragment.trim()) |
| 111 | + .map(fragment => fragment.substring(1, fragment.length - 1)) |
| 112 | + .filter(Boolean) |
| 113 | + ); |
217 | 114 | }
|
218 |
| -*/ |
0 commit comments