-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathcli.test.js
158 lines (132 loc) Β· 5.37 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
import path from 'path';
import {fix, git} from '@commitlint/test';
import test from 'ava';
import execa from 'execa';
import {merge} from 'lodash';
import * as sander from 'sander';
import stream from 'string-to-stream';
const bin = path.join(__dirname, './cli.js');
const cli = (args, options) => {
return (input = '') => {
const c = execa(bin, args, {
capture: ['stdout'],
cwd: options.cwd,
env: options.env
});
stream(input).pipe(c.stdin);
return c.catch(err => err);
};
};
test('should throw when called without [input]', async t => {
const cwd = await git.bootstrap('fixtures/empty');
const actual = await cli([], {cwd})();
t.is(actual.code, 1);
});
test('should reprint input from stdin', async t => {
const cwd = await git.bootstrap('fixtures/empty');
const actual = await cli([], {cwd})('foo: bar');
t.true(actual.stdout.includes('foo: bar'));
});
test('should produce no success output with --quiet flag', async t => {
const cwd = await git.bootstrap('fixtures/empty');
const actual = await cli(['--quiet'], {cwd})('foo: bar');
t.is(actual.stdout, '');
t.is(actual.stderr, '');
});
test('should produce no success output with -q flag', async t => {
const cwd = await git.bootstrap('fixtures/empty');
const actual = await cli(['-q'], {cwd})('foo: bar');
t.is(actual.stdout, '');
t.is(actual.stderr, '');
});
test('should succeed for input from stdin without rules', async t => {
const cwd = await git.bootstrap('fixtures/empty');
const actual = await cli([], {cwd})('foo: bar');
t.is(actual.code, 0);
});
test('should fail for input from stdin with rule from rc', async t => {
const cwd = await git.bootstrap('fixtures/simple');
const actual = await cli([], {cwd})('foo: bar');
t.true(actual.stdout.includes('type must not be one of [foo]'));
t.is(actual.code, 1);
});
test('should fail for input from stdin with rule from js', async t => {
const cwd = await git.bootstrap('fixtures/extends-root');
const actual = await cli(['--extends', './extended'], {cwd})('foo: bar');
t.true(actual.stdout.includes('type must not be one of [foo]'));
t.is(actual.code, 1);
});
test('should produce no error output with --quiet flag', async t => {
const cwd = await git.bootstrap('fixtures/simple');
const actual = await cli(['--quiet'], {cwd})('foo: bar');
t.is(actual.stdout, '');
t.is(actual.stderr, '');
t.is(actual.code, 1);
});
test('should produce no error output with -q flag', async t => {
const cwd = await git.bootstrap('fixtures/simple');
const actual = await cli(['-q'], {cwd})('foo: bar');
t.is(actual.stdout, '');
t.is(actual.stderr, '');
t.is(actual.code, 1);
});
test('should work with husky commitmsg hook and git commit', async () => {
const cwd = await git.bootstrap('fixtures/husky/integration');
await writePkg({scripts: {commitmsg: `${bin} -e`}}, {cwd});
await execa('npm', ['install'], {cwd});
await execa('git', ['add', 'package.json'], {cwd});
await execa('git', ['commit', '-m', '"test: this should work"'], {cwd});
});
test('should work with husky commitmsg hook in sub packages', async () => {
const upper = await git.bootstrap('fixtures/husky');
const cwd = path.join(upper, 'integration');
await writePkg({scripts: {commitmsg: `${bin} -e`}}, {cwd: upper});
await execa('npm', ['install'], {cwd});
await execa('git', ['add', 'package.json'], {cwd});
await execa('git', ['commit', '-m', '"test: this should work"'], {cwd});
});
test('should pick up parser preset and fail accordingly', async t => {
const cwd = await git.bootstrap('fixtures/parser-preset');
const actual = await cli(['--parser-preset', './parser-preset'], {cwd})(
'type(scope): subject'
);
t.is(actual.code, 1);
t.true(actual.stdout.includes('message may not be empty [subject-empty]'));
});
test('should pick up parser preset and succeed accordingly', async t => {
const cwd = await git.bootstrap('fixtures/parser-preset');
const actual = await cli(['--parser-preset', './parser-preset'], {cwd})(
'----type(scope): subject'
);
t.is(actual.code, 0);
});
test('should pick up config from outside git repo and fail accordingly', async t => {
const outer = await fix.bootstrap('fixtures/outer-scope');
const cwd = await git.init(path.join(outer, 'inner-scope'));
const actual = await cli([], {cwd})('inner: bar');
t.is(actual.code, 1);
});
test('should pick up config from outside git repo and succeed accordingly', async t => {
const outer = await fix.bootstrap('fixtures/outer-scope');
const cwd = await git.init(path.join(outer, 'inner-scope'));
const actual = await cli([], {cwd})('outer: bar');
t.is(actual.code, 0);
});
test('should pick up config from inside git repo with precedence and succeed accordingly', async t => {
const outer = await fix.bootstrap('fixtures/inner-scope');
const cwd = await git.init(path.join(outer, 'inner-scope'));
const actual = await cli([], {cwd})('inner: bar');
t.is(actual.code, 0);
});
test('should pick up config from inside git repo with precedence and fail accordingly', async t => {
const outer = await fix.bootstrap('fixtures/inner-scope');
const cwd = await git.init(path.join(outer, 'inner-scope'));
const actual = await cli([], {cwd})('outer: bar');
t.is(actual.code, 1);
});
async function writePkg(payload, options) {
const pkgPath = path.join(options.cwd, 'package.json');
const pkg = JSON.parse(await sander.readFile(pkgPath));
const result = merge(pkg, payload);
await sander.writeFile(pkgPath, JSON.stringify(result, null, ' '));
}