Merge branch 'main' into versions/4.0.0

# Conflicts:
#	test/unit/storage/accessors/FileDataAccessor.test.ts
This commit is contained in:
Joachim Van Herwegen 2022-04-15 10:28:50 +02:00
commit e6519992bf
16 changed files with 150 additions and 95 deletions

View File

@ -4,6 +4,7 @@
"files-scs:config/app/main/default.json", "files-scs:config/app/main/default.json",
"files-scs:config/app/init/default.json", "files-scs:config/app/init/default.json",
"files-scs:config/app/setup/required.json", "files-scs:config/app/setup/required.json",
"files-scs:config/app/variables/default.json",
"files-scs:config/http/handler/default.json", "files-scs:config/http/handler/default.json",
"files-scs:config/http/middleware/websockets.json", "files-scs:config/http/middleware/websockets.json",
"files-scs:config/http/server-factory/websockets.json", "files-scs:config/http/server-factory/websockets.json",

View File

@ -4,6 +4,7 @@
"files-scs:config/app/main/default.json", "files-scs:config/app/main/default.json",
"files-scs:config/app/init/default.json", "files-scs:config/app/init/default.json",
"files-scs:config/app/setup/required.json", "files-scs:config/app/setup/required.json",
"files-scs:config/app/variables/default.json",
"files-scs:config/http/handler/default.json", "files-scs:config/http/handler/default.json",
"files-scs:config/http/middleware/websockets.json", "files-scs:config/http/middleware/websockets.json",
"files-scs:config/http/server-factory/websockets.json", "files-scs:config/http/server-factory/websockets.json",

View File

@ -1,4 +1,4 @@
import { promises as fsPromises } from 'fs'; import { readJson } from 'fs-extra';
import type { RegistrationManager } from '../identity/interaction/email-password/util/RegistrationManager'; import type { RegistrationManager } from '../identity/interaction/email-password/util/RegistrationManager';
import { getLoggerFor } from '../logging/LogUtil'; import { getLoggerFor } from '../logging/LogUtil';
import { Initializer } from './Initializer'; import { Initializer } from './Initializer';
@ -23,8 +23,7 @@ export class SeededPodInitializer extends Initializer {
if (!this.configFilePath) { if (!this.configFilePath) {
return; return;
} }
const configText = await fsPromises.readFile(this.configFilePath, 'utf8'); const configuration = await readJson(this.configFilePath, 'utf8');
const configuration: NodeJS.Dict<unknown>[] = JSON.parse(configText);
let count = 0; let count = 0;
for await (const input of configuration) { for await (const input of configuration) {

View File

@ -1,5 +1,5 @@
import { mkdirSync, promises as fsPromises } from 'fs';
import type { Readable } from 'stream'; import type { Readable } from 'stream';
import { ensureDirSync, rename, unlink } from 'fs-extra';
import { v4 } from 'uuid'; import { v4 } from 'uuid';
import type { RepresentationMetadata } from '../../http/representation/RepresentationMetadata'; import type { RepresentationMetadata } from '../../http/representation/RepresentationMetadata';
import type { ResourceIdentifier } from '../../http/representation/ResourceIdentifier'; import type { ResourceIdentifier } from '../../http/representation/ResourceIdentifier';
@ -20,8 +20,7 @@ export class AtomicFileDataAccessor extends FileDataAccessor implements AtomicDa
public constructor(resourceMapper: FileIdentifierMapper, rootFilePath: string, tempFilePath: string) { public constructor(resourceMapper: FileIdentifierMapper, rootFilePath: string, tempFilePath: string) {
super(resourceMapper); super(resourceMapper);
this.tempFilePath = joinFilePath(rootFilePath, tempFilePath); this.tempFilePath = joinFilePath(rootFilePath, tempFilePath);
// Cannot use fsPromises in constructor ensureDirSync(this.tempFilePath);
mkdirSync(this.tempFilePath, { recursive: true });
} }
/** /**
@ -45,12 +44,12 @@ export class AtomicFileDataAccessor extends FileDataAccessor implements AtomicDa
await this.verifyExistingExtension(link); await this.verifyExistingExtension(link);
// When no quota errors occur move the file to its desired location // When no quota errors occur move the file to its desired location
await fsPromises.rename(tempFilePath, link.filePath); await rename(tempFilePath, link.filePath);
} catch (error: unknown) { } catch (error: unknown) {
// Delete the data already written // Delete the data already written
try { try {
if ((await this.getStats(tempFilePath)).isFile()) { if ((await this.getStats(tempFilePath)).isFile()) {
await fsPromises.unlink(tempFilePath); await unlink(tempFilePath);
} }
} catch { } catch {
throw error; throw error;

View File

@ -1,6 +1,6 @@
import type { Stats } from 'fs';
import { createWriteStream, createReadStream, promises as fsPromises } from 'fs';
import type { Readable } from 'stream'; import type { Readable } from 'stream';
import type { Stats } from 'fs-extra';
import { ensureDir, remove, stat, lstat, createWriteStream, createReadStream, opendir } from 'fs-extra';
import type { Quad } from 'rdf-js'; import type { Quad } from 'rdf-js';
import type { Representation } from '../../http/representation/Representation'; import type { Representation } from '../../http/representation/Representation';
import { RepresentationMetadata } from '../../http/representation/RepresentationMetadata'; import { RepresentationMetadata } from '../../http/representation/RepresentationMetadata';
@ -96,7 +96,7 @@ export class FileDataAccessor implements DataAccessor {
// Delete the metadata if there was an error writing the file // Delete the metadata if there was an error writing the file
if (wroteMetadata) { if (wroteMetadata) {
const metaLink = await this.resourceMapper.mapUrlToFilePath(identifier, true); const metaLink = await this.resourceMapper.mapUrlToFilePath(identifier, true);
await fsPromises.unlink(metaLink.filePath); await remove(metaLink.filePath);
} }
throw error; throw error;
} }
@ -107,14 +107,7 @@ export class FileDataAccessor implements DataAccessor {
*/ */
public async writeContainer(identifier: ResourceIdentifier, metadata: RepresentationMetadata): Promise<void> { public async writeContainer(identifier: ResourceIdentifier, metadata: RepresentationMetadata): Promise<void> {
const link = await this.resourceMapper.mapUrlToFilePath(identifier, false); const link = await this.resourceMapper.mapUrlToFilePath(identifier, false);
try { await ensureDir(link.filePath);
await fsPromises.mkdir(link.filePath, { recursive: true });
} catch (error: unknown) {
// Don't throw if directory already exists
if (!isSystemError(error) || error.code !== 'EEXIST') {
throw error;
}
}
await this.writeMetadata(link, metadata); await this.writeMetadata(link, metadata);
} }
@ -123,23 +116,16 @@ export class FileDataAccessor implements DataAccessor {
* Removes the corresponding file/folder (and metadata file). * Removes the corresponding file/folder (and metadata file).
*/ */
public async deleteResource(identifier: ResourceIdentifier): Promise<void> { public async deleteResource(identifier: ResourceIdentifier): Promise<void> {
const metaLink = await this.resourceMapper.mapUrlToFilePath(identifier, true);
await remove(metaLink.filePath);
const link = await this.resourceMapper.mapUrlToFilePath(identifier, false); const link = await this.resourceMapper.mapUrlToFilePath(identifier, false);
const stats = await this.getStats(link.filePath); const stats = await this.getStats(link.filePath);
try {
const metaLink = await this.resourceMapper.mapUrlToFilePath(identifier, true);
await fsPromises.unlink(metaLink.filePath);
} catch (error: unknown) {
// Ignore if it doesn't exist
if (!isSystemError(error) || error.code !== 'ENOENT') {
throw error;
}
}
if (!isContainerIdentifier(identifier) && stats.isFile()) { if (!isContainerIdentifier(identifier) && stats.isFile()) {
await fsPromises.unlink(link.filePath); await remove(link.filePath);
} else if (isContainerIdentifier(identifier) && stats.isDirectory()) { } else if (isContainerIdentifier(identifier) && stats.isDirectory()) {
await fsPromises.rmdir(link.filePath); await remove(link.filePath);
} else { } else {
throw new NotFoundHttpError(); throw new NotFoundHttpError();
} }
@ -155,7 +141,7 @@ export class FileDataAccessor implements DataAccessor {
*/ */
protected async getStats(path: string): Promise<Stats> { protected async getStats(path: string): Promise<Stats> {
try { try {
return await fsPromises.stat(path); return await stat(path);
} catch (error: unknown) { } catch (error: unknown) {
if (isSystemError(error) && error.code === 'ENOENT') { if (isSystemError(error) && error.code === 'ENOENT') {
throw new NotFoundHttpError('', { cause: error }); throw new NotFoundHttpError('', { cause: error });
@ -216,14 +202,7 @@ export class FileDataAccessor implements DataAccessor {
// Delete (potentially) existing metadata file if no metadata needs to be stored // Delete (potentially) existing metadata file if no metadata needs to be stored
} else { } else {
try { await remove(metadataLink.filePath);
await fsPromises.unlink(metadataLink.filePath);
} catch (error: unknown) {
// Metadata file doesn't exist so nothing needs to be removed
if (!isSystemError(error) || error.code !== 'ENOENT') {
throw error;
}
}
wroteMetadata = false; wroteMetadata = false;
} }
return wroteMetadata; return wroteMetadata;
@ -255,7 +234,7 @@ export class FileDataAccessor implements DataAccessor {
const metadataLink = await this.resourceMapper.mapUrlToFilePath(identifier, true); const metadataLink = await this.resourceMapper.mapUrlToFilePath(identifier, true);
// Check if the metadata file exists first // Check if the metadata file exists first
await fsPromises.lstat(metadataLink.filePath); await lstat(metadataLink.filePath);
const readMetadataStream = guardStream(createReadStream(metadataLink.filePath)); const readMetadataStream = guardStream(createReadStream(metadataLink.filePath));
return await parseQuads(readMetadataStream, { format: metadataLink.contentType, baseIRI: identifier.path }); return await parseQuads(readMetadataStream, { format: metadataLink.contentType, baseIRI: identifier.path });
@ -274,7 +253,7 @@ export class FileDataAccessor implements DataAccessor {
* @param link - Path related metadata. * @param link - Path related metadata.
*/ */
private async* getChildMetadata(link: ResourceLink): AsyncIterableIterator<RepresentationMetadata> { private async* getChildMetadata(link: ResourceLink): AsyncIterableIterator<RepresentationMetadata> {
const dir = await fsPromises.opendir(link.filePath); const dir = await opendir(link.filePath);
// For every child in the container we want to generate specific metadata // For every child in the container we want to generate specific metadata
for await (const entry of dir) { for await (const entry of dir) {
@ -345,17 +324,10 @@ export class FileDataAccessor implements DataAccessor {
* @param link - ResourceLink corresponding to the new resource data. * @param link - ResourceLink corresponding to the new resource data.
*/ */
protected async verifyExistingExtension(link: ResourceLink): Promise<void> { protected async verifyExistingExtension(link: ResourceLink): Promise<void> {
try {
// Delete the old file with the (now) wrong extension // Delete the old file with the (now) wrong extension
const oldLink = await this.resourceMapper.mapUrlToFilePath(link.identifier, false); const oldLink = await this.resourceMapper.mapUrlToFilePath(link.identifier, false);
if (oldLink.filePath !== link.filePath) { if (oldLink.filePath !== link.filePath) {
await fsPromises.unlink(oldLink.filePath); await remove(oldLink.filePath);
}
} catch (error: unknown) {
// Ignore it if the file didn't exist yet and couldn't be unlinked
if (!isSystemError(error) || error.code !== 'ENOENT') {
throw error;
}
} }
} }

View File

@ -1,4 +1,4 @@
import { promises as fsPromises } from 'fs'; import { writeJson, readJson } from 'fs-extra';
import type { ResourceIdentifier } from '../../http/representation/ResourceIdentifier'; import type { ResourceIdentifier } from '../../http/representation/ResourceIdentifier';
import { isSystemError } from '../../util/errors/SystemError'; import { isSystemError } from '../../util/errors/SystemError';
import type { ReadWriteLocker } from '../../util/locking/ReadWriteLocker'; import type { ReadWriteLocker } from '../../util/locking/ReadWriteLocker';
@ -70,8 +70,7 @@ export class JsonFileStorage implements KeyValueStorage<string, unknown> {
return this.locker.withWriteLock(this.lockIdentifier, async(): Promise<T> => { return this.locker.withWriteLock(this.lockIdentifier, async(): Promise<T> => {
const json = await this.getJson(); const json = await this.getJson();
const result = updateFn(json); const result = updateFn(json);
const updatedText = JSON.stringify(json, null, 2); await writeJson(this.filePath, json, { encoding: 'utf8', spaces: 2 });
await fsPromises.writeFile(this.filePath, updatedText, 'utf8');
return result; return result;
}); });
} }
@ -81,8 +80,7 @@ export class JsonFileStorage implements KeyValueStorage<string, unknown> {
*/ */
private async getJson(): Promise<NodeJS.Dict<unknown>> { private async getJson(): Promise<NodeJS.Dict<unknown>> {
try { try {
const text = await fsPromises.readFile(this.filePath, 'utf8'); return await readJson(this.filePath, 'utf8');
return JSON.parse(text);
} catch (error: unknown) { } catch (error: unknown) {
if (isSystemError(error) && error.code === 'ENOENT') { if (isSystemError(error) && error.code === 'ENOENT') {
return {}; return {};

View File

@ -1,12 +1,13 @@
import { promises as fsPromises } from 'fs'; import { writeJson } from 'fs-extra';
import type { RegistrationManager } from '../../../src/identity/interaction/email-password/util/RegistrationManager'; import type { RegistrationManager } from '../../../src/identity/interaction/email-password/util/RegistrationManager';
import { SeededPodInitializer } from '../../../src/init/SeededPodInitializer'; import { SeededPodInitializer } from '../../../src/init/SeededPodInitializer';
import { mockFs } from '../../util/Util'; import { mockFileSystem } from '../../util/Util';
jest.mock('fs'); jest.mock('fs');
jest.mock('fs-extra');
describe('A SeededPodInitializer', (): void => { describe('A SeededPodInitializer', (): void => {
const dummyConfig = JSON.stringify([ const dummyConfig = [
{ {
podName: 'example', podName: 'example',
email: 'hello@example.com', email: 'hello@example.com',
@ -17,7 +18,7 @@ describe('A SeededPodInitializer', (): void => {
email: 'hello2@example.com', email: 'hello2@example.com',
password: '123abc', password: '123abc',
}, },
]); ];
let registrationManager: RegistrationManager; let registrationManager: RegistrationManager;
let configFilePath: string | null; let configFilePath: string | null;
@ -28,8 +29,8 @@ describe('A SeededPodInitializer', (): void => {
register: jest.fn(), register: jest.fn(),
} as any; } as any;
mockFs('/'); mockFileSystem('/');
await fsPromises.writeFile(configFilePath, dummyConfig); await writeJson(configFilePath, dummyConfig);
}); });
it('does not generate any accounts or pods if no config file is specified.', async(): Promise<void> => { it('does not generate any accounts or pods if no config file is specified.', async(): Promise<void> => {

View File

@ -9,7 +9,7 @@ import type {
import { ensureTrailingSlash, trimTrailingSlashes } from '../../../../src/util/PathUtil'; import { ensureTrailingSlash, trimTrailingSlashes } from '../../../../src/util/PathUtil';
import { readableToString } from '../../../../src/util/StreamUtil'; import { readableToString } from '../../../../src/util/StreamUtil';
import { HandlebarsTemplateEngine } from '../../../../src/util/templates/HandlebarsTemplateEngine'; import { HandlebarsTemplateEngine } from '../../../../src/util/templates/HandlebarsTemplateEngine';
import { mockFs } from '../../../util/Util'; import { mockFileSystem } from '../../../util/Util';
const { namedNode } = DataFactory; const { namedNode } = DataFactory;
@ -55,7 +55,7 @@ describe('A TemplatedResourcesGenerator', (): void => {
const webId = 'http://alice/#profile'; const webId = 'http://alice/#profile';
beforeEach(async(): Promise<void> => { beforeEach(async(): Promise<void> => {
cache = mockFs(rootFilePath); cache = mockFileSystem(rootFilePath);
}); });
it('fills in a template with the given options.', async(): Promise<void> => { it('fills in a template with the given options.', async(): Promise<void> => {

View File

@ -9,7 +9,7 @@ import { NotFoundHttpError } from '../../../src/util/errors/NotFoundHttpError';
import type { IdentifierStrategy } from '../../../src/util/identifiers/IdentifierStrategy'; import type { IdentifierStrategy } from '../../../src/util/identifiers/IdentifierStrategy';
import { SingleRootIdentifierStrategy } from '../../../src/util/identifiers/SingleRootIdentifierStrategy'; import { SingleRootIdentifierStrategy } from '../../../src/util/identifiers/SingleRootIdentifierStrategy';
import { PIM, RDF } from '../../../src/util/Vocabularies'; import { PIM, RDF } from '../../../src/util/Vocabularies';
import { mockFs } from '../../util/Util'; import { mockFileSystem } from '../../util/Util';
jest.mock('fs'); jest.mock('fs');
@ -24,7 +24,7 @@ describe('PodQuotaStrategy', (): void => {
beforeEach((): void => { beforeEach((): void => {
jest.restoreAllMocks(); jest.restoreAllMocks();
mockFs(rootFilePath, new Date()); mockFileSystem(rootFilePath, new Date());
mockSize = { amount: 2000, unit: UNIT_BYTES }; mockSize = { amount: 2000, unit: UNIT_BYTES };
identifierStrategy = new SingleRootIdentifierStrategy(base); identifierStrategy = new SingleRootIdentifierStrategy(base);
mockReporter = { mockReporter = {

View File

@ -4,7 +4,7 @@ import { UNIT_BYTES } from '../../../src/storage/size-reporter/Size';
import type { Size } from '../../../src/storage/size-reporter/Size'; import type { Size } from '../../../src/storage/size-reporter/Size';
import type { SizeReporter } from '../../../src/storage/size-reporter/SizeReporter'; import type { SizeReporter } from '../../../src/storage/size-reporter/SizeReporter';
import { guardedStreamFrom, pipeSafely } from '../../../src/util/StreamUtil'; import { guardedStreamFrom, pipeSafely } from '../../../src/util/StreamUtil';
import { mockFs } from '../../util/Util'; import { mockFileSystem } from '../../util/Util';
jest.mock('fs'); jest.mock('fs');
@ -26,7 +26,7 @@ describe('A QuotaStrategy', (): void => {
beforeEach((): void => { beforeEach((): void => {
jest.restoreAllMocks(); jest.restoreAllMocks();
mockFs(rootFilePath, new Date()); mockFileSystem(rootFilePath, new Date());
mockSize = { amount: 2000, unit: UNIT_BYTES }; mockSize = { amount: 2000, unit: UNIT_BYTES };
mockReporter = { mockReporter = {
getSize: jest.fn().mockResolvedValue({ unit: mockSize.unit, amount: 50 }), getSize: jest.fn().mockResolvedValue({ unit: mockSize.unit, amount: 50 }),

View File

@ -7,9 +7,10 @@ import { APPLICATION_OCTET_STREAM } from '../../../../src/util/ContentTypes';
import type { Guarded } from '../../../../src/util/GuardedStream'; import type { Guarded } from '../../../../src/util/GuardedStream';
import { guardedStreamFrom } from '../../../../src/util/StreamUtil'; import { guardedStreamFrom } from '../../../../src/util/StreamUtil';
import { CONTENT_TYPE } from '../../../../src/util/Vocabularies'; import { CONTENT_TYPE } from '../../../../src/util/Vocabularies';
import { mockFs } from '../../../util/Util'; import { mockFileSystem } from '../../../util/Util';
jest.mock('fs'); jest.mock('fs');
jest.mock('fs-extra');
describe('AtomicFileDataAccessor', (): void => { describe('AtomicFileDataAccessor', (): void => {
const rootFilePath = 'uploads'; const rootFilePath = 'uploads';
@ -20,7 +21,7 @@ describe('AtomicFileDataAccessor', (): void => {
let data: Guarded<Readable>; let data: Guarded<Readable>;
beforeEach(async(): Promise<void> => { beforeEach(async(): Promise<void> => {
cache = mockFs(rootFilePath, new Date()); cache = mockFileSystem(rootFilePath, new Date());
accessor = new AtomicFileDataAccessor( accessor = new AtomicFileDataAccessor(
new ExtensionBasedMapper(base, rootFilePath), new ExtensionBasedMapper(base, rootFilePath),
rootFilePath, rootFilePath,
@ -59,27 +60,27 @@ describe('AtomicFileDataAccessor', (): void => {
data.emit('error', new Error('error')); data.emit('error', new Error('error'));
return null; return null;
}); });
jest.requireMock('fs').promises.stat = jest.fn((): any => ({ jest.requireMock('fs-extra').stat = jest.fn((): any => ({
isFile: (): boolean => false, isFile: (): boolean => false,
})); }));
await expect(accessor.writeDocument({ path: `${base}res.ttl` }, data, metadata)).rejects.toThrow('error'); await expect(accessor.writeDocument({ path: `${base}res.ttl` }, data, metadata)).rejects.toThrow('error');
}); });
it('should throw when renaming / moving the file goes wrong.', async(): Promise<void> => { it('should throw when renaming / moving the file goes wrong.', async(): Promise<void> => {
jest.requireMock('fs').promises.rename = jest.fn((): any => { jest.requireMock('fs-extra').rename = jest.fn((): any => {
throw new Error('error'); throw new Error('error');
}); });
jest.requireMock('fs').promises.stat = jest.fn((): any => ({ jest.requireMock('fs-extra').stat = jest.fn((): any => ({
isFile: (): boolean => true, isFile: (): boolean => true,
})); }));
await expect(accessor.writeDocument({ path: `${base}res.ttl` }, data, metadata)).rejects.toThrow('error'); await expect(accessor.writeDocument({ path: `${base}res.ttl` }, data, metadata)).rejects.toThrow('error');
}); });
it('should (on error) not unlink the temp file if it does not exist.', async(): Promise<void> => { it('should (on error) not unlink the temp file if it does not exist.', async(): Promise<void> => {
jest.requireMock('fs').promises.rename = jest.fn((): any => { jest.requireMock('fs-extra').rename = jest.fn((): any => {
throw new Error('error'); throw new Error('error');
}); });
jest.requireMock('fs').promises.stat = jest.fn((): any => ({ jest.requireMock('fs-extra').stat = jest.fn((): any => ({
isFile: (): boolean => false, isFile: (): boolean => false,
})); }));
await expect(accessor.writeDocument({ path: `${base}res.ttl` }, data, metadata)).rejects.toThrow('error'); await expect(accessor.writeDocument({ path: `${base}res.ttl` }, data, metadata)).rejects.toThrow('error');
@ -87,10 +88,10 @@ describe('AtomicFileDataAccessor', (): void => {
it('should throw when renaming / moving the file goes wrong and the temp file does not exist.', it('should throw when renaming / moving the file goes wrong and the temp file does not exist.',
async(): Promise<void> => { async(): Promise<void> => {
jest.requireMock('fs').promises.rename = jest.fn((): any => { jest.requireMock('fs-extra').rename = jest.fn((): any => {
throw new Error('error'); throw new Error('error');
}); });
jest.requireMock('fs').promises.stat = jest.fn(); jest.requireMock('fs-extra').stat = jest.fn();
await expect(accessor.writeDocument({ path: `${base}res.ttl` }, data, metadata)).rejects.toThrow('error'); await expect(accessor.writeDocument({ path: `${base}res.ttl` }, data, metadata)).rejects.toThrow('error');
}); });
}); });

View File

@ -16,11 +16,12 @@ import { isContainerPath } from '../../../../src/util/PathUtil';
import { guardedStreamFrom, readableToString } from '../../../../src/util/StreamUtil'; import { guardedStreamFrom, readableToString } from '../../../../src/util/StreamUtil';
import { toLiteral } from '../../../../src/util/TermUtil'; import { toLiteral } from '../../../../src/util/TermUtil';
import { CONTENT_TYPE, DC, LDP, POSIX, RDF, SOLID_META, XSD } from '../../../../src/util/Vocabularies'; import { CONTENT_TYPE, DC, LDP, POSIX, RDF, SOLID_META, XSD } from '../../../../src/util/Vocabularies';
import { mockFs } from '../../../util/Util'; import { mockFileSystem } from '../../../util/Util';
const { namedNode } = DataFactory; const { namedNode } = DataFactory;
jest.mock('fs'); jest.mock('fs');
jest.mock('fs-extra');
const rootFilePath = 'uploads'; const rootFilePath = 'uploads';
const now = new Date(); const now = new Date();
@ -36,7 +37,7 @@ describe('A FileDataAccessor', (): void => {
let data: Guarded<Readable>; let data: Guarded<Readable>;
beforeEach(async(): Promise<void> => { beforeEach(async(): Promise<void> => {
cache = mockFs(rootFilePath, now); cache = mockFileSystem(rootFilePath, now);
mapper = new ExtensionBasedMapper(base, rootFilePath); mapper = new ExtensionBasedMapper(base, rootFilePath);
accessor = new FileDataAccessor(mapper); accessor = new FileDataAccessor(mapper);
@ -71,6 +72,13 @@ describe('A FileDataAccessor', (): void => {
const stream = await accessor.getData({ path: `${base}resource` }); const stream = await accessor.getData({ path: `${base}resource` });
await expect(readableToString(stream)).resolves.toBe('data'); await expect(readableToString(stream)).resolves.toBe('data');
}); });
it('throws an error if something else went wrong.', async(): Promise<void> => {
jest.requireMock('fs-extra').stat = (): any => {
throw new Error('error');
};
await expect(accessor.getData({ path: `${base}resource` })).rejects.toThrow('error');
});
}); });
describe('getting metadata', (): void => { describe('getting metadata', (): void => {
@ -88,7 +96,7 @@ describe('A FileDataAccessor', (): void => {
}); });
it('throws an error if something else went wrong.', async(): Promise<void> => { it('throws an error if something else went wrong.', async(): Promise<void> => {
jest.requireMock('fs').promises.lstat = (): any => { jest.requireMock('fs-extra').lstat = (): any => {
throw new Error('error'); throw new Error('error');
}; };
await expect(accessor.getMetadata({ path: base })).rejects.toThrow('error'); await expect(accessor.getMetadata({ path: base })).rejects.toThrow('error');
@ -298,7 +306,7 @@ describe('A FileDataAccessor', (): void => {
it('errors if there is a problem deleting the old metadata file.', async(): Promise<void> => { it('errors if there is a problem deleting the old metadata file.', async(): Promise<void> => {
cache.data = { resource: 'data', 'resource.meta': 'metadata!' }; cache.data = { resource: 'data', 'resource.meta': 'metadata!' };
jest.requireMock('fs').promises.unlink = (): any => { jest.requireMock('fs-extra').remove = (): any => {
throw new Error('error'); throw new Error('error');
}; };
await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata)) await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata))
@ -352,7 +360,7 @@ describe('A FileDataAccessor', (): void => {
it('throws an error if there is an issue deleting the original file.', async(): Promise<void> => { it('throws an error if there is an issue deleting the original file.', async(): Promise<void> => {
cache.data = { 'resource$.ttl': '<this> <is> <data>.' }; cache.data = { 'resource$.ttl': '<this> <is> <data>.' };
jest.requireMock('fs').promises.unlink = (): any => { jest.requireMock('fs-extra').remove = (): any => {
const error = new Error('error') as SystemError; const error = new Error('error') as SystemError;
error.code = 'EISDIR'; error.code = 'EISDIR';
error.syscall = 'unlink'; error.syscall = 'unlink';
@ -382,7 +390,7 @@ describe('A FileDataAccessor', (): void => {
}); });
it('throws other errors when making a directory.', async(): Promise<void> => { it('throws other errors when making a directory.', async(): Promise<void> => {
jest.requireMock('fs').promises.mkdir = (): any => { jest.requireMock('fs-extra').ensureDir = (): any => {
throw new Error('error'); throw new Error('error');
}; };
await expect(accessor.writeContainer({ path: base }, metadata)).rejects.toThrow('error'); await expect(accessor.writeContainer({ path: base }, metadata)).rejects.toThrow('error');
@ -445,6 +453,9 @@ describe('A FileDataAccessor', (): void => {
it('throws error if there is a problem with deleting existing metadata.', async(): Promise<void> => { it('throws error if there is a problem with deleting existing metadata.', async(): Promise<void> => {
cache.data = { resource: 'apple', 'resource.meta': {}}; cache.data = { resource: 'apple', 'resource.meta': {}};
jest.requireMock('fs-extra').remove = (): any => {
throw new Error('error');
};
await expect(accessor.deleteResource({ path: `${base}resource` })).rejects.toThrow(); await expect(accessor.deleteResource({ path: `${base}resource` })).rejects.toThrow();
}); });

View File

@ -1,9 +1,10 @@
import type { ResourceIdentifier } from '../../../../src/http/representation/ResourceIdentifier'; import type { ResourceIdentifier } from '../../../../src/http/representation/ResourceIdentifier';
import { JsonFileStorage } from '../../../../src/storage/keyvalue/JsonFileStorage'; import { JsonFileStorage } from '../../../../src/storage/keyvalue/JsonFileStorage';
import type { ReadWriteLocker } from '../../../../src/util/locking/ReadWriteLocker'; import type { ReadWriteLocker } from '../../../../src/util/locking/ReadWriteLocker';
import { mockFs } from '../../../util/Util'; import { mockFileSystem } from '../../../util/Util';
jest.mock('fs'); jest.mock('fs');
jest.mock('fs-extra');
describe('A JsonFileStorage', (): void => { describe('A JsonFileStorage', (): void => {
const rootFilePath = 'files/'; const rootFilePath = 'files/';
@ -13,7 +14,7 @@ describe('A JsonFileStorage', (): void => {
let storage: JsonFileStorage; let storage: JsonFileStorage;
beforeEach(async(): Promise<void> => { beforeEach(async(): Promise<void> => {
cache = mockFs(rootFilePath); cache = mockFileSystem(rootFilePath);
locker = { locker = {
withReadLock: withReadLock:
jest.fn(async(identifier: ResourceIdentifier, whileLocked: () => any): Promise<any> => await whileLocked()), jest.fn(async(identifier: ResourceIdentifier, whileLocked: () => any): Promise<any> => await whileLocked()),

View File

@ -5,7 +5,7 @@ import type { FileIdentifierMapper, ResourceLink } from '../../../../src/storage
import { FileSizeReporter } from '../../../../src/storage/size-reporter/FileSizeReporter'; import { FileSizeReporter } from '../../../../src/storage/size-reporter/FileSizeReporter';
import { UNIT_BYTES } from '../../../../src/storage/size-reporter/Size'; import { UNIT_BYTES } from '../../../../src/storage/size-reporter/Size';
import { joinFilePath } from '../../../../src/util/PathUtil'; import { joinFilePath } from '../../../../src/util/PathUtil';
import { mockFs } from '../../../util/Util'; import { mockFileSystem } from '../../../util/Util';
jest.mock('fs'); jest.mock('fs');
@ -28,7 +28,7 @@ describe('A FileSizeReporter', (): void => {
); );
beforeEach(async(): Promise<void> => { beforeEach(async(): Promise<void> => {
mockFs(fileRoot); mockFileSystem(fileRoot);
}); });
it('should work without the ignoreFolders constructor parameter.', async(): Promise<void> => { it('should work without the ignoreFolders constructor parameter.', async(): Promise<void> => {

View File

@ -1,6 +1,6 @@
import { resolveAssetPath } from '../../../../src/util/PathUtil'; import { resolveAssetPath } from '../../../../src/util/PathUtil';
import { getTemplateFilePath, readTemplate } from '../../../../src/util/templates/TemplateEngine'; import { getTemplateFilePath, readTemplate } from '../../../../src/util/templates/TemplateEngine';
import { mockFs } from '../../../util/Util'; import { mockFileSystem } from '../../../util/Util';
jest.mock('fs'); jest.mock('fs');
@ -10,7 +10,7 @@ describe('TemplateEngine', (): void => {
const templatePath = 'other'; const templatePath = 'other';
beforeEach(async(): Promise<void> => { beforeEach(async(): Promise<void> => {
const { data } = mockFs(resolveAssetPath('')); const { data } = mockFileSystem(resolveAssetPath(''));
Object.assign(data, { Object.assign(data, {
'template.xyz': '{{template}}', 'template.xyz': '{{template}}',
other: { other: {
@ -45,7 +45,7 @@ describe('TemplateEngine', (): void => {
const templatePath = 'other'; const templatePath = 'other';
beforeEach(async(): Promise<void> => { beforeEach(async(): Promise<void> => {
const { data } = mockFs(resolveAssetPath('')); const { data } = mockFileSystem(resolveAssetPath(''));
Object.assign(data, { Object.assign(data, {
'template.xyz': '{{template}}', 'template.xyz': '{{template}}',
other: { other: {

View File

@ -75,7 +75,7 @@ export async function flushPromises(): Promise<void> {
* @param rootFilepath - The name of the root folder in which fs will start. * @param rootFilepath - The name of the root folder in which fs will start.
* @param time - The date object to use for time functions (currently only mtime from lstats) * @param time - The date object to use for time functions (currently only mtime from lstats)
*/ */
export function mockFs(rootFilepath?: string, time?: Date): { data: any } { export function mockFileSystem(rootFilepath?: string, time?: Date): { data: any } {
const cache: { data: any } = { data: {}}; const cache: { data: any } = { data: {}};
rootFilepath = rootFilepath ?? 'folder'; rootFilepath = rootFilepath ?? 'folder';
@ -112,7 +112,7 @@ export function mockFs(rootFilepath?: string, time?: Date): { data: any } {
return { folder, name }; return { folder, name };
} }
const mock = { const mockFs = {
createReadStream(path: string): any { createReadStream(path: string): any {
const { folder, name } = getFolder(path); const { folder, name } = getFolder(path);
return Readable.from([ folder[name] ]); return Readable.from([ folder[name] ]);
@ -235,8 +235,79 @@ export function mockFs(rootFilepath?: string, time?: Date): { data: any } {
}, },
}; };
const mockFsExtra = {
async readJson(path: string): Promise<NodeJS.Dict<unknown>> {
const { folder, name } = getFolder(path);
if (!folder[name]) {
throwSystemError('ENOENT');
}
return JSON.parse(folder[name]);
},
async writeJson(path: string, json: NodeJS.Dict<unknown>): Promise<void> {
const { folder, name } = getFolder(path);
const data = JSON.stringify(json, null, 2);
folder[name] = data;
},
async ensureDir(path: string): Promise<void> {
const { folder, name } = getFolder(path);
folder[name] = {};
},
async remove(path: string): Promise<void> {
const { folder, name } = getFolder(path);
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete folder[name];
},
createReadStream(path: string): any {
return mockFs.createReadStream(path);
},
createWriteStream(path: string): any {
return mockFs.createWriteStream(path);
},
async realpath(path: string): Promise<string> {
return await mockFs.promises.realpath(path);
},
async stat(path: string): Promise<Stats> {
return mockFs.promises.lstat(await mockFs.promises.realpath(path));
},
async lstat(path: string): Promise<Stats> {
return mockFs.promises.lstat(path);
},
async unlink(path: string): Promise<void> {
await mockFs.promises.unlink(path);
},
async symlink(target: string, path: string): Promise<void> {
await mockFs.promises.symlink(target, path);
},
async rmdir(path: string): Promise<void> {
await mockFs.promises.rmdir(path);
},
async readdir(path: string): Promise<string[]> {
return await mockFs.promises.readdir(path);
},
async* opendir(path: string): AsyncIterableIterator<Dirent> {
for await (const entry of mockFs.promises.opendir(path)) {
yield entry;
}
},
async mkdir(path: string): Promise<void> {
await mockFs.promises.mkdir(path);
},
async readFile(path: string): Promise<string> {
return await mockFs.promises.readFile(path);
},
async writeFile(path: string, data: string): Promise<void> {
await mockFs.promises.writeFile(path, data);
},
async rename(path: string, destination: string): Promise<void> {
await mockFs.promises.rename(path, destination);
},
};
const fs = jest.requireMock('fs'); const fs = jest.requireMock('fs');
Object.assign(fs, mock); Object.assign(fs, mockFs);
const fsExtra = jest.requireMock('fs-extra');
Object.assign(fsExtra, mockFsExtra);
return cache; return cache;
} }