Skip to content

Commit

Permalink
Merge pull request #1170 from contentstack/fix/write-csv-fix
Browse files Browse the repository at this point in the history
Fix: Test case fix, Write csv fix
  • Loading branch information
antonyagustine authored Nov 21, 2023
2 parents 3fbc1cf + d6803a2 commit f56f374
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
13 changes: 6 additions & 7 deletions packages/contentstack-audit/src/audit-base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,11 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
* @returns The function `prepareCSV` returns a Promise that resolves to `void`.
*/
prepareCSV(moduleName: keyof typeof config.moduleConfig, listOfMissingRefs: Record<string, any>): Promise<void> {
const csvStream = csv.format({ headers: true });
const csvPath = join(this.sharedConfig.reportPath, `${moduleName}.csv`);
const assetFileStream = createWriteStream(csvPath);

return new Promise<void>((resolve, reject) => {
assetFileStream.on('error', reject);
csvStream.pipe(assetFileStream).on('close', resolve).on('error', reject);
// file deepcode ignore MissingClose: Will auto close once csv stream end
const ws = createWriteStream(csvPath).on('error', reject);
const defaultColumns = Object.keys(OutputColumn);
const userDefinedColumns = this.sharedConfig.flags.columns ? this.sharedConfig.flags.columns.split(',') : null;
let missingRefs: RefErrorReturnType[] = Object.values(listOfMissingRefs).flat();
Expand All @@ -301,6 +299,8 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
missingRefs = missingRefs.filter((row: RefErrorReturnType) => row[OutputColumn[column]] === value);
}

const rowData: Record<string, string | string[]>[] = [];

for (const issue of missingRefs) {
let row: Record<string, string | string[]> = {};

Expand All @@ -313,11 +313,10 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
row['Fix status'] = row.fixStatus;
}

csvStream.write(row, 'utf8');
rowData.push(row);
}

csvStream.end();
assetFileStream.destroy();
csv.write(rowData, { headers: true }).pipe(ws).on('error', reject).on('finish', resolve);
});
}
}
2 changes: 2 additions & 0 deletions packages/contentstack-audit/test/unit/commands/fix.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs';
import winston from 'winston';
import { expect } from '@oclif/test';
import { fancy } from '@contentstack/cli-dev-dependencies';
Expand All @@ -13,6 +14,7 @@ describe('AuditFix command', () => {
describe('AuditFix run method', () => {
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(fs, 'rmSync', () => {})
.stub(winston.transports, 'File', () => fsTransport)
.stub(winston, 'createLogger', () => ({ log: () => {}, error: () => {} }))
.stub(AuditBaseCommand.prototype, 'start', () => {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ describe('Content types', () => {
});

fancy
.stub(fs, 'rmSync', () => {})
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(ContentType.prototype, 'writeFixContent', async () => {})
.it('perform audit operation on the given CT schema', async () => {
Expand All @@ -120,6 +121,7 @@ describe('Content types', () => {
});

fancy
.stub(fs, 'rmSync', () => {})
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(ContentType.prototype, 'writeFixContent', async () => {})
.it('perform audit and fix operation on the given CT schema', async () => {
Expand Down Expand Up @@ -274,6 +276,7 @@ describe('Content types', () => {
describe('fixGlobalFieldReferences method', () => {
fancy
.stdout({ print: process.env.PRINT === 'true' || false })
.stub(fs, 'rmSync', () => {})
.stub(ContentType.prototype, 'runFixOnSchema', () => {})
.stub(ContentType.prototype, 'lookForReference', () => {})
.it('should identify missing global-field schema and attach with content-type schema', async () => {
Expand Down

0 comments on commit f56f374

Please sign in to comment.