-
-
Notifications
You must be signed in to change notification settings - Fork 313
/
commit.spec.ts
157 lines (134 loc) · 5.37 KB
/
commit.spec.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
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
import {
assertExecutedCommands,
closeWithSuccess,
commitResultNoneStaged,
commitResultSingleFile,
commitToBranch,
commitToRepoRoot,
like,
newSimpleGit
} from './__fixtures__';
import { SimpleGit } from '../../typings';
import { parseCommitResult } from '../../src/lib/parsers/parse-commit';
describe('commit', () => {
let git: SimpleGit;
let callback: jest.Mock;
beforeEach(() => {
git = newSimpleGit();
callback = jest.fn();
});
describe('usage', () => {
it('empty commit', async () => {
git.commit([], {'--amend': null, '--no-edit': null});
await closeWithSuccess();
assertExecutedCommands('commit', '--amend', '--no-edit');
});
it('single message, no files, no options and callback', async () => {
const task = git.commit('message', callback);
await closeWithSuccess();
assertExecutedCommands('commit', '-m', 'message');
expect(callback).toHaveBeenCalledWith(null, await task);
});
it('multi message, no files, no options and callback', async () => {
const task = git.commit(['aaa', 'bbb'], callback);
await closeWithSuccess();
assertExecutedCommands('commit', '-m', 'aaa', '-m', 'bbb');
expect(callback).toHaveBeenCalledWith(null, await task);
});
it('single message, no files, with options object and callback', async () => {
const task = git.commit('message', {'--opt': null}, callback);
await closeWithSuccess();
assertExecutedCommands('commit', '-m', 'message', '--opt');
expect(callback).toHaveBeenCalledWith(null, await task);
});
it('single message, single file, options object and callback', async () => {
const task = git.commit('msg', 'aaa.txt', {'--opt': null}, callback);
await closeWithSuccess();
assertExecutedCommands('commit', '-m', 'msg', 'aaa.txt', '--opt');
expect(callback).toHaveBeenCalledWith(null, await task);
});
it('single message, single file, no options with callback', async () => {
const task = git.commit('msg', 'aaa.txt', callback);
await closeWithSuccess();
assertExecutedCommands('commit', '-m', 'msg', 'aaa.txt',);
expect(callback).toHaveBeenCalledWith(null, await task);
});
it('multi message, single file, no options with callback', async () => {
const task = git.commit(['aaa', 'bbb'], 'aaa.txt', callback);
await closeWithSuccess();
assertExecutedCommands('commit', '-m', 'aaa', '-m', 'bbb', 'aaa.txt');
expect(callback).toHaveBeenCalledWith(null, await task);
});
it('multi message, multi file, no options with callback', async () => {
const task = git.commit(['aaa', 'bbb'], ['a.txt', 'b.txt'], callback);
await closeWithSuccess();
assertExecutedCommands('commit', '-m', 'aaa', '-m', 'bbb', 'a.txt', 'b.txt');
expect(callback).toHaveBeenCalledWith(null, await task);
});
it('multi message, multi file, options object with callback', async () => {
const task = git.commit(['aaa', 'bbb'], ['a.txt', 'b.txt'], {'--foo': null}, callback);
await closeWithSuccess();
assertExecutedCommands('commit', '-m', 'aaa', '-m', 'bbb', 'a.txt', 'b.txt', '--foo');
expect(callback).toHaveBeenCalledWith(null, await task);
});
});
describe('parsing', () => {
it('handles no files staged', () => {
expect(parseCommitResult(commitResultNoneStaged)).toEqual({
author: null,
branch: '',
commit: '',
root: false,
summary: {
changes: 0,
insertions: 0,
deletions: 0,
},
})
});
it('detects author', () => {
expect(parseCommitResult(commitResultSingleFile)).toEqual(like({
author: {
email: 'some@author.com',
name: 'Some Author'
}
}));
});
it('detects change summary with segments missing', () => {
expect(parseCommitResult(` 1 files changed, 2 deletions(-) `)).toHaveProperty('summary', like({
changes: 1,
insertions: 0,
deletions: 2,
}));
});
it('detects multi-file change summary', () => {
expect(parseCommitResult(`3 files changed, 29 insertions(+), 12 deletions(-)`)).toHaveProperty('summary', like({
changes: 3,
insertions: 29,
deletions: 12,
}));
});
it('detects branch name and commit hash', () => {
expect(parseCommitResult(`[branchNameInHere CommitHash] Add nodeunit test runner`)).toEqual(like({
branch: 'branchNameInHere',
commit: 'CommitHash',
root: false,
}));
});
it('handles the root commit', () => {
const actual = parseCommitResult(commitToRepoRoot({hash: 'foo', message: 'bar'}));
expect(actual).toEqual(like({
branch: 'master',
commit: 'foo',
root: true
}));
});
it('handles files with square brackets', () => {
const actual = parseCommitResult(commitToBranch({fileName: '[AB] CDE FGH.txt', branch: 'alpha'}));
expect(actual).toEqual(like({
branch: 'alpha',
root: false
}));
})
});
});