smartfile/test/test.ts

187 lines
7.5 KiB
TypeScript
Raw Normal View History

2017-07-30 20:53:54 +00:00
import * as smartfile from '../ts/index'
2016-09-20 15:56:49 +00:00
import path = require('path')
2017-01-20 23:47:48 +00:00
2017-04-27 14:48:08 +00:00
import { expect, tap } from '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 () => {
expect(smartfile.fs.fileExistsSync('./test/mytest.json')).to.be.true
expect(smartfile.fs.fileExistsSync('./test/notthere.json')).be.false
})
2017-04-27 14:48:08 +00:00
tap.test('.fs.fileExists should resolve or reject a promise', async () => {
expect(smartfile.fs.fileExists('./test/mytest.json')).to.be.instanceof(Promise)
await expect(smartfile.fs.fileExists('./test/mytest.json')).to.eventually.be.fulfilled
await expect(smartfile.fs.fileExists('./test/notthere.json')).to.eventually.be.rejected
})
2017-04-27 14:48:08 +00:00
tap.test('.fs.listFoldersSync() -> should get the file type from a string', async () => {
expect(smartfile.fs.listFoldersSync('./test/')).to.deep.include('testfolder')
expect(smartfile.fs.listFoldersSync('./test/')).to.not.deep.include('notExistentFolder')
})
tap.test('.fs.listFolders() -> should get the file type from a string', async () => {
let folderArrayArg = await smartfile.fs.listFolders('./test/')
expect(folderArrayArg).to.deep.include('testfolder')
expect(folderArrayArg).to.not.deep.include('notExistentFolder')
})
tap.test('.fs.listFilesSync() -> should get the file type from a string', async () => {
expect(smartfile.fs.listFilesSync('./test/')).to.deep.include('mytest.json')
expect(smartfile.fs.listFilesSync('./test/')).to.not.deep.include('notExistentFile')
expect(smartfile.fs.listFilesSync('./test/', /mytest\.json/)).to.deep.include('mytest.json')
expect(smartfile.fs.listFilesSync('./test/', /mytests.json/)).to.not.deep.include('mytest.json')
})
tap.test('.fs.listFiles() -> should get the file type from a string', async () => {
let folderArrayArg = await smartfile.fs.listFiles('./test/')
expect(folderArrayArg).to.deep.include('mytest.json')
expect(folderArrayArg).to.not.deep.include('notExistentFile')
})
tap.test('.fs.listFileTree() -> should get a file tree', async () => {
let folderArrayArg = await smartfile.fs.listFileTree(path.resolve('./test/'), '**/*.txt')
expect(folderArrayArg).to.deep.include('testfolder/testfile1.txt')
expect(folderArrayArg).to.not.deep.include('mytest.json')
})
2017-05-01 17:49:34 +00:00
tap.test('.fs.fileTreeToObject -> should read a file tree into an Object', async () => {
2017-04-27 14:48:08 +00:00
let fileArrayArg = await smartfile.fs.fileTreeToObject(path.resolve('./test/'), '**/*.txt')
2017-05-26 12:47:41 +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 () => {
smartfile.fs.copy('./test/testfolder/', './test/temp/')
})
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 () => {
smartfile.fs.copy('./test/mytest.yaml', './test/temp/')
2017-04-27 14:48:08 +00:00
})
2017-04-28 09:28:11 +00:00
tap.test('.fs.copy() -> should copy a file and rename it', async () => {
smartfile.fs.copy('./test/mytest.yaml', './test/temp/mytestRenamed.yaml')
})
tap.test('.fs.remove() -> should remove an entire directory', async () => {
})
2017-04-30 16:13:17 +00:00
tap.test('.fs.remove -> should remove single files', async () => {
2017-04-28 09:28:11 +00:00
await expect(smartfile.fs.remove('./test/temp/mytestRenamed.yaml')).to.eventually.be.fulfilled
})
2017-04-30 16:13:17 +00:00
tap.test('.fs.removeSync -> should remove single files synchronouly', async () => {
2017-04-28 09:28:11 +00:00
smartfile.fs.removeSync('./test/temp/testfile1.txt')
expect(smartfile.fs.fileExistsSync('./test/temp/testfile1.txt')).to.be.false
})
2017-04-30 16:13:17 +00:00
tap.test('.fs.removeMany -> should remove and array of files', async () => {
2017-04-28 09:28:11 +00:00
smartfile.fs.removeMany([ './test/temp/testfile1.txt', './test/temp/testfile2.txt' ]).then(() => {
2017-04-27 14:48:08 +00:00
expect(smartfile.fs.fileExistsSync('./test/temp/testfile1.txt')).to.be.false
expect(smartfile.fs.fileExistsSync('./test/temp/testfile2.txt')).to.be.false
})
})
2017-04-30 16:13:17 +00:00
tap.test('.fs.removeManySync -> should remove and array of single files synchronouly', async () => {
2017-04-28 09:28:11 +00:00
smartfile.fs.removeManySync([ './test/temp/testfile1.txt', './test/temp/testfile2.txt' ])
expect(smartfile.fs.fileExistsSync('./test/temp/testfile1.txt')).to.be.false
expect(smartfile.fs.fileExistsSync('./test/temp/testfile2.txt')).to.be.false
})
tap.test('.fs.toObjectSync() -> should read an ' + '.yaml' + ' file to an object', async () => {
let testData = smartfile.fs.toObjectSync('./test/mytest.yaml')
expect(testData).have.property('key1', 'this works')
expect(testData).have.property('key2', 'this works too')
2017-04-28 09:28:11 +00:00
})
tap.test('.fs.toObjectSync() -> should state unknown file type for unknown file types', async () => {
let testData = smartfile.fs.toObjectSync('./test/mytest.txt')
})
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 () => {
let testData = smartfile.fs.toObjectSync('./test/mytest.json')
expect(testData).have.property('key1', 'this works')
expect(testData).have.property('key2', 'this works too')
2017-04-27 14:48:08 +00:00
})
2017-04-28 09:28:11 +00:00
tap.test('.fs.toStringSync() -> should read a file to a string', async () => {
expect(smartfile.fs.toStringSync('./test/mytest.txt'))
.to.equal('Some TestString &&%$')
})
2017-04-30 16:13:17 +00:00
// ---------------------------
// smartfile.interpreter
// ---------------------------
tap.test('.interpreter.filetype() -> should get the file type from a string', async () => {
expect(smartfile.interpreter.filetype('./somefolder/data.json')).equal('json')
})
// ---------------------------
// 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 () => {
let localString = 'myString'
2017-04-28 09:32:05 +00:00
await smartfile.memory.toFs(
2017-04-28 09:28:11 +00:00
localString,
path.join(process.cwd(), './test/temp/testMemToFs.txt')
)
})
tap.test('.memory.toFsSync() -> should write a file to disk and return true if successfull', async () => {
let localString = 'myString'
smartfile.memory.toFsSync(
localString,
path.join(process.cwd(), './test/temp/testMemToFsSync.txt')
)
2017-04-27 14:48:08 +00:00
})
tap.test('.remote.toString() -> should load a remote file to a variable', async () => {
let responseString = await smartfile.remote.toString(
'https://raw.githubusercontent.com/pushrocks/smartfile/master/test/mytest.txt'
)
expect(responseString).to.equal('Some TestString &&%$')
2016-09-20 15:56:49 +00:00
})
2017-04-27 14:48:08 +00:00
tap.test('.remote.toString() -> should reject a Promise when the link is false', async () => {
await expect(smartfile.remote.toString('https://push.rocks/doesnotexist.txt'))
.to.eventually.be.rejected
})
2017-04-30 16:13:17 +00:00
// ---------------------------
// smartfile.Smartfile
// ---------------------------
tap.test('.Smartfile -> should produce vinyl compatible files', async () => {
let smartfileArray = await smartfile.fs.fileTreeToObject(process.cwd(), './test/testfolder/**/*')
2017-05-26 12:47:41 +00:00
let localSmartfile = smartfileArray[ 0 ]
2017-04-30 16:13:17 +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
2017-04-30 16:13:17 +00:00
expect(localSmartfile.isBuffer()).to.be.true
2017-05-26 23:07:32 +00:00
// tslint:disable-next-line:no-unused-expression
2017-04-30 16:13:17 +00:00
expect(localSmartfile.isDirectory()).to.be.false
2017-05-26 23:07:32 +00:00
// tslint:disable-next-line:no-unused-expression
2017-04-30 16:13:17 +00:00
expect(localSmartfile.isNull()).to.be.false
})
2017-05-26 12:47:41 +00:00
tap.test('should output a smartfile array to disk', async () => {
let smartfileArray = await smartfile.fs.fileTreeToObject('./test/testfolder/', '*')
for (let smartfile of smartfileArray) {
2017-05-27 21:47:39 +00:00
console.log(smartfile.relative)
console.log(smartfile.path)
console.log(smartfile.base)
console.log(smartfile.parsedPath)
2017-05-26 12:47:41 +00:00
}
await smartfile.memory.smartfileArrayToFs(smartfileArray, path.resolve('./test/temp/testoutput/'))
})
2017-04-27 14:48:08 +00:00
tap.start()