From e332503fe6407d7210bd8916e481d36ebe3cd3ac Mon Sep 17 00:00:00 2001 From: Christian Emmer <10749361+emmercm@users.noreply.github.com> Date: Sun, 25 Jun 2023 21:21:28 -0700 Subject: [PATCH] Chore: increase test coverage (#465) --- .github/workflows/codecov.yml | 2 +- ..._template__of_JavaScriptTestRunnerJest.xml | 1 - src/console/progressBarCLI.ts | 2 +- src/console/singleBarFormatted.ts | 2 +- src/modules/patchCandidateGenerator.ts | 1 + test/console/singleBarFormatted.test.ts | 55 ++++++++++--------- test/modules/candidateGenerator.test.ts | 1 + test/modules/movedRomDeleter.test.ts | 20 ++++++- test/polyfill/filePoly.test.ts | 8 ++- 9 files changed, 58 insertions(+), 34 deletions(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index cd3a6d17b..a92dc0c49 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -39,7 +39,7 @@ jobs: - run: npm ci # Run test coverage - - run: npm run test:coverage || npm run test:coverage -- --bail + - run: npm run test:coverage - uses: codecov/codecov-action@v3 with: token: ${{ secrets.CODECOV_TOKEN }} diff --git a/.idea/runConfigurations/_template__of_JavaScriptTestRunnerJest.xml b/.idea/runConfigurations/_template__of_JavaScriptTestRunnerJest.xml index 5742f1442..b106d3f2a 100644 --- a/.idea/runConfigurations/_template__of_JavaScriptTestRunnerJest.xml +++ b/.idea/runConfigurations/_template__of_JavaScriptTestRunnerJest.xml @@ -2,7 +2,6 @@ - diff --git a/src/console/progressBarCLI.ts b/src/console/progressBarCLI.ts index 0b30b54ab..cadb429f9 100644 --- a/src/console/progressBarCLI.ts +++ b/src/console/progressBarCLI.ts @@ -178,7 +178,7 @@ export default class ProgressBarCLI extends ProgressBar { } async incrementProgress(): Promise { - this.payload.inProgress = (this.payload.inProgress || 0) + 1; + this.payload.inProgress = Math.max(this.payload.inProgress || 0, 0) + 1; return this.render(); } diff --git a/src/console/singleBarFormatted.ts b/src/console/singleBarFormatted.ts index 409e2278f..6e86c6cdd 100644 --- a/src/console/singleBarFormatted.ts +++ b/src/console/singleBarFormatted.ts @@ -116,7 +116,7 @@ export default class SingleBarFormatted { const barSize = options.barsize || 0; const completeSize = Math.floor(params.progress * barSize); const inProgressSize = params.total > 0 - ? Math.ceil((payload.inProgress || 0) / params.total) + ? Math.ceil((Math.max(payload.inProgress || 0, 0)) / params.total) : 0; const incompleteSize = barSize - inProgressSize - completeSize; diff --git a/src/modules/patchCandidateGenerator.ts b/src/modules/patchCandidateGenerator.ts index bceb242bb..971bbeeb1 100644 --- a/src/modules/patchCandidateGenerator.ts +++ b/src/modules/patchCandidateGenerator.ts @@ -176,6 +176,7 @@ export default class PatchCandidateGenerator extends Module { let patchedRelease; const unpatchedRelease = unpatchedReleaseCandidate.getRelease(); if (unpatchedRelease) { + // If the original ROM has release info, continue to use that patchedRelease = new Release( patchedRomName, unpatchedRelease.getRegion(), diff --git a/test/console/singleBarFormatted.test.ts b/test/console/singleBarFormatted.test.ts index 8c3a04265..0efdc0735 100644 --- a/test/console/singleBarFormatted.test.ts +++ b/test/console/singleBarFormatted.test.ts @@ -5,42 +5,45 @@ import ProgressBarPayload from '../../src/console/progressBarPayload.js'; import SingleBarFormatted from '../../src/console/singleBarFormatted.js'; import ProgressBarCLISpy from './progressBarCLISpy.js'; -function buildSingleBarFormatted( - spy: ProgressBarCLISpy, - initialTotal = 0, - initialPayload: ProgressBarPayload = {}, -): SingleBarFormatted { +function testSingleBarFormatted( + initialTotal: number, + initialPayload: ProgressBarPayload, + callback: (singleBarFormatted: SingleBarFormatted) => void, +): void { + const spy = new ProgressBarCLISpy(); const multiBar = new cliProgress.MultiBar({ stream: spy.getLogger().getStream(), fps: Number.MAX_SAFE_INTEGER, + noTTYOutput: true, }); - return new SingleBarFormatted(multiBar, initialTotal, initialPayload); + + const singleBarFormatted = new SingleBarFormatted(multiBar, initialTotal, initialPayload); + callback(singleBarFormatted); + + multiBar.stop(); } describe('getSingleBar', () => { it('should return a SingleBar', () => { - const spy = new ProgressBarCLISpy(); - const singleBarFormatted = buildSingleBarFormatted(spy); - - expect(singleBarFormatted.getSingleBar()).toBeDefined(); + testSingleBarFormatted(0, {}, (singleBarFormatted) => { + expect(singleBarFormatted.getSingleBar()).toBeDefined(); + }); }); }); describe('getLastOutput', () => { it('should be empty before render', () => { - const spy = new ProgressBarCLISpy(); - const singleBarFormatted = buildSingleBarFormatted(spy); - - expect(singleBarFormatted.getLastOutput()).toEqual(''); + testSingleBarFormatted(0, {}, (singleBarFormatted) => { + expect(singleBarFormatted.getLastOutput()).toEqual(''); + }); }); it('should be non-empty after render', () => { - const spy = new ProgressBarCLISpy(); - const singleBarFormatted = buildSingleBarFormatted(spy); - - singleBarFormatted.getSingleBar().render(); + testSingleBarFormatted(100, {}, (singleBarFormatted) => { + singleBarFormatted.getSingleBar().render(); - expect(singleBarFormatted.getLastOutput()).toEqual('| ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ | 0/100'); + expect(singleBarFormatted.getLastOutput()).toEqual('| ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ | 0/100'); + }); }); }); @@ -50,16 +53,16 @@ describe('format', () => { [{ symbol: '@' }, '@ | ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ | 1/100'], [{ symbol: '@', name: 'name' }, '@ name ························· | ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ | 1/100'], [{ name: 'name' }, 'name ························· | ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ | 1/100'], + [{ name: 'name', waitingMessage: 'waiting' }, 'name ························· | ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ | 1/100 | waiting'], [{ name: 'name', finishedMessage: 'done' }, 'name ························· | done'], [{ name: 'name', finishedMessage: 'done', waitingMessage: 'waiting' }, 'name ························· | done'], ] satisfies [ProgressBarPayload, string][])('should: %s', (payload, expected) => { - const spy = new ProgressBarCLISpy(); - const singleBarFormatted = buildSingleBarFormatted(spy); - - singleBarFormatted.getSingleBar().increment(); - singleBarFormatted.getSingleBar().update(payload); - singleBarFormatted.getSingleBar().render(); + testSingleBarFormatted(100, {}, (singleBarFormatted) => { + singleBarFormatted.getSingleBar().increment(); + singleBarFormatted.getSingleBar().update(payload); + singleBarFormatted.getSingleBar().render(); - expect(stripAnsi(singleBarFormatted.getLastOutput())).toEqual(expected); + expect(stripAnsi(singleBarFormatted.getLastOutput())).toEqual(expected); + }); }); }); diff --git a/test/modules/candidateGenerator.test.ts b/test/modules/candidateGenerator.test.ts index af70fe7a1..cc7d86541 100644 --- a/test/modules/candidateGenerator.test.ts +++ b/test/modules/candidateGenerator.test.ts @@ -31,6 +31,7 @@ const gameWithOneRom = new Game({ }); const gameWithTwoRoms = new Game({ name: 'game with two ROMs', + release: new Release('game with two ROMs', 'WORLD'), rom: [ new ROM('two.a', 2, 'abcdef90'), new ROM('two.b', 3, '09876543'), diff --git a/test/modules/movedRomDeleter.test.ts b/test/modules/movedRomDeleter.test.ts index fefbe8234..057a7c289 100644 --- a/test/modules/movedRomDeleter.test.ts +++ b/test/modules/movedRomDeleter.test.ts @@ -1,3 +1,19 @@ -it('should be tested', () => { - // TODO(cemmer) +import MovedROMDeleter from '../../src/modules/movedRomDeleter.js'; +import ROMScanner from '../../src/modules/romScanner.js'; +import fsPoly from '../../src/polyfill/fsPoly.js'; +import Options from '../../src/types/options.js'; +import ProgressBarFake from '../console/progressBarFake.js'; + +it('should do nothing if no ROMs moved', async () => { + const romFiles = await new ROMScanner(new Options({ + input: ['./test/fixtures/roms'], + }), new ProgressBarFake()).scan(); + expect(romFiles.length).toBeGreaterThan(0); + + await new MovedROMDeleter(new ProgressBarFake()).delete(romFiles, [], new Map()); + + const exists = Promise.all(romFiles.map(async (romFile) => fsPoly.exists(romFile.getFilePath()))); + expect(exists).not.toContain(false); }); + +// TODO(cemmer): more meaningful tests diff --git a/test/polyfill/filePoly.test.ts b/test/polyfill/filePoly.test.ts index 9d7f0b70b..13aab2d22 100644 --- a/test/polyfill/filePoly.test.ts +++ b/test/polyfill/filePoly.test.ts @@ -13,7 +13,9 @@ describe('fileOfSize', () => { await expect(fsPoly.exists(tempFile)).resolves.toEqual(true); try { - await (await filePoly.fileOfSize(tempFile, 'r', size)).close(); + const file = await filePoly.fileOfSize(tempFile, 'r', size); + await file.close(); + expect(file.getPathLike()).toEqual(tempFile); await expect(fsPoly.size(tempFile)).resolves.toEqual(size); } finally { await fsPoly.rm(tempFile); @@ -25,7 +27,9 @@ describe('fileOfSize', () => { await expect(fsPoly.exists(tempFile)).resolves.toEqual(false); try { - await (await filePoly.fileOfSize(tempFile, 'r', size)).close(); + const file = await filePoly.fileOfSize(tempFile, 'r', size); + await file.close(); + expect(file.getPathLike()).toEqual(tempFile); await expect(fsPoly.size(tempFile)).resolves.toEqual(size); } finally { await fsPoly.rm(tempFile);