forked from conventional-changelog/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.test.js
147 lines (125 loc) Β· 3.75 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
import execa from 'execa';
import {git} from '@commitlint/test';
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 validBaseEnv = {
TRAVIS: true,
CI: true,
TRAVIS_COMMIT: 'TRAVIS_COMMIT',
TRAVIS_COMMITLINT_BIN: TRAVIS_COMMITLINT_BIN,
TRAVIS_COMMITLINT_GIT_BIN: TRAVIS_COMMITLINT_GIT_BIN,
TRAVIS_COMMIT_RANGE: 'TRAVIS_COMMIT_A.TRAVIS_COMMIT_B',
TRAVIS_EVENT_TYPE: 'TRAVIS_EVENT_TYPE',
TRAVIS_REPO_SLUG: 'TRAVIS_REPO_SLUG',
TRAVIS_PULL_REQUEST_SLUG: 'TRAVIS_PULL_REQUEST_SLUG'
};
const cli = async (config = {}, args = []) => {
try {
return await execa(bin, args, config);
} catch (err) {
throw new Error([err.stdout, err.stderr].join('\n'));
}
};
test('should throw when not on travis ci', async () => {
const env = {
CI: false,
TRAVIS: false
};
await expect(cli({env})).rejects.toThrow(
'@commitlint/travis-cli is intended to be used on Travis CI'
);
});
test('should throw when on travis ci, but env vars are missing', async () => {
const env = {
TRAVIS: true,
CI: true
};
await expect(cli({env})).rejects.toThrow(
'TRAVIS_COMMIT, TRAVIS_COMMIT_RANGE, TRAVIS_EVENT_TYPE, TRAVIS_REPO_SLUG, TRAVIS_PULL_REQUEST_SLUG'
);
});
test('should call git with expected args', async () => {
const cwd = await git.clone(
'https://github.com/conventional-changelog/commitlint.git',
['--depth=10'],
__dirname,
TRAVIS_COMMITLINT_GIT_BIN
);
const result = await cli({
cwd,
env: validBaseEnv
});
const invocations = await getInvocations(result.stdout);
expect(invocations.length).toBe(3);
const [stash, branches, commilint] = invocations;
expect(stash).toEqual(['git', 'stash', '-k', '-u', '--quiet']);
expect(branches).toEqual(['git', 'stash', 'pop', '--quiet']);
expect(commilint).toEqual(['commitlint']);
});
test('should call git with expected args on pull_request', async () => {
const cwd = await git.clone(
'https://github.com/conventional-changelog/commitlint.git',
['--depth=10'],
__dirname,
TRAVIS_COMMITLINT_GIT_BIN
);
const result = await cli({
cwd,
env: {...validBaseEnv, TRAVIS_EVENT_TYPE: 'pull_request'}
});
const invocations = await getInvocations(result.stdout);
expect(invocations.length).toBe(3);
const [stash, branches, commilint] = invocations;
expect(stash).toEqual(['git', 'stash', '-k', '-u', '--quiet']);
expect(branches).toEqual(['git', 'stash', 'pop', '--quiet']);
expect(commilint).toEqual([
'commitlint',
'--from',
'TRAVIS_COMMIT_A',
'--to',
'TRAVIS_COMMIT_B'
]);
});
test('should call git with extra expected args on pull_request', async () => {
const cwd = await git.clone(
'https://github.com/conventional-changelog/commitlint.git',
['--depth=10'],
__dirname,
TRAVIS_COMMITLINT_GIT_BIN
);
const result = await cli(
{
cwd,
env: {...validBaseEnv, TRAVIS_EVENT_TYPE: 'pull_request'}
},
['--config', './config/commitlint.config.js']
);
const invocations = await getInvocations(result.stdout);
expect(invocations.length).toBe(3);
const [stash, branches, commilint] = invocations;
expect(stash).toEqual(['git', 'stash', '-k', '-u', '--quiet']);
expect(branches).toEqual(['git', 'stash', 'pop', '--quiet']);
expect(commilint).toEqual([
'commitlint',
'--from',
'TRAVIS_COMMIT_A',
'--to',
'TRAVIS_COMMIT_B',
'--config',
'./config/commitlint.config.js'
]);
});
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)
);
}