Skip to content

Commit c66ec4b

Browse files
committed
test(setup): simplify hook coverage
1 parent 43d6be6 commit c66ec4b

5 files changed

Lines changed: 11 additions & 147 deletions

File tree

‎packages/rstack/tests/cli/setup/index.test.ts‎

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,11 @@ test('displays setup help', ({ execCli, expect }) => {
5151
expect(execCli('setup -h', { cwd })).toBe(output);
5252
expect(output).toContain('Usage:\n $ rs setup [options]');
5353
expect(output).toContain('--hooks-dir <path>');
54-
expect(output).not.toContain('--force');
5554
expect(output).toContain('-h, --help');
5655
});
5756

5857
test('rejects unknown setup options', ({ execCli, expect }) => {
5958
expect(() => execCli('setup --unknown', { cwd })).toThrow();
60-
expect(() => execCli('setup --force', { cwd })).toThrow();
6159
});
6260

6361
test('reports missing and repeated hooks directory options', ({ expect }) => {
@@ -98,29 +96,18 @@ test('installs hooks silently without loading Rstack config', ({ execCli, expect
9896
expect(execCli('setup', { cwd, env })).toBe('');
9997
});
10098

101-
test('installs a root-relative custom hooks directory from a nested project', ({
102-
execCli,
103-
expect,
104-
}) => {
105-
initRepository();
106-
const projectDirectory = path.join(cwd, 'frontend');
107-
mkdirSync(projectDirectory);
108-
109-
expect(execCli('setup --hooks-dir "custom hooks"', { cwd: projectDirectory, env })).toBe('');
110-
expect(git(['config', '--local', '--get', 'core.hooksPath'])).toBe('custom hooks/_');
111-
expect(existsSync(path.join(cwd, 'custom hooks', '_', 'runner'))).toBe(true);
112-
expect(existsSync(path.join(projectDirectory, 'custom hooks'))).toBe(false);
113-
});
114-
115-
test('reports a different project owner without replacing it', ({ execCli, expect }) => {
99+
test('installs root-relative hooks and reports owner conflicts', ({ execCli, expect }) => {
116100
initRepository();
117101
const frontend = path.join(cwd, 'frontend');
118102
const docs = path.join(cwd, 'docs');
119103
mkdirSync(frontend);
120104
mkdirSync(docs);
121105

122-
expect(execCli('setup', { cwd: frontend, env })).toBe('');
123-
const conflict = runSetup([], docs);
106+
expect(execCli('setup --hooks-dir "custom hooks"', { cwd: frontend, env })).toBe('');
107+
expect(git(['config', '--local', '--get', 'core.hooksPath'])).toBe('custom hooks/_');
108+
expect(existsSync(path.join(cwd, 'custom hooks', '_', 'runner'))).toBe(true);
109+
110+
const conflict = runSetup(['--hooks-dir', 'custom hooks'], docs);
124111
expect(conflict.status).toBe(0);
125112
expect(`${conflict.stdout}${conflict.stderr}`).toContain(
126113
'Git hooks are already managed by Rstack project "frontend"',

‎packages/rstack/tests/setup/directories.test.ts‎

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ test('installs repository-level hooks from a nested project', () => {
3838
});
3939
expect(runGit(cwd, ['config', '--local', '--get', 'core.hooksPath'])).toBe(hooksPath);
4040
expect(existsSync(path.join(cwd, hooksPath, 'runner'))).toBe(true);
41-
expect(existsSync(path.join(projectDirectory, '.rstack'))).toBe(false);
4241
expect(readFileSync(path.join(cwd, hooksPath, '.owner'), 'utf8')).toBe('frontend\n');
4342

4443
expect(runHook(cwd).status).toBe(0);
@@ -50,7 +49,6 @@ test('installs a root-relative custom hooks directory from a nested project', ()
5049
withRepository((cwd) => {
5150
const projectDirectory = path.join(cwd, 'frontend app');
5251
mkdirSync(projectDirectory);
53-
writeHook(cwd, "printf 'ran\\n' > custom-hook-ran\n", 'config/hooks');
5452

5553
expect(installHooks({ cwd: projectDirectory, hooksDir: 'config\\hooks' })).toEqual({
5654
status: 'installed',
@@ -62,60 +60,5 @@ test('installs a root-relative custom hooks directory from a nested project', ()
6260
});
6361
expect(runGit(cwd, ['config', '--local', '--get', 'core.hooksPath'])).toBe('config/hooks/_');
6462
expect(existsSync(path.join(cwd, 'config', 'hooks', '_', 'runner'))).toBe(true);
65-
expect(existsSync(path.join(projectDirectory, 'config'))).toBe(false);
66-
expect(runHook(cwd).status).toBe(0);
67-
expect(readFileSync(path.join(projectDirectory, 'custom-hook-ran'), 'utf8')).toBe('ran\n');
68-
});
69-
});
70-
71-
test('does not replace another Rstack project owner', () => {
72-
withRepository((cwd) => {
73-
const frontend = path.join(cwd, 'frontend');
74-
const docs = path.join(cwd, 'docs');
75-
mkdirSync(frontend);
76-
mkdirSync(docs);
77-
writeHook(cwd, 'printf \'%s\\n\' "$PWD" > hook-cwd\n');
78-
79-
expect(installHooks({ cwd: frontend }).status).toBe('installed');
80-
expect(installHooks({ cwd: docs })).toEqual({
81-
status: 'skipped',
82-
reason: 'owned-by-another-project',
83-
message: 'Git hooks are already managed by Rstack project "frontend"',
84-
});
85-
expect(readFileSync(path.join(cwd, hooksPath, '.owner'), 'utf8')).toBe('frontend\n');
86-
87-
expect(runHook(cwd).status).toBe(0);
88-
expect(readFileSync(path.join(frontend, 'hook-cwd'), 'utf8')).toBe(`${frontend}\n`);
89-
expect(existsSync(path.join(docs, 'hook-cwd'))).toBe(false);
90-
});
91-
});
92-
93-
test('installs generated hooks relative to the current worktree', () => {
94-
withRepository((cwd) => {
95-
runGit(cwd, [
96-
'-c',
97-
'user.name=Rstack',
98-
'-c',
99-
'user.email=rstack@example.com',
100-
'commit',
101-
'--allow-empty',
102-
'--quiet',
103-
'-m',
104-
'Initial commit',
105-
]);
106-
const worktree = path.join(cwd, 'linked', 'secondary');
107-
mkdirSync(path.dirname(worktree), { recursive: true });
108-
runGit(cwd, ['worktree', 'add', '--quiet', '-b', 'secondary', worktree]);
109-
110-
const projectDirectory = path.join(worktree, 'frontend');
111-
mkdirSync(projectDirectory);
112-
writeHook(worktree, "printf 'ran\\n' > worktree-hook-ran\n");
113-
114-
expect(installHooks({ cwd: projectDirectory }).status).toBe('installed');
115-
expect(existsSync(path.join(worktree, hooksPath, 'runner'))).toBe(true);
116-
expect(existsSync(path.join(cwd, hooksPath, 'runner'))).toBe(false);
117-
118-
expect(runHook(worktree).status).toBe(0);
119-
expect(readFileSync(path.join(projectDirectory, 'worktree-hook-ran'), 'utf8')).toBe('ran\n');
12063
});
12164
});

‎packages/rstack/tests/setup/hooks.test.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ test.runIf(process.platform !== 'win32')('preserves backslashes in POSIX Node pa
4343

4444
test.runIf(process.platform !== 'win32')('runs generated hooks', () => {
4545
withDirectory((directory) => {
46-
const hooksDir = "hooks with ' quotes";
47-
const hooksDirectory = path.join(directory, hooksDir);
46+
const hooksDirectory = path.join(directory, "hooks with ' quotes");
4847
const generatedDirectory = path.join(hooksDirectory, '_');
4948
const generatedHook = path.join(generatedDirectory, 'pre-commit');
5049
const userHook = path.join(hooksDirectory, 'pre-commit');

‎packages/rstack/tests/setup/install.test.ts‎

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -63,37 +63,6 @@ test('repairs generated files without rewriting an unchanged hooksPath', () => {
6363
});
6464
});
6565

66-
test.runIf(process.platform !== 'win32')('repairs files without rewriting the owner', () => {
67-
withRepository((cwd) => {
68-
expect(installHooks({ cwd }).status).toBe('installed');
69-
const directory = path.join(cwd, hooksPath);
70-
const owner = path.join(directory, '.owner');
71-
writeFileSync(path.join(directory, 'runner'), 'stale\n');
72-
chmodSync(owner, 0o444);
73-
74-
try {
75-
expect(installHooks({ cwd })).toEqual({ status: 'installed', hooksPath });
76-
} finally {
77-
chmodSync(owner, 0o644);
78-
}
79-
});
80-
});
81-
82-
test('does not claim an owner file without a trailing newline', () => {
83-
withRepository((cwd) => {
84-
const directory = path.join(cwd, hooksPath);
85-
const owner = path.join(directory, '.owner');
86-
mkdirSync(directory, { recursive: true });
87-
writeFileSync(owner, '.');
88-
89-
expect(installHooks({ cwd })).toMatchObject({
90-
status: 'skipped',
91-
reason: 'hooks-directory-conflict',
92-
});
93-
expect(readFileSync(owner, 'utf8')).toBe('.');
94-
});
95-
});
96-
9766
test('resolves repository context with a single Git process when unchanged', () => {
9867
withRepository((cwd) => {
9968
expect(installHooks({ cwd }).status).toBe('installed');
@@ -181,19 +150,6 @@ test('does not replace another Git hooks path', () => {
181150
});
182151
});
183152

184-
test('does not override a global Git hooks path', () => {
185-
withRepository((cwd) => {
186-
runGit(cwd, ['config', '--global', 'core.hooksPath', 'global-hooks']);
187-
188-
expect(installHooks({ cwd })).toMatchObject({
189-
status: 'skipped',
190-
reason: 'hooks-path-conflict',
191-
});
192-
expect(git(cwd, ['config', '--local', '--get', 'core.hooksPath']).status).toBe(1);
193-
expect(runGit(cwd, ['config', '--global', '--get', 'core.hooksPath'])).toBe('global-hooks');
194-
});
195-
});
196-
197153
test('does not bypass existing Git hooks', () => {
198154
withRepository((cwd) => {
199155
const existingHook = path.join(cwd, '.git', 'hooks', 'pre-commit');

‎packages/rstack/tests/setup/runtime.test.ts‎

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { runHook, withRepository, writeHook, writeInit } from './helpers.ts';
66

77
test('loads user init and project binaries', () => {
88
withRepository((cwd) => {
9-
const binDirectory = path.join(cwd, 'node_modules', '.bin');
9+
const projectDirectory = path.join(cwd, 'frontend');
10+
const binDirectory = path.join(projectDirectory, 'node_modules', '.bin');
1011
mkdirSync(binDirectory, { recursive: true });
1112
writeInit(cwd, 'set -u\nexport RSTACK_INIT=loaded\n');
1213

@@ -26,32 +27,10 @@ rstack-hook-command
2627
`,
2728
);
2829

29-
expect(installHooks({ cwd }).status).toBe('installed');
30-
31-
expect(runHook(cwd).status).toBe(0);
32-
expect(readFileSync(path.join(cwd, 'init-ran'), 'utf8')).toBe('loaded\n');
33-
expect(readFileSync(path.join(cwd, 'project-bin-ran'), 'utf8')).toBe('ran\n');
34-
});
35-
});
36-
37-
test('loads binaries from a nested project while running the root hook', () => {
38-
withRepository((cwd) => {
39-
const projectDirectory = path.join(cwd, 'frontend');
40-
const binDirectory = path.join(projectDirectory, 'node_modules', '.bin');
41-
mkdirSync(binDirectory, { recursive: true });
42-
43-
const command = path.join(binDirectory, 'rstack-hook-command');
44-
writeFileSync(
45-
command,
46-
`#!/usr/bin/env sh
47-
printf 'ran\n' > project-bin-ran
48-
`,
49-
);
50-
chmodSync(command, 0o755);
51-
writeHook(cwd, 'rstack-hook-command\n');
52-
5330
expect(installHooks({ cwd: projectDirectory }).status).toBe('installed');
31+
5432
expect(runHook(cwd).status).toBe(0);
33+
expect(readFileSync(path.join(projectDirectory, 'init-ran'), 'utf8')).toBe('loaded\n');
5534
expect(readFileSync(path.join(projectDirectory, 'project-bin-ran'), 'utf8')).toBe('ran\n');
5635
});
5736
});

0 commit comments

Comments
 (0)