-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathruntime.test.ts
More file actions
100 lines (84 loc) · 2.92 KB
/
Copy pathruntime.test.ts
File metadata and controls
100 lines (84 loc) · 2.92 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
import {
chmodSync,
existsSync,
mkdirSync,
readFileSync,
writeFileSync,
} from 'node:fs';
import path from 'node:path';
import { expect, test } from 'rstack/test';
import { installHooks } from '../../src/setup/install.ts';
import {
runGitHook,
runHook,
withRepository,
writeHook,
writeInit,
} from './helpers.ts';
test('loads user init and project binaries', () => {
withRepository((cwd) => {
const projectDirectory = path.join(cwd, 'frontend');
const binDirectory = path.join(projectDirectory, 'node_modules', '.bin');
mkdirSync(binDirectory, { recursive: true });
writeInit(cwd, 'set -u\nexport RSTACK_INIT=loaded\n');
const command = path.join(binDirectory, 'rstack-hook-command');
writeFileSync(
command,
`#!/usr/bin/env sh
printf 'ran\\n' > project-bin-ran
`,
);
chmodSync(command, 0o755);
writeHook(
cwd,
`printf '%s\\n' "$RSTACK_INIT" > init-ran
rstack-hook-command
`,
);
expect(installHooks({ cwd: projectDirectory }).status).toBe('installed');
expect(runHook(cwd).status).toBe(0);
expect(readFileSync(path.join(projectDirectory, 'init-ran'), 'utf8')).toBe(
'loaded\n',
);
expect(
readFileSync(path.join(projectDirectory, 'project-bin-ran'), 'utf8'),
).toBe('ran\n');
});
});
test('preserves cwd-sensitive hook arguments for a nested project', () => {
withRepository((cwd) => {
const projectDirectory = path.join(cwd, 'frontend');
const messagePath = '.git/COMMIT_EDITMSG';
mkdirSync(projectDirectory);
writeFileSync(path.join(cwd, messagePath), 'commit message\n');
expect(installHooks({ cwd: projectDirectory }).status).toBe('installed');
for (const name of ['applypatch-msg', 'commit-msg', 'prepare-commit-msg']) {
writeFileSync(path.join(cwd, '.rstack', 'hooks', name), 'cat "$1"\n');
expect(runGitHook(cwd, name, [messagePath])).toMatchObject({
status: 0,
stderr: 'commit message\n',
});
}
mkdirSync(path.join(cwd, 'remote.git'));
writeFileSync(
path.join(cwd, '.rstack', 'hooks', 'pre-push'),
'[ -d "$2" ] || [ "$2" = "git@example.com:repo.git" ]\n',
);
for (const remote of ['remote.git', 'git@example.com:repo.git']) {
expect(runGitHook(cwd, 'pre-push', ['origin', remote]).status).toBe(0);
}
});
});
test('skips user hooks when disabled by the environment or init', () => {
withRepository((cwd) => {
writeHook(cwd, 'echo ran >> hook-ran\n');
expect(installHooks({ cwd }).status).toBe('installed');
expect(runHook(cwd, '0').status).toBe(0);
expect(existsSync(path.join(cwd, 'hook-ran'))).toBe(false);
writeInit(cwd, 'set -u\nexport RSTACK_HOOKS=0\n');
expect(runHook(cwd).status).toBe(0);
expect(existsSync(path.join(cwd, 'hook-ran'))).toBe(false);
writeFileSync(path.join(cwd, '.rstack', 'hooks', 'commit-msg'), 'exit 1\n');
expect(runGitHook(cwd, 'commit-msg', []).status).toBe(0);
});
});