Skip to content

Commit ac65ce9

Browse files
committed
fix: correct color marker when output to hook.sh script
1 parent 774e33a commit ac65ce9

4 files changed

Lines changed: 93 additions & 16 deletions

File tree

.changeset/funny-clocks-fry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vnphanquang/githooks": patch
3+
---
4+
5+
escape to correct color marker in `hook.sh`

deno.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@vnphanquang/githooks",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"exports": {
55
".": "./src/mod.ts",
66
"./bin": "./src/bin.ts",
@@ -17,7 +17,7 @@
1717
},
1818
"tasks": {
1919
"init": "deno run --allow-read --allow-write=\".githooks\" --allow-run=\"git\" src/bin.ts init",
20-
"test": "deno test --allow-read --allow-write --allow-run --allow-net --clean --coverage",
20+
"test": "deno test -A --clean --coverage",
2121
"coverage": "deno coverage --html",
2222
"changesets": "deno run -A npm:@changesets/cli",
2323
"ci:test": "deno test -A --clean --coverage=coverage",

src/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ export const GITHOOKS_SCRIPT: string = `\
5858
#!/usr/bin/env sh
5959
[ "$GITHOOKS" = "2" ] && set -x
6060
61-
# skip if GITHOOKS_CURRENT_HOOK not set
6261
hook_script=$(dirname "$(dirname "$0")")/\${GITHOOKS_CURRENT_HOOK:-$(basename "$0")}
6362
6463
# skip if hook has not been setup
6564
[ ! -f "$hook_script" ] && exit 0
6665
67-
[ -z "$GITHOOKS_CURRENT_HOOK" ] && echo -e "\e[33mWarning: unset GITHOOKS_CURRENT_HOOK. Hook may not function properly. Check "$0"\e[0m"
66+
# warn if GITHOOKS_CURRENT_HOOK not set
67+
[ -z "$GITHOOKS_CURRENT_HOOK" ] && echo -e "\\e[33mWarning: unset GITHOOKS_CURRENT_HOOK. Hook may not function properly. Check "${GITHOOKS_UNDERSCORED_DIRNAME}/$(basename "$0")"\\e[0m"
6868
6969
# source the global init script if any
7070
init_script="\${XDG_CONFIG_HOME:-$HOME/.config}/githooks/init"

tests/init.test.ts

Lines changed: 84 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { GIT_HOOKS, GITHOOKS_DIRNAME, GITHOOKS_UNDERSCORED_DIRNAME } from '../sr
88
import { NotGitDirectoryError } from '../src/errors.ts';
99
import { init } from '../src/init.ts';
1010
import { git } from '../src/git.ts';
11+
import { GITHOOKS_SCRIPT_NAME } from '../src/constants.ts';
1112

1213
Deno.test({
1314
name: 'run "init" in non-git directory',
@@ -193,6 +194,23 @@ Deno.test({
193194
},
194195
});
195196

197+
async function expectCommonCommit() {
198+
// write
199+
await Deno.writeTextFile('main.ts', 'console.log("hello")');
200+
201+
// add
202+
const { success } = await git(Deno.cwd(), 'add', 'main.ts').output();
203+
expect(success).toBe(true);
204+
205+
// commit
206+
return await git(
207+
Deno.cwd(),
208+
'commit',
209+
'-m',
210+
'test pre-commit hook',
211+
).output();
212+
}
213+
196214
Deno.test({
197215
name: 'git commit should trigger pre-commit hook',
198216
permissions: {
@@ -207,21 +225,75 @@ Deno.test({
207225
await expectCommonGitInit(Deno.cwd(), true);
208226
await expectCommonInit(Deno.cwd());
209227

210-
await Deno.writeTextFile('main.ts', 'console.log("hello")');
228+
const { success, stderr } = await expectCommonCommit();
229+
expect(new TextDecoder().decode(stderr)).toContain('Checked 1 file');
230+
expect(success).toBe(true);
231+
} finally {
232+
await sbox[Symbol.asyncDispose]();
233+
}
234+
},
235+
});
236+
237+
Deno.test({
238+
name: 'GITHOOKS=0 should skip hook',
239+
permissions: {
240+
read: true,
241+
write: true,
242+
run: true,
243+
env: true,
244+
},
245+
async fn() {
246+
await using sbox = await sandbox();
247+
248+
try {
249+
await expectCommonGitInit(Deno.cwd(), true);
250+
await expectCommonInit(Deno.cwd());
211251

212-
// add
213-
const { success } = await git(Deno.cwd(), 'add', 'main.ts').output();
252+
Deno.env.set('GITHOOKS', '0');
253+
const { success, stderr } = await expectCommonCommit();
254+
expect(new TextDecoder().decode(stderr)).not.toContain('Checked 1 file');
214255
expect(success).toBe(true);
256+
} finally {
257+
await sbox[Symbol.asyncDispose]();
258+
Deno.env.delete('GITHOOKS');
259+
}
260+
},
261+
});
215262

216-
// commit
217-
const { success: commitSuccess, stderr } = await git(
218-
Deno.cwd(),
219-
'commit',
220-
'-m',
221-
'test pre-commit hook',
222-
).output();
223-
expect(new TextDecoder().decode(stderr)).toContain('Checked 1 file');
224-
expect(commitSuccess).toBe(true);
263+
Deno.test({
264+
name: 'unset GITHOOKS_CURRENT_HOOK should warn user',
265+
permissions: {
266+
read: true,
267+
write: true,
268+
run: true,
269+
env: true,
270+
},
271+
async fn() {
272+
await using sbox = await sandbox();
273+
274+
try {
275+
await expectCommonGitInit(Deno.cwd(), true);
276+
await expectCommonInit(Deno.cwd());
277+
278+
await Deno.writeTextFile('main.ts', 'console.log("hello")');
279+
280+
// overwrite pre-commit
281+
const script = `\
282+
#!/usr/bin/env sh
283+
. "$(dirname "$0")/${GITHOOKS_SCRIPT_NAME}" $@
284+
`;
285+
await Deno.writeTextFile(
286+
path.join(Deno.cwd(), GITHOOKS_UNDERSCORED_DIRNAME, 'pre-commit'),
287+
script,
288+
);
289+
290+
const { success, stderr } = await expectCommonCommit();
291+
const err = new TextDecoder().decode(stderr);
292+
expect(err).toContain('Checked 1 file');
293+
expect(err).toContain(
294+
'unset GITHOOKS_CURRENT_HOOK. Hook may not function properly.',
295+
);
296+
expect(success).toBe(true);
225297
} finally {
226298
await sbox[Symbol.asyncDispose]();
227299
}

0 commit comments

Comments
 (0)