Skip to content

Commit e7951c0

Browse files
committed
test: checkEmptyBranch cases
1 parent 381f00f commit e7951c0

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
const chai = require('chai');
2+
const sinon = require('sinon');
3+
const proxyquire = require('proxyquire');
4+
const { Action, Step } = require('../../src/proxy/actions');
5+
6+
chai.should();
7+
const expect = chai.expect;
8+
9+
describe.only('checkEmptyBranch', () => {
10+
let exec;
11+
let simpleGitStub;
12+
let gitRawStub;
13+
14+
beforeEach(() => {
15+
gitRawStub = sinon.stub();
16+
simpleGitStub = sinon.stub().callsFake((workingDir) => {
17+
return {
18+
raw: gitRawStub,
19+
cwd: workingDir
20+
};
21+
});
22+
23+
const checkEmptyBranch = proxyquire('../../src/proxy/processors/push-action/checkEmptyBranch', {
24+
'simple-git': {
25+
default: simpleGitStub,
26+
__esModule: true,
27+
'@global': true,
28+
'@noCallThru': true
29+
},
30+
// deeply mocking fs to prevent simple-git from validating directories (which fails)
31+
'fs': {
32+
existsSync: sinon.stub().returns(true),
33+
lstatSync: sinon.stub().returns({
34+
isDirectory: () => true,
35+
isFile: () => false
36+
}),
37+
'@global': true
38+
}
39+
});
40+
41+
exec = checkEmptyBranch.exec;
42+
});
43+
44+
afterEach(() => {
45+
sinon.restore();
46+
});
47+
48+
describe('exec', () => {
49+
let action;
50+
let req;
51+
52+
beforeEach(() => {
53+
req = {};
54+
action = new Action(
55+
'1234567890',
56+
'push',
57+
'POST',
58+
1234567890,
59+
'test/repo'
60+
);
61+
action.proxyGitPath = '/tmp/gitproxy';
62+
action.repoName = 'test-repo';
63+
action.commitFrom = '0000000000000000000000000000000000000000';
64+
action.commitTo = 'abcdef1234567890abcdef1234567890abcdef12';
65+
action.commitData = [];
66+
});
67+
68+
it('should pass through if commitData is already populated', async () => {
69+
action.commitData = [{ message: 'Existing commit' }];
70+
71+
const result = await exec(req, action);
72+
73+
expect(result.steps).to.have.lengthOf(0);
74+
expect(simpleGitStub.called).to.be.false;
75+
});
76+
77+
it('should block empty branch pushes with a commit that exists', async () => {
78+
gitRawStub.resolves('commit\n');
79+
80+
const result = await exec(req, action);
81+
82+
expect(simpleGitStub.calledWith('/tmp/gitproxy/test-repo')).to.be.true;
83+
expect(gitRawStub.calledWith(['cat-file', '-t', action.commitTo])).to.be.true;
84+
85+
const step = result.steps.find(s => s.stepName === 'checkEmptyBranch');
86+
expect(step).to.exist;
87+
expect(step.error).to.be.true;
88+
expect(step.errorMessage).to.include('Push blocked: Empty branch');
89+
});
90+
91+
it('should block pushes if commitTo does not resolve', async () => {
92+
gitRawStub.rejects(new Error('fatal: Not a valid object name'));
93+
94+
const result = await exec(req, action);
95+
96+
expect(gitRawStub.calledWith(['cat-file', '-t', action.commitTo])).to.be.true;
97+
98+
const step = result.steps.find(s => s.stepName === 'checkEmptyBranch');
99+
expect(step).to.exist;
100+
expect(step.error).to.be.true;
101+
expect(step.errorMessage).to.include('Push blocked: Commit data not found');
102+
});
103+
104+
it('should block non-empty branch pushes with empty commitData', async () => {
105+
action.commitFrom = 'abcdef1234567890abcdef1234567890abcdef12';
106+
107+
const result = await exec(req, action);
108+
109+
expect(simpleGitStub.called).to.be.true;
110+
111+
const step = result.steps.find(s => s.stepName === 'checkEmptyBranch');
112+
expect(step).to.exist;
113+
expect(step.error).to.be.true;
114+
expect(step.errorMessage).to.include('Push blocked: Commit data not found');
115+
});
116+
});
117+
});

0 commit comments

Comments
 (0)