|
| 1 | +import * as chai from 'chai'; |
| 2 | +import * as chaiAsPromised from 'chai-as-promised'; |
| 3 | +chai.use(chaiAsPromised); |
| 4 | + |
| 5 | +import { assert, expect } from 'chai'; |
| 6 | +import 'mocha'; |
| 7 | +import * as path from 'path'; |
| 8 | +import { Stream } from 'stream'; |
| 9 | +import { stdout } from 'test-console'; |
| 10 | +import { StringIncludesAll } from '../../test/utilities'; |
| 11 | +import InstallerSingleton, { |
| 12 | + DependenciesInstaller, |
| 13 | + ERRORS, |
| 14 | + Installer, |
| 15 | +} from './dependencies-installer'; |
| 16 | +import FileReader from './file-reader'; |
| 17 | + |
| 18 | +describe('DependenciesInstaller', () => { |
| 19 | + let installer: DependenciesInstaller; |
| 20 | + let fileReader: FileReader; |
| 21 | + |
| 22 | + describe('Instance', () => { |
| 23 | + beforeEach(async function () { |
| 24 | + this.timeout(12000); |
| 25 | + installer = await InstallerSingleton.getInstance(process.cwd()); |
| 26 | + }); |
| 27 | + |
| 28 | + |
| 29 | + it('instantiates', () => { |
| 30 | + expect(installer).to.be.an('object') |
| 31 | + .to.contain.keys('add', 'run'); |
| 32 | + }); |
| 33 | + |
| 34 | + |
| 35 | + it('instantiates with custom options parameter', () => { |
| 36 | + const instance = Installer(process.cwd(), { |
| 37 | + logChannel: 'PluginDependencies', |
| 38 | + managers: ['npm', 'yarn'], |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + |
| 43 | + it('throws if first parameter <cwd> is not passed', () => { |
| 44 | + assert.throws(() => Installer(), ERRORS.CWD_INVALID); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + |
| 49 | + describe('add()', () => { |
| 50 | + beforeEach(async () => { |
| 51 | + installer = await InstallerSingleton.getInstance(process.cwd()); |
| 52 | + fileReader = FileReader(path.join(process.cwd(), 'test.package.json')); |
| 53 | + const fileContent = await fileReader.read(); |
| 54 | + await fileReader.write( |
| 55 | + { |
| 56 | + ...fileContent, |
| 57 | + ...{ |
| 58 | + devDependencies: { |
| 59 | + 'my-dev-package-1': '^1.0.0', |
| 60 | + 'my-dev-package-2': '^2.0.0', |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + |
| 68 | + it('adds all dependencies', async () => { |
| 69 | + await installer.add({ |
| 70 | + dependencies: { |
| 71 | + 'my-production-package-1': '^1.0.0', |
| 72 | + 'my-production-package-2': '^2.0.0', |
| 73 | + }, |
| 74 | + }); |
| 75 | + |
| 76 | + const { dependencies } = await fileReader.read(); |
| 77 | + expect(Object.keys(dependencies)).to.contain('my-production-package-1', 'my-production-package-2'); |
| 78 | + }); |
| 79 | + |
| 80 | + |
| 81 | + it('adds missing dependency', async () => { |
| 82 | + await installer.add({ |
| 83 | + devDependencies: { |
| 84 | + 'my-dev-package-3': '^3.0.0', |
| 85 | + }, |
| 86 | + }); |
| 87 | + |
| 88 | + const { devDependencies } = await fileReader.read(); |
| 89 | + expect(Object.keys(devDependencies)).to.contain('my-dev-package-3'); |
| 90 | + }); |
| 91 | + |
| 92 | + |
| 93 | + it('doesn\'t change version of existing dependency', async () => { |
| 94 | + await installer.add({ |
| 95 | + devDependencies: { |
| 96 | + 'my-dev-package-1': '^2.0.0', |
| 97 | + }, |
| 98 | + }); |
| 99 | + const { devDependencies } = await fileReader.read(); |
| 100 | + expect(devDependencies['my-dev-package-1']).to.equal('^1.0.0'); |
| 101 | + }); |
| 102 | + |
| 103 | + |
| 104 | + it('doesn\'t do anything if existing dependency version matches', async () => { |
| 105 | + await installer.add({ |
| 106 | + devDependencies: { |
| 107 | + 'my-dev-package-1': '^1.0.0', |
| 108 | + }, |
| 109 | + }); |
| 110 | + const { devDependencies } = await fileReader.read(); |
| 111 | + expect(devDependencies['my-dev-package-1']).to.equal('^1.0.0'); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + |
| 116 | + describe('run()', () => { |
| 117 | + it('starts the package manager install process', async () => { |
| 118 | + const instance = await InstallerSingleton.getInstance(process.cwd()); |
| 119 | + return expect(installer.run()).to.eventually.be.fulfilled; |
| 120 | + }).timeout(60 * 1000); |
| 121 | + |
| 122 | + |
| 123 | + it('throws if there are no available package managers', async () => { |
| 124 | + const instance = await Installer(process.cwd(), { managers: ['non-existent-manager'] }); |
| 125 | + assert.throws(() => instance.run(), ERRORS.MANAGERS_REQUIRED); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + |
| 130 | + describe('private method hasNoManagers()', () => { |
| 131 | + it('returns true when no managers are found', async () => { |
| 132 | + const instance = await Installer(process.cwd(), { managers: ['non-existent-manager'] }); |
| 133 | + expect(instance.hasNoManagers()).to.be.true; |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + |
| 138 | + describe('private method isManagerInstalled()', () => { |
| 139 | + beforeEach(async () => { |
| 140 | + installer = await InstallerSingleton.getInstance(process.cwd()); |
| 141 | + }); |
| 142 | + |
| 143 | + |
| 144 | + it('returns true if yarn is installed on the system', async () => { |
| 145 | + expect(await installer.isManagerInstalled('yarn')).to.be.true; |
| 146 | + }); |
| 147 | + |
| 148 | + |
| 149 | + it('returns true if npm is installed on the system', async () => { |
| 150 | + expect(await installer.isManagerInstalled('npm')).to.be.true; |
| 151 | + }); |
| 152 | + |
| 153 | + |
| 154 | + it('returns false if unknown package manager is not installed', async () => { |
| 155 | + expect(await installer.isManagerInstalled('non-existent-manager')).to.be.false; |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + |
| 160 | + describe('private method logError()', () => { |
| 161 | + beforeEach(async () => { |
| 162 | + installer = await InstallerSingleton.getInstance(process.cwd()); |
| 163 | + }); |
| 164 | + |
| 165 | + it('logs out using the error channel', () => { |
| 166 | + // Catch all console logs |
| 167 | + const inspect = stdout.inspect(); |
| 168 | + |
| 169 | + // Mocking the stderr stream |
| 170 | + (new Stream()) |
| 171 | + .on('data', data => installer.logError(data)) |
| 172 | + .emit('data', 'Some nasty error here'); |
| 173 | + |
| 174 | + inspect.restore(); |
| 175 | + expect(inspect.output.join(' ')).to.satisfy(StringIncludesAll('ERROR', 'Some nasty error here')); |
| 176 | + }); |
| 177 | + }); |
| 178 | + |
| 179 | + |
| 180 | + describe('private method logOutput()', () => { |
| 181 | + beforeEach(async () => { |
| 182 | + installer = await InstallerSingleton.getInstance(process.cwd()); |
| 183 | + }); |
| 184 | + |
| 185 | + it('logs out using the debug channel', () => { |
| 186 | + // Catch all console logs |
| 187 | + const inspect = stdout.inspect(); |
| 188 | + |
| 189 | + // Mocking the stderr stream |
| 190 | + (new Stream()) |
| 191 | + .on('data', data => installer.logOutput(data)) |
| 192 | + .emit('data', 'Some standard output message'); |
| 193 | + |
| 194 | + inspect.restore(); |
| 195 | + expect(inspect.output.join(' ')).to.satisfy(StringIncludesAll('Some standard output message')); |
| 196 | + }); |
| 197 | + }); |
| 198 | + |
| 199 | + |
| 200 | + after(async () => { |
| 201 | + fileReader = FileReader(path.join(process.cwd(), 'test.package.json')); |
| 202 | + const fileContents = await fileReader.read(); |
| 203 | + delete fileContents.dependencies; |
| 204 | + delete fileContents.devDependencies; |
| 205 | + await fileReader.write(fileContents); |
| 206 | + }); |
| 207 | +}); |
0 commit comments