Skip to content

Commit

Permalink
Chore: increase test coverage (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm authored Sep 11, 2023
1 parent b2f7448 commit 8935a0a
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 0 deletions.
16 changes: 16 additions & 0 deletions test/igir.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,22 @@ describe('with inferred DATs', () => {
});
});

it('should move to the same directory', async () => {
await copyFixturesToTemp(async (inputTemp, outputTemp) => {
const inputDir = path.join(inputTemp, 'roms', 'raw');
const inputBefore = await walkWithCrc(inputDir, inputDir);

await runIgir({
commands: ['move', 'test'],
input: [inputDir],
output: inputDir,
});

await expect(walkWithCrc(inputDir, inputDir)).resolves.toEqual(inputBefore);
await expect(walkWithCrc(inputTemp, outputTemp)).resolves.toHaveLength(0);
});
});

it('should move, extract, and test', async () => {
await copyFixturesToTemp(async (inputTemp, outputTemp) => {
const result = await runIgir({
Expand Down
12 changes: 12 additions & 0 deletions test/modules/candidateWriter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,18 @@ describe('zip', () => {
});
});

it('should not move if tested zip has wrong number of entries', () => {
// TODO(cemmer)
});

it('should not move if tested zip is missing an entry', () => {
// TODO(cemmer)
});

it('should not move if tested zip has an entry with an unexpected checksum', () => {
// TODO(cemmer)
});

test.each([
// Control group of un-headered files
['raw/empty.rom', 'empty.rom', '00000000'],
Expand Down
28 changes: 28 additions & 0 deletions test/polyfill/filePoly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,31 @@ describe('fileOfSize', () => {
}
});
});

describe('readAt', () => {
it('should read a small file', async () => {
const tempFile = await fsPoly.mktemp(path.join(Constants.GLOBAL_TEMP_DIR, 'file'));
await expect(fsPoly.exists(tempFile)).resolves.toEqual(false);

try {
const file = await filePoly.fileOfSize(tempFile, 'r', Constants.MAX_MEMORY_FILE_SIZE - 1);
await expect(file.readAt(0, 16)).resolves.toEqual(Buffer.alloc(16));
await file.close();
} finally {
await fsPoly.rm(tempFile);
}
});

it('should read a large file', async () => {
const tempFile = await fsPoly.mktemp(path.join(Constants.GLOBAL_TEMP_DIR, 'file'));
await expect(fsPoly.exists(tempFile)).resolves.toEqual(false);

try {
const file = await filePoly.fileOfSize(tempFile, 'r', Constants.MAX_MEMORY_FILE_SIZE + 1);
await expect(file.readAt(0, 16)).resolves.toEqual(Buffer.alloc(16));
await file.close();
} finally {
await fsPoly.rm(tempFile);
}
});
});
75 changes: 75 additions & 0 deletions test/polyfill/fsPoly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,79 @@ import path from 'path';
import Constants from '../../src/constants.js';
import fsPoly from '../../src/polyfill/fsPoly.js';

describe('canSymlink', () => {
it('should not throw', async () => {
await expect(fsPoly.canSymlink(Constants.GLOBAL_TEMP_DIR)).resolves.not.toThrow();
});
});

describe('isDirectory', () => {
it('should return true for a directory', async () => {
const tempDir = await fsPoly.mkdtemp(Constants.GLOBAL_TEMP_DIR);
await expect(fsPoly.isDirectory(tempDir)).resolves.toEqual(true);
});

it('should return false for a file', async () => {
const tempFile = await fsPoly.mktemp(path.join(Constants.GLOBAL_TEMP_DIR, 'temp'));
await fsPoly.touch(tempFile);
await expect(fsPoly.isDirectory(tempFile)).resolves.toEqual(false);
await fsPoly.rm(tempFile);
});

it('should return false for non-existent file', async () => {
const tempFile = await fsPoly.mktemp(path.join(Constants.GLOBAL_TEMP_DIR, 'temp'));
await expect(fsPoly.isDirectory(tempFile)).resolves.toEqual(false);
});
});

describe('isDirectorySync', () => {
it('should return true for a directory', async () => {
const tempDir = await fsPoly.mkdtemp(Constants.GLOBAL_TEMP_DIR);
expect(fsPoly.isDirectorySync(tempDir)).toEqual(true);
});

it('should return false for a file', async () => {
const tempFile = await fsPoly.mktemp(path.join(Constants.GLOBAL_TEMP_DIR, 'temp'));
await fsPoly.touch(tempFile);
expect(fsPoly.isDirectorySync(tempFile)).toEqual(false);
await fsPoly.rm(tempFile);
});

it('should return false for non-existent file', async () => {
const tempFile = await fsPoly.mktemp(path.join(Constants.GLOBAL_TEMP_DIR, 'temp'));
expect(fsPoly.isDirectorySync(tempFile)).toEqual(false);
});
});

describe('isSymlink', () => {
it('should return true for a symlink', async () => {
const tempFile = await fsPoly.mktemp(path.join(Constants.GLOBAL_TEMP_DIR, 'temp'));
await fsPoly.touch(tempFile);
const tempLink = await fsPoly.mktemp(path.join(Constants.GLOBAL_TEMP_DIR, 'link'));
await fsPoly.symlink(tempFile, tempLink);
await expect(fsPoly.isSymlink(tempLink)).resolves.toEqual(true);
await fsPoly.rm(tempLink);
await fsPoly.rm(tempFile);
});

it('should return false for a plain directory', async () => {
const tempDir = await fsPoly.mkdtemp(Constants.GLOBAL_TEMP_DIR);
await expect(fsPoly.isSymlink(tempDir)).resolves.toEqual(false);
});

it('should return false for a plain file', async () => {
const tempFile = await fsPoly.mktemp(path.join(Constants.GLOBAL_TEMP_DIR, 'temp'));
await fsPoly.touch(tempFile);
await expect(fsPoly.isSymlink(tempFile)).resolves.toEqual(false);
await fsPoly.rm(tempFile);
});

it('should return false for non-existent file', async () => {
const tempFile = await fsPoly.mktemp(path.join(Constants.GLOBAL_TEMP_DIR, 'temp'));
await expect(fsPoly.isSymlink(tempFile)).resolves.toEqual(false);
});
});

describe('makeLegal', () => {
describe('unix', () => {
test.each([
Expand Down Expand Up @@ -70,6 +143,7 @@ describe('rm', () => {
await fsPoly.rm(tempLink);
await expect(fsPoly.exists(tempLink)).resolves.toEqual(false);
await expect(fsPoly.exists(tempFile)).resolves.toEqual(true);
await fsPoly.rm(tempFile);
});
});

Expand Down Expand Up @@ -110,5 +184,6 @@ describe('rmSync', () => {
fsPoly.rmSync(tempLink);
await expect(fsPoly.exists(tempLink)).resolves.toEqual(false);
await expect(fsPoly.exists(tempFile)).resolves.toEqual(true);
fsPoly.rmSync(tempFile);
});
});
18 changes: 18 additions & 0 deletions test/types/files/archives/archiveEntry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import fsPoly from '../../../../src/polyfill/fsPoly.js';
import ArchiveEntry from '../../../../src/types/files/archives/archiveEntry.js';
import SevenZip from '../../../../src/types/files/archives/sevenZip.js';
import Zip from '../../../../src/types/files/archives/zip.js';
import File from '../../../../src/types/files/file.js';
import FileFactory from '../../../../src/types/files/fileFactory.js';
import ROMHeader from '../../../../src/types/files/romHeader.js';
import Options from '../../../../src/types/options.js';
import IPSPatch from '../../../../src/types/patches/ipsPatch.js';
import ProgressBarFake from '../../../console/progressBarFake.js';

describe('getEntryPath', () => {
Expand Down Expand Up @@ -157,6 +159,22 @@ describe('createReadStream', () => {
});
});

describe('withPatch', () => {
it('should attach a matching patch', async () => {
const entry = await ArchiveEntry.entryOf(new Zip('file.zip'), 'entry.rom', 0, '00000000');
const patch = IPSPatch.patchFrom(await File.fileOf('patch 00000000.ips'));
const patchedEntry = entry.withPatch(patch);
expect(patchedEntry.getPatch()).toEqual(patch);
});

it('should not attach a non-matching patch', async () => {
const entry = await ArchiveEntry.entryOf(new Zip('file.zip'), 'entry.rom', 0, 'FFFFFFFF');
const patch = IPSPatch.patchFrom(await File.fileOf('patch 00000000.ips'));
const patchedEntry = entry.withPatch(patch);
expect(patchedEntry.getPatch()).toBeUndefined();
});
});

describe('equals', () => {
it('should equal itself', async () => {
const entry = await ArchiveEntry.entryOf(new Zip('file.zip'), 'entry.rom', 0, '00000000');
Expand Down
28 changes: 28 additions & 0 deletions test/types/files/file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import fsPoly from '../../../src/polyfill/fsPoly.js';
import File from '../../../src/types/files/file.js';
import ROMHeader from '../../../src/types/files/romHeader.js';
import Options from '../../../src/types/options.js';
import IPSPatch from '../../../src/types/patches/ipsPatch.js';
import ProgressBarFake from '../../console/progressBarFake.js';

describe('getFilePath', () => {
Expand Down Expand Up @@ -68,6 +69,17 @@ describe('getCrc32WithoutHeader', () => {
});

describe('getSymlinkSourceResolved', () => {
it('should not resolve non-symlinks', async () => {
const tempDir = await fsPoly.mkdtemp(Constants.GLOBAL_TEMP_DIR);
try {
const tempFile = path.resolve(await fsPoly.mktemp(path.join(tempDir, 'file')));
const fileLink = await File.fileOf(tempFile);
expect(fileLink.getSymlinkSourceResolved()).toBeUndefined();
} finally {
await fsPoly.rm(tempDir, { recursive: true });
}
});

it('should resolve absolute symlinks', async () => {
const tempDir = await fsPoly.mkdtemp(Constants.GLOBAL_TEMP_DIR);
try {
Expand Down Expand Up @@ -143,6 +155,22 @@ describe('createReadStream', () => {
});
});

describe('withPatch', () => {
it('should attach a matching patch', async () => {
const file = await File.fileOf('file.rom', 0, '00000000');
const patch = IPSPatch.patchFrom(await File.fileOf('patch 00000000.ips'));
const patchedFile = file.withPatch(patch);
expect(patchedFile.getPatch()).toEqual(patch);
});

it('should not attach a non-matching patch', async () => {
const file = await File.fileOf('file.rom', 0, 'FFFFFFFF');
const patch = IPSPatch.patchFrom(await File.fileOf('patch 00000000.ips'));
const patchedFile = file.withPatch(patch);
expect(patchedFile.getPatch()).toBeUndefined();
});
});

describe('equals', () => {
it('should equal itself', async () => {
const file = await File.fileOf('file.rom', 0, '00000000');
Expand Down

0 comments on commit 8935a0a

Please sign in to comment.