mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
refactor: Use fs-extra instead of fs to simplify file access
* refactor: use fs-extra instead of fs * tests: manual mocks for fs-extra base + ensureDir * refactor: mockFileSystem + mockFs and mockFsExtra * add remove mock and some further test tweaks * test: FileDataAccessor tests passing * refactor: remove try-catch due to fs-extra handlin * refactor: fs-extra in atomicFileDataAccessor * refactor: AtomicFileDataAccessor fs-extra * test: fix coverage * refactor: use read/writeJson from fs-extra * refactor: less duplicate mocking code * refactor: re-use opendir mocking code
This commit is contained in:
@@ -7,9 +7,10 @@ import { APPLICATION_OCTET_STREAM } from '../../../../src/util/ContentTypes';
|
||||
import type { Guarded } from '../../../../src/util/GuardedStream';
|
||||
import { guardedStreamFrom } from '../../../../src/util/StreamUtil';
|
||||
import { CONTENT_TYPE } from '../../../../src/util/Vocabularies';
|
||||
import { mockFs } from '../../../util/Util';
|
||||
import { mockFileSystem } from '../../../util/Util';
|
||||
|
||||
jest.mock('fs');
|
||||
jest.mock('fs-extra');
|
||||
|
||||
describe('AtomicFileDataAccessor', (): void => {
|
||||
const rootFilePath = 'uploads';
|
||||
@@ -20,7 +21,7 @@ describe('AtomicFileDataAccessor', (): void => {
|
||||
let data: Guarded<Readable>;
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
cache = mockFs(rootFilePath, new Date());
|
||||
cache = mockFileSystem(rootFilePath, new Date());
|
||||
accessor = new AtomicFileDataAccessor(
|
||||
new ExtensionBasedMapper(base, rootFilePath),
|
||||
rootFilePath,
|
||||
@@ -59,27 +60,27 @@ describe('AtomicFileDataAccessor', (): void => {
|
||||
data.emit('error', new Error('error'));
|
||||
return null;
|
||||
});
|
||||
jest.requireMock('fs').promises.stat = jest.fn((): any => ({
|
||||
jest.requireMock('fs-extra').stat = jest.fn((): any => ({
|
||||
isFile: (): boolean => false,
|
||||
}));
|
||||
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> => {
|
||||
jest.requireMock('fs').promises.rename = jest.fn((): any => {
|
||||
jest.requireMock('fs-extra').rename = jest.fn((): any => {
|
||||
throw new Error('error');
|
||||
});
|
||||
jest.requireMock('fs').promises.stat = jest.fn((): any => ({
|
||||
jest.requireMock('fs-extra').stat = jest.fn((): any => ({
|
||||
isFile: (): boolean => true,
|
||||
}));
|
||||
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> => {
|
||||
jest.requireMock('fs').promises.rename = jest.fn((): any => {
|
||||
jest.requireMock('fs-extra').rename = jest.fn((): any => {
|
||||
throw new Error('error');
|
||||
});
|
||||
jest.requireMock('fs').promises.stat = jest.fn((): any => ({
|
||||
jest.requireMock('fs-extra').stat = jest.fn((): any => ({
|
||||
isFile: (): boolean => false,
|
||||
}));
|
||||
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.',
|
||||
async(): Promise<void> => {
|
||||
jest.requireMock('fs').promises.rename = jest.fn((): any => {
|
||||
jest.requireMock('fs-extra').rename = jest.fn((): any => {
|
||||
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');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user