Files
smartfile/test/test.ts

257 lines
10 KiB
TypeScript
Raw Normal View History

2022-06-07 15:43:28 +02:00
import * as smartfile from '../ts/index.js';
2020-10-11 15:34:24 +00:00
import * as path from 'path';
2017-01-21 00:47:48 +01:00
import { expect, tap } from '@git.zone/tstest/tapbundle';
2017-01-21 00:47:48 +01:00
2017-04-29 16:50:06 +02:00
// ---------------------------
// smartfile.fs
// ---------------------------
2017-04-27 16:48:08 +02:00
tap.test('.fs.fileExistsSync -> should return an accurate boolean', async () => {
2020-10-11 15:34:24 +00:00
// tslint:disable-next-line: no-unused-expression
2022-06-07 15:43:28 +02:00
expect(smartfile.fs.fileExistsSync('./test/testassets/mytest.json')).toBeTrue();
2020-10-11 15:34:24 +00:00
// tslint:disable-next-line: no-unused-expression
2022-06-07 15:43:28 +02:00
expect(smartfile.fs.fileExistsSync('./test/testassets/notthere.json')).toBeFalse();
});
2018-02-16 21:57:44 +01:00
tap.test('.fs.fileExists -> should resolve or reject a promise', async () => {
await expect(smartfile.fs.fileExists('./test/testassets/mytest.json')).resolves.toBeTrue();
await expect(smartfile.fs.fileExists('./test/testassets/notthere.json')).resolves.toBeFalse();
});
2017-04-27 16:48:08 +02:00
tap.test('.fs.listFoldersSync() -> should get the file type from a string', async () => {
2022-06-07 15:43:28 +02:00
expect(smartfile.fs.listFoldersSync('./test/testassets/')).toContain('testfolder');
expect(smartfile.fs.listFoldersSync('./test/testassets/')).not.toContain('notExistentFolder');
});
2017-04-27 16:48:08 +02:00
tap.test('.fs.listFolders() -> should get the file type from a string', async () => {
const folderArrayArg = await smartfile.fs.listFolders('./test/testassets/');
2022-06-07 15:43:28 +02:00
expect(folderArrayArg).toContain('testfolder');
expect(folderArrayArg).not.toContain('notExistentFolder');
});
2017-04-27 16:48:08 +02:00
tap.test('.fs.listFilesSync() -> should get the file type from a string', async () => {
2022-06-07 15:43:28 +02:00
expect(smartfile.fs.listFilesSync('./test/testassets/')).toContain('mytest.json');
expect(smartfile.fs.listFilesSync('./test/testassets/')).not.toContain('notExistentFile');
2022-07-24 23:11:41 +02:00
expect(smartfile.fs.listFilesSync('./test/testassets/', /mytest\.json/)).toContain('mytest.json');
2022-06-07 15:43:28 +02:00
expect(smartfile.fs.listFilesSync('./test/testassets/', /mytests.json/)).not.toContain(
2019-09-27 11:00:17 +02:00
'mytest.json'
);
});
2017-04-27 16:48:08 +02:00
tap.test('.fs.listFiles() -> should get the file type from a string', async () => {
const folderArrayArg = await smartfile.fs.listFiles('./test/testassets/');
2022-06-07 15:43:28 +02:00
expect(folderArrayArg).toContain('mytest.json');
expect(folderArrayArg).not.toContain('notExistentFile');
});
2017-04-27 16:48:08 +02:00
tap.test('.fs.listFileTree() -> should get a file tree', async () => {
const folderArrayArg = await smartfile.fs.listFileTree(
2019-09-27 11:00:17 +02:00
path.resolve('./test/testassets/'),
'**/*.txt'
);
2022-06-07 15:43:28 +02:00
expect(folderArrayArg).toContain('testfolder/testfile1.txt');
expect(folderArrayArg).not.toContain('mytest.json');
});
2017-04-27 16:48:08 +02:00
tap.test('.fs.listFileTree() -> should find both root and nested .ts files with **/*.ts pattern', async () => {
const tsFiles = await smartfile.fs.listFileTree(
process.cwd(),
'**/*.ts'
);
// Should find both root-level and nested TypeScript files
expect(tsFiles).toContain('ts/index.ts');
expect(tsFiles).toContain('ts/classes.smartfile.ts');
expect(tsFiles).toContain('test/test.ts');
// Should find files in multiple levels of nesting
expect(tsFiles.filter(f => f.endsWith('.ts')).length).toBeGreaterThan(5);
// Verify it finds files at all levels (root 'ts/' and nested 'test/')
const hasRootLevelTs = tsFiles.some(f => f.startsWith('ts/') && f.endsWith('.ts'));
const hasNestedTs = tsFiles.some(f => f.startsWith('test/') && f.endsWith('.ts'));
expect(hasRootLevelTs).toBeTrue();
expect(hasNestedTs).toBeTrue();
});
tap.test('.fs.listFileTree() -> should handle edge cases with **/ patterns consistently', async () => {
// Test that our fix ensures no duplicate files in results
const jsonFiles = await smartfile.fs.listFileTree(
path.resolve('./test/testassets/'),
'**/*.json'
);
const uniqueFiles = [...new Set(jsonFiles)];
expect(jsonFiles.length).toEqual(uniqueFiles.length);
// Test that it finds root level files with **/ patterns
const txtFiles = await smartfile.fs.listFileTree(
path.resolve('./test/testassets/'),
'**/*.txt'
);
// Should include both direct files and nested files
expect(txtFiles).toContain('mytest.txt');
expect(txtFiles).toContain('testfolder/testfile1.txt');
});
2017-05-01 19:49:34 +02:00
tap.test('.fs.fileTreeToObject -> should read a file tree into an Object', async () => {
const fileArrayArg = await smartfile.fs.fileTreeToObject(
2019-09-27 11:00:17 +02:00
path.resolve('./test/testassets/'),
'**/*.txt'
);
2023-11-04 20:07:43 +01:00
expect(fileArrayArg[0]).toBeInstanceOf(smartfile.SmartFile);
2022-06-07 15:43:28 +02:00
expect(fileArrayArg[0].contents.toString()).toEqual(fileArrayArg[0].contentBuffer.toString());
});
2017-04-27 16:48:08 +02:00
2017-04-28 11:28:11 +02:00
tap.test('.fs.copy() -> should copy a directory', async () => {
2019-06-27 10:42:35 +02:00
await smartfile.fs.copy('./test/testassets/testfolder/', './test/testassets/temp/');
});
2017-04-27 16:48:08 +02:00
2017-04-28 11:28:11 +02:00
tap.test('.fs.copy() -> should copy a file', async () => {
2019-06-27 10:42:35 +02:00
await smartfile.fs.copy('./test/testassets/mytest.yaml', './test/testassets/temp/mytest.yaml');
});
2017-04-28 11:28:11 +02:00
tap.test('.fs.copy() -> should copy a file and rename it', async () => {
2019-09-27 11:00:17 +02:00
await smartfile.fs.copy(
'./test/testassets/mytest.yaml',
'./test/testassets/temp/mytestRenamed.yaml'
);
});
2017-04-28 11:28:11 +02:00
tap.test('.fs.remove() -> should remove an entire directory', async () => {});
2017-04-28 11:28:11 +02:00
2017-04-30 18:13:17 +02:00
tap.test('.fs.remove -> should remove single files', async () => {
2019-06-26 21:17:58 +02:00
await smartfile.fs.remove('./test/testassets/temp/mytestRenamed.yaml');
});
2017-04-28 11:28:11 +02:00
2017-04-30 18:13:17 +02:00
tap.test('.fs.removeSync -> should remove single files synchronouly', async () => {
2019-06-26 21:17:58 +02:00
smartfile.fs.removeSync('./test/testassets/temp/testfile1.txt');
2022-06-07 15:43:28 +02:00
expect(smartfile.fs.fileExistsSync('./test/testassets/temp/testfile1.txt')).toBeFalse();
});
2017-04-28 11:28:11 +02:00
2017-04-30 18:13:17 +02:00
tap.test('.fs.removeMany -> should remove and array of files', async () => {
2019-09-27 11:00:17 +02:00
smartfile.fs
.removeMany(['./test/testassets/temp/testfile1.txt', './test/testassets/temp/testfile2.txt'])
.then(() => {
2022-06-07 15:43:28 +02:00
expect(smartfile.fs.fileExistsSync('./test/testassets/temp/testfile1.txt')).toBeFalse();
expect(smartfile.fs.fileExistsSync('./test/testassets/temp/testfile2.txt')).toBeFalse();
2019-09-27 11:00:17 +02:00
});
});
2017-04-30 18:13:17 +02:00
tap.test('.fs.removeManySync -> should remove and array of single files synchronouly', async () => {
2019-09-27 11:00:17 +02:00
smartfile.fs.removeManySync([
'./test/testassets/temp/testfile1.txt',
'./test/testassets/temp/testfile2.txt',
2019-09-27 11:00:17 +02:00
]);
2022-06-07 15:43:28 +02:00
expect(smartfile.fs.fileExistsSync('./test/testassets/temp/testfile1.txt')).toBeFalse();
expect(smartfile.fs.fileExistsSync('./test/testassets/temp/testfile2.txt')).toBeFalse();
});
2017-04-28 11:28:11 +02:00
2022-06-07 15:43:28 +02:00
tap.test('.fs.toObjectSync() -> should read an .yaml file to an object', async () => {
const testData = smartfile.fs.toObjectSync('./test/testassets/mytest.yaml');
2022-06-07 15:43:28 +02:00
expect(testData.key1).toEqual('this works');
expect(testData.key2).toEqual('this works too');
});
tap.test(
'.fs.toObjectSync() -> should state unknown file type for unknown file types',
async () => {
const testData = smartfile.fs.toObjectSync('./test/testassets/mytest.txt');
}
);
2017-04-27 16:48:08 +02:00
2022-06-07 15:43:28 +02:00
tap.test('.fs.toObjectSync() -> should read an .json file to an object', async () => {
const testData = smartfile.fs.toObjectSync('./test/testassets/mytest.json');
2022-06-07 15:43:28 +02:00
expect(testData.key1).toEqual('this works');
expect(testData.key2).toEqual('this works too');
});
2017-04-28 11:28:11 +02:00
tap.test('.fs.toStringSync() -> should read a file to a string', async () => {
2022-07-24 23:11:41 +02:00
expect(smartfile.fs.toStringSync('./test/testassets/mytest.txt')).toEqual('Some TestString &&%$');
});
2017-04-28 11:28:11 +02:00
2017-04-30 18:13:17 +02:00
// ---------------------------
// smartfile.interpreter
// ---------------------------
tap.test('.interpreter.filetype() -> should get the file type from a string', async () => {
2022-06-07 15:43:28 +02:00
expect(smartfile.interpreter.filetype('./somefolder/data.json')).toEqual('json');
});
2017-04-30 18:13:17 +02:00
// ---------------------------
// smartfile.memory
// ---------------------------
2017-04-28 11:28:11 +02:00
tap.test('.memory.toFs() -> should write a file to disk and return a promise', async () => {
const localString = 'myString';
2019-09-27 11:00:17 +02:00
await smartfile.memory.toFs(
localString,
path.join(process.cwd(), './test/testassets/temp/testMemToFs.txt')
);
});
tap.test(
'.memory.toFsSync() -> should write a file to disk and return true if successfull',
async () => {
const localString = 'myString';
smartfile.memory.toFsSync(
localString,
2019-06-26 21:17:58 +02:00
path.join(process.cwd(), './test/testassets/temp/testMemToFsSync.txt')
);
}
);
2017-04-27 16:48:08 +02:00
2017-04-30 18:13:17 +02:00
// ---------------------------
// smartfile.Smartfile
// ---------------------------
tap.test('.Smartfile -> should produce vinyl compatible files', async () => {
const smartfileArray = await smartfile.fs.fileTreeToObject(
2019-09-27 11:00:17 +02:00
process.cwd(),
'./test/testassets/testfolder/**/*'
);
const localSmartfile = smartfileArray[0];
2023-11-04 20:07:43 +01:00
expect(localSmartfile).toBeInstanceOf(smartfile.SmartFile);
2022-06-07 15:43:28 +02:00
expect(localSmartfile.contents).toBeInstanceOf(Buffer);
2017-05-27 01:07:32 +02:00
// tslint:disable-next-line:no-unused-expression
2022-06-07 15:43:28 +02:00
expect(localSmartfile.isBuffer()).toBeTrue();
2017-05-27 01:07:32 +02:00
// tslint:disable-next-line:no-unused-expression
2022-06-07 15:43:28 +02:00
expect(localSmartfile.isDirectory()).toBeFalse();
2017-05-27 01:07:32 +02:00
// tslint:disable-next-line:no-unused-expression
2022-06-07 15:43:28 +02:00
expect(localSmartfile.isNull()).toBeFalse();
});
2017-04-30 18:13:17 +02:00
2017-05-26 14:47:41 +02:00
tap.test('should output a smartfile array to disk', async () => {
const smartfileArray = await smartfile.fs.fileTreeToObject('./test/testassets/testfolder/', '*');
for (const smartfileInstance of smartfileArray) {
console.log(smartfileInstance.relative);
console.log(smartfileInstance.path);
console.log(smartfileInstance.base);
console.log(smartfileInstance.parsedPath);
2017-05-26 14:47:41 +02:00
}
await smartfile.memory.smartfileArrayToFs(
smartfileArray,
2019-06-26 21:17:58 +02:00
path.resolve('./test/testassets/temp/testoutput/')
);
});
2017-05-26 14:47:41 +02:00
tap.test('should create, store and retrieve valid smartfiles', async () => {
const fileString = 'hi there';
const filePath = './test/testassets/utf8.txt';
2023-11-04 20:07:43 +01:00
const smartfileInstance = await smartfile.SmartFile.fromString(filePath, fileString, 'utf8');
smartfileInstance.write();
2023-11-04 20:07:43 +01:00
const smartfileInstance2 = await smartfile.SmartFile.fromFilePath(filePath);
const retrievedString = smartfileInstance.contents.toString();
2022-06-07 15:43:28 +02:00
expect(retrievedString).toEqual(fileString);
});
2023-01-09 15:32:37 +01:00
tap.test('should get a hash', async () => {
const fileString = 'hi there';
const filePath = './test/testassets/utf8.txt';
2023-11-04 20:07:43 +01:00
const smartfileInstance = await smartfile.SmartFile.fromString(filePath, fileString, 'utf8');
2023-01-09 15:32:37 +01:00
const hash = await smartfileInstance.getHash();
console.log(hash);
});
2021-12-22 19:08:53 +01:00
tap.test('should wait for file to be ready', async () => {
2022-03-11 09:46:54 +01:00
await smartfile.fs.waitForFileToBeReady('./test/testassets/mytest.json');
});
2021-12-22 19:08:53 +01:00
tap.start();