2018-07-03 06:55:09 +00:00
|
|
|
import * as smartfile from '../ts/index';
|
2020-10-11 15:34:24 +00:00
|
|
|
import * as path from 'path';
|
2017-01-20 23:47:48 +00:00
|
|
|
|
2018-08-19 19:21:26 +00:00
|
|
|
import { expect, tap } from '@pushrocks/tapbundle';
|
2017-01-20 23:47:48 +00:00
|
|
|
|
2017-04-29 14:50:06 +00:00
|
|
|
// ---------------------------
|
|
|
|
// smartfile.fs
|
|
|
|
// ---------------------------
|
|
|
|
|
2017-04-27 14:48:08 +00: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
|
2019-06-26 19:17:58 +00:00
|
|
|
expect(smartfile.fs.fileExistsSync('./test/testassets/mytest.json')).to.be.true;
|
2020-10-11 15:34:24 +00:00
|
|
|
// tslint:disable-next-line: no-unused-expression
|
2019-06-26 19:17:58 +00:00
|
|
|
expect(smartfile.fs.fileExistsSync('./test/testassets/notthere.json')).be.false;
|
2018-07-03 06:55:09 +00:00
|
|
|
});
|
2016-04-14 18:33:58 +00:00
|
|
|
|
2018-02-16 20:57:44 +00:00
|
|
|
tap.test('.fs.fileExists -> should resolve or reject a promise', async () => {
|
2019-06-26 19:17:58 +00:00
|
|
|
expect(smartfile.fs.fileExists('./test/testassets/mytest.json')).to.be.instanceof(Promise);
|
|
|
|
await smartfile.fs.fileExists('./test/testassets/mytest.json');
|
2020-08-10 20:58:22 +00:00
|
|
|
await smartfile.fs.fileExists('./test/testassets/notthere.json').catch((err) => {
|
2018-07-03 06:55:09 +00:00
|
|
|
return expect(err.message).to.equal(
|
2019-06-26 19:17:58 +00:00
|
|
|
"ENOENT: no such file or directory, access './test/testassets/notthere.json'"
|
2018-07-03 06:55:09 +00:00
|
|
|
);
|
2018-02-16 20:57:44 +00:00
|
|
|
});
|
2018-07-03 06:55:09 +00:00
|
|
|
});
|
2017-03-04 20:10:46 +00:00
|
|
|
|
2017-04-27 14:48:08 +00:00
|
|
|
tap.test('.fs.listFoldersSync() -> should get the file type from a string', async () => {
|
2019-06-26 19:17:58 +00:00
|
|
|
expect(smartfile.fs.listFoldersSync('./test/testassets/')).to.include('testfolder');
|
|
|
|
expect(smartfile.fs.listFoldersSync('./test/testassets/')).to.not.include('notExistentFolder');
|
2018-07-03 06:55:09 +00:00
|
|
|
});
|
2017-04-27 14:48:08 +00:00
|
|
|
|
|
|
|
tap.test('.fs.listFolders() -> should get the file type from a string', async () => {
|
2020-08-10 20:58:22 +00:00
|
|
|
const folderArrayArg = await smartfile.fs.listFolders('./test/testassets/');
|
2018-07-03 06:55:09 +00:00
|
|
|
expect(folderArrayArg).to.include('testfolder');
|
|
|
|
expect(folderArrayArg).to.not.include('notExistentFolder');
|
|
|
|
});
|
2017-04-27 14:48:08 +00:00
|
|
|
|
|
|
|
tap.test('.fs.listFilesSync() -> should get the file type from a string', async () => {
|
2019-06-26 19:17:58 +00:00
|
|
|
expect(smartfile.fs.listFilesSync('./test/testassets/')).to.include('mytest.json');
|
|
|
|
expect(smartfile.fs.listFilesSync('./test/testassets/')).to.not.include('notExistentFile');
|
2019-09-27 09:00:17 +00:00
|
|
|
expect(smartfile.fs.listFilesSync('./test/testassets/', /mytest\.json/)).to.include(
|
|
|
|
'mytest.json'
|
|
|
|
);
|
|
|
|
expect(smartfile.fs.listFilesSync('./test/testassets/', /mytests.json/)).to.not.include(
|
|
|
|
'mytest.json'
|
|
|
|
);
|
2018-07-03 06:55:09 +00:00
|
|
|
});
|
2017-04-27 14:48:08 +00:00
|
|
|
|
|
|
|
tap.test('.fs.listFiles() -> should get the file type from a string', async () => {
|
2020-08-10 20:58:22 +00:00
|
|
|
const folderArrayArg = await smartfile.fs.listFiles('./test/testassets/');
|
2018-07-03 06:55:09 +00:00
|
|
|
expect(folderArrayArg).to.include('mytest.json');
|
|
|
|
expect(folderArrayArg).to.not.include('notExistentFile');
|
|
|
|
});
|
2017-04-27 14:48:08 +00:00
|
|
|
|
|
|
|
tap.test('.fs.listFileTree() -> should get a file tree', async () => {
|
2020-08-10 20:58:22 +00:00
|
|
|
const folderArrayArg = await smartfile.fs.listFileTree(
|
2019-09-27 09:00:17 +00:00
|
|
|
path.resolve('./test/testassets/'),
|
|
|
|
'**/*.txt'
|
|
|
|
);
|
2018-07-03 06:55:09 +00:00
|
|
|
expect(folderArrayArg).to.include('testfolder/testfile1.txt');
|
|
|
|
expect(folderArrayArg).to.not.include('mytest.json');
|
|
|
|
});
|
2017-04-27 14:48:08 +00:00
|
|
|
|
2017-05-01 17:49:34 +00:00
|
|
|
tap.test('.fs.fileTreeToObject -> should read a file tree into an Object', async () => {
|
2020-08-10 20:58:22 +00:00
|
|
|
const fileArrayArg = await smartfile.fs.fileTreeToObject(
|
2019-09-27 09:00:17 +00:00
|
|
|
path.resolve('./test/testassets/'),
|
|
|
|
'**/*.txt'
|
|
|
|
);
|
2018-07-03 06:55:09 +00:00
|
|
|
expect(fileArrayArg[0]).to.be.instanceof(smartfile.Smartfile);
|
|
|
|
expect(fileArrayArg[0].contents.toString()).to.equal(fileArrayArg[0].contentBuffer.toString());
|
|
|
|
});
|
2017-04-27 14:48:08 +00:00
|
|
|
|
2017-04-28 09:28:11 +00:00
|
|
|
tap.test('.fs.copy() -> should copy a directory', async () => {
|
2019-06-27 08:42:35 +00:00
|
|
|
await smartfile.fs.copy('./test/testassets/testfolder/', './test/testassets/temp/');
|
2018-07-03 06:55:09 +00:00
|
|
|
});
|
2017-04-27 14:48:08 +00:00
|
|
|
|
2017-04-28 09:28:11 +00:00
|
|
|
tap.test('.fs.copy() -> should copy a file', async () => {
|
2019-06-27 08:42:35 +00:00
|
|
|
await smartfile.fs.copy('./test/testassets/mytest.yaml', './test/testassets/temp/mytest.yaml');
|
2018-07-03 06:55:09 +00:00
|
|
|
});
|
2016-03-18 16:34:31 +00:00
|
|
|
|
2017-04-28 09:28:11 +00:00
|
|
|
tap.test('.fs.copy() -> should copy a file and rename it', async () => {
|
2019-09-27 09:00:17 +00:00
|
|
|
await smartfile.fs.copy(
|
|
|
|
'./test/testassets/mytest.yaml',
|
|
|
|
'./test/testassets/temp/mytestRenamed.yaml'
|
|
|
|
);
|
2018-07-03 06:55:09 +00:00
|
|
|
});
|
2017-04-28 09:28:11 +00:00
|
|
|
|
2018-07-03 06:55:09 +00:00
|
|
|
tap.test('.fs.remove() -> should remove an entire directory', async () => {});
|
2017-04-28 09:28:11 +00:00
|
|
|
|
2017-04-30 16:13:17 +00:00
|
|
|
tap.test('.fs.remove -> should remove single files', async () => {
|
2019-06-26 19:17:58 +00:00
|
|
|
await smartfile.fs.remove('./test/testassets/temp/mytestRenamed.yaml');
|
2018-07-03 06:55:09 +00:00
|
|
|
});
|
2017-04-28 09:28:11 +00:00
|
|
|
|
2017-04-30 16:13:17 +00:00
|
|
|
tap.test('.fs.removeSync -> should remove single files synchronouly', async () => {
|
2019-06-26 19:17:58 +00:00
|
|
|
smartfile.fs.removeSync('./test/testassets/temp/testfile1.txt');
|
|
|
|
expect(smartfile.fs.fileExistsSync('./test/testassets/temp/testfile1.txt')).to.be.false;
|
2018-07-03 06:55:09 +00:00
|
|
|
});
|
2017-04-28 09:28:11 +00:00
|
|
|
|
2017-04-30 16:13:17 +00:00
|
|
|
tap.test('.fs.removeMany -> should remove and array of files', async () => {
|
2019-09-27 09:00:17 +00:00
|
|
|
smartfile.fs
|
|
|
|
.removeMany(['./test/testassets/temp/testfile1.txt', './test/testassets/temp/testfile2.txt'])
|
|
|
|
.then(() => {
|
|
|
|
expect(smartfile.fs.fileExistsSync('./test/testassets/temp/testfile1.txt')).to.be.false;
|
|
|
|
expect(smartfile.fs.fileExistsSync('./test/testassets/temp/testfile2.txt')).to.be.false;
|
|
|
|
});
|
2018-07-03 06:55:09 +00:00
|
|
|
});
|
2016-03-18 16:34:31 +00:00
|
|
|
|
2017-04-30 16:13:17 +00:00
|
|
|
tap.test('.fs.removeManySync -> should remove and array of single files synchronouly', async () => {
|
2019-09-27 09:00:17 +00:00
|
|
|
smartfile.fs.removeManySync([
|
|
|
|
'./test/testassets/temp/testfile1.txt',
|
2020-08-10 20:58:22 +00:00
|
|
|
'./test/testassets/temp/testfile2.txt',
|
2019-09-27 09:00:17 +00:00
|
|
|
]);
|
2019-06-26 19:17:58 +00:00
|
|
|
expect(smartfile.fs.fileExistsSync('./test/testassets/temp/testfile1.txt')).to.be.false;
|
|
|
|
expect(smartfile.fs.fileExistsSync('./test/testassets/temp/testfile2.txt')).to.be.false;
|
2018-07-03 06:55:09 +00:00
|
|
|
});
|
2017-04-28 09:28:11 +00:00
|
|
|
|
|
|
|
tap.test('.fs.toObjectSync() -> should read an ' + '.yaml' + ' file to an object', async () => {
|
2020-08-10 20:58:22 +00:00
|
|
|
const testData = smartfile.fs.toObjectSync('./test/testassets/mytest.yaml');
|
2018-07-03 06:55:09 +00:00
|
|
|
expect(testData).to.include({ key1: 'this works' });
|
|
|
|
expect(testData).to.include({ key2: 'this works too' });
|
|
|
|
});
|
|
|
|
tap.test(
|
|
|
|
'.fs.toObjectSync() -> should state unknown file type for unknown file types',
|
|
|
|
async () => {
|
2020-08-10 20:58:22 +00:00
|
|
|
const testData = smartfile.fs.toObjectSync('./test/testassets/mytest.txt');
|
2018-07-03 06:55:09 +00:00
|
|
|
}
|
|
|
|
);
|
2017-04-27 14:48:08 +00:00
|
|
|
|
2017-04-28 09:28:11 +00:00
|
|
|
tap.test('.fs.toObjectSync() -> should read an ' + '.json' + ' file to an object', async () => {
|
2020-08-10 20:58:22 +00:00
|
|
|
const testData = smartfile.fs.toObjectSync('./test/testassets/mytest.json');
|
2018-07-03 06:55:09 +00:00
|
|
|
expect(testData).to.include({ key1: 'this works' });
|
|
|
|
expect(testData).to.include({ key2: 'this works too' });
|
|
|
|
});
|
2017-04-28 09:28:11 +00:00
|
|
|
|
|
|
|
tap.test('.fs.toStringSync() -> should read a file to a string', async () => {
|
2019-09-27 09:00:17 +00:00
|
|
|
expect(smartfile.fs.toStringSync('./test/testassets/mytest.txt')).to.equal(
|
|
|
|
'Some TestString &&%$'
|
|
|
|
);
|
2018-07-03 06:55:09 +00:00
|
|
|
});
|
2017-04-28 09:28:11 +00:00
|
|
|
|
2017-04-30 16:13:17 +00:00
|
|
|
// ---------------------------
|
|
|
|
// smartfile.interpreter
|
|
|
|
// ---------------------------
|
|
|
|
|
|
|
|
tap.test('.interpreter.filetype() -> should get the file type from a string', async () => {
|
2018-07-03 06:55:09 +00:00
|
|
|
expect(smartfile.interpreter.filetype('./somefolder/data.json')).equal('json');
|
|
|
|
});
|
2017-04-30 16:13:17 +00:00
|
|
|
|
|
|
|
// ---------------------------
|
|
|
|
// smartfile.memory
|
|
|
|
// ---------------------------
|
|
|
|
|
2017-04-28 09:28:11 +00:00
|
|
|
tap.test('.memory.toFs() -> should write a file to disk and return a promise', async () => {
|
2020-08-10 20:58:22 +00:00
|
|
|
const localString = 'myString';
|
2019-09-27 09:00:17 +00:00
|
|
|
await smartfile.memory.toFs(
|
|
|
|
localString,
|
|
|
|
path.join(process.cwd(), './test/testassets/temp/testMemToFs.txt')
|
|
|
|
);
|
2018-07-03 06:55:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tap.test(
|
|
|
|
'.memory.toFsSync() -> should write a file to disk and return true if successfull',
|
|
|
|
async () => {
|
2020-08-10 20:58:22 +00:00
|
|
|
const localString = 'myString';
|
2018-07-03 06:55:09 +00:00
|
|
|
smartfile.memory.toFsSync(
|
|
|
|
localString,
|
2019-06-26 19:17:58 +00:00
|
|
|
path.join(process.cwd(), './test/testassets/temp/testMemToFsSync.txt')
|
2018-07-03 06:55:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2017-04-27 14:48:08 +00:00
|
|
|
|
2017-04-30 16:13:17 +00:00
|
|
|
// ---------------------------
|
|
|
|
// smartfile.Smartfile
|
|
|
|
// ---------------------------
|
|
|
|
|
|
|
|
tap.test('.Smartfile -> should produce vinyl compatible files', async () => {
|
2020-08-10 20:58:22 +00:00
|
|
|
const smartfileArray = await smartfile.fs.fileTreeToObject(
|
2019-09-27 09:00:17 +00:00
|
|
|
process.cwd(),
|
|
|
|
'./test/testassets/testfolder/**/*'
|
|
|
|
);
|
2020-08-10 20:58:22 +00:00
|
|
|
const localSmartfile = smartfileArray[0];
|
2018-07-03 06:55:09 +00:00
|
|
|
expect(localSmartfile).to.be.instanceof(smartfile.Smartfile);
|
|
|
|
expect(localSmartfile.contents).to.be.instanceof(Buffer);
|
2017-05-26 23:07:32 +00:00
|
|
|
// tslint:disable-next-line:no-unused-expression
|
2018-07-03 06:55:09 +00:00
|
|
|
expect(localSmartfile.isBuffer()).to.be.true;
|
2017-05-26 23:07:32 +00:00
|
|
|
// tslint:disable-next-line:no-unused-expression
|
2018-07-03 06:55:09 +00:00
|
|
|
expect(localSmartfile.isDirectory()).to.be.false;
|
2017-05-26 23:07:32 +00:00
|
|
|
// tslint:disable-next-line:no-unused-expression
|
2018-07-03 06:55:09 +00:00
|
|
|
expect(localSmartfile.isNull()).to.be.false;
|
|
|
|
});
|
2017-04-30 16:13:17 +00:00
|
|
|
|
2017-05-26 12:47:41 +00:00
|
|
|
tap.test('should output a smartfile array to disk', async () => {
|
2020-08-10 20:58:22 +00:00
|
|
|
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 12:47:41 +00:00
|
|
|
}
|
2018-07-03 06:55:09 +00:00
|
|
|
await smartfile.memory.smartfileArrayToFs(
|
|
|
|
smartfileArray,
|
2019-06-26 19:17:58 +00:00
|
|
|
path.resolve('./test/testassets/temp/testoutput/')
|
2018-07-03 06:55:09 +00:00
|
|
|
);
|
|
|
|
});
|
2017-05-26 12:47:41 +00:00
|
|
|
|
2020-08-10 20:58:22 +00:00
|
|
|
tap.test('should create, store and retrieve valid smartfiles', async () => {
|
|
|
|
const fileString = 'hi there';
|
|
|
|
const filePath = './test/testassets/utf8.txt';
|
|
|
|
const smartfileInstance = await smartfile.Smartfile.fromString(filePath, fileString, 'utf8');
|
|
|
|
smartfileInstance.write();
|
|
|
|
const smartfileInstance2 = await smartfile.Smartfile.fromFilePath(filePath);
|
|
|
|
const retrievedString = smartfileInstance.contents.toString();
|
|
|
|
expect(retrievedString).to.equal(fileString);
|
|
|
|
});
|
|
|
|
|
2021-12-22 18:08:53 +00:00
|
|
|
tap.test('should wait for file to be ready', async () => {
|
2022-03-11 08:46:54 +00:00
|
|
|
await smartfile.fs.waitForFileToBeReady('./test/testassets/mytest.json');
|
|
|
|
});
|
2021-12-22 18:08:53 +00:00
|
|
|
|
2018-07-03 06:55:09 +00:00
|
|
|
tap.start();
|