forked from rstackjs/rstack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks.test.ts
More file actions
117 lines (101 loc) · 3.79 KB
/
Copy pathhooks.test.ts
File metadata and controls
117 lines (101 loc) · 3.79 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
import { spawnSync } from 'node:child_process';
import { mkdirSync, symlinkSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import { expect, test } from 'rstack/test';
import { createHookFiles } from '../../src/setup/hooks.ts';
import { withDirectory } from './helpers.ts';
test('generates the runner and all client-side Git hook shims', () => {
expect(Object.keys(createHookFiles()).filter((name) => name !== 'runner')).toEqual([
'pre-commit',
'pre-merge-commit',
'prepare-commit-msg',
'commit-msg',
'post-commit',
'applypatch-msg',
'pre-applypatch',
'post-applypatch',
'pre-rebase',
'post-rewrite',
'post-checkout',
'post-merge',
'pre-push',
'pre-auto-gc',
]);
});
test.runIf(process.platform === 'win32')('converts Windows Node paths', () => {
const { runner } = createHookFiles(String.raw`C:\Program Files\nodejs\node.exe`);
expect(runner).toContain("rs_node_fallback='/c/Program Files/nodejs/node.exe'");
});
test.runIf(process.platform !== 'win32')('preserves backslashes in POSIX Node paths', () => {
const nodeExecutable = String.raw`/opt/node\24/bin/node`;
const { runner } = createHookFiles(nodeExecutable);
expect(runner).toContain(`rs_node_fallback='${nodeExecutable}'`);
});
test.runIf(process.platform !== 'win32')('runs generated hooks', () => {
withDirectory((directory) => {
const hooksDirectory = path.join(directory, "hooks with ' quotes");
const generatedDirectory = path.join(hooksDirectory, '_');
const generatedHook = path.join(generatedDirectory, 'pre-commit');
const userHook = path.join(hooksDirectory, 'pre-commit');
const fallbackNode = path.join(hooksDirectory, 'node');
const configDirectory = path.join(directory, 'runtime config');
const runtimeDirectory = path.join(configDirectory, 'rstack');
const init = path.join(runtimeDirectory, 'hooks-init.sh');
const files = createHookFiles(fallbackNode);
const env: NodeJS.ProcessEnv = {
...process.env,
XDG_CONFIG_HOME: configDirectory,
};
mkdirSync(generatedDirectory, { recursive: true });
writeFileSync(path.join(generatedDirectory, '.owner'), '.\n');
writeFileSync(path.join(generatedDirectory, 'runner'), files.runner);
writeFileSync(generatedHook, files['pre-commit']);
expect(spawnSync('sh', [generatedHook], { cwd: directory, env }).status).toBe(0);
writeFileSync(
userHook,
`read -r input
printf '%s\\n' "$1|$input"
`,
);
const result = spawnSync('sh', [generatedHook, 'argument with spaces'], {
cwd: directory,
encoding: 'utf8',
env,
input: 'standard input\n',
});
expect(result.status).toBe(0);
expect(result.stdout).toBe('argument with spaces|standard input\n');
writeFileSync(
userHook,
`false
printf 'unreachable\\n'
`,
);
const errexitResult = spawnSync('sh', [generatedHook], {
cwd: directory,
encoding: 'utf8',
env,
});
expect(errexitResult.status).toBe(1);
expect(errexitResult.stdout).toBe('Rstack - pre-commit hook failed (code 1)\n');
mkdirSync(runtimeDirectory, { recursive: true });
writeFileSync(init, `export PATH="${runtimeDirectory}"\n`);
writeFileSync(userHook, 'command -v node\n');
symlinkSync('/bin/sh', path.join(runtimeDirectory, 'sh'));
symlinkSync('/bin/sh', fallbackNode);
const fallbackResult = spawnSync('sh', [generatedHook], {
cwd: directory,
encoding: 'utf8',
env,
});
expect(fallbackResult.stdout).toBe(`${fallbackNode}\n`);
const activeNode = path.join(runtimeDirectory, 'node');
symlinkSync('/bin/sh', activeNode);
const activeResult = spawnSync('sh', [generatedHook], {
cwd: directory,
encoding: 'utf8',
env,
});
expect(activeResult.stdout).toBe(`${activeNode}\n`);
});
});