Repository navigation
Expand file tree
/
Copy pathcheck-git-hooks.cli.test.js
More file actions
137 lines (113 loc) · 4.3 KB
/
Copy pathcheck-git-hooks.cli.test.js
File metadata and controls
137 lines (113 loc) · 4.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
/**
* Runs scripts/check-git-hooks.mjs (#3493) against throwaway Git repositories.
*/
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { execFileSync, spawnSync } = require('node:child_process');
const script = path.join(__dirname, '..', 'check-git-hooks.mjs');
function repo(setup) {
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'hooks-check-'));
execFileSync('git', ['init', '-q', directory]);
setup(directory);
return directory;
}
function run(directory, ...args) {
const env = { ...process.env };
delete env.CI;
return spawnSync(process.execPath, [script, ...args], {
cwd: directory,
encoding: 'utf8',
env,
});
}
const git = (directory, ...args) => execFileSync('git', ['-C', directory, ...args]);
function writeHook(directory, hook, mode = 0o755) {
const file = path.join(directory, '.husky', '_', hook);
fs.writeFileSync(file, '');
if (process.platform !== 'win32') fs.chmodSync(file, mode);
}
describe('check-git-hooks CLI', () => {
const directories = [];
afterAll(() => directories.forEach((d) => fs.rmSync(d, { force: true, recursive: true })));
test('warns when core.hooksPath points at a missing directory', () => {
const directory = repo((d) => git(d, 'config', 'core.hooksPath', '.husky/_'));
directories.push(directory);
const result = run(directory);
expect(result.status).toBe(0);
expect(result.stderr).toContain('does not exist');
expect(result.stderr).toContain('npm run prepare');
expect(run(directory, '--strict').status).toBe(1);
});
test('warns when core.hooksPath uses another directory', () => {
const directory = repo((d) => {
fs.mkdirSync(path.join(d, 'custom-hooks'), { recursive: true });
for (const hook of ['pre-commit', 'pre-push']) {
fs.writeFileSync(path.join(d, 'custom-hooks', hook), '');
}
git(d, 'config', 'core.hooksPath', 'custom-hooks');
});
directories.push(directory);
expect(run(directory, '--strict').stderr).toContain('not .husky/_');
});
test('warns when core.hooksPath is unset', () => {
const directory = repo(() => {});
directories.push(directory);
expect(run(directory, '--strict').stderr).toContain('not set');
});
test('names a missing hook', () => {
const directory = repo((d) => {
fs.mkdirSync(path.join(d, '.husky', '_'), { recursive: true });
fs.writeFileSync(path.join(d, '.husky', '_', 'pre-commit'), '');
git(d, 'config', 'core.hooksPath', '.husky/_');
});
directories.push(directory);
expect(run(directory, '--strict').stderr).toContain('pre-push is missing');
});
test('reports a non-executable hook as missing', () => {
if (process.platform === 'win32') return;
const directory = repo((d) => {
fs.mkdirSync(path.join(d, '.husky', '_'), { recursive: true });
writeHook(d, 'pre-commit');
writeHook(d, 'pre-push', 0o644);
git(d, 'config', 'core.hooksPath', '.husky/_');
});
directories.push(directory);
expect(run(directory, '--strict').stderr).toContain('pre-push is missing or not executable');
});
test('passes silently when both hooks are installed', () => {
const directory = repo((d) => {
fs.mkdirSync(path.join(d, '.husky', '_'), { recursive: true });
for (const hook of ['pre-commit', 'pre-push']) {
writeHook(d, hook);
}
git(d, 'config', 'core.hooksPath', '.husky/_');
});
directories.push(directory);
const result = run(directory, '--strict');
expect(result.status).toBe(0);
expect(result.stderr).toBe('');
});
test('is skipped in CI', () => {
const directory = repo(() => {});
directories.push(directory);
const result = spawnSync(process.execPath, [script, '--strict'], {
cwd: directory,
encoding: 'utf8',
env: { ...process.env, CI: 'true' },
});
expect(result.status).toBe(0);
expect(result.stderr).toBe('');
});
test('does not treat CI=false as CI', () => {
const directory = repo(() => {});
directories.push(directory);
const result = spawnSync(process.execPath, [script, '--strict'], {
cwd: directory,
encoding: 'utf8',
env: { ...process.env, CI: 'false' },
});
expect(result.status).toBe(1);
expect(result.stderr).toContain('Git hooks are not installed');
});
});