|
| 1 | +//@ts-nocheck |
| 2 | +// todo: Allow ts with any and remove ts-nocheck |
| 3 | +import { expect } from 'chai'; |
| 4 | +import { stub, createSandbox } from 'sinon'; |
| 5 | +import { cliux as ux, configHandler } from '@contentstack/cli-utilities'; |
| 6 | +import fs from 'fs'; |
| 7 | +import path from 'path'; |
| 8 | +import { BaseCommand } from '../../src/base-command'; |
| 9 | +import config from '../../src/config'; |
| 10 | +import { getLaunchHubUrl } from '../../src/util'; |
| 11 | + |
| 12 | +describe('BaseCommand', () => { |
| 13 | + let sandbox; |
| 14 | + let baseCommandInstance; |
| 15 | + let flags; |
| 16 | + |
| 17 | + describe('prepareConfig', () => { |
| 18 | + let statSyncResultObj; |
| 19 | + let statSyncStub; |
| 20 | + let existsSyncStub; |
| 21 | + let processCwdStub; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + sandbox = createSandbox(); |
| 25 | + |
| 26 | + baseCommandInstance = new (class extends BaseCommand<typeof BaseCommand> { |
| 27 | + async run() {} |
| 28 | + })([], {} as any); |
| 29 | + |
| 30 | + baseCommandInstance.flags = {}; |
| 31 | + |
| 32 | + baseCommandInstance.exit = sandbox.stub().callsFake((code) => { |
| 33 | + throw new Error(code); |
| 34 | + }); |
| 35 | + |
| 36 | + statSyncResultObj = { |
| 37 | + isDirectory: sandbox.stub().returns(true), |
| 38 | + }; |
| 39 | + statSyncStub = sandbox.stub(fs, 'statSync').returns(statSyncResultObj); |
| 40 | + |
| 41 | + existsSyncStub = sandbox.stub(fs, 'existsSync').returns(true); |
| 42 | + |
| 43 | + processCwdStub = sandbox.stub(process, 'cwd').returns('/root/'); |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + sandbox.restore(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should initialize sharedConfig with default values if no flags passed', async () => { |
| 51 | + |
| 52 | + await baseCommandInstance.prepareConfig(); |
| 53 | + |
| 54 | + expect(baseCommandInstance.sharedConfig).to.deep.equal({ |
| 55 | + ...require('../../src/config').default, |
| 56 | + projectBasePath: '/root/' |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should successfully initialize sharedConfig.projectBasePath if "data-dir" flag is passed', async () => { |
| 61 | + baseCommandInstance.flags = { |
| 62 | + 'data-dir': '/root/subdirectory/project1', |
| 63 | + }; |
| 64 | + |
| 65 | + await baseCommandInstance.prepareConfig(); |
| 66 | + |
| 67 | + expect(existsSyncStub.args[0][0]).to.equal('/root/subdirectory/project1'); |
| 68 | + expect(statSyncStub.args[0][0]).to.equal('/root/subdirectory/project1'); |
| 69 | + expect(statSyncResultObj.isDirectory.calledOnce).to.be.true; |
| 70 | + expect(baseCommandInstance.sharedConfig).to.deep.equal({ |
| 71 | + ...require('../../src/config').default, |
| 72 | + projectBasePath: '/root/subdirectory/project1' |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should exit with error if "data-dir" flag specified but path does not exist', async () => { |
| 77 | + existsSyncStub.returns(false); |
| 78 | + baseCommandInstance.flags = { |
| 79 | + 'data-dir': '/root/subdirectory/project1', |
| 80 | + }; |
| 81 | + let exitStatusCode; |
| 82 | + |
| 83 | + try { |
| 84 | + await baseCommandInstance.prepareConfig(); |
| 85 | + } catch (err) { |
| 86 | + exitStatusCode = err.message; |
| 87 | + } |
| 88 | + |
| 89 | + expect(existsSyncStub.args[0][0]).to.equal('/root/subdirectory/project1'); |
| 90 | + expect(exitStatusCode).to.equal('1'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should exit with error if "data-dir" flag specified with a non-directory path', async () => { |
| 94 | + statSyncResultObj.isDirectory.returns(false); |
| 95 | + baseCommandInstance.flags = { |
| 96 | + 'data-dir': '/root/subdirectory/project1/file.txt', |
| 97 | + }; |
| 98 | + let exitStatusCode; |
| 99 | + |
| 100 | + try { |
| 101 | + await baseCommandInstance.prepareConfig(); |
| 102 | + } catch (err) { |
| 103 | + exitStatusCode = err.message; |
| 104 | + } |
| 105 | + |
| 106 | + expect(existsSyncStub.args[0][0]).to.equal('/root/subdirectory/project1/file.txt'); |
| 107 | + expect(statSyncStub.args[0][0]).to.equal('/root/subdirectory/project1/file.txt'); |
| 108 | + expect(statSyncResultObj.isDirectory.calledOnce).to.be.true; |
| 109 | + expect(exitStatusCode).to.equal('1'); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments