Skip to content

Commit

Permalink
Chore: increase test coverage (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm authored Jun 26, 2023
1 parent 908e412 commit e332503
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/console/progressBarCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export default class ProgressBarCLI extends ProgressBar {
}

async incrementProgress(): Promise<void> {
this.payload.inProgress = (this.payload.inProgress || 0) + 1;
this.payload.inProgress = Math.max(this.payload.inProgress || 0, 0) + 1;
return this.render();
}

Expand Down
2 changes: 1 addition & 1 deletion src/console/singleBarFormatted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions src/modules/patchCandidateGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
55 changes: 29 additions & 26 deletions test/console/singleBarFormatted.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});

Expand All @@ -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);
});
});
});
1 change: 1 addition & 0 deletions test/modules/candidateGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
20 changes: 18 additions & 2 deletions test/modules/movedRomDeleter.test.ts
Original file line number Diff line number Diff line change
@@ -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
8 changes: 6 additions & 2 deletions test/polyfill/filePoly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit e332503

Please sign in to comment.