Skip to content

Commit eb78943

Browse files
committed
test: add basic tests for preReceive
1 parent d2dc9a9 commit eb78943

File tree

6 files changed

+80
-2
lines changed

6 files changed

+80
-2
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
"eslint-plugin-react": "^7.21.5",
9292
"eslint-plugin-standard": "^5.0.0",
9393
"husky": "^9.0.0",
94-
"mocha": "^10.2.0",
94+
"mocha": "^10.8.2",
9595
"nyc": "^17.0.0",
9696
"prettier": "^3.0.0",
9797
"sinon": "^19.0.2",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Mock repository.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
while read oldrev newrev refname; do
3+
echo "Push allowed to $refname"
4+
done
5+
exit 0
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
while read oldrev newrev refname; do
3+
echo "Push rejected to $refname"
4+
done
5+
exit 1

test/preReceive/preReceive.test.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const { expect } = require('chai');
2+
const sinon = require('sinon');
3+
const fs = require('fs');
4+
const path = require('path');
5+
const { spawnSync } = require('child_process');
6+
const { exec } = require('../../src/proxy/processors/push-action/preReceive');
7+
8+
describe('Pre-Receive Hook Execution', function () {
9+
let action;
10+
let req;
11+
12+
beforeEach(() => {
13+
req = {};
14+
action = {
15+
steps: [],
16+
commitFrom: 'oldCommitHash',
17+
commitTo: 'newCommitHash',
18+
branch: 'feature-branch',
19+
proxyGitPath: 'test/preReceive/mock/repo',
20+
repoName: 'test-repo',
21+
addStep: function (step) {
22+
this.steps.push(step);
23+
},
24+
};
25+
});
26+
27+
afterEach(() => {
28+
sinon.restore();
29+
});
30+
31+
it('should execute hook successfully', async () => {
32+
const path = require('path');
33+
const scriptPath = path.resolve(__dirname, 'pre-receive-hooks/always-allow.sh');
34+
35+
const result = await exec(req, action, scriptPath);
36+
37+
expect(result.steps).to.have.lengthOf(1);
38+
expect(result.steps[0].error).to.be.false;
39+
expect(
40+
result.steps[0].logs.some((log) => log.includes('Pre-receive hook executed successfully')),
41+
).to.be.true;
42+
});
43+
44+
it('should fail when hook file does not exist', async () => {
45+
const path = require('path');
46+
const scriptPath = path.resolve(__dirname, 'pre-receive-hooks/missing-hook.sh');
47+
48+
const result = await exec(req, action, scriptPath);
49+
50+
expect(result.steps).to.have.lengthOf(1);
51+
expect(result.steps[0].error).to.be.true;
52+
expect(
53+
result.steps[0].logs.some((log) => log.includes('Hook execution error: Hook file not found')),
54+
).to.be.true;
55+
});
56+
57+
it('should fail when hook execution returns an error', async () => {
58+
const path = require('path');
59+
const scriptPath = path.resolve(__dirname, 'pre-receive-hooks/always-reject.sh');
60+
61+
const result = await exec(req, action, scriptPath);
62+
63+
expect(result.steps).to.have.lengthOf(1);
64+
expect(result.steps[0].error).to.be.true;
65+
expect(result.steps[0].logs.some((log) => log.includes('Hook execution error:'))).to.be.true;
66+
});
67+
});

0 commit comments

Comments
 (0)