Skip to content

Commit 444bc97

Browse files
committed
fix: avoid excessive help text #606
1 parent 3874735 commit 444bc97

13 files changed

+2195
-347
lines changed

@commitlint/cli/src/cli.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,12 @@ async function main(options) {
204204
}
205205
);
206206

207-
const output = format(report, {color: flags.color});
207+
const output = format(report, {
208+
color: flags.color,
209+
verbose: flags.verbose,
210+
helpUrl:
211+
'https://github.com/conventional-changelog/commitlint/#what-is-commitlint'
212+
});
208213

209214
if (!flags.quiet) {
210215
console.log(output);

@commitlint/cli/src/cli.test.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,45 @@ test('should reprint input from stdin', async t => {
3232
t.true(actual.stdout.includes('foo: bar'));
3333
});
3434

35-
test('should produce no success output with --quiet flag', async t => {
35+
test('should produce success output with --verbose flag', async t => {
36+
const cwd = await git.bootstrap('fixtures/default');
37+
const actual = await cli(['--verbose'], {cwd})('type: bar');
38+
t.true(actual.stdout.includes('0 problems, 0 warnings'));
39+
t.is(actual.stderr, '');
40+
});
41+
42+
test('should produce no output with --quiet flag', async t => {
3643
const cwd = await git.bootstrap('fixtures/default');
3744
const actual = await cli(['--quiet'], {cwd})('foo: bar');
3845
t.is(actual.stdout, '');
3946
t.is(actual.stderr, '');
4047
});
4148

42-
test('should produce no success output with -q flag', async t => {
49+
test('should produce no output with -q flag', async t => {
4350
const cwd = await git.bootstrap('fixtures/default');
4451
const actual = await cli(['-q'], {cwd})('foo: bar');
4552
t.is(actual.stdout, '');
4653
t.is(actual.stderr, '');
4754
});
4855

56+
test('should produce help for empty config', async t => {
57+
const cwd = await git.bootstrap('fixtures/empty');
58+
const actual = await cli([], {cwd})('foo: bar');
59+
t.true(actual.stdout.includes('Please add rules'));
60+
t.is(actual.code, 1);
61+
});
62+
63+
test('should produce help for problems', async t => {
64+
const cwd = await git.bootstrap('fixtures/default');
65+
const actual = await cli([], {cwd})('foo: bar');
66+
t.true(
67+
actual.stdout.includes(
68+
'Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint'
69+
)
70+
);
71+
t.is(actual.code, 1);
72+
});
73+
4974
test('should fail for input from stdin without rules', async t => {
5075
const cwd = await git.bootstrap('fixtures/empty');
5176
const actual = await cli([], {cwd})('foo: bar');

@commitlint/format/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./lib";

@commitlint/format/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib');

@commitlint/format/jest.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node'
4+
};

@commitlint/format/package.json

+10-30
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,13 @@
77
"lib/"
88
],
99
"scripts": {
10-
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
10+
"build": "tsc",
1111
"clean": "npx rimraf lib",
1212
"deps": "dep-check",
1313
"pkg": "pkg-check --skip-import",
14-
"start": "concurrently \"ava -c 4 --verbose --watch\" \"yarn run watch\"",
15-
"test": "ava -c 4 --verbose",
16-
"watch": "babel src --out-dir lib --watch --source-maps"
17-
},
18-
"ava": {
19-
"files": [
20-
"src/**/*.test.js",
21-
"!lib/**/*"
22-
],
23-
"source": [
24-
"src/**/*.js",
25-
"!lib/**/*"
26-
],
27-
"babel": "inherit",
28-
"require": [
29-
"babel-register"
30-
]
31-
},
32-
"babel": {
33-
"presets": [
34-
"babel-preset-commitlint"
35-
]
14+
"start": "concurrently \"yarn test --watchAll\" \"yarn run watch\"",
15+
"test": "jest",
16+
"watch": "tsc -w"
3617
},
3718
"engines": {
3819
"node": ">=4"
@@ -59,17 +40,16 @@
5940
"devDependencies": {
6041
"@commitlint/test": "7.5.0",
6142
"@commitlint/utils": "^7.5.0",
62-
"ava": "0.22.0",
63-
"babel-cli": "6.26.0",
64-
"babel-preset-commitlint": "^7.5.0",
65-
"babel-register": "6.26.0",
43+
"@types/jest": "^24.0.12",
44+
"@types/lodash": "^4.14.123",
6645
"concurrently": "3.5.1",
6746
"cross-env": "5.1.1",
68-
"lodash": "4.17.11",
69-
"rimraf": "2.6.1"
47+
"jest": "^24.7.1",
48+
"rimraf": "2.6.1",
49+
"ts-jest": "^24.0.2",
50+
"typescript": "^3.4.5"
7051
},
7152
"dependencies": {
72-
"babel-runtime": "^6.23.0",
7353
"chalk": "^2.0.1"
7454
}
7555
}

@commitlint/format/src/format.test.ts

+248
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
import {format, formatResult} from '.';
2+
3+
test('does nothing without arguments', () => {
4+
const actual = format();
5+
expect(actual).toEqual('');
6+
});
7+
8+
test('does nothing without report results', () => {
9+
const actual = format({results: []});
10+
expect(actual).toEqual('');
11+
});
12+
13+
test('does nothing without .errors and .warnings', () => {
14+
const actual = format({results: [{}]});
15+
expect(actual).toEqual('');
16+
});
17+
18+
test('returns empty summary if verbose', () => {
19+
const actual = format({
20+
results: [
21+
{
22+
errors: [],
23+
warnings: []
24+
}
25+
]
26+
}, {
27+
verbose: true
28+
});
29+
30+
expect(actual).toContain('0 problems, 0 warnings');
31+
});
32+
33+
test('returns a correct summary of empty .errors and .warnings', () => {
34+
const actualError = format({
35+
results: [
36+
{
37+
errors: [
38+
{
39+
level: 2,
40+
name: 'error-name',
41+
message: 'There was an error'
42+
}
43+
]
44+
}
45+
]
46+
});
47+
48+
const actualWarning = format({
49+
results: [
50+
{
51+
warnings: [
52+
{
53+
level: 1,
54+
name: 'warning-name',
55+
message: 'There was a problem'
56+
}
57+
]
58+
}
59+
]
60+
});
61+
62+
expect(actualError).toContain('There was an error');
63+
expect(actualError).toContain('1 problems, 0 warnings');
64+
expect(actualWarning).toContain('There was a problem');
65+
expect(actualWarning).toContain('0 problems, 1 warnings');
66+
});
67+
68+
test('uses appropriate signs by default', () => {
69+
const actualError = format({
70+
results: [
71+
{
72+
errors: [
73+
{
74+
level: 2,
75+
name: 'error-name',
76+
message: 'There was an error'
77+
}
78+
]
79+
}
80+
]
81+
});
82+
83+
const actualWarning = format({
84+
results: [
85+
{
86+
warnings: [
87+
{
88+
level: 1,
89+
name: 'warning-name',
90+
message: 'There was a problem'
91+
}
92+
]
93+
}
94+
]
95+
});
96+
97+
expect(actualError).toContain('✖');
98+
expect(actualWarning).toContain('⚠');
99+
});
100+
101+
test('uses signs as configured', () => {
102+
const options = {signs: ['HNT', 'WRN', 'ERR'] as [string, string, string]};
103+
const actualError = format(
104+
{
105+
results: [
106+
{
107+
errors: [
108+
{
109+
level: 2,
110+
name: 'error-name',
111+
message: 'There was an error'
112+
}
113+
]
114+
}
115+
]
116+
},
117+
options
118+
);
119+
120+
const actualWarning = format(
121+
{
122+
results: [
123+
{
124+
warnings: [
125+
{
126+
level: 1,
127+
name: 'warning-name',
128+
message: 'There was a problem'
129+
}
130+
]
131+
}
132+
]
133+
},
134+
options
135+
);
136+
137+
expect(actualError).toContain('ERR');
138+
expect(actualWarning).toContain('WRN');
139+
});
140+
141+
test('format result provides summary without arguments', () => {
142+
const actual = formatResult();
143+
const actualText = actual.join('\n');
144+
145+
expect(actualText).toContain('0 problems, 0 warnings');
146+
});
147+
148+
test('format result transforms error to text', () => {
149+
const actual = formatResult({
150+
errors: [
151+
{
152+
level: 2,
153+
name: 'error-name',
154+
message: 'There was an error'
155+
}
156+
]
157+
});
158+
159+
const actualText = actual.join('\n');
160+
161+
expect(actualText).toContain('error-name');
162+
expect(actualText).toContain('There was an error');
163+
expect(actualText).toContain('1 problems, 0 warnings');
164+
});
165+
166+
test('format result transforms warning to text', () => {
167+
const actual = formatResult({
168+
warnings: [
169+
{
170+
level: 1,
171+
name: 'warning-name',
172+
message: 'There was a warning'
173+
}
174+
]
175+
});
176+
177+
const actualText = actual.join('\n');
178+
179+
expect(actualText).toContain('warning-name');
180+
expect(actualText).toContain('There was a warning');
181+
expect(actualText).toContain('0 problems, 1 warnings');
182+
});
183+
184+
test('format result prints help for errors', () => {
185+
const actual = formatResult({
186+
errors: [
187+
{
188+
level: 2,
189+
name: 'error-name',
190+
message: 'There was an error'
191+
}
192+
]
193+
}, {
194+
helpUrl: 'https://example.com'
195+
});
196+
197+
expect(actual).toEqual(expect.arrayContaining([
198+
expect.stringContaining('Get help:')
199+
]));
200+
});
201+
202+
test('format result prints help for warnings', () => {
203+
const actual = formatResult({
204+
warnings: [
205+
{
206+
level: 2,
207+
name: 'warning-name',
208+
message: 'There was a warning'
209+
}
210+
]
211+
}, {
212+
helpUrl: 'https://example.com'
213+
});
214+
215+
expect(actual).toEqual(expect.arrayContaining([
216+
expect.stringContaining('Get help:')
217+
]));
218+
});
219+
220+
test('format result help cotains options.helpUrl', () => {
221+
const helpUrl = 'https://example.com';
222+
223+
const actual = formatResult({
224+
warnings: [
225+
{
226+
level: 2,
227+
name: 'warning-name',
228+
message: 'There was a warning'
229+
}
230+
]
231+
}, {
232+
helpUrl
233+
});
234+
235+
expect(actual).toEqual(expect.arrayContaining([
236+
expect.stringContaining(helpUrl)
237+
]));
238+
});
239+
240+
test('format result omits help for empty problems', () => {
241+
const actual = formatResult({
242+
warnings: []
243+
});
244+
245+
expect(actual).not.toEqual(expect.arrayContaining([
246+
expect.stringContaining('Get help:')
247+
]));
248+
});

0 commit comments

Comments
 (0)