Skip to content

Commit 7076975

Browse files
Fix: fixed unit testcases
1 parent 22c0a0c commit 7076975

4 files changed

Lines changed: 19 additions & 17 deletions

File tree

packages/contentstack-audit/src/modules/content-types.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import map from 'lodash/map';
22
import find from 'lodash/find';
33
import isEmpty from 'lodash/isEmpty';
4+
import fs from 'fs';
45
import { join, resolve } from 'path';
5-
import { existsSync, readFileSync, writeFileSync } from 'fs';
66

77
import { sanitizePath, cliux, log } from '@contentstack/cli-utilities';
88

@@ -82,7 +82,7 @@ export default class ContentType extends BaseClass {
8282
try {
8383
this.inMemoryFix = returnFixSchema;
8484

85-
if (!existsSync(this.folderPath)) {
85+
if (!fs.existsSync(this.folderPath)) {
8686
log.warn(`Skipping ${this.moduleName} audit`, this.config.auditContext);
8787
cliux.print($t(auditMsg.NOT_VALID_PATH, { path: this.folderPath }), { color: 'yellow' });
8888
return returnFixSchema ? [] : {};
@@ -170,10 +170,10 @@ export default class ContentType extends BaseClass {
170170
const extensionPath = resolve(this.config.basePath, 'extensions', 'extensions.json');
171171
const marketplacePath = resolve(this.config.basePath, 'marketplace_apps', 'marketplace_apps.json');
172172

173-
if (existsSync(extensionPath)) {
173+
if (fs.existsSync(extensionPath)) {
174174
log.debug(`Loading extensions from: ${extensionPath}`, this.config.auditContext);
175175
try {
176-
this.extensions = Object.keys(JSON.parse(readFileSync(extensionPath, 'utf8')));
176+
this.extensions = Object.keys(JSON.parse(fs.readFileSync(extensionPath, 'utf8')));
177177
log.debug(`Loaded ${this.extensions.length} extensions`, this.config.auditContext);
178178
} catch (error) {
179179
log.debug(`Failed to load extensions: ${error}`, this.config.auditContext);
@@ -182,10 +182,10 @@ export default class ContentType extends BaseClass {
182182
log.debug('No extensions.json found', this.config.auditContext);
183183
}
184184

185-
if (existsSync(marketplacePath)) {
185+
if (fs.existsSync(marketplacePath)) {
186186
log.debug(`Loading marketplace apps from: ${marketplacePath}`, this.config.auditContext);
187187
try {
188-
const marketplaceApps: MarketplaceAppsInstallationData[] = JSON.parse(readFileSync(marketplacePath, 'utf8'));
188+
const marketplaceApps: MarketplaceAppsInstallationData[] = JSON.parse(fs.readFileSync(marketplacePath, 'utf8'));
189189
log.debug(`Found ${marketplaceApps.length} marketplace apps`, this.config.auditContext);
190190

191191
for (const app of marketplaceApps) {
@@ -232,7 +232,7 @@ export default class ContentType extends BaseClass {
232232
if (contentType.uid) {
233233
const filePath = join(this.folderPath, `${contentType.uid}.json`);
234234
log.debug(`Writing fixed content type to: ${filePath}`, this.config.auditContext);
235-
writeFileSync(filePath, JSON.stringify(contentType));
235+
fs.writeFileSync(filePath, JSON.stringify(contentType));
236236
} else {
237237
log.warn(
238238
`Skipping content type without uid: ${JSON.stringify(contentType).substring(0, 100)}`,

packages/contentstack-audit/src/modules/field_rules.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import map from 'lodash/map';
2+
import fs from 'fs';
23
import { join, resolve } from 'path';
3-
import { existsSync, readFileSync, writeFileSync } from 'fs';
44

55
import { FsUtility, Locale, sanitizePath, cliux, log } from '@contentstack/cli-utilities';
66

@@ -95,7 +95,7 @@ export default class FieldRule extends BaseClass {
9595
log.debug(`Field rules folder path: ${this.folderPath}`, this.config.auditContext);
9696
log.debug(`Fix mode: ${this.fix}`, this.config.auditContext);
9797

98-
if (!existsSync(this.folderPath)) {
98+
if (!fs.existsSync(this.folderPath)) {
9999
log.debug(`Skipping ${this.moduleName} audit - path does not exist`, this.config.auditContext);
100100
log.warn(`Skipping ${this.moduleName} audit`, this.config.auditContext);
101101
cliux.print($t(auditMsg.NOT_VALID_PATH, { path: this.folderPath }), { color: 'yellow' });
@@ -412,10 +412,10 @@ export default class FieldRule extends BaseClass {
412412
log.debug(`Extensions path: ${extensionPath}`, this.config.auditContext);
413413
log.debug(`Marketplace apps path: ${marketplacePath}`, this.config.auditContext);
414414

415-
if (existsSync(extensionPath)) {
415+
if (fs.existsSync(extensionPath)) {
416416
log.debug(`Loading extensions from file`, this.config.auditContext);
417417
try {
418-
this.extensions = Object.keys(JSON.parse(readFileSync(extensionPath, 'utf8')));
418+
this.extensions = Object.keys(JSON.parse(fs.readFileSync(extensionPath, 'utf8')));
419419
log.debug(`Loaded ${this.extensions.length} extensions`, this.config.auditContext);
420420
} catch (error) {
421421
log.debug(`Error loading extensions: ${error}`, this.config.auditContext);
@@ -424,10 +424,10 @@ export default class FieldRule extends BaseClass {
424424
log.debug(`Extensions file not found`, this.config.auditContext);
425425
}
426426

427-
if (existsSync(marketplacePath)) {
427+
if (fs.existsSync(marketplacePath)) {
428428
log.debug(`Loading marketplace apps from file`, this.config.auditContext);
429429
try {
430-
const marketplaceApps: MarketplaceAppsInstallationData[] = JSON.parse(readFileSync(marketplacePath, 'utf8'));
430+
const marketplaceApps: MarketplaceAppsInstallationData[] = JSON.parse(fs.readFileSync(marketplacePath, 'utf8'));
431431
log.debug(`Found ${marketplaceApps.length} marketplace apps`, this.config.auditContext);
432432

433433
for (const app of marketplaceApps) {
@@ -490,7 +490,7 @@ export default class FieldRule extends BaseClass {
490490
continue;
491491
}
492492
const filePath = join(this.folderPath, `${schema.uid}.json`);
493-
writeFileSync(filePath, JSON.stringify(schema));
493+
fs.writeFileSync(filePath, JSON.stringify(schema));
494494
log.debug(`Wrote fixed schema: ${schema.uid}${filePath}`, this.config.auditContext);
495495
}
496496
} else {
@@ -635,12 +635,12 @@ export default class FieldRule extends BaseClass {
635635
log.debug(`Master locales path: ${masterLocalesPath}`, this.config.auditContext);
636636

637637
log.debug(`Loading master locales`, this.config.auditContext);
638-
this.locales = existsSync(masterLocalesPath) ? values(JSON.parse(readFileSync(masterLocalesPath, 'utf8'))) : [];
638+
this.locales = fs.existsSync(masterLocalesPath) ? values(JSON.parse(fs.readFileSync(masterLocalesPath, 'utf8'))) : [];
639639
log.debug(`Loaded ${this.locales.length} master locales`, this.config.auditContext);
640640

641-
if (existsSync(localesPath)) {
641+
if (fs.existsSync(localesPath)) {
642642
log.debug(`Loading additional locales from file`, this.config.auditContext);
643-
this.locales.push(...values(JSON.parse(readFileSync(localesPath, 'utf8'))));
643+
this.locales.push(...values(JSON.parse(fs.readFileSync(localesPath, 'utf8'))));
644644
log.debug(`Total locales after loading: ${this.locales.length}`, this.config.auditContext);
645645
} else {
646646
log.debug(`Additional locales file not found`, this.config.auditContext);

packages/contentstack-audit/test/unit/modules/content-types.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ describe('Content types', () => {
152152
.stub(cliux, 'confirm', async () => true)
153153
.it('should not write the file', async () => {
154154
const ctInstance = new ContentType({ ...constructorParam, fix: true });
155+
(ctInstance as any).schema = [constructorParam.ctSchema[0]];
155156
const fsSpy = sinon.spy(fs, 'writeFileSync');
156157
await ctInstance.writeFixContent();
157158
expect(fsSpy.callCount).to.be.equals(1);

packages/contentstack-audit/test/unit/modules/field-rules.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ describe('Field Rules', () => {
147147
.stub(cliux, 'confirm', async () => true)
148148
.it('should not write the file', async () => {
149149
const ctInstance = new FieldRule({ ...constructorParam, fix: true });
150+
(ctInstance as any).schema = [constructorParam.ctSchema[0]];
150151
const fsSpy = sinon.spy(fs, 'writeFileSync');
151152
await ctInstance.writeFixContent();
152153
expect(fsSpy.callCount).to.be.equals(1);

0 commit comments

Comments
 (0)