|
| 1 | +const exec = require('@actions/exec'); |
| 2 | +const fs = require('fs'); |
| 3 | +const os = require('os'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +const { assert } = require('chai'); |
| 7 | +const itParam = require('mocha-param'); |
| 8 | +const sinon = require('sinon'); |
| 9 | + |
| 10 | +const { |
| 11 | + Installer, |
| 12 | + UnsupportedOSError, |
| 13 | + UnsupportedVersionError |
| 14 | +} = require('../src/installer'); |
| 15 | + |
| 16 | +const fixture = ['Darwin', 'Windows_NT']; |
| 17 | + |
| 18 | +const validVersions = ['3.1.2', '3.0-rc1', '3.1-rc1', '3.1.1']; |
| 19 | +const invalidVersion = 'y50pgz2b'; |
| 20 | + |
| 21 | +describe('Test Installer class', () => { |
| 22 | + let fsChmodSyncStub; |
| 23 | + let execExecStub; |
| 24 | + let osTypeStub; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + fsChmodSyncStub = sinon.stub(fs, 'chmodSync'); |
| 28 | + execExecStub = sinon.stub(exec, 'exec'); |
| 29 | + osTypeStub = sinon.stub(os, 'type'); |
| 30 | + }); |
| 31 | + |
| 32 | + itParam('should build correct exec file name for Linux OS (${value})', |
| 33 | + validVersions, (validVersion) => { |
| 34 | + osTypeStub.returns('Linux'); |
| 35 | + |
| 36 | + const installer = new Installer(validVersion); |
| 37 | + |
| 38 | + const expected = 'install-cobol-linux.sh'; |
| 39 | + const actual = installer._execFileName(); |
| 40 | + assert.equal(expected, actual); |
| 41 | + }); |
| 42 | + |
| 43 | + itParam('should not build exec file name for ${value} OS', fixture, |
| 44 | + (osType) => { |
| 45 | + osTypeStub.returns(osType); |
| 46 | + |
| 47 | + const installer = new Installer(validVersions[0]); |
| 48 | + |
| 49 | + try { |
| 50 | + installer._execFileName(); |
| 51 | + } catch (e) { |
| 52 | + if (e instanceof UnsupportedOSError) { |
| 53 | + return; |
| 54 | + } |
| 55 | + } |
| 56 | + // eslint-disable-next-line new-cap |
| 57 | + assert.Throw(); |
| 58 | + }); |
| 59 | + |
| 60 | + itParam('should install correctly for Linux OS (${value})', |
| 61 | + validVersions, async (version) => { |
| 62 | + const execFileName = 'install-cobol-linux.sh'; |
| 63 | + |
| 64 | + osTypeStub.returns('Linux'); |
| 65 | + |
| 66 | + const installer = new Installer(version); |
| 67 | + await installer.install(); |
| 68 | + |
| 69 | + execExecStub.calledOnceWith( |
| 70 | + path.join(__dirname, execFileName), |
| 71 | + [version] |
| 72 | + ); |
| 73 | + fsChmodSyncStub.calledOnceWith(path.join(__dirname, execFileName), '777'); |
| 74 | + }); |
| 75 | + |
| 76 | + itParam('should not install for ${value} OS', fixture, async (osType) => { |
| 77 | + osTypeStub.returns(osType); |
| 78 | + |
| 79 | + const installer = new Installer(validVersions[0]); |
| 80 | + try { |
| 81 | + await installer.install(); |
| 82 | + } catch (e) { |
| 83 | + if (e instanceof UnsupportedOSError) { |
| 84 | + assert.isTrue(execExecStub.notCalled); |
| 85 | + assert.isTrue(fsChmodSyncStub.notCalled); |
| 86 | + return; |
| 87 | + } |
| 88 | + } |
| 89 | + // eslint-disable-next-line new-cap |
| 90 | + assert.Throw(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should not install invalid version', async () => { |
| 94 | + osTypeStub.returns('Linux'); |
| 95 | + |
| 96 | + const installer = new Installer(invalidVersion); |
| 97 | + try { |
| 98 | + await installer.install(); |
| 99 | + } catch (e) { |
| 100 | + if (e instanceof UnsupportedVersionError) { |
| 101 | + assert.isTrue(execExecStub.notCalled); |
| 102 | + assert.isTrue(fsChmodSyncStub.notCalled); |
| 103 | + return; |
| 104 | + } |
| 105 | + } |
| 106 | + // eslint-disable-next-line new-cap |
| 107 | + assert.Throw(); |
| 108 | + }); |
| 109 | + |
| 110 | + afterEach(() => { |
| 111 | + sinon.restore(); |
| 112 | + }); |
| 113 | +}); |
0 commit comments