ADD unit tests, split and refactor source code

This commit is contained in:
RecuencoJones
2019-03-25 20:38:29 +01:00
parent 6eaf5fc9af
commit 8257010937
18 changed files with 782 additions and 392 deletions

View File

@@ -0,0 +1,51 @@
const fs = require('fs');
const common = require('../../src/common');
const verifyAndPlaceBinary = require('../../src/assets/binary');
jest.mock('fs');
jest.mock('../../src/common');
describe('verifyAndPlaceBinary()', () => {
let callback;
beforeEach(() => {
callback = jest.fn();
});
it('should call callback with error if binary downloaded differs from config', () => {
fs.existsSync.mockReturnValueOnce(false);
verifyAndPlaceBinary('command', './bin', callback);
expect(callback).toHaveBeenCalledWith('Downloaded binary does not contain the binary specified in configuration - command');
});
it('should call callback with error if installation path cannot be found', () => {
const error = new Error();
fs.existsSync.mockReturnValueOnce(true);
common.getInstallationPath.mockImplementationOnce((cb) => cb(error));
verifyAndPlaceBinary('command', './bin', callback);
expect(callback).toHaveBeenCalledWith(error);
});
it('should call callback with null on success', () => {
fs.existsSync.mockReturnValueOnce(true);
common.getInstallationPath.mockImplementationOnce((cb) => cb(null, '/usr/local/bin'));
verifyAndPlaceBinary('command', './bin', callback);
expect(callback).toHaveBeenCalledWith(null);
});
it('should move the binary to installation directory', () => {
fs.existsSync.mockReturnValueOnce(true);
common.getInstallationPath.mockImplementationOnce((cb) => cb(null, '/usr/local/bin'));
verifyAndPlaceBinary('command', './bin', callback);
expect(fs.renameSync).toHaveBeenCalledWith('bin/command', '/usr/local/bin/command');
});
});

View File

@@ -0,0 +1,49 @@
const { EventEmitter } = require('events');
const fs = require('fs');
const move = require('../../src/assets/move');
jest.mock('fs');
describe('move()', () => {
let streamEvents, pipe, onSuccess, onError;
beforeEach(() => {
streamEvents = new EventEmitter();
pipe = jest.fn();
onSuccess = jest.fn();
onError = jest.fn();
createWriteStream = jest.fn();
fs.createWriteStream.mockReturnValueOnce(streamEvents);
});
it('should download resource to given binPath', () => {
move({ opts: { binPath: './bin', binName: 'command' }, req: { pipe }, onSuccess, onError });
expect(fs.createWriteStream).toHaveBeenCalledWith('bin/command');
});
it('should call onSuccess on stream closed', () => {
move({ opts: { binPath: './bin', binName: 'command' }, req: { pipe }, onSuccess, onError });
streamEvents.emit('close');
expect(onSuccess).toHaveBeenCalled();
});
it('should call onError with error on write stream error', () => {
const error = new Error();
move({ opts: { binPath: './bin', binName: 'command' }, req: { pipe }, onSuccess, onError });
streamEvents.emit('error', error);
expect(onError).toHaveBeenCalledWith(error);
});
});

View File

@@ -0,0 +1,65 @@
const { EventEmitter } = require('events');
const zlib = require('zlib');
const tar = require('tar');
const untar = require('../../src/assets/untar');
jest.mock('zlib');
jest.mock('tar', () => ({
Extract: jest.fn()
}));
describe('untar()', () => {
let ungzEvents, untarEvents, pipe, onSuccess, onError;
beforeEach(() => {
ungzEvents = new EventEmitter();
untarEvents = new EventEmitter();
pipe = jest.fn();
onSuccess = jest.fn();
onError = jest.fn();
pipe.mockReturnValueOnce({ pipe });
tar.Extract.mockReturnValueOnce(untarEvents);
zlib.createGunzip.mockReturnValueOnce(ungzEvents);
});
it('should download resource and untar to given binPath', () => {
untar({ opts: { binPath: './bin', binName: 'command' }, req: { pipe }, onSuccess, onError });
expect(tar.Extract).toHaveBeenCalledWith({ path: './bin' });
});
it('should call onSuccess on untar end', () => {
untar({ opts: { binPath: './bin', binName: 'command' }, req: { pipe }, onSuccess, onError });
untarEvents.emit('end');
expect(onSuccess).toHaveBeenCalled();
});
it('should call onError with error on ungz error', () => {
const error = new Error();
untar({ opts: { binPath: './bin', binName: 'command' }, req: { pipe }, onSuccess, onError });
ungzEvents.emit('error', error);
expect(onError).toHaveBeenCalledWith(error);
});
it('should call onError with error on untar error', () => {
const error = new Error();
untar({ opts: { binPath: './bin', binName: 'command' }, req: { pipe }, onSuccess, onError });
untarEvents.emit('error', error);
expect(onError).toHaveBeenCalledWith(error);
});
});