Skip to content

Commit aea56b8

Browse files
committed
WIP Unit tests
1 parent 17a9380 commit aea56b8

File tree

2 files changed

+131
-19
lines changed

2 files changed

+131
-19
lines changed

src/base-command.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -109,33 +109,33 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
109109

110110
const configPath = this.flags.config ?? resolve(process.cwd(), config.configName);
111111

112-
let baseUrl = config.launchBaseUrl || this.launchHubUrl;
113-
if (!baseUrl) {
114-
baseUrl = getLaunchHubUrl();
115-
}
112+
// let baseUrl = config.launchBaseUrl || this.launchHubUrl;
113+
// if (!baseUrl) {
114+
// baseUrl = getLaunchHubUrl();
115+
// }
116116

117117
this.sharedConfig = {
118118
...require('./config').default,
119-
currentConfig: {},
120-
...this.flags,
121-
flags: this.flags,
122-
host: this.cmaHost,
119+
// currentConfig: {},
120+
// ...this.flags,
121+
// flags: this.flags,
122+
// host: this.cmaHost,
123123
config: configPath,
124124
projectBasePath: projectBasePath,
125-
authtoken: configHandler.get('authtoken'),
126-
authType: configHandler.get('authorisationType'),
127-
authorization: configHandler.get('oauthAccessToken'),
128-
logsApiBaseUrl: `${baseUrl}/${config.logsApiEndpoint}`,
129-
manageApiBaseUrl: `${baseUrl}/${config.manageApiEndpoint}`,
125+
// authtoken: configHandler.get('authtoken'),
126+
// authType: configHandler.get('authorisationType'),
127+
// authorization: configHandler.get('oauthAccessToken'),
128+
// logsApiBaseUrl: `${baseUrl}/${config.logsApiEndpoint}`,
129+
// manageApiBaseUrl: `${baseUrl}/${config.manageApiEndpoint}`,
130130
};
131131

132-
if (this.flags.type) {
133-
this.sharedConfig.provider = this.flags.type;
134-
}
132+
// if (this.flags.type) {
133+
// this.sharedConfig.provider = this.flags.type;
134+
// }
135135

136-
if (existsSync(configPath)) {
137-
this.sharedConfig.isExistingProject = true;
138-
}
136+
// if (existsSync(configPath)) {
137+
// this.sharedConfig.isExistingProject = true;
138+
// }
139139
}
140140

141141
/**

test/unit/base-command.test.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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

Comments
 (0)