-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADD unit testing for resource strategies
- Loading branch information
1 parent
6204be6
commit 9071f9b
Showing
3 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.idea | ||
node_modules | ||
src | ||
__test__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
const rewire = require('rewire'); | ||
const EventEmitter = require('events').EventEmitter; | ||
|
||
describe('go-npm', function() { | ||
let mod; | ||
|
||
beforeEach(function() { | ||
mod = rewire('../src/index.js'); | ||
}); | ||
|
||
describe('Resource handling strategies', function() { | ||
|
||
describe('untarStrategy()', function() { | ||
|
||
let untarStrategy, ungzEvents, untarEvents, pipe, callback, zlib, createGunzip, tar; | ||
|
||
beforeEach(function() { | ||
untarStrategy = mod.__get__('untarStrategy'); | ||
zlib = mod.__get__('zlib'); | ||
tar = mod.__get__('tar'); | ||
ungzEvents = new EventEmitter(); | ||
untarEvents = new EventEmitter(); | ||
|
||
createGunzip = jest.fn(); | ||
pipe = jest.fn(); | ||
callback = jest.fn(); | ||
|
||
pipe.mockReturnValueOnce({ pipe }); | ||
createGunzip.mockReturnValueOnce(ungzEvents); | ||
jest.spyOn(tar, 'Extract').mockReturnValueOnce(untarEvents); | ||
|
||
// jest.spyOn not working on read-only properties | ||
Object.defineProperty(zlib, 'createGunzip', { value: createGunzip }); | ||
}); | ||
|
||
it('should download resource and untar to given binPath', function() { | ||
|
||
untarStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback); | ||
|
||
expect(tar.Extract).toHaveBeenCalledWith({ path: './bin' }); | ||
}); | ||
|
||
it('should call verifyAndPlaceBinary on untar end', function() { | ||
|
||
const verifyAndPlaceBinary = jest.fn(); | ||
|
||
mod.__set__('verifyAndPlaceBinary', verifyAndPlaceBinary); | ||
|
||
untarStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback); | ||
|
||
untarEvents.emit('end'); | ||
|
||
expect(verifyAndPlaceBinary).toHaveBeenCalledWith('command', './bin', callback); | ||
}); | ||
|
||
it('should call callback with error on ungz error', function() { | ||
|
||
const error = new Error(); | ||
|
||
untarStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback); | ||
|
||
ungzEvents.emit('error', error); | ||
|
||
expect(callback).toHaveBeenCalledWith(error); | ||
}); | ||
|
||
it('should call callback with error on untar error', function() { | ||
|
||
const error = new Error(); | ||
|
||
untarStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback); | ||
|
||
untarEvents.emit('error', error); | ||
|
||
expect(callback).toHaveBeenCalledWith(error); | ||
}); | ||
}); | ||
|
||
describe('moveStrategy()', function() { | ||
|
||
let moveStrategy, streamEvents, pipe, callback, fs; | ||
|
||
beforeEach(function() { | ||
|
||
moveStrategy = mod.__get__('moveStrategy'); | ||
fs = mod.__get__('fs'); | ||
streamEvents = new EventEmitter(); | ||
|
||
pipe = jest.fn(); | ||
callback = jest.fn(); | ||
|
||
jest.spyOn(fs, 'createWriteStream').mockReturnValueOnce(streamEvents); | ||
}); | ||
|
||
it('should download resource to given binPath', function() { | ||
|
||
moveStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback); | ||
|
||
expect(fs.createWriteStream).toHaveBeenCalledWith('bin/command'); | ||
}); | ||
|
||
it('should call verifyAndPlaceBinary on stream closed', function() { | ||
|
||
const verifyAndPlaceBinary = jest.fn(); | ||
|
||
mod.__set__('verifyAndPlaceBinary', verifyAndPlaceBinary); | ||
|
||
moveStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback); | ||
|
||
streamEvents.emit('close'); | ||
|
||
expect(verifyAndPlaceBinary).toHaveBeenCalledWith('command', './bin', callback); | ||
}); | ||
|
||
it('should call callback with error on write stream error', function() { | ||
|
||
const error = new Error(); | ||
|
||
moveStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback); | ||
|
||
streamEvents.emit('error', error); | ||
|
||
expect(callback).toHaveBeenCalledWith(error); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters