Skip to content

Commit

Permalink
Feature/base fs (#13)
Browse files Browse the repository at this point in the history
* remove unused test data from repository

* fix missing write in stringify function

* add new bundle to replace current fs module
  • Loading branch information
CordlessWool authored Mar 27, 2024
1 parent 044ba53 commit 542d138
Show file tree
Hide file tree
Showing 18 changed files with 228 additions and 697 deletions.
10 changes: 7 additions & 3 deletions packages/base/package.json → packages/base-fs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc -p tsconfig.json"
},
"devDependencies": {
"@loom-io/test-utils": "workspace:*"
},
"dependencies": {
"@loom-io/core": "workspace:^",
"@loom-io/jsonConverter": "workspace:^",
"@loom-io/yamlConverter": "workspace:^"
"@loom-io/core": "workspace:^0.6.0",
"@loom-io/node-filesystem-adapter": "workspace:^0.6.0",
"@loom-io/yamlConverter": "workspace:^0.6.0",
"@loom-io/jsonConverter": "workspace:^0.6.0"
},
"exports": {
".": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import LoomIO from '@loom-io/core';
import jsonConverter from '@loom-io/jsonConverter';
import yamlConverter from '@loom-io/yamlConverter';
import fileSystemAdapter from '@loom-io/node-filesystem-adapter';

LoomIO.register(fileSystemAdapter('file://'));
LoomIO.register(jsonConverter);
LoomIO.register(yamlConverter);

export * from '@loom-io/core';

export default LoomIO;
20 changes: 20 additions & 0 deletions packages/base-fs/tests/exports.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, test, expect } from 'vitest';
import LoomIO, {isDirectory, isFile} from '../src/bundle';

describe('minimal export test', async () => {

test('exports', async () => {
expect(LoomIO).toBeDefined();
});

test('isDirectory', async () => {
expect(isDirectory).toBeDefined();
expect(isDirectory).toBeInstanceOf(Function);
});

test('isFile', async () => {
expect(isFile).toBeDefined();
expect(isFile).toBeInstanceOf(Function);
});

});
183 changes: 183 additions & 0 deletions packages/base-fs/tests/integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import { describe, test, expect, afterAll } from 'vitest';
import { rmdir } from 'fs/promises';
import { resolve as resolvePath } from 'node:path';
import LoomIO, { Directory, DirectoryNotEmptyException, isDirectory } from '../src/bundle';

const BASE_TEST_PATH = './tmp-test/';

const randomStrings = new Set<string>();

const createRandomDirectory = async (): Promise<Directory> => {
const random = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
if ( randomStrings.has(random) ) {
return createRandomDirectory();
}
randomStrings.add(random);
return LoomIO.dir(`file://${BASE_TEST_PATH}/${random}`);
};

const deleteRandomDirectory = async (dir: Directory) => {
const random = dir.path;
await rmdir(resolvePath(random), {recursive: true});
randomStrings.delete(random);
};

describe.concurrent('test integration of bundle for interacting with filesystem', () => {

afterAll(async () => {
await rmdir(resolvePath(BASE_TEST_PATH), {recursive: true});
});

describe('test base functionality', () => {

test('create directory', async () => {
const dir = await createRandomDirectory();
expect(dir).toBeDefined();
expect(isDirectory(dir)).toBeTruthy();
await dir.create();
expect(await dir.exists()).toBeTruthy();
await deleteRandomDirectory(dir);
await expect(dir.exists()).resolves.toBeFalsy();
});

test('delete directory', async () => {
const dir = await createRandomDirectory();
await dir.delete();
expect(await dir.exists()).toBeFalsy();
});

test('list empty directory', async () => {
const dir = await createRandomDirectory();
await dir.create();
const list = await dir.list();
expect(list).toBeDefined();
expect(list.length).toBe(0);
await deleteRandomDirectory(dir);
});

test('list directory with files', async () => {
const dir = await createRandomDirectory();
await dir.create();
const file = dir.file('test.txt');
await file.create();
const list = await dir.list();
expect(list).toBeDefined();
expect(list.length).toBe(1);
await deleteRandomDirectory(dir);
});

test('delete directory with files', async () => {
const dir = await createRandomDirectory();
await dir.create();
const file = dir.file('test.txt');
await file.create();
expect(await dir.exists()).toBeTruthy();
expect(await file.exists()).toBeTruthy();
await expect(dir.delete()).rejects.toThrow(DirectoryNotEmptyException);
await dir.delete(true);
expect(await dir.exists()).toBeFalsy();
});

test('create file', async () => {
const dir = await createRandomDirectory();
await dir.create();
const file = dir.file('test.txt');
await file.create();
expect(await file.exists()).toBeTruthy();
await deleteRandomDirectory(dir);
});

test('delete file', async () => {
const dir = await createRandomDirectory();
await dir.create();
const file = dir.file('test.txt');
await file.create();
expect(await file.exists()).toBeTruthy();
await file.delete();
expect(await file.exists()).toBeFalsy();
await deleteRandomDirectory(dir);
});

test('write file', async () => {
const dir = await createRandomDirectory();
await dir.create();
const file = dir.file('test.txt');
await file.create();
const content = 'test content';
await file.write(content);
expect(await file.text()).toBe(content);
await deleteRandomDirectory(dir);
});

test('read file', async () => {
const dir = await createRandomDirectory();
await dir.create();
const file = dir.file('test.txt');
await file.create();
const content = 'test content';
await file.write(content);
expect(await file.text()).toBe(content);
await deleteRandomDirectory(dir);
});

test('read yml file', async () => {
const dir = await createRandomDirectory();
await dir.create();
const file = dir.file('test.yml');
await file.create();
const content = 'test: content';
await file.write(content);
const json = await file.json();
expect(json).toStrictEqual({test: 'content'});
await deleteRandomDirectory(dir);
});

test('read json file', async () => {
const dir = await createRandomDirectory();
await dir.create();
const file = dir.file('test.json');
await file.create();
const content = '{"test": "content"}';
await file.write(content);
const json = await file.json();
expect(json).toStrictEqual({test: 'content'});
await deleteRandomDirectory(dir);
});

test('read log file', async () => {
const dir = await createRandomDirectory();
await dir.create();
const file = dir.file('test.log');
await file.create();
const content = 'test content';
await file.write(content);
await expect(file.text()).resolves.toBe(content);
await deleteRandomDirectory(dir);
});

test('write json file', async () => {
const dir = await createRandomDirectory();
await dir.create();
const file = dir.file('test.json');
await file.create();
const content = {test: 'content'};
await file.stringify(content);
await expect(file.text()).resolves.toBe(JSON.stringify(content));
await deleteRandomDirectory(dir);
});

test('write yml file', async () => {
const dir = await createRandomDirectory();
await dir.create();
const file = dir.file('test.yml');
await file.create();
const content = {test: 'content', test2: ['content', 'content2']};
await file.stringify(content);
expect(await file.text()).toBe('test: content\ntest2:\n - content\n - content2\n');
await deleteRandomDirectory(dir);
});


});

});
File renamed without changes.
5 changes: 2 additions & 3 deletions packages/core/src/core/file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ describe('Test File Service', () => {

test('Write file with json plugin', async () => {
const testContent = {test: true};
const testFile = await testHelper.createFile(faker.system.commonFileName('json'), JSON.stringify(testContent));
const testFile = await testHelper.createFile(faker.system.commonFileName('json'));

const file = LoomFile.from( adapter, testFile);
const plugin: LoomFileConverter = {
Expand All @@ -220,8 +220,7 @@ describe('Test File Service', () => {
};
LoomFile.register(plugin);
await file.stringify(testContent);
const content = await file.text();
expect(content).toBe(JSON.stringify(testContent));
await expect(file.text()).resolves.toBe(JSON.stringify(testContent));
});

test('No plugin for file', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/core/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class LoomFile {
throw new PluginNotFoundException(this.path);
}

return plugin.stringify(content);
this.write(plugin.stringify(content));
}

static register(plugin: LoomFileConverter) {
Expand Down
15 changes: 11 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions tests/integration/test-data/editor.md

This file was deleted.

Empty file.
1 change: 0 additions & 1 deletion tests/integration/test-data/line.txt

This file was deleted.

3 changes: 0 additions & 3 deletions tests/integration/test-data/test.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/integration/test-data/test.txt

This file was deleted.

2 changes: 0 additions & 2 deletions tests/integration/test-data/test.yaml

This file was deleted.

2 changes: 0 additions & 2 deletions tests/integration/test-data/test.yml

This file was deleted.

Loading

0 comments on commit 542d138

Please sign in to comment.