Skip to content

Commit

Permalink
refactor: 💡 make memfs() return a collection of things
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 25, 2023
1 parent 93f909b commit 2ce01b7
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 107 deletions.
2 changes: 1 addition & 1 deletion demo/git/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import git from 'isomorphic-git';
import {memfs} from '../../src';

const main = async () => {
const fs = memfs();
const {fs} = memfs();

fs.mkdirSync('/repo');
console.log('New folder:', (<any>fs).__vol.toJSON());
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/volume/readFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { memfs } from '../..';

describe('.readFile()', () => {
it('can read a file', async () => {
const fs = memfs({ '/dir/test.txt': '01234567' });
const { fs } = memfs({ '/dir/test.txt': '01234567' });
const data = await fs.promises.readFile('/dir/test.txt', { encoding: 'utf8' });
expect(data).toBe('01234567');
});

it('throws if file does not exist', async () => {
const fs = memfs({ '/dir/test.txt': '01234567' });
const { fs } = memfs({ '/dir/test.txt': '01234567' });
const [, err] = await of(fs.promises.readFile('/dir/test-NOT-FOUND.txt', { encoding: 'utf8' }));
expect(err).toBeInstanceOf(Error);
expect((<any>err).code).toBe('ENOENT');
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/volume/writeSync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('.writeSync(fd, buffer, offset, length, position)', () => {
});

it('can write at offset', () => {
const fs = memfs({ foo: '123' });
const { fs } = memfs({ foo: '123' });
const fd = fs.openSync('/foo', 'a+');
expect(fs.readFileSync('/foo', 'utf8')).toBe('123');
fs.writeSync(fd, 'x', 1);
Expand Down
6 changes: 3 additions & 3 deletions src/crud-to-cas/__tests__/CrudCas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { NodeCrud } from '../../node-to-crud/NodeCrud';

onlyOnNode20('CrudCas on FsaCrud', () => {
const setup = () => {
const fs = memfs();
const { fs } = memfs();
const fsa = new NodeFileSystemDirectoryHandle(fs, '/', { mode: 'readwrite' });
const crud = new FsaCrud(fsa);
const cas = new CrudCas(crud, { hash });
Expand All @@ -19,7 +19,7 @@ onlyOnNode20('CrudCas on FsaCrud', () => {

onlyOnNode20('CrudCas on NodeCrud at root', () => {
const setup = () => {
const fs = memfs();
const { fs } = memfs();
const crud = new NodeCrud({ fs: fs.promises, dir: '/' });
const cas = new CrudCas(crud, { hash });
return { fs, crud, cas, snapshot: () => (<any>fs).__vol.toJSON() };
Expand All @@ -29,7 +29,7 @@ onlyOnNode20('CrudCas on NodeCrud at root', () => {

onlyOnNode20('CrudCas on NodeCrud at in sub-folder', () => {
const setup = () => {
const fs = memfs({ '/a/b/c': null });
const { fs } = memfs({ '/a/b/c': null });
const crud = new NodeCrud({ fs: fs.promises, dir: '/a/b/c' });
const cas = new CrudCas(crud, { hash });
return { fs, crud, cas, snapshot: () => (<any>fs).__vol.toJSON() };
Expand Down
12 changes: 6 additions & 6 deletions src/crud/__tests__/matryoshka.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import { FsaCrud } from '../../fsa-to-crud';
onlyOnNode20('CRUD matryoshka', () => {
describe('crud(memfs)', () => {
testCrudfs(() => {
const fs = memfs();
const { fs } = memfs();
const crud = new NodeCrud({ fs: fs.promises, dir: '/' });
return { crud, snapshot: () => (<any>fs).__vol.toJSON() };
});
});

describe('crud(fsa(memfs))', () => {
testCrudfs(() => {
const fs = memfs();
const { fs } = memfs();
const fsa = new NodeFileSystemDirectoryHandle(fs, '/', { mode: 'readwrite' });
const crud = new FsaCrud(fsa);
return { crud, snapshot: () => (<any>fs).__vol.toJSON() };
Expand All @@ -26,7 +26,7 @@ onlyOnNode20('CRUD matryoshka', () => {

describe('crud(fs(fsa(memfs)))', () => {
testCrudfs(() => {
const fs = memfs();
const { fs } = memfs();
const fsa = new NodeFileSystemDirectoryHandle(fs, '/', { mode: 'readwrite' });
const fs2 = new FsaNodeFs(fsa);
const crud = new NodeCrud({ fs: fs2.promises, dir: '/' });
Expand All @@ -36,7 +36,7 @@ onlyOnNode20('CRUD matryoshka', () => {

describe('crud(fsa(fs(fsa(memfs))))', () => {
testCrudfs(() => {
const fs = memfs();
const { fs } = memfs();
const fsa = new NodeFileSystemDirectoryHandle(fs, '/', { mode: 'readwrite' });
const fs2 = new FsaNodeFs(fsa);
const fsa2 = new NodeFileSystemDirectoryHandle(fs2, '/', { mode: 'readwrite' });
Expand All @@ -47,7 +47,7 @@ onlyOnNode20('CRUD matryoshka', () => {

describe('crud(fs(fsa(fs(fsa(memfs)))))', () => {
testCrudfs(() => {
const fs = memfs();
const { fs } = memfs();
const fsa = new NodeFileSystemDirectoryHandle(fs, '/', { mode: 'readwrite' });
const fs2 = new FsaNodeFs(fsa);
const fsa2 = new NodeFileSystemDirectoryHandle(fs2, '/', { mode: 'readwrite' });
Expand All @@ -59,7 +59,7 @@ onlyOnNode20('CRUD matryoshka', () => {

describe('crud(fsa(fs(fsa(fs(fsa(memfs))))))', () => {
testCrudfs(() => {
const fs = memfs();
const { fs } = memfs();
const fsa = new NodeFileSystemDirectoryHandle(fs, '/', { mode: 'readwrite' });
const fs2 = new FsaNodeFs(fsa);
const fsa2 = new NodeFileSystemDirectoryHandle(fs2, '/', { mode: 'readwrite' });
Expand Down
2 changes: 1 addition & 1 deletion src/fsa-to-crud/__tests__/FsaCrud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FsaCrud } from '../FsaCrud';
import { testCrudfs } from '../../crud/__tests__/testCrudfs';

const setup = () => {
const fs = memfs();
const { fs } = memfs();
const fsa = new NodeFileSystemDirectoryHandle(fs, '/', { mode: 'readwrite' });
const crud = new FsaCrud(fsa);
return { fs, fsa, crud, snapshot: () => (<any>fs).__vol.toJSON() };
Expand Down
Loading

0 comments on commit 2ce01b7

Please sign in to comment.