|
| 1 | +'use strict'; |
| 2 | +/** |
| 3 | + * Unit tests for Configuration. |
| 4 | + */ |
| 5 | + |
| 6 | +const _ = require('lodash'); |
| 7 | +const chai = require('chai'); |
| 8 | +const sinon = require('sinon'); |
| 9 | +const childProcess = require('child_process'); |
| 10 | +const Utils = require('./Utils'); |
| 11 | + |
| 12 | +chai.use(require('chai-as-promised')); |
| 13 | +chai.use(require('sinon-chai')); |
| 14 | + |
| 15 | +const expect = chai.expect; |
| 16 | + |
| 17 | +describe('Utils', () => { |
| 18 | + let sandbox; |
| 19 | + |
| 20 | + before(() => { |
| 21 | + sandbox = sinon.sandbox.create(); |
| 22 | + }); |
| 23 | + |
| 24 | + after(() => { |
| 25 | + sandbox.restore(); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('guid', () => { |
| 29 | + it('should return different unique ids', () => { |
| 30 | + const guids = []; |
| 31 | + for (let i=0; i<10; i++) { |
| 32 | + guids.push(Utils.guid()); |
| 33 | + } |
| 34 | + |
| 35 | + expect(_.size(_.uniq(guids))).to.equal(10); |
| 36 | + expect(_.size(_.compact(guids))).to.equal(10); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('SpawnError', () => { |
| 41 | + it('should store stdout and stderr', () => { |
| 42 | + const err = new Utils.SpawnError('message', 'stdout', 'stderr'); |
| 43 | + expect(err).to.have.a.property('message').that.equals('message'); |
| 44 | + expect(err).to.have.a.property('stdout').that.equals('stdout'); |
| 45 | + expect(err).to.have.a.property('stderr').that.equals('stderr'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should print message and stderr', () => { |
| 49 | + const err = new Utils.SpawnError('message', 'stdout', 'stderr'); |
| 50 | + |
| 51 | + expect(err.toString()).to.equal('message\nstderr'); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('spawnProcess', () => { |
| 56 | + const childMock = { |
| 57 | + stdout: { |
| 58 | + setEncoding: sinon.stub(), |
| 59 | + on: sinon.stub() |
| 60 | + }, |
| 61 | + stderr: { |
| 62 | + setEncoding: sinon.stub(), |
| 63 | + on: sinon.stub() |
| 64 | + }, |
| 65 | + on: sinon.stub() |
| 66 | + }; |
| 67 | + |
| 68 | + beforeEach(() => { |
| 69 | + sandbox.stub(childProcess, 'spawn').returns(childMock); |
| 70 | + }); |
| 71 | + |
| 72 | + afterEach(() => { |
| 73 | + childProcess.spawn.restore(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should call child_process.spawn', () => { |
| 77 | + childMock.on.reset(); |
| 78 | + childMock.on.withArgs('close').yields(0); |
| 79 | + return expect(Utils.spawnProcess('cmd', [])).to.be.fulfilled |
| 80 | + .then(result => { |
| 81 | + expect(childProcess.spawn).to.have.been.calledOnce; |
| 82 | + expect(result).to.have.a.property('stdout').that.is.empty; |
| 83 | + expect(result).to.have.a.property('stderr').that.is.empty; |
| 84 | + return null; |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should return stdout and stderr', () => { |
| 89 | + childMock.stdout.on.withArgs('data').yields('myOutData'); |
| 90 | + childMock.stderr.on.withArgs('data').yields('myErrData'); |
| 91 | + childMock.on.reset(); |
| 92 | + childMock.on.withArgs('close').yields(0); |
| 93 | + return expect(Utils.spawnProcess('cmd', [])).to.be.fulfilled |
| 94 | + .then(result => { |
| 95 | + expect(result).to.have.a.property('stdout').that.equals('myOutData'); |
| 96 | + expect(result).to.have.a.property('stderr').that.equals('myErrData'); |
| 97 | + return null; |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should reject on spawn internal error', () => { |
| 102 | + childMock.on.reset(); |
| 103 | + childMock.on.withArgs('error').yields(new Error('spawn ENOENT')); |
| 104 | + return expect(Utils.spawnProcess('cmd', [])).to.be.rejectedWith('spawn ENOENT'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should reject on positive exit code', () => { |
| 108 | + childMock.on.reset(); |
| 109 | + childMock.on.withArgs('close').yields(1); |
| 110 | + return expect(Utils.spawnProcess('cmd', [])).to.be.rejectedWith(Utils.SpawnError); |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
0 commit comments