Skip to content

Commit

Permalink
feat(utils): test filesize logging
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Sep 22, 2023
1 parent a9cf842 commit ef7c8bb
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 22 deletions.
12 changes: 9 additions & 3 deletions packages/cli/src/lib/collect/command-object.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import {collect, CollectOptions, CollectOutputError, persistReport, logPersistedResults} from '@quality-metrics/utils';
import {CommandModule} from 'yargs';
import {pluginOutputSchema} from '@quality-metrics/models';
import {
collect,
CollectOptions,
CollectOutputError,
persistReport,
logPersistedResults,
} from '@quality-metrics/utils';
import { CommandModule } from 'yargs';
import { pluginOutputSchema } from '@quality-metrics/models';

export function yargsCollectCommandObject() {
const handler = async (
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export {
persistReport,
PersistDirError,
PersistError,
logPersistedResults
logPersistedResults,
} from './lib/collect/implementation/persist';
23 changes: 22 additions & 1 deletion packages/utils/src/lib/collect/implementation/persist.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { persistReport } from './persist';
import { logPersistedResults, persistReport } from './persist';
import { readFileSync, unlinkSync } from 'fs';
import { Report } from '@quality-metrics/models';
import { mockConsole, unmockConsole } from './mock/helper.mock';
Expand Down Expand Up @@ -134,3 +134,24 @@ describe('persistReport', () => {
// @TODO
});
});

describe('logPersistedResults', () => {
beforeEach(async () => {
logs = [];
mockConsole(msg => logs.push(msg));
});
afterEach(() => {
logs = [];
unmockConsole();
});

it('should log report sizes correctly`', async () => {
logPersistedResults([
{ status: 'fulfilled', value: ['out.json', 10000] },
{ status: 'rejected', reason: 'fail' },
]);
expect(logs.length).toBe(2);
expect(logs).toContain('Generated reports successfully: ');
expect(logs).toContain('- out.json (9.77 KB)');
});
});
23 changes: 14 additions & 9 deletions packages/utils/src/lib/collect/implementation/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { join } from 'path';
import { CoreConfig, Report } from '@quality-metrics/models';
import { reportToStdout } from './report-to-stdout';
import { reportToMd } from './report-to-md';
import {formatBytes} from "./utils";
import chalk from "chalk";
import { formatBytes } from './utils';
import chalk from 'chalk';

export class PersistDirError extends Error {
constructor(outputPath: string) {
Expand All @@ -21,7 +21,10 @@ export class PersistError extends Error {

export type PersistResult = PromiseSettledResult<readonly [string, number]>[];

export async function persistReport(report: Report, config: CoreConfig): Promise<PersistResult> {
export async function persistReport(
report: Report,
config: CoreConfig,
): Promise<PersistResult> {
const { persist } = config;
const outputPath = persist.outputPath;
let { format } = persist;
Expand Down Expand Up @@ -59,7 +62,7 @@ export async function persistReport(report: Report, config: CoreConfig): Promise
writeFile(reportPath, content)
// return reportPath instead of void
.then(() => {
const stats = statSync(reportPath)
const stats = statSync(reportPath);
return [reportPath, stats.size] as const;
})
.catch(e => {
Expand All @@ -72,11 +75,13 @@ export async function persistReport(report: Report, config: CoreConfig): Promise
}

export function logPersistedResults(persistResult: PersistResult) {
console.log(`Generated reports successfully: `)
for(const result of persistResult) {
if(result.status === "fulfilled") {
const [fileName, size] = result.value
console.log(`- ${chalk.bold(fileName)} (${chalk.gray(formatBytes(size))})`)
console.log(`Generated reports successfully: `);
for (const result of persistResult) {
if (result.status === 'fulfilled') {
const [fileName, size] = result.value;
console.log(
`- ${chalk.bold(fileName)} (${chalk.gray(formatBytes(size))})`,
);
}
}
}
48 changes: 48 additions & 0 deletions packages/utils/src/lib/collect/implementation/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { describe, expect, it } from 'vitest';
import { formatBytes } from './utils';

describe('formatBytes', () => {
it('should log file sizes in Bytes`', async () => {
expect(formatBytes(1000)).toBe('1000 Bytes');
});

it('should log file sizes in KB`', async () => {
expect(formatBytes(10000)).toBe('9.77 KB');
});

it('should log file sizes in MB`', async () => {
expect(formatBytes(10000000)).toBe('9.54 MB');
});

it('should log file sizes in bytes`', async () => {
expect(formatBytes(10000000000)).toBe('9.31 GB');
});

it('should log file sizes in TB`', async () => {
expect(formatBytes(10000000000000)).toBe('9.09 TB');
});

it('should log file sizes in PB`', async () => {
expect(formatBytes(10000000000000000)).toBe('8.88 PB');
});

it('should log file sizes in EB`', async () => {
expect(formatBytes(10000000000000000000)).toBe('8.67 EB');
});

it('should log file sizes in ZB`', async () => {
expect(formatBytes(10000000000000000000000)).toBe('8.47 ZB');
});

it('should log file sizes in YB`', async () => {
expect(formatBytes(10000000000000000000000000)).toBe('8.27 YB');
});

it('should log file sizes correctly with correct decimal`', async () => {
expect(formatBytes(10000, 1)).toBe('9.8 KB');
});

it('should log file sizes of 0 if no size is given`', async () => {
expect(formatBytes(0)).toBe('0 Bytes');
});
});
14 changes: 6 additions & 8 deletions packages/utils/src/lib/collect/implementation/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,14 @@ export async function readPackageJson() {
}
}


export function formatBytes(bytes: number, decimals = 2) {
if (!+bytes) return '0 Bytes'
if (!+bytes) return '0 Bytes';

const k = 1024
const dm = decimals < 0 ? 0 : decimals
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

const i = Math.floor(Math.log(bytes) / Math.log(k))
const i = Math.floor(Math.log(bytes) / Math.log(k));

return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
}

0 comments on commit ef7c8bb

Please sign in to comment.