forked from PaulHatch/semantic-version
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
426 lines (307 loc) · 12.3 KB
/
Copy pathindex.test.js
File metadata and controls
426 lines (307 loc) · 12.3 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
const cp = require('child_process');
const path = require('path');
const process = require('process');
// Action input variables
const defaultInputs = {
branch: "master",
tag_prefix: "v",
major_pattern: "(MAJOR)",
minor_pattern: "(MINOR)",
format: "${major}.${minor}.${patch}"
};
// Creates a randomly named git repository and returns a function to execute commands in it
const createTestRepo = (inputs) => {
const repoDirectory = `/tmp/test${Math.random().toString(36).substring(2, 15)}`;
cp.execSync(`mkdir ${repoDirectory} && git init ${repoDirectory}`);
const run = (command, extraInputs) => {
const allInputs = Object.assign({ ...defaultInputs }, inputs, extraInputs);
let env = {};
for (let key in allInputs) {
env[`INPUT_${key.toUpperCase()}`] = allInputs[key];
}
return execute(repoDirectory, command, env);
}
// Configure up git user
run(`git config user.name "Test User"`);
run(`git config user.email "test@example.com"`);
let i = 1;
return {
clean: () => execute('/tmp', `rm -rf ${repoDirectory}`),
makeCommit: (msg, path) => {
run(`touch ${path !== undefined ? path.trim('/') + '/' : ''}test${i++}`);
run(`git add --all`);
run(`git commit -m '${msg}'`);
},
runAction: (inputs) => run(`node ${path.join(__dirname, 'index.js')}`, inputs),
exec: run
};
};
// Executes a set of commands in the specified directory
const execute = (workingDirectory, command, env) => {
try {
return String(cp.execSync(command, { env: { ...process.env, ...env }, cwd: workingDirectory }));
}
catch (e) {
console.error(String(e.stdout));
console.error(String(e.stderr));
throw e;
}
};
test('Empty repository version is correct', () => {
const repo = createTestRepo(); // 0.0.0+0
var result = repo.runAction();
expect(result).toMatch('Version is 0.0.0+0');
repo.clean();
});
test('Repository with commits shows increment', () => {
const repo = createTestRepo(); // 0.0.0+0
repo.makeCommit('Initial Commit'); // 0.0.1+0
repo.makeCommit(`Second Commit`); // 0.0.1+1
const result = repo.runAction();
expect(result).toMatch('Version is 0.0.1+1');
repo.clean();
});
test('Tagging does not break version', () => {
const repo = createTestRepo(); // 0.0.0+0
repo.makeCommit('Initial Commit'); // 0.0.1+0
repo.makeCommit(`Second Commit`); // 0.0.1+1
repo.exec('git tag v0.0.1')
const result = repo.runAction();
expect(result).toMatch('Version is 0.0.1+1');
repo.clean();
});
test('Minor update bumps minor version and resets increment', () => {
const repo = createTestRepo(); // 0.0.0+0
repo.makeCommit('Initial Commit'); // 0.0.1+0
repo.makeCommit('Second Commit (MINOR)'); // 0.1.0+0
const result = repo.runAction();
expect(result).toMatch('Version is 0.1.0+0');
repo.clean();
});
test('Major update bumps major version and resets increment', () => {
const repo = createTestRepo(); // 0.0.0+0
repo.makeCommit('Initial Commit'); // 0.0.1+0
repo.makeCommit('Second Commit (MAJOR)'); // 1.0.0+0
const result = repo.runAction();
expect(result).toMatch('Version is 1.0.0+0');
repo.clean();
});
test('Multiple major commits are idempotent', () => {
const repo = createTestRepo(); // 0.0.0+0
repo.makeCommit('Initial Commit'); // 0.0.1+0
repo.makeCommit('Second Commit (MAJOR)'); // 1.0.0+0
repo.makeCommit('Third Commit (MAJOR)'); // 1.0.0+1
const result = repo.runAction();
expect(result).toMatch('Version is 1.0.0+1');
repo.clean();
});
test('Minor commits after a major commit are ignored', () => {
const repo = createTestRepo(); // 0.0.0+0
repo.makeCommit('Initial Commit'); // 0.0.1+0
repo.makeCommit('Second Commit (MAJOR)'); // 1.0.0+0
repo.makeCommit('Third Commit (MINOR)'); // 1.0.0+1
const result = repo.runAction();
expect(result).toMatch('Version is 1.0.0+1');
repo.clean();
});
test('Tags start new version', () => {
const repo = createTestRepo(); // 0.0.0+0
repo.makeCommit('Initial Commit'); // 0.0.1+0
repo.makeCommit('Second Commit'); // 0.0.1+1
repo.exec('git tag v0.0.1');
repo.makeCommit('Third Commit'); // 0.0.2+0
const result = repo.runAction();
expect(result).toMatch('Version is 0.0.2+0');
repo.clean();
});
test('Version pulled from last release branch', () => {
const repo = createTestRepo(); // 0.0.0+0
repo.makeCommit('Initial Commit'); // 0.0.1+0
repo.exec('git tag v0.0.1');
repo.makeCommit('Second Commit'); // 0.0.2+0
repo.exec('git tag v5.6.7');
repo.makeCommit('Third Commit'); // 5.6.7+0
const result = repo.runAction();
expect(result).toMatch('Version is 5.6.8+0');
repo.clean();
});
/* Removed for now
test('Tags on branches are used', () => {
// This test checks that tags are counted correctly even if they are not on
// the main branch:
// master o--o--o--o <- expecting v0.0.2
// \
// release o--o <- taged v0.0.1
const repo = createTestRepo(); // 0.0.0+0
repo.makeCommit('Initial Commit'); // 0.0.1+0
repo.makeCommit('Second Commit'); // 0.0.1+1
repo.makeCommit('Third Commit'); // 0.1.1+2
repo.exec('git checkout -b release/0.0.1')
repo.makeCommit('Fourth Commit'); // 0.1.1+3
repo.exec('git tag v0.0.1');
repo.exec('git checkout master');
repo.makeCommit('Fifth Commit'); // 0.0.2.0
const result = repo.runAction();
expect(result).toMatch('Version is 0.0.2+0');
repo.clean();
});
*/
test('Merged tags do not affect version', () => {
// This test checks that merges don't override tags
// Tagged v0.0.2
// v
// master o--o--o---o---o <- expecting v0.0.3+1
// \ /
// release o---o <- taged v0.0.1
const repo = createTestRepo(); // 0.0.0+0
repo.makeCommit('Initial Commit'); // 0.0.1+0
repo.makeCommit('Second Commit'); // 0.0.1+1
repo.makeCommit('Third Commit'); // 0.1.1+2
repo.exec('git checkout -b release/0.0.1')
repo.makeCommit('Fourth Commit'); // 0.1.1+3
repo.exec('git tag v0.0.1');
repo.exec('git checkout master');
repo.makeCommit('Fifth Commit'); // 0.0.2.0
repo.exec('git tag v0.0.2');
repo.exec('git merge release/0.0.1');
const result = repo.runAction();
expect(result).toMatch('Version is 0.0.3+1');
repo.clean();
});
test('Version tags do not require all three version numbers', () => {
const repo = createTestRepo(); // 0.0.0+0
repo.makeCommit('Initial Commit (MAJOR)'); // 1.0.0+0
repo.exec('git tag v1');
repo.makeCommit(`Second Commit`); // 1.0.1+0
const result = repo.runAction();
expect(result).toMatch('Version is 1.0.1+0');
repo.clean();
});
test('Format input is respected', () => {
const repo = createTestRepo({ format: 'M${major}m${minor}p${patch}i${increment}' }); // M0m0p0i0
repo.makeCommit('Initial Commit'); // M1m2p3i0
repo.exec('git tag v1.2.3');
repo.makeCommit(`Second Commit`); // M1m2p4i0
const result = repo.runAction();
expect(result).toMatch('M1m2p4i0');
repo.clean();
});
test('Version prefixes are not required/can be empty', () => {
const repo = createTestRepo({ tag_prefix: '' }); // 0.0.0
repo.makeCommit('Initial Commit'); // 0.0.1
repo.exec('git tag 0.0.1');
repo.makeCommit(`Second Commit`); // 0.0.2
const result = repo.runAction();
expect(result).toMatch('Version is 0.0.2');
repo.clean();
});
test('Tag order comes from commit order, not tag create order', () => {
const repo = createTestRepo(); // 0.0.0+0
repo.makeCommit('Initial Commit'); // 0.0.1+0
repo.makeCommit('Second Commit'); // 0.0.1+1
repo.makeCommit('Third Commit'); // 0.0.1+2
repo.exec('git tag v2.0.0');
repo.exec('sleep 2');
repo.exec('git tag v1.0.0 HEAD~1');
repo.makeCommit('Fourth Commit'); // 0.0.1+2
const result = repo.runAction();
expect(result).toMatch('Version is 2.0.1+0');
repo.clean();
});
test('Change detection is true by default', () => {
const repo = createTestRepo({ tag_prefix: '' }); // 0.0.0
repo.makeCommit('Initial Commit'); // 0.0.1
repo.exec('git tag 0.0.1');
repo.makeCommit(`Second Commit`); // 0.0.2
const result = repo.runAction();
expect(result).toMatch('::set-output name=changed::true');
repo.clean();
});
test('Changes to monitored path is true when change is in path', () => {
const repo = createTestRepo({ tag_prefix: '' }); // 0.0.0
repo.makeCommit('Initial Commit'); // 0.0.1
repo.exec('git tag 0.0.1');
repo.exec('mkdir project1');
repo.makeCommit(`Second Commit`, 'project1'); // 0.0.2
const result = repo.runAction({ change_path: "project1" });
expect(result).toMatch('::set-output name=changed::true');
repo.clean();
});
test('Changes to monitored path is false when changes are not in path', () => {
const repo = createTestRepo({ tag_prefix: '' }); // 0.0.0
repo.makeCommit('Initial Commit'); // 0.0.1
repo.exec('git tag 0.0.1');
repo.exec('mkdir project1');
repo.exec('mkdir project2');
repo.makeCommit(`Second Commit`, 'project2'); // 0.0.2
const result = repo.runAction({ change_path: "project1" });
expect(result).toMatch('::set-output name=changed::false');
repo.clean();
});
test('Changes can be detected without tags', () => {
const repo = createTestRepo({ tag_prefix: '' }); // 0.0.0
repo.makeCommit('Initial Commit'); // 0.0.1
repo.exec('mkdir project1');
repo.makeCommit(`Second Commit`, 'project1'); // 0.0.2
const result = repo.runAction({ change_path: "project1" });
expect(result).toMatch('::set-output name=changed::true');
repo.clean();
});
test('Changes to multiple monitored path is true when change is in path', () => {
const repo = createTestRepo({ tag_prefix: '' }); // 0.0.0
repo.makeCommit('Initial Commit'); // 0.0.1
repo.exec('git tag 0.0.1');
repo.exec('mkdir project1');
repo.exec('mkdir project2');
repo.makeCommit(`Second Commit`, 'project2'); // 0.0.2
const result = repo.runAction({ change_path: "project1 project2" });
expect(result).toMatch('::set-output name=changed::true');
repo.clean();
});
test('Changes to multiple monitored path is false when change is not in path', () => {
const repo = createTestRepo({ tag_prefix: '' }); // 0.0.0
repo.makeCommit('Initial Commit'); // 0.0.1
repo.exec('git tag 0.0.1');
repo.exec('mkdir project1');
repo.exec('mkdir project2');
repo.exec('mkdir project3');
repo.makeCommit(`Second Commit`, 'project3'); // 0.0.2
const result = repo.runAction({ change_path: "project1 project2" });
expect(result).toMatch('::set-output name=changed::false');
repo.clean();
});
test('Namespace is tracked separately', () => {
const repo = createTestRepo({ tag_prefix: '' }); // 0.0.0
repo.makeCommit('Initial Commit'); // 0.0.1
repo.exec('git tag 0.0.1');
repo.makeCommit('Second Commit'); // 0.0.2
repo.exec('git tag 0.1.0-subproject');
repo.makeCommit('Third Commit'); // 0.0.2 / 0.1.1
const result = repo.runAction();
const subprojectResult = repo.runAction({ namespace: "subproject" });
expect(result).toMatch('Version is 0.0.2+1');
expect(subprojectResult).toMatch('Version is 0.1.1+0');
repo.clean();
});
test('Commits outside of path are not counted', () => {
const repo = createTestRepo({ tag_prefix: '' }); // 0.0.0
repo.makeCommit('Initial Commit');
repo.makeCommit('Second Commit');
repo.makeCommit('Third Commit');
const result = repo.runAction({ change_path: "project1" });
expect(result).toMatch('Version is 0.0.1+0');
repo.clean();
});
test('Commits inside path are counted', () => {
const repo = createTestRepo({ tag_prefix: '' }); // 0.0.0
repo.makeCommit('Initial Commit');
repo.makeCommit('Second Commit');
repo.makeCommit('Third Commit');
repo.exec('mkdir project1');
repo.makeCommit('Fourth Commit', 'project1'); // 0.0.1+0
repo.makeCommit('Fifth Commit', 'project1'); // 0.0.1+1
repo.makeCommit('Sixth Commit', 'project1'); // 0.0.1+2
const result = repo.runAction({ change_path: "project1" });
expect(result).toMatch('Version is 0.0.1+2');
repo.clean();
});