mirror of
https://github.com/ershisan99/go-npm.git
synced 2025-12-17 05:09:30 +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');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user