forked from conventional-changelog/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.test.ts
132 lines (102 loc) Β· 4.52 KB
/
read.test.ts
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
import {test, expect} from 'vitest';
import fs from 'fs/promises';
import path from 'path';
import {git} from '@commitlint/test';
import {execa} from 'execa';
import read from './read.js';
test('get edit commit message specified by the `edit` flag', async () => {
const cwd: string = await git.bootstrap();
await fs.writeFile(path.join(cwd, 'commit-msg-file'), 'foo');
const expected = ['foo\n'];
const actual = await read({edit: 'commit-msg-file', cwd});
expect(actual).toEqual(expected);
});
test('get edit commit message from git root', async () => {
const cwd: string = await git.bootstrap();
await fs.writeFile(path.join(cwd, 'alpha.txt'), 'alpha');
await execa('git', ['add', '.'], {cwd});
await execa('git', ['commit', '-m', 'alpha'], {cwd});
const expected = ['alpha\n\n'];
const actual = await read({edit: true, cwd});
expect(actual).toEqual(expected);
});
test('get history commit messages', async () => {
const cwd: string = await git.bootstrap();
await fs.writeFile(path.join(cwd, 'alpha.txt'), 'alpha');
await execa('git', ['add', 'alpha.txt'], {cwd});
await execa('git', ['commit', '-m', 'alpha'], {cwd});
await execa('git', ['rm', 'alpha.txt'], {cwd});
await execa('git', ['commit', '-m', 'remove alpha'], {cwd});
const expected = ['remove alpha\n\n', 'alpha\n\n'];
const actual = await read({cwd});
expect(actual).toEqual(expected);
});
test('get edit commit message from git subdirectory', async () => {
const cwd: string = await git.bootstrap();
await fs.mkdir(path.join(cwd, 'beta'));
await fs.writeFile(path.join(cwd, 'beta/beta.txt'), 'beta');
await execa('git', ['add', '.'], {cwd});
await execa('git', ['commit', '-m', 'beta'], {cwd});
const expected = ['beta\n\n'];
const actual = await read({edit: true, cwd});
expect(actual).toEqual(expected);
});
test('get edit commit message while skipping first commit', async () => {
const cwd: string = await git.bootstrap();
await fs.mkdir(path.join(cwd, 'beta'));
await fs.writeFile(path.join(cwd, 'beta/beta.txt'), 'beta');
await fs.writeFile(path.join(cwd, 'alpha.txt'), 'alpha');
await execa('git', ['add', 'alpha.txt'], {cwd});
await execa('git', ['commit', '-m', 'alpha'], {cwd});
await fs.writeFile(path.join(cwd, 'beta.txt'), 'beta');
await execa('git', ['add', 'beta.txt'], {cwd});
await execa('git', ['commit', '-m', 'beta'], {cwd});
await fs.writeFile(path.join(cwd, 'gamma.txt'), 'gamma');
await execa('git', ['add', 'gamma.txt'], {cwd});
await execa('git', ['commit', '-m', 'gamma'], {cwd});
const expected = ['beta\n\n'];
const actual = await read({from: 'HEAD~2', cwd, gitLogArgs: '--skip 1'});
expect(actual).toEqual(expected);
});
test('should only read the last commit', async () => {
const cwd: string = await git.bootstrap();
await execa('git', ['commit', '--allow-empty', '-m', 'commit Z'], {cwd});
await execa('git', ['commit', '--allow-empty', '-m', 'commit Y'], {cwd});
await execa('git', ['commit', '--allow-empty', '-m', 'commit X'], {cwd});
const result = await read({cwd, last: true});
expect(result).toEqual(['commit X']);
});
test('should read commits from the last annotated tag', async () => {
const cwd: string = await git.bootstrap();
await execa(
'git',
['commit', '--allow-empty', '-m', 'chore: release v1.0.0'],
{cwd}
);
await execa('git', ['tag', 'v1.0.0', '--annotate', '-m', 'v1.0.0'], {cwd});
await execa('git', ['commit', '--allow-empty', '-m', 'commit 1'], {cwd});
await execa('git', ['commit', '--allow-empty', '-m', 'commit 2'], {cwd});
const result = await read({cwd, fromLastTag: true});
expect(result).toEqual(['commit 2\n\n', 'commit 1\n\n']);
});
test('should read commits from the last lightweight tag', async () => {
const cwd: string = await git.bootstrap();
await execa(
'git',
['commit', '--allow-empty', '-m', 'chore: release v9.9.9-alpha.1'],
{cwd}
);
await execa('git', ['tag', 'v9.9.9-alpha.1'], {cwd});
await execa('git', ['commit', '--allow-empty', '-m', 'commit A'], {cwd});
await execa('git', ['commit', '--allow-empty', '-m', 'commit B'], {cwd});
const result = await read({cwd, fromLastTag: true});
expect(result).toEqual(['commit B\n\n', 'commit A\n\n']);
});
test('should not read any commits when there are no tags', async () => {
const cwd: string = await git.bootstrap();
await execa('git', ['commit', '--allow-empty', '-m', 'commit 7'], {cwd});
await execa('git', ['commit', '--allow-empty', '-m', 'commit 8'], {cwd});
await execa('git', ['commit', '--allow-empty', '-m', 'commit 9'], {cwd});
const result = await read({cwd, fromLastTag: true});
expect(result).toHaveLength(0);
});