|
| 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('writePack', () => { |
| 10 | + let exec; |
| 11 | + let spawnSyncStub; |
| 12 | + let stepLogSpy; |
| 13 | + let stepSetContentSpy; |
| 14 | + let stepSetErrorSpy; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + spawnSyncStub = sinon.stub().returns({ |
| 18 | + stdout: 'git receive-pack output', |
| 19 | + stderr: '', |
| 20 | + status: 0 |
| 21 | + }); |
| 22 | + |
| 23 | + stepLogSpy = sinon.spy(Step.prototype, 'log'); |
| 24 | + stepSetContentSpy = sinon.spy(Step.prototype, 'setContent'); |
| 25 | + stepSetErrorSpy = sinon.spy(Step.prototype, 'setError'); |
| 26 | + |
| 27 | + const writePack = proxyquire('../../src/proxy/processors/push-action/writePack', { |
| 28 | + 'child_process': { spawnSync: spawnSyncStub } |
| 29 | + }); |
| 30 | + |
| 31 | + exec = writePack.exec; |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(() => { |
| 35 | + sinon.restore(); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('exec', () => { |
| 39 | + let action; |
| 40 | + let req; |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + req = { |
| 44 | + body: 'pack data' |
| 45 | + }; |
| 46 | + action = new Action( |
| 47 | + '1234567890', |
| 48 | + 'push', |
| 49 | + 'POST', |
| 50 | + 1234567890, |
| 51 | + 'test/repo' |
| 52 | + ); |
| 53 | + action.proxyGitPath = '/path/to/repo'; |
| 54 | + }); |
| 55 | + |
| 56 | + it('should execute git receive-pack with correct parameters', async () => { |
| 57 | + const result = await exec(req, action); |
| 58 | + |
| 59 | + expect(spawnSyncStub.calledOnce).to.be.true; |
| 60 | + expect(spawnSyncStub.firstCall.args[0]).to.equal('git'); |
| 61 | + expect(spawnSyncStub.firstCall.args[1]).to.deep.equal(['receive-pack', 'repo']); |
| 62 | + expect(spawnSyncStub.firstCall.args[2]).to.deep.equal({ |
| 63 | + cwd: '/path/to/repo', |
| 64 | + input: 'pack data', |
| 65 | + encoding: 'utf-8' |
| 66 | + }); |
| 67 | + |
| 68 | + expect(stepLogSpy.calledWith('executing git receive-pack repo')).to.be.true; |
| 69 | + expect(stepLogSpy.calledWith('git receive-pack output')).to.be.true; |
| 70 | + |
| 71 | + expect(stepSetContentSpy.calledWith('git receive-pack output')).to.be.true; |
| 72 | + |
| 73 | + expect(result.steps).to.have.lengthOf(1); |
| 74 | + expect(result.steps[0].error).to.be.false; |
| 75 | + }); |
| 76 | + |
| 77 | + it('should handle errors from git receive-pack', async () => { |
| 78 | + const error = new Error('git error'); |
| 79 | + spawnSyncStub.throws(error); |
| 80 | + |
| 81 | + try { |
| 82 | + await exec(req, action); |
| 83 | + throw new Error('Expected error to be thrown'); |
| 84 | + } catch (e) { |
| 85 | + expect(stepSetErrorSpy.calledOnce).to.be.true; |
| 86 | + expect(stepSetErrorSpy.firstCall.args[0]).to.include('git error'); |
| 87 | + |
| 88 | + expect(action.steps).to.have.lengthOf(1); |
| 89 | + expect(action.steps[0].error).to.be.true; |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + it('should always add the step to the action even if error occurs', async () => { |
| 94 | + spawnSyncStub.throws(new Error('git error')); |
| 95 | + |
| 96 | + try { |
| 97 | + await exec(req, action); |
| 98 | + } catch (e) { |
| 99 | + expect(action.steps).to.have.lengthOf(1); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + it('should have the correct displayName', () => { |
| 104 | + expect(exec.displayName).to.equal('writePack.exec'); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments