Skip to content

Commit

Permalink
chore(utils): fix ESLint issues primarily in test files, enable node-…
Browse files Browse the repository at this point in the history
…protocol rule
  • Loading branch information
Tlacenka committed Jan 10, 2024
1 parent a2ab7fb commit c0cb5ea
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 59 deletions.
9 changes: 3 additions & 6 deletions packages/utils/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,29 @@
"@typescript-eslint/naming-convention": "off", // 8 warnings
"@typescript-eslint/no-floating-promises": "off", // 2 errors
"@typescript-eslint/no-shadow": "off", // 8 warnings
"@typescript-eslint/no-unnecessary-condition": "off", // 25 warnings
"@typescript-eslint/no-unnecessary-condition": "off", // 18 warnings
"@typescript-eslint/no-unnecessary-type-assertion": "off", // 1 error
"@typescript-eslint/no-unsafe-argument": "off", // 1 error
"@typescript-eslint/no-unsafe-assignment": "off", // 15 errors
"@typescript-eslint/no-unsafe-call": "off", // 8 errors
"@typescript-eslint/no-unsafe-call": "off", // 2 errors
"@typescript-eslint/no-unsafe-member-access": "off", // 12 errors
"@typescript-eslint/no-unsafe-return": "off", // 13 errors
"@typescript-eslint/prefer-nullish-coalescing": "off", // 11 warnings
"@typescript-eslint/require-await": "off", // 23 warnings
"@typescript-eslint/restrict-plus-operands": "off", // 1 error
"@typescript-eslint/restrict-template-expressions": "off", // 2 errors
"functional/immutable-data": "off", // 22 errors, 4 warnings
"functional/immutable-data": "off", // 25 errors
"functional/no-let": "off", // 12 warnings
"import/no-cycle": "off", // 2 errors
"import/no-named-as-default": "off", // 2 warnings
"import/no-useless-path-segments": "off", // 1 warning
"sonarjs/cognitive-complexity": "off", // 1 error
"sonarjs/no-duplicate-string": "off", // 1 warning
"unicorn/better-regex": "off", // 2 warnings
"unicorn/consistent-destructuring": "off", // 31 warnings
"unicorn/explicit-length-check": "off", // 2 warnings
"unicorn/import-style": "off", // 3 warnings
"unicorn/no-negated-condition": "off", // 1 warning
"unicorn/no-new-array": "off", // 6 warnings
"unicorn/numeric-separators-style": "off", // 12 warnings
"unicorn/prefer-node-protocol": "off", // 7 warnings
"unicorn/prefer-number-properties": "off", // 4 warnings
"unicorn/prefer-spread": "off", // 7 warnings
"unicorn/prefer-ternary": "off" // 1 warning
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/perf/crawl-file-system/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Benchmark from 'benchmark';
import { join } from 'path';
import { join } from 'node:path';
import {
CrawlFileSystemOptions,
crawlFileSystem,
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/lib/execute-process.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { spawn } from 'child_process';
import { spawn } from 'node:child_process';
import { calcDuration } from './report';

/**
Expand Down
26 changes: 13 additions & 13 deletions packages/utils/src/lib/execute-process.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ describe('executeProcess', () => {
args: ['-v'],
observer: spyObserver,
});
expect(spyObserver?.onStdout).toHaveBeenCalledTimes(1);
expect(spyObserver?.onComplete).toHaveBeenCalledTimes(1);
expect(spyObserver?.onError).toHaveBeenCalledTimes(0);
expect(processResult.stdout).toMatch(/v[0-9]{1,2}(\.[0-9]{1,2}){0,2}/);
expect(spyObserver.onStdout).toHaveBeenCalledTimes(1);
expect(spyObserver.onComplete).toHaveBeenCalledTimes(1);
expect(spyObserver.onError).toHaveBeenCalledTimes(0);
expect(processResult.stdout).toMatch(/v\d{1,2}(\.\d{1,2}){0,2}/);
});

it('should work with npx command `npx --help`', async () => {
Expand All @@ -32,9 +32,9 @@ describe('executeProcess', () => {
args: ['--help'],
observer: spyObserver,
});
expect(spyObserver?.onStdout).toHaveBeenCalledTimes(1);
expect(spyObserver?.onComplete).toHaveBeenCalledTimes(1);
expect(spyObserver?.onError).toHaveBeenCalledTimes(0);
expect(spyObserver.onStdout).toHaveBeenCalledTimes(1);
expect(spyObserver.onComplete).toHaveBeenCalledTimes(1);
expect(spyObserver.onError).toHaveBeenCalledTimes(0);
expect(processResult.stdout).toContain('npm exec');
});

Expand All @@ -46,9 +46,9 @@ describe('executeProcess', () => {

expect(errorSpy).toHaveBeenCalledTimes(0);
expect(processResult.stdout).toContain('process:complete');
expect(spyObserver?.onStdout).toHaveBeenCalledTimes(6); // intro + 4 runs + complete
expect(spyObserver?.onError).toHaveBeenCalledTimes(0);
expect(spyObserver?.onComplete).toHaveBeenCalledTimes(1);
expect(spyObserver.onStdout).toHaveBeenCalledTimes(6); // intro + 4 runs + complete
expect(spyObserver.onError).toHaveBeenCalledTimes(0);
expect(spyObserver.onComplete).toHaveBeenCalledTimes(1);
});

it('should work with async script `node custom-script.js` that throws an error', async () => {
Expand All @@ -63,8 +63,8 @@ describe('executeProcess', () => {

expect(errorSpy).toHaveBeenCalledTimes(1);
expect(processResult).toBeUndefined();
expect(spyObserver?.onStdout).toHaveBeenCalledTimes(2); // intro + 1 run before error
expect(spyObserver?.onError).toHaveBeenCalledTimes(1);
expect(spyObserver?.onComplete).toHaveBeenCalledTimes(0);
expect(spyObserver.onStdout).toHaveBeenCalledTimes(2); // intro + 1 run before error
expect(spyObserver.onError).toHaveBeenCalledTimes(1);
expect(spyObserver.onComplete).toHaveBeenCalledTimes(0);
});
});
4 changes: 2 additions & 2 deletions packages/utils/src/lib/file-system.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type Options, bundleRequire } from 'bundle-require';
import chalk from 'chalk';
import { mkdir, readFile, readdir, stat } from 'fs/promises';
import { join } from 'path';
import { mkdir, readFile, readdir, stat } from 'node:fs/promises';
import { join } from 'node:path';
import { formatBytes } from './formatting';
import { logMultipleResults } from './log-results';

Expand Down
6 changes: 3 additions & 3 deletions packages/utils/src/lib/file-system.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { stat } from 'fs/promises';
import { vol } from 'memfs';
import { join } from 'path';
import { stat } from 'node:fs/promises';
import { join } from 'node:path';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { MEMFS_VOLUME } from '@code-pushup/testing-utils';
import {
Expand Down Expand Up @@ -34,7 +34,7 @@ describe('logMultipleFileResults', () => {
const persistResult = [
{
status: 'fulfilled',
value: ['out.json', 10000],
value: ['out.json', 10_000],
} as PromiseFulfilledResult<FileResult>,
];
const messagePrefix = 'Generated reports';
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/lib/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function slugify(text: string): string {
.trim()
.toLowerCase()
.replace(/\s+|\//g, '-')
.replace(/[^a-z0-9-]/g, '');
.replace(/[^a-z\d-]/g, '');
}

export function pluralize(text: string): string {
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/lib/formatting.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('formatBytes', () => {
});

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

Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/lib/git.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('getLatestCommit', () => {
it('should log latest commit', async () => {
await expect(getLatestCommit()).resolves.toEqual(
expect.objectContaining({
hash: expect.stringMatching(/^[0-9a-f]{40}$/),
hash: expect.stringMatching(/^[\da-f]{40}$/),
message: expect.stringMatching(/.+/),
author: expect.stringMatching(/.+/),
date: expect.stringMatching(gitCommitDateRegex),
Expand Down
10 changes: 5 additions & 5 deletions packages/utils/src/lib/log-results.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('logMultipleResults', () => {
const succeededCallbackMock = vi.fn();
const failedCallbackMock = vi.fn();

it('should call logPromiseResults with successfull plugin result', async () => {
it('should call logPromiseResults with successfull plugin result', () => {
logMultipleResults(
[
{
Expand All @@ -23,7 +23,7 @@ describe('logMultipleResults', () => {
expect(failedCallbackMock).not.toHaveBeenCalled();
});

it('should call logPromiseResults with failed plugin result', async () => {
it('should call logPromiseResults with failed plugin result', () => {
logMultipleResults(
[{ status: 'rejected', reason: 'fail' } as PromiseRejectedResult],
'Generated reports',
Expand All @@ -35,7 +35,7 @@ describe('logMultipleResults', () => {
expect(succeededCallbackMock).not.toHaveBeenCalled();
});

it('should call logPromiseResults twice', async () => {
it('should call logPromiseResults twice', () => {
logMultipleResults(
[
{
Expand All @@ -55,7 +55,7 @@ describe('logMultipleResults', () => {
});

describe('logPromiseResults', () => {
it('should log on success', async () => {
it('should log on success', () => {
logPromiseResults(
[
{
Expand All @@ -76,7 +76,7 @@ describe('logPromiseResults', () => {
expect(console.info).toHaveBeenNthCalledWith(2, ['out.json']);
});

it('should log on fail', async () => {
it('should log on fail', () => {
logPromiseResults(
[{ status: 'rejected', reason: 'fail' } as PromiseRejectedResult],
'Generated reports failed:',
Expand Down
24 changes: 12 additions & 12 deletions packages/utils/src/lib/progress.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ describe('getSingletonMultiProgressBars', () => {

const taskAName = 'a';
const bars = getSingletonProgressBars({ progressWidth: 1 });
const tasks = bars?.['tasks'];
const progressBuffer = bars?.['logger'].progressBuffer;
const tasks = bars['tasks'];
const progressBuffer = bars['logger'].progressBuffer;

describe('getProgressBar', () => {
it('should init task', async () => {
it('should init task', () => {
expect(bars.getIndex(taskAName)).toBeUndefined();
getProgressBar(taskAName);
expect(bars.getIndex(taskAName)).toBe(0);
expect(progressBuffer[0].trim()).toBe(
expect(progressBuffer[0]!.trim()).toBe(
`${taskAName}: ${barStyles.idle(' ')} 0% |`,
);

Expand All @@ -44,45 +44,45 @@ describe('getProgressBar', () => {
expect(bars.getIndex(taskAName)).toBeUndefined();
});

it('should update task message', async () => {
it('should update task message', () => {
const taskA = getProgressBar(taskAName);
expect(tasks[taskAName].message).toBe('');

taskA.updateTitle('0');
expect(tasks[taskAName].message).toBe('0');
expect(progressBuffer[0].trim()).toBe(
expect(progressBuffer[0]!.trim()).toBe(
`${taskAName}: ${barStyles.active(' ')} 0% | 0`,
);
taskA.updateTitle('1');
expect(tasks[taskAName].message).toBe('1');
expect(progressBuffer[0].trim()).toBe(
expect(progressBuffer[0]!.trim()).toBe(
`${taskAName}: ${barStyles.active(' ')} 0% | 1`,
);

bars.removeTask(taskAName);
});

it('should update task progress', async () => {
it('should update task progress', () => {
const taskA = getProgressBar(taskAName);
taskA.incrementInSteps(2);
expect(tasks[taskAName].message).toBe('');
expect(progressBuffer[0].trim()).toBe(
expect(progressBuffer[0]!.trim()).toBe(
`${taskAName}: ${barStyles.active('▌')} 50% |`,
);
taskA.incrementInSteps(2);
expect(tasks[taskAName].message).toBe('');
expect(progressBuffer[0].trim()).toBe(
expect(progressBuffer[0]!.trim()).toBe(
`${taskAName}: ${barStyles.active('█')} 100% |`,
);
bars.removeTask(taskAName);
});

it('should end task when endProgress is called', async () => {
it('should end task when endProgress is called', () => {
const taskA = getProgressBar(taskAName);
expect(bars.isDone(taskAName)).toBe(false);
taskA.endProgress();
expect(bars.isDone(taskAName)).toBe(true);
expect(progressBuffer[0].trim()).toBe(
expect(progressBuffer[0]!.trim()).toBe(
`${taskAName}: ${barStyles.done('█')} 100% |`,
);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/lib/report.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join } from 'path';
import { join } from 'node:path';
import {
CategoryRef,
IssueSeverity as CliIssueSeverity,
Expand Down
17 changes: 6 additions & 11 deletions packages/utils/src/lib/report.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { vol } from 'memfs';
import { describe, expect, it } from 'vitest';
import { Issue, IssueSeverity, PluginReport } from '@code-pushup/models';
import { Issue, IssueSeverity, Report } from '@code-pushup/models';
import { reportMock } from '@code-pushup/models/testing';
import { MEMFS_VOLUME } from '@code-pushup/testing-utils';
import { MEMFS_VOLUME, REPORT_MOCK } from '@code-pushup/testing-utils';
import {
calcDuration,
compareIssueSeverity,
Expand Down Expand Up @@ -102,17 +102,12 @@ describe('loadReport', () => {
});

it('should throw for an invalid JSON report', async () => {
const invalidReportMock = reportMock();
invalidReportMock.plugins = [
{
...invalidReportMock.plugins[0],
slug: '-Invalud_slug',
} as unknown as PluginReport,
];

vol.fromJSON(
{
[`report.json`]: JSON.stringify(invalidReportMock),
[`report.json`]: JSON.stringify({
...REPORT_MOCK,
plugins: [{ ...REPORT_MOCK.plugins[0]!, slug: '-Invalid_slug' }],
} satisfies Report),
},
MEMFS_VOLUME,
);
Expand Down
2 changes: 1 addition & 1 deletion testing-utils/src/lib/fixtures/report.mock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Report } from '../../../../packages/models/src';
import type { Report } from '../../../../packages/models/src';

export const MINIMAL_REPORT_MOCK = {
packageName: '@code-pushup/core',
Expand Down

0 comments on commit c0cb5ea

Please sign in to comment.