@stryker-mutator/core
Version:
The extendable JavaScript mutation testing framework
76 lines • 2.52 kB
JavaScript
import { factory } from '@stryker-mutator/test-helpers';
import { createDirent } from './producers.js';
/**
* A test double for the file system.
* Only supports sync, in-memory, text files.
*/
export class FileSystemTestDouble {
constructor(files = Object.create(null)) {
this.files = files;
this.dirs = new Set();
}
async dispose() {
// Idle, nothing to do here
}
async readFile(fileName) {
if (typeof fileName !== 'string') {
this.throwNotSupported();
}
const file = this.files[fileName];
if (file === undefined) {
throw factory.fileNotFoundError();
}
return file;
}
async copyFile(src, dest) {
if (typeof src !== 'string' || typeof dest !== 'string') {
this.throwNotSupported();
}
if (this.files[src] === undefined) {
throw factory.fileNotFoundError();
}
this.files[dest] = this.files[src];
}
async writeFile(name, data) {
if (typeof name !== 'string' || typeof data !== 'string') {
this.throwNotSupported();
}
this.files[name] = data;
}
async mkdir(path) {
this.dirs.add(path.toString());
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async readdir(path, options) {
if (!(options === null || options === void 0 ? void 0 : options.withFileTypes)) {
this.throwNotSupported();
}
const dirName = String(path);
const dirents = Object.keys(this.files)
.filter((file) => file.startsWith(dirName))
.map((fileName) => {
const filePath = fileName
.substring(dirName.length)
.split(/[\/\\]/)
.filter(Boolean);
const name = filePath[0];
const isDirectory = filePath.length > 1;
return createDirent({ name, isDirectory });
});
return dirents;
}
throwNotSupported() {
throw new Error('Not supported');
}
/**
* Creates file descriptions for each file in this test double
*/
toFileDescriptions(mutate = true) {
return Object.keys(this.files).reduce((files, fileName) => {
files[fileName] = { mutate };
return files;
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
}, Object.create(null));
}
}
//# sourceMappingURL=file-system-test-double.js.map