mirror of
https://github.com/ershisan99/go-npm.git
synced 2026-02-03 12:35:05 +00:00
ADD unit tests, split and refactor source code
This commit is contained in:
51
__test__/assets/binary.spec.js
Normal file
51
__test__/assets/binary.spec.js
Normal 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');
|
||||
});
|
||||
});
|
||||
49
__test__/assets/move.spec.js
Normal file
49
__test__/assets/move.spec.js
Normal 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);
|
||||
});
|
||||
});
|
||||
65
__test__/assets/untar.spec.js
Normal file
65
__test__/assets/untar.spec.js
Normal 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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user