forked from toplenboren/simple-git-hooks
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-git-hooks.test.js
558 lines (470 loc) · 19.7 KB
/
simple-git-hooks.test.js
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
const isEqual = require("lodash.isequal");
const simpleGitHooks = require("./simple-git-hooks");
const { version: packageVersion } = require("./package.json");
// Util functions:
describe("Simple Git Hooks tests", () => {
/**
* This section of tests is used to test how simple util functions perform
* If you are adding a new util function, you should create unit test suite (describe) for it
*/
describe('Unit tests', () => {
describe("getProjectRootDirectory", () => {
it("returns correct dir in typical case:", () => {
expect(
simpleGitHooks.getProjectRootDirectoryFromNodeModules(
"var/my-project/node_modules/simple-git-hooks"
)
).toBe("var/my-project");
});
it("returns correct dir when used with windows delimiters:", () => {
expect(
simpleGitHooks.getProjectRootDirectoryFromNodeModules(
"user\\allProjects\\project\\node_modules\\simple-git-hooks"
)
).toBe("user/allProjects/project");
});
it("falls back to undefined when we are not in node_modules:", () => {
expect(
simpleGitHooks.getProjectRootDirectoryFromNodeModules(
"var/my-project/simple-git-hooks"
)
).toBe(undefined);
});
it("return correct dir when installed using pnpm:", () => {
expect(
simpleGitHooks.getProjectRootDirectoryFromNodeModules(
`var/my-project/node_modules/.pnpm/simple-git-hooks@${packageVersion}`
)
).toBe("var/my-project");
expect(
simpleGitHooks.getProjectRootDirectoryFromNodeModules(
`var/my-project/node_modules/.pnpm/simple-git-hooks@${packageVersion}/node_modules/simple-git-hooks`
)
).toBe("var/my-project");
});
it("return correct dir when installed using yarn3 nodeLinker pnpm:", () => {
expect(
simpleGitHooks.getProjectRootDirectoryFromNodeModules(
`var/my-project/node_modules/.store/simple-git-hooks@${packageVersion}/node_modules/simple-git-hooks`
)
).toBe("var/my-project");
});
});
describe("getGitProjectRoot", () => {
const gitProjectRoot = path.normalize(path.join(__dirname, ".git"));
const currentPath = path.normalize(path.join(__dirname));
const currentFilePath = path.normalize(path.join(__filename));
it("works from .git directory itself", () => {
expect(simpleGitHooks.getGitProjectRoot(gitProjectRoot)).toBe(gitProjectRoot);
});
it("works from any directory", () => {
expect(simpleGitHooks.getGitProjectRoot(currentPath)).toBe(gitProjectRoot);
});
it("works from any file", () => {
expect(simpleGitHooks.getGitProjectRoot(currentFilePath)).toBe(gitProjectRoot);
});
});
describe("checkSimpleGitHooksInDependencies", () => {
const PROJECT_WITH_SIMPLE_GIT_HOOKS_IN_DEPS = path.normalize(
path.join(process.cwd(), "_tests", "project_with_simple_git_hooks_in_deps")
);
const PROJECT_WITH_SIMPLE_GIT_HOOKS_IN_DEV_DEPS = path.normalize(
path.join(
process.cwd(), "_tests", "project_with_simple_git_hooks_in_dev_deps"
)
);
const PROJECT_WITHOUT_SIMPLE_GIT_HOOKS = path.normalize(
path.join(process.cwd(), "_tests", "project_without_simple_git_hooks")
);
it("returns true if simple-git-hooks really in deps", () => {
expect(
simpleGitHooks.checkSimpleGitHooksInDependencies(PROJECT_WITH_SIMPLE_GIT_HOOKS_IN_DEPS)
).toBe(true);
});
it("returns true if simple-git-hooks really in devDeps", () => {
expect(
simpleGitHooks.checkSimpleGitHooksInDependencies(PROJECT_WITH_SIMPLE_GIT_HOOKS_IN_DEV_DEPS)
).toBe(true);
});
it("returns false if simple-git-hooks isn`t in deps", () => {
expect(
simpleGitHooks.checkSimpleGitHooksInDependencies(PROJECT_WITHOUT_SIMPLE_GIT_HOOKS)
).toBe(false);
});
});
});
/**
* This section of tests should test end 2 end use scenarios.
* If you are adding a new feature, you should create an e2e test suite (describe) for it
*/
describe('E2E tests', () => {
const TEST_SCRIPT = `${simpleGitHooks.PREPEND_SCRIPT}exit 1`;
const COMMON_GIT_HOOKS = { "pre-commit": TEST_SCRIPT, "pre-push": TEST_SCRIPT };
// To test this package, we often need to create and manage files.
// Best to use real file system and real files under _tests folder
const testsFolder = path.normalize(path.join(process.cwd(), "_tests"));
// Configuration in Package.json
const PROJECT_WITH_CONF_IN_PACKAGE_JSON = path.normalize(
path.join(testsFolder, "project_with_configuration_in_package_json")
);
// Configuration in .js file
const PROJECT_WITH_CONF_IN_SEPARATE_JS = path.normalize(
path.join(testsFolder, "project_with_configuration_in_separate_js")
);
const PROJECT_WITH_CONF_IN_SEPARATE_JS_ALT = path.normalize(
path.join(testsFolder, "project_with_configuration_in_alternative_separate_js")
);
// Configuration in .cjs file
const PROJECT_WITH_CONF_IN_SEPARATE_CJS = path.normalize(
path.join(testsFolder, "project_with_configuration_in_separate_cjs")
);
const PROJECT_WITH_CONF_IN_SEPARATE_CJS_ALT = path.normalize(
path.join(testsFolder, "project_with_configuration_in_alternative_separate_cjs")
);
// Configuration in .json file
const PROJECT_WITH_CONF_IN_SEPARATE_JSON = path.normalize(
path.join(testsFolder, "project_with_configuration_in_separate_json")
);
const PROJECT_WITH_CONF_IN_SEPARATE_JSON_ALT = path.normalize(
path.join(testsFolder, "project_with_configuration_in_alternative_separate_json")
);
// Other correct configurations
const PROJECT_WITH_UNUSED_CONF_IN_PACKAGE_JSON = path.normalize(
path.join(testsFolder, "project_with_unused_configuration_in_package_json")
);
const PROJECT_WITH_CUSTOM_CONF = path.normalize(
path.join(testsFolder, "project_with_custom_configuration")
);
// Incorrect configurations
const PROJECT_WITH_BAD_CONF_IN_PACKAGE_JSON_ = path.normalize(
path.join(testsFolder, "project_with_incorrect_configuration_in_package_json")
);
const PROJECT_WO_CONF = path.normalize(
path.join(testsFolder, "project_without_configuration")
);
/**
* Creates .git/hooks dir from root
* @param {string} root
*/
function createGitHooksFolder(root) {
if (fs.existsSync(root + "/.git")) {
return;
}
fs.mkdirSync(root + "/.git");
fs.mkdirSync(root + "/.git/hooks");
}
/**
* Removes .git directory from root
* @param {string} root
*/
function removeGitHooksFolder(root) {
if (fs.existsSync(root + "/.git")) {
fs.rmdirSync(root + "/.git", { recursive: true });
}
}
/**
* Returns all installed git hooks
* @return { {string: string} }
*/
function getInstalledGitHooks(hooksDir) {
const result = {};
const hooks = fs.readdirSync(hooksDir);
for (let hook of hooks) {
result[hook] = fs
.readFileSync(path.normalize(path.join(hooksDir, hook)))
.toString();
}
return result;
}
describe('Configuration tests', function () {
describe("Valid configurations", () => {
it("creates git hooks if configuration is correct from .simple-git-hooks.js", () => {
createGitHooksFolder(PROJECT_WITH_CONF_IN_SEPARATE_JS_ALT);
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CONF_IN_SEPARATE_JS_ALT);
const installedHooks = getInstalledGitHooks(
path.normalize(
path.join(
PROJECT_WITH_CONF_IN_SEPARATE_JS_ALT,
".git",
"hooks"
)
)
);
expect(isEqual(installedHooks, COMMON_GIT_HOOKS)).toBe(true);
});
it("creates git hooks if configuration is correct from .simple-git-hooks.cjs", () => {
createGitHooksFolder(PROJECT_WITH_CONF_IN_SEPARATE_CJS_ALT);
simpleGitHooks.setHooksFromConfig(
PROJECT_WITH_CONF_IN_SEPARATE_CJS_ALT
);
const installedHooks = getInstalledGitHooks(
path.normalize(
path.join(
PROJECT_WITH_CONF_IN_SEPARATE_CJS_ALT,
".git",
"hooks"
)
)
);
expect(isEqual(installedHooks, COMMON_GIT_HOOKS)).toBe(true);
});
it("creates git hooks if configuration is correct from simple-git-hooks.cjs", () => {
createGitHooksFolder(PROJECT_WITH_CONF_IN_SEPARATE_CJS);
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CONF_IN_SEPARATE_CJS);
const installedHooks = getInstalledGitHooks(
path.normalize(
path.join(PROJECT_WITH_CONF_IN_SEPARATE_CJS, ".git", "hooks")
)
);
expect(isEqual(installedHooks, COMMON_GIT_HOOKS)).toBe(true);
});
it("creates git hooks if configuration is correct from simple-git-hooks.js", () => {
createGitHooksFolder(PROJECT_WITH_CONF_IN_SEPARATE_JS);
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CONF_IN_SEPARATE_JS);
const installedHooks = getInstalledGitHooks(
path.normalize(
path.join(PROJECT_WITH_CONF_IN_SEPARATE_JS, ".git", "hooks")
)
);
expect(isEqual(installedHooks, COMMON_GIT_HOOKS)).toBe(true);
});
it("creates git hooks if configuration is correct from .simple-git-hooks.json", () => {
createGitHooksFolder(PROJECT_WITH_CONF_IN_SEPARATE_JSON_ALT);
simpleGitHooks.setHooksFromConfig(
PROJECT_WITH_CONF_IN_SEPARATE_JSON_ALT
);
const installedHooks = getInstalledGitHooks(
path.normalize(
path.join(
PROJECT_WITH_CONF_IN_SEPARATE_JSON_ALT,
".git",
"hooks"
)
)
);
expect(isEqual(installedHooks, COMMON_GIT_HOOKS)).toBe(true);
});
it("creates git hooks if configuration is correct from simple-git-hooks.json", () => {
createGitHooksFolder(PROJECT_WITH_CONF_IN_SEPARATE_JSON);
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CONF_IN_SEPARATE_JSON);
const installedHooks = getInstalledGitHooks(
path.normalize(
path.join(PROJECT_WITH_CONF_IN_SEPARATE_JSON, ".git", "hooks")
)
);
expect(isEqual(installedHooks, COMMON_GIT_HOOKS)).toBe(true);
});
it("creates git hooks if configuration is correct from package.json", () => {
createGitHooksFolder(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
const installedHooks = getInstalledGitHooks(
path.normalize(
path.join(PROJECT_WITH_CONF_IN_PACKAGE_JSON, ".git", "hooks")
)
);
expect(isEqual(installedHooks, { "pre-commit": TEST_SCRIPT })).toBe(true);
});
});
describe("Invalid configurations", () => {
it("fails to create git hooks if configuration contains bad git hooks", () => {
createGitHooksFolder(PROJECT_WITH_BAD_CONF_IN_PACKAGE_JSON_);
expect(() =>
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_BAD_CONF_IN_PACKAGE_JSON_)
).toThrow(
"[ERROR] Config was not in correct format. Please check git hooks or options name"
);
});
it("fails to create git hooks if not configured", () => {
createGitHooksFolder(PROJECT_WO_CONF);
expect(() => simpleGitHooks.setHooksFromConfig(PROJECT_WO_CONF)).toThrow(
"[ERROR] Config was not found! Please add `.simple-git-hooks.js` or `simple-git-hooks.js` or `.simple-git-hooks.json` or `simple-git-hooks.json` or `simple-git-hooks` entry in package.json."
);
});
});
});
describe("Remove hooks tests", () => {
it("removes git hooks", () => {
createGitHooksFolder(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
let installedHooks = getInstalledGitHooks(
path.normalize(
path.join(PROJECT_WITH_CONF_IN_PACKAGE_JSON, ".git", "hooks")
)
);
expect(isEqual(installedHooks, { "pre-commit": TEST_SCRIPT })).toBe(true);
simpleGitHooks.removeHooks(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
installedHooks = getInstalledGitHooks(
path.normalize(
path.join(PROJECT_WITH_CONF_IN_PACKAGE_JSON, ".git", "hooks")
)
);
expect(isEqual(installedHooks, {})).toBe(true);
});
it("creates git hooks and removes unused git hooks", () => {
createGitHooksFolder(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
const installedHooksDir = path.normalize(
path.join(PROJECT_WITH_CONF_IN_PACKAGE_JSON, ".git", "hooks")
);
fs.writeFileSync(
path.resolve(installedHooksDir, "pre-push"),
"# do nothing"
);
let installedHooks = getInstalledGitHooks(installedHooksDir);
expect(isEqual(installedHooks, { "pre-push": "# do nothing" })).toBe(true);
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
installedHooks = getInstalledGitHooks(installedHooksDir);
expect(isEqual(installedHooks, { "pre-commit": TEST_SCRIPT })).toBe(true);
});
it("creates git hooks and removes unused but preserves specific git hooks", () => {
createGitHooksFolder(PROJECT_WITH_UNUSED_CONF_IN_PACKAGE_JSON);
const installedHooksDir = path.normalize(
path.join(
PROJECT_WITH_UNUSED_CONF_IN_PACKAGE_JSON,
".git",
"hooks"
)
);
fs.writeFileSync(
path.resolve(installedHooksDir, "commit-msg"),
"# do nothing"
);
fs.writeFileSync(
path.resolve(installedHooksDir, "pre-push"),
"# do nothing"
);
let installedHooks = getInstalledGitHooks(installedHooksDir);
expect(
isEqual(installedHooks, {
"commit-msg": "# do nothing",
"pre-push": "# do nothing",
})
).toBe(true);
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_UNUSED_CONF_IN_PACKAGE_JSON);
installedHooks = getInstalledGitHooks(installedHooksDir);
expect(
isEqual(installedHooks, {
"commit-msg": "# do nothing",
"pre-commit": TEST_SCRIPT,
})
).toBe(true);
});
});
describe("CLI tests", () => {
const testCases = [
["npx", "simple-git-hooks", "./git-hooks.js"],
["node", require.resolve(`./cli`), "./git-hooks.js"],
[
"node",
require.resolve(`./cli`),
require.resolve(`${PROJECT_WITH_CUSTOM_CONF}/git-hooks.js`),
],
];
testCases.forEach((args) => {
it(`creates git hooks and removes unused but preserves specific git hooks for command: ${args.join(
" "
)}`, () => {
createGitHooksFolder(PROJECT_WITH_CUSTOM_CONF);
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CUSTOM_CONF, args);
const installedHooks = getInstalledGitHooks(
path.normalize(
path.join(PROJECT_WITH_CUSTOM_CONF, ".git", "hooks")
)
);
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify(COMMON_GIT_HOOKS));
expect(isEqual(installedHooks, COMMON_GIT_HOOKS)).toBe(true);
});
});
});
describe("ENV vars features tests", () => {
const GIT_USER_NAME = "github-actions";
const GIT_USER_EMAIL = "[email protected]";
const initializeGitRepository = (path) => {
execSync(
`git init \
&& git config user.name ${GIT_USER_NAME} \
&& git config user.email ${GIT_USER_EMAIL}`,
{ cwd: path }
);
};
const tryToPerformTestCommit = (path, env = process.env) => {
try {
execSync(
'git add . && git commit --allow-empty -m "Test commit" && git commit --allow-empty -am "Change commit msg"',
{ cwd: path, env: env, }
);
return true;
} catch (e) {
return false;
}
};
const expectCommitToSucceed = (path) => {
expect(tryToPerformTestCommit(path)).toBe(true);
};
const expectCommitToFail = (path) => {
expect(tryToPerformTestCommit(path)).toBe(false);
};
beforeEach(() => {
initializeGitRepository(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
createGitHooksFolder(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
});
describe("SKIP_SIMPLE_GIT_HOOKS", () => {
afterEach(() => {
delete process.env.SKIP_SIMPLE_GIT_HOOKS;
});
it('commits successfully when SKIP_SIMPLE_GIT_HOOKS is set to "1"', () => {
process.env.SKIP_SIMPLE_GIT_HOOKS = "1";
expectCommitToSucceed(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
});
it("fails to commit when SKIP_SIMPLE_GIT_HOOKS is not set", () => {
expectCommitToFail(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
});
it('fails to commit when SKIP_SIMPLE_GIT_HOOKS is set to "0"', () => {
process.env.SKIP_SIMPLE_GIT_HOOKS = "0";
expectCommitToFail(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
});
it("fails to commit when SKIP_SIMPLE_GIT_HOOKS is set to a random string", () => {
process.env.SKIP_SIMPLE_GIT_HOOKS = "simple-git-hooks";
expectCommitToFail(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
});
})
describe("SIMPLE_GIT_HOOKS_RC", () => {
afterEach(() => {
delete process.env.SIMPLE_GIT_HOOKS_RC;
});
it("fails to commit when SIMPLE_GIT_HOOKS_RC is not set", () => {
expectCommitToFail(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
});
it('commits successfully when SIMPLE_GIT_HOOKS_RC points to initrc_that_prevents_hook_fail.sh', () => {
process.env.SIMPLE_GIT_HOOKS_RC = path.join(PROJECT_WITH_CONF_IN_PACKAGE_JSON, "initrc_that_prevents_hook_fail.sh");
expectCommitToSucceed(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
});
it('fails to commit when SIMPLE_GIT_HOOKS_RC points to initrc_that_does_nothing.sh', () => {
process.env.SIMPLE_GIT_HOOKS_RC = path.join(PROJECT_WITH_CONF_IN_PACKAGE_JSON, "initrc_that_does_nothing.sh");
expectCommitToFail(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
});
})
});
afterEach(() => {
[
PROJECT_WITH_CONF_IN_SEPARATE_JS_ALT,
PROJECT_WITH_CONF_IN_SEPARATE_CJS_ALT,
PROJECT_WITH_CONF_IN_SEPARATE_CJS,
PROJECT_WITH_CONF_IN_SEPARATE_JS,
PROJECT_WITH_CONF_IN_SEPARATE_JSON_ALT,
PROJECT_WITH_CONF_IN_SEPARATE_JSON,
PROJECT_WITH_CONF_IN_PACKAGE_JSON,
PROJECT_WITH_BAD_CONF_IN_PACKAGE_JSON_,
PROJECT_WO_CONF,
PROJECT_WITH_CONF_IN_PACKAGE_JSON,
PROJECT_WITH_UNUSED_CONF_IN_PACKAGE_JSON,
PROJECT_WITH_CUSTOM_CONF,
].forEach((testCase) => {
removeGitHooksFolder(testCase);
});
});
})
})