|
| 1 | +const assert = require('power-assert'); |
| 2 | +const child_process = require('child_process'); |
| 3 | +const fs = require('fs'); |
| 4 | +const https = require('https'); |
| 5 | +const sinon = require('sinon'); |
| 6 | + |
| 7 | +const api = require('../index.js'); |
| 8 | + |
| 9 | +const backetURL = 'https://s3-ap-northeast-1.amazonaws.com/aws-lambda-bins'; |
| 10 | + |
| 11 | +describe('API', () => { |
| 12 | + describe('.set()', () => { |
| 13 | + afterEach(() => api.resetOptions()); |
| 14 | + |
| 15 | + it('should update options', () => { |
| 16 | + api.set({verify: true}); |
| 17 | + assert(api.options.verify === true); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('.install', () => { |
| 22 | + const originalPATH = process.env.PATH; |
| 23 | + beforeEach(() => sinon.stub(api, 'download')); |
| 24 | + afterEach(() => { |
| 25 | + process.env.PATH = originalPATH |
| 26 | + api.download.restore(); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('.use({bin: "git"})', () => { |
| 30 | + it('should setup environment for the latest Git', () => { |
| 31 | + const callback = sinon.stub(); |
| 32 | + api.install({bin: "git"}, callback); |
| 33 | + |
| 34 | + assert(process.env.PATH === `/tmp/usr/bin:${originalPATH}`); |
| 35 | + const url = `${backetURL}/git/git-master.tar.gz`; |
| 36 | + assert(api.download.withArgs(url, callback)); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('.use({bin: "git", tag: "v2.11.0"})', () => { |
| 41 | + it('should setup environment for Git v2.11.0', () => { |
| 42 | + const callback = sinon.stub(); |
| 43 | + api.install({bin: "git", tag: "v2.11.0"}, callback); |
| 44 | + |
| 45 | + assert(process.env.PATH === `/tmp/usr/bin:${originalPATH}`); |
| 46 | + const url = `${backetURL}/git/git-v2.11.0.tar.gz`; |
| 47 | + assert(api.download.withArgs(url, callback)); |
| 48 | + }); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('.download()', () => { |
| 53 | + beforeEach(() => { |
| 54 | + const close = { on: (e, callback) => callback() }; |
| 55 | + sinon.stub(https, 'get', () => close); |
| 56 | + }); |
| 57 | + afterEach(() => https.get.restore()); |
| 58 | + |
| 59 | + context('/tmp/vendor/filename not exists', () => { |
| 60 | + beforeEach(() => { |
| 61 | + sinon.stub(child_process, 'exec', (_command, callback) => callback()); |
| 62 | + sinon.stub(fs, 'mkdir', (_path, callback) => callback()); |
| 63 | + sinon.stub(fs, 'stat', (_path, callback) => callback('no such file or directory')); |
| 64 | + }); |
| 65 | + afterEach(() => { |
| 66 | + child_process.exec.restore(); |
| 67 | + fs.mkdir.restore(); |
| 68 | + fs.stat.restore(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should download a file from url', (done) => { |
| 72 | + const url = `${backetURL}/git/git-master.tar.gz`; |
| 73 | + api.download(url, done); |
| 74 | + |
| 75 | + assert(child_process.exec.calledWith('tar -C /tmp -xf vendor/git-master.tar.gz')); |
| 76 | + assert(fs.mkdir.calledWith('/tmp/vendor')); |
| 77 | + assert(fs.stat.calledWith('/tmp/vendor/git-master.tar.gz')); |
| 78 | + assert(https.get.calledWith(url)); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + context('/tmp/vendor/filename already exists', () => { |
| 83 | + beforeEach(() => { |
| 84 | + sinon.stub(fs, 'mkdir', (_path, callback) => callback()); |
| 85 | + sinon.stub(fs, 'stat', (_path, callback) => callback()); |
| 86 | + }); |
| 87 | + afterEach(() => { |
| 88 | + fs.mkdir.restore(); |
| 89 | + fs.stat.restore(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should not download', (done) => { |
| 93 | + api.download(`${backetURL}/git/git-master.tar.gz`, done); |
| 94 | + |
| 95 | + assert(fs.mkdir.calledWith('/tmp/vendor')); |
| 96 | + assert(fs.stat.calledWith('/tmp/vendor/git-master.tar.gz')); |
| 97 | + assert(https.get.called === false); |
| 98 | + }); |
| 99 | + }); |
| 100 | + }); |
| 101 | +}); |
0 commit comments