|
| 1 | +const rewire = require('rewire'); |
| 2 | +const EventEmitter = require('events').EventEmitter; |
| 3 | + |
| 4 | +describe('go-npm', function() { |
| 5 | + let mod; |
| 6 | + |
| 7 | + beforeEach(function() { |
| 8 | + mod = rewire('../src/index.js'); |
| 9 | + }); |
| 10 | + |
| 11 | + describe('Resource handling strategies', function() { |
| 12 | + |
| 13 | + describe('untarStrategy()', function() { |
| 14 | + |
| 15 | + let untarStrategy, ungzEvents, untarEvents, pipe, callback, zlib, createGunzip, tar; |
| 16 | + |
| 17 | + beforeEach(function() { |
| 18 | + untarStrategy = mod.__get__('untarStrategy'); |
| 19 | + zlib = mod.__get__('zlib'); |
| 20 | + tar = mod.__get__('tar'); |
| 21 | + ungzEvents = new EventEmitter(); |
| 22 | + untarEvents = new EventEmitter(); |
| 23 | + |
| 24 | + createGunzip = jest.fn(); |
| 25 | + pipe = jest.fn(); |
| 26 | + callback = jest.fn(); |
| 27 | + |
| 28 | + pipe.mockReturnValueOnce({ pipe }); |
| 29 | + createGunzip.mockReturnValueOnce(ungzEvents); |
| 30 | + jest.spyOn(tar, 'Extract').mockReturnValueOnce(untarEvents); |
| 31 | + |
| 32 | + // jest.spyOn not working on read-only properties |
| 33 | + Object.defineProperty(zlib, 'createGunzip', { value: createGunzip }); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should download resource and untar to given binPath', function() { |
| 37 | + |
| 38 | + untarStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback); |
| 39 | + |
| 40 | + expect(tar.Extract).toHaveBeenCalledWith({ path: './bin' }); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should call verifyAndPlaceBinary on untar end', function() { |
| 44 | + |
| 45 | + const verifyAndPlaceBinary = jest.fn(); |
| 46 | + |
| 47 | + mod.__set__('verifyAndPlaceBinary', verifyAndPlaceBinary); |
| 48 | + |
| 49 | + untarStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback); |
| 50 | + |
| 51 | + untarEvents.emit('end'); |
| 52 | + |
| 53 | + expect(verifyAndPlaceBinary).toHaveBeenCalledWith('command', './bin', callback); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should call callback with error on ungz error', function() { |
| 57 | + |
| 58 | + const error = new Error(); |
| 59 | + |
| 60 | + untarStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback); |
| 61 | + |
| 62 | + ungzEvents.emit('error', error); |
| 63 | + |
| 64 | + expect(callback).toHaveBeenCalledWith(error); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should call callback with error on untar error', function() { |
| 68 | + |
| 69 | + const error = new Error(); |
| 70 | + |
| 71 | + untarStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback); |
| 72 | + |
| 73 | + untarEvents.emit('error', error); |
| 74 | + |
| 75 | + expect(callback).toHaveBeenCalledWith(error); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('moveStrategy()', function() { |
| 80 | + |
| 81 | + let moveStrategy, streamEvents, pipe, callback, fs; |
| 82 | + |
| 83 | + beforeEach(function() { |
| 84 | + |
| 85 | + moveStrategy = mod.__get__('moveStrategy'); |
| 86 | + fs = mod.__get__('fs'); |
| 87 | + streamEvents = new EventEmitter(); |
| 88 | + |
| 89 | + pipe = jest.fn(); |
| 90 | + callback = jest.fn(); |
| 91 | + |
| 92 | + jest.spyOn(fs, 'createWriteStream').mockReturnValueOnce(streamEvents); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should download resource to given binPath', function() { |
| 96 | + |
| 97 | + moveStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback); |
| 98 | + |
| 99 | + expect(fs.createWriteStream).toHaveBeenCalledWith('bin/command'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should call verifyAndPlaceBinary on stream closed', function() { |
| 103 | + |
| 104 | + const verifyAndPlaceBinary = jest.fn(); |
| 105 | + |
| 106 | + mod.__set__('verifyAndPlaceBinary', verifyAndPlaceBinary); |
| 107 | + |
| 108 | + moveStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback); |
| 109 | + |
| 110 | + streamEvents.emit('close'); |
| 111 | + |
| 112 | + expect(verifyAndPlaceBinary).toHaveBeenCalledWith('command', './bin', callback); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should call callback with error on write stream error', function() { |
| 116 | + |
| 117 | + const error = new Error(); |
| 118 | + |
| 119 | + moveStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback); |
| 120 | + |
| 121 | + streamEvents.emit('error', error); |
| 122 | + |
| 123 | + expect(callback).toHaveBeenCalledWith(error); |
| 124 | + }); |
| 125 | + }); |
| 126 | + }); |
| 127 | +}); |
0 commit comments