-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.test.ts
260 lines (237 loc) · 9.17 KB
/
validator.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* SPDX-FileCopyrightText: 2023 Kevin de Jong <monkaii@hotmail.com>
* SPDX-License-Identifier: MIT
*/
import { Commit, ConventionalCommit } from "@dev-build-deploy/commit-it";
import * as validator from "../src/validator";
describe("Validate commit messages", () => {
test("Valid commit message", () => {
const result = validator.validateCommits([
Commit.fromString({
hash: "0a0b0c0d",
message: "feat: Add new feature",
}),
]);
let count = 0;
result.forEach(item => (count += item.errors.length));
expect(count).toBe(0);
});
test("Invalid commit message", () => {
const result = validator.validateCommits([
Commit.fromString({
hash: "0a0b0c0d",
message: "feat (no noun): Add new feature",
}),
]);
let count = 0;
result.forEach(item => (count += item.errors.length));
// Space in between type and scope
// Scope is not a noun
expect(count).toBe(2);
});
test("Commit messages with warnings", () => {
const result = validator.validateCommits([
Commit.fromString({
hash: "0a0b0c0d",
message: `feat: Add new feature
BREAKING CHANGE: this will be ignored and raise a warning...
... as it is followed by a new paragraph`,
}),
]);
let errorCount = 0;
let warningCount = 0;
result.forEach(item => (errorCount += item.errors.length));
result.forEach(item => (warningCount += item.warnings.length));
expect(errorCount).toBe(0);
expect(warningCount).toBe(1);
});
test("Valid Pull Request message", () => {
const result = validator.validatePullRequest(
Commit.fromString({ hash: "0a0b0c0d", message: "feat: Add new feature" }),
[
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "feat: Add new feature" }),
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "fix: Fixed a bug" }),
]
);
expect(result.errors.length).toBe(0);
});
test("Invalid Pull Request message", () => {
const result = validator.validatePullRequest(
Commit.fromString({
hash: "0a0b0c0d",
message: "feat (no noun): Add new feature",
}),
[
ConventionalCommit.fromString({
hash: "0a0b0c0d",
message: "feat: Add new feature",
}),
]
);
// Space in between type and scope
// Scope is not a noun
expect(result.errors.length).toBe(2);
});
});
describe("Validate invalid Pull Request vs Commits", () => {
const testData = [
{
description: "Invalid Pull Request",
pullRequest: Commit.fromString({ hash: "0a0b0c0d", message: "feat (no noun): Add new feature" }),
commits: [
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "chore: silly change" }),
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "feat: add new feature" }),
],
errorCount: 2,
},
{
description: "Valid and Invalid Commits",
pullRequest: Commit.fromString({ hash: "0a0b0c0d", message: "feat: Add new feature" }),
commits: [
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "chore: silly change" }),
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "feat (no noun)!: silly change" }),
],
errorCount: 0,
},
{
description: "Only Invalid Commits",
pullRequest: Commit.fromString({ hash: "0a0b0c0d", message: "feat: Add new feature" }),
commits: [ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "feat (no noun)!: silly change" })],
errorCount: 0,
},
{
description: "No Commits",
pullRequest: Commit.fromString({ hash: "0a0b0c0d", message: "feat: Add new feature" }),
commits: [],
errorCount: 0,
},
];
it.each(testData)("$test.description", test => {
const result = validator.validatePullRequest(test.pullRequest, test.commits);
expect(result.errors.length).toBe(test.errorCount);
});
});
describe("Validate valid Pull Request vs Commits", () => {
const testData = [
{
description: "Pull Request < Commit",
pullRequest: Commit.fromString({ hash: "0a0b0c0d", message: "chore: silly change" }),
commits: [ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "feat: add new feature" })],
error: true,
},
{
description: "Pull Request < Commits",
pullRequest: Commit.fromString({ hash: "0a0b0c0d", message: "chore: silly change" }),
commits: [
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "chore: silly change" }),
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "feat: add new feature" }),
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "chore(deps-dev): update dependencies" }),
],
error: true,
},
{
description: "Breaking Pull Request === Breaking Commits",
pullRequest: Commit.fromString({ hash: "0a0b0c0d", message: "chore!: silly change" }),
commits: [
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "chore: silly change" }),
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "feat!: add new feature" }),
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "chore(deps-dev): update dependencies" }),
],
error: false,
},
{
description: "Pull Request === Invalid Commits",
pullRequest: Commit.fromString({ hash: "0a0b0c0d", message: "feat: add a new feature" }),
commits: [
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "chore (no noun): silly change" }),
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "add new feature" }),
],
error: false,
},
{
description: "Pull Request === No Commits",
pullRequest: Commit.fromString({ hash: "0a0b0c0d", message: "feat: add a new feature" }),
commits: [],
error: false,
},
{
description: "Pull Request (BREAKING CHANGE) === Breaking Commits",
pullRequest: Commit.fromString({
hash: "0a0b0c0d",
message: "feat: add a new feature\n\nBREAKING-CHANGE: This is breaking",
}),
commits: [
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "chore: silly change" }),
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "feat!: add new feature" }),
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "chore(deps-dev): update dependencies" }),
],
error: false,
},
{
description: "Pull Request (BREAKING CHANGE) === Commits (BREAKING CHANGE)",
pullRequest: Commit.fromString({
hash: "0a0b0c0d",
message: "feat: add a new feature\n\nBREAKING-CHANGE: This is breaking",
}),
commits: [
ConventionalCommit.fromString({
hash: "0a0b0c0d",
message: "chore: silly change\n\nBREAKING CHANGE: This is breaking?",
}),
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "feat: add new feature" }),
],
error: false,
},
{
description: "Pull Request (BREAKING CHANGE) > Commits",
pullRequest: Commit.fromString({
hash: "0a0b0c0d",
message: "fix: add fix\n\nBREAKING-CHANGE: This is breaking",
}),
commits: [
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "chore: silly change" }),
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "fix: fixed a bug" }),
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "chore(deps-dev): update dependencies" }),
],
error: false,
},
{
description: "Pull Request > Commits",
pullRequest: Commit.fromString({ hash: "0a0b0c0d", message: "feat: add a new feature" }),
commits: [
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "chore: silly change" }),
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "fix: fixed a bug" }),
ConventionalCommit.fromString({ hash: "0a0b0c0d", message: "chore(deps-dev): update dependencies" }),
],
error: false,
},
];
it.each(testData)("$test.description", test => {
const result = validator.validatePullRequest(test.pullRequest, test.commits);
if (test.error) {
expect(result.errors.length).toBe(1);
expect(result.errors[0].message.text).toBe(
"A Pull Request MUST correlate with a Semantic Versioning identifier (`MAJOR`, `MINOR`, or `PATCH`) with the same or higher precedence than its associated commits"
);
} else {
expect(result.errors.length).toBe(0);
}
});
});
describe("Ignore fixup and merge commits", () => {
const testData = [
"fixup! feat: add new feature",
"fixup! fixup! feat: add new feature",
"Merge pull request #123 from some-branch/feature/branch",
"Merge pull request #123 from 'some-branch/feature/branch'",
"Merged in ci/some-branch (pull request #123)",
"Merged in 'ci/some-branch' (pull request #123)",
"Merge branch 'ci/some-branch' into 'main'",
"Merge branch 'ci/some-branch' into main",
"Merge branch ci/some-branch into main",
];
it.each(testData)("$test", test => {
const result = validator.validateCommits([Commit.fromString({ hash: "0a0b0c0d", message: test })]);
expect(result.length).toBe(0);
});
});