smartfile/test/test.js

217 lines
23 KiB
JavaScript
Raw Normal View History

2016-04-04 13:44:00 +00:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("typings-test");
2016-07-17 15:34:15 +00:00
const smartfile = require("../dist/index");
const path = require("path");
2017-01-20 23:47:48 +00:00
const smartchai_1 = require("smartchai");
const vinyl = require("vinyl");
describe('smartfile', function () {
describe('.fs', function () {
describe('.fileExistsSync', function () {
2016-09-20 15:56:49 +00:00
it('should return an accurate boolean', function () {
2017-01-20 23:47:48 +00:00
smartchai_1.expect(smartfile.fs.fileExistsSync('./test/mytest.json')).to.be.true;
smartchai_1.expect(smartfile.fs.fileExistsSync('./test/notthere.json')).be.false;
2016-02-04 13:21:48 +00:00
});
});
describe('.fileExists', function () {
2016-09-20 15:56:49 +00:00
it('should return a working promise', function () {
2017-01-20 23:47:48 +00:00
smartchai_1.expect(smartfile.fs.fileExists('./test/mytest.json')).to.be.a('promise');
smartchai_1.expect(smartfile.fs.fileExists('./test/mytest.json')).to.be.fulfilled;
smartchai_1.expect(smartfile.fs.fileExists('./test/notthere.json')).to.not.be.fulfilled;
2016-06-23 16:31:55 +00:00
});
});
2016-09-20 15:56:49 +00:00
describe('.listFoldersSync()', function () {
it('should get the file type from a string', function () {
2017-01-20 23:47:48 +00:00
smartchai_1.expect(smartfile.fs.listFoldersSync('./test/')).to.deep.include('testfolder');
smartchai_1.expect(smartfile.fs.listFoldersSync('./test/')).to.not.deep.include('notExistentFolder');
2016-06-23 16:31:55 +00:00
});
});
2016-09-20 15:56:49 +00:00
describe('.listFolders()', function () {
it('should get the file type from a string', function (done) {
smartfile.fs.listFolders('./test/')
2016-06-23 16:31:55 +00:00
.then(function (folderArrayArg) {
2017-01-20 23:47:48 +00:00
smartchai_1.expect(folderArrayArg).to.deep.include('testfolder');
smartchai_1.expect(folderArrayArg).to.not.deep.include('notExistentFolder');
2016-06-23 16:31:55 +00:00
done();
});
2016-02-04 13:21:48 +00:00
});
});
2016-09-20 15:56:49 +00:00
describe('.listFilesSync()', function () {
it('should get the file type from a string', function () {
2017-01-20 23:47:48 +00:00
smartchai_1.expect(smartfile.fs.listFilesSync('./test/')).to.deep.include('mytest.json');
smartchai_1.expect(smartfile.fs.listFilesSync('./test/')).to.not.deep.include('notExistentFile');
smartchai_1.expect(smartfile.fs.listFilesSync('./test/', /mytest\.json/)).to.deep.include('mytest.json');
smartchai_1.expect(smartfile.fs.listFilesSync('./test/', /mytests.json/)).to.not.deep.include('mytest.json');
2016-06-28 07:59:59 +00:00
});
});
2016-09-20 15:56:49 +00:00
describe('.listFiles()', function () {
it('should get the file type from a string', function (done) {
smartfile.fs.listFiles('./test/')
2016-06-28 07:59:59 +00:00
.then(function (folderArrayArg) {
2017-01-20 23:47:48 +00:00
smartchai_1.expect(folderArrayArg).to.deep.include('mytest.json');
smartchai_1.expect(folderArrayArg).to.not.deep.include('notExistentFile');
2016-06-28 07:59:59 +00:00
done();
});
});
});
2016-09-20 15:56:49 +00:00
describe('.listFileTree()', function () {
it('should get a file tree', function (done) {
smartfile.fs.listFileTree(path.resolve('./test/'), '**/*.txt')
2016-06-30 23:37:48 +00:00
.then(function (folderArrayArg) {
2017-01-20 23:47:48 +00:00
smartchai_1.expect(folderArrayArg).to.deep.include('testfolder/testfile1.txt');
smartchai_1.expect(folderArrayArg).to.not.deep.include('mytest.json');
2016-06-30 23:37:48 +00:00
done();
});
});
});
describe('toObjectFromFileTree', function () {
it('should read a file tree into an Object', function () {
smartfile.fs.fileTreeToObject(path.resolve('./test/'), '**/*.txt')
.then((fileArrayArg) => {
// expect(fileArrayArg[1]).to.be.instanceof(smartfile.Smartfile)
});
});
});
describe('.copy()', function () {
2016-09-20 15:56:49 +00:00
it('should copy a directory', function () {
smartfile.fs.copy('./test/testfolder/', './test/temp/');
});
2016-09-20 15:56:49 +00:00
it('should copy a file', function () {
smartfile.fs.copy('./test/mytest.yaml', './test/temp/');
});
2016-09-20 15:56:49 +00:00
it('should copy a file and rename it', function () {
smartfile.fs.copy('./test/mytest.yaml', './test/temp/mytestRenamed.yaml');
});
});
2016-09-20 15:56:49 +00:00
describe('.remove()', function () {
it('should remove an entire directory', function () {
2016-03-20 23:28:29 +00:00
});
2016-09-29 12:17:46 +00:00
it('smartfile.fs.remove -> should remove single files', function (done) {
smartfile.fs.remove('./test/temp/mytestRenamed.yaml')
.then(() => { done(); });
});
it('smartfile.fs.removeSync -> should remove single files synchronouly', function () {
smartfile.fs.removeSync('./test/temp/testfile1.txt');
2017-01-20 23:47:48 +00:00
smartchai_1.expect(smartfile.fs.fileExistsSync('./test/temp/testfile1.txt')).to.be.false;
2016-09-29 12:17:46 +00:00
});
it('smartfile.fs.removeMany -> should remove and array of files', function (done) {
smartfile.fs.removeMany(['./test/temp/testfile1.txt', './test/temp/testfile2.txt']).then(() => {
2017-01-20 23:47:48 +00:00
smartchai_1.expect(smartfile.fs.fileExistsSync('./test/temp/testfile1.txt')).to.be.false;
smartchai_1.expect(smartfile.fs.fileExistsSync('./test/temp/testfile2.txt')).to.be.false;
2016-09-29 12:17:46 +00:00
done();
});
});
it('smartfile.fs.removeManySync -> should remove and array of single files synchronouly', function () {
smartfile.fs.removeManySync(['./test/temp/testfile1.txt', './test/temp/testfile2.txt']);
2017-01-20 23:47:48 +00:00
smartchai_1.expect(smartfile.fs.fileExistsSync('./test/temp/testfile1.txt')).to.be.false;
smartchai_1.expect(smartfile.fs.fileExistsSync('./test/temp/testfile2.txt')).to.be.false;
2016-04-02 21:03:18 +00:00
});
2016-03-20 23:28:29 +00:00
});
});
2016-09-20 15:56:49 +00:00
describe('.interpreter', function () {
describe('.filetype()', function () {
it('should get the file type from a string', function () {
2017-01-20 23:47:48 +00:00
smartchai_1.expect(smartfile.interpreter.filetype('./somefolder/data.json')).equal('json');
});
});
});
describe('.fs', function () {
describe('.toObjectSync()', function () {
it('should read an ' + '.yaml' + ' file to an object', function () {
2016-09-20 15:56:49 +00:00
let testData = smartfile.fs.toObjectSync('./test/mytest.yaml');
2017-01-20 23:47:48 +00:00
smartchai_1.expect(testData).have.property('key1', 'this works');
smartchai_1.expect(testData).have.property('key2', 'this works too');
});
2016-09-20 15:56:49 +00:00
it('should state unknown file type for unknown file types', function () {
let testData = smartfile.fs.toObjectSync('./test/mytest.txt');
});
it('should read an ' + '.json' + ' file to an object', function () {
2016-09-20 15:56:49 +00:00
let testData = smartfile.fs.toObjectSync('./test/mytest.json');
2017-01-20 23:47:48 +00:00
smartchai_1.expect(testData).have.property('key1', 'this works');
smartchai_1.expect(testData).have.property('key2', 'this works too');
});
2016-03-14 02:50:14 +00:00
});
describe('.toStringSync()', function () {
2016-09-20 15:56:49 +00:00
it('should read a file to a string', function () {
2017-01-20 23:47:48 +00:00
smartchai_1.expect(smartfile.fs.toStringSync('./test/mytest.txt'))
.to.equal('Some TestString &&%$');
2016-03-19 21:07:59 +00:00
});
});
describe('.toVinylSync', function () {
it('should read an ' + '.json OR .yaml' + ' file to an ' + 'vinyl file object', function () {
2016-09-20 15:56:49 +00:00
let testData = smartfile.fs.toVinylSync('./test/mytest.json');
2017-01-20 23:47:48 +00:00
smartchai_1.expect(vinyl.isVinyl(testData)).to.be.true;
});
});
});
2016-09-20 15:56:49 +00:00
describe('.memory', function () {
describe('.toGulpStream()', function () {
it('should produce a valid gulp stream', function () {
let localArray = ['test1', 'test2', 'test3'];
2016-10-27 16:55:18 +00:00
smartfile.memory.toGulpStream(localArray);
2016-04-04 13:44:00 +00:00
});
});
2016-09-20 15:56:49 +00:00
describe('toVinylFileSync()', function () {
it('should produce a vinylFile', function () {
let localString = 'myString';
let localOptions = { filename: 'vinylfile2', base: '/someDir' };
2017-01-20 23:47:48 +00:00
smartchai_1.expect(smartfile.memory.toVinylFileSync(localString, localOptions) instanceof vinyl).to.be.true;
2016-04-04 13:44:00 +00:00
});
});
2016-09-20 15:56:49 +00:00
describe('toVinylArraySync()', function () {
it('should produce a an array of vinylfiles', function () {
let localStringArray = ['string1', 'string2', 'string3'];
let localOptions = { filename: 'vinylfile2', base: '/someDir' };
2016-07-17 15:34:15 +00:00
let testResult = smartfile.memory.toVinylArraySync(localStringArray, localOptions);
2017-01-20 23:47:48 +00:00
smartchai_1.expect(testResult).to.be.a('array');
smartchai_1.expect(testResult.length === 3).to.be.true;
2016-07-17 15:34:15 +00:00
for (let myKey in testResult) {
2017-01-20 23:47:48 +00:00
smartchai_1.expect(testResult[myKey] instanceof vinyl).to.be.true;
2016-04-04 13:44:00 +00:00
}
});
});
describe('vinylToStringSync()', function () {
2016-09-20 15:56:49 +00:00
it('should produce a String from vinyl file', function () {
let localString = smartfile.memory.vinylToStringSync(new vinyl({
2016-09-20 15:56:49 +00:00
base: '/',
path: '/test.txt',
contents: new Buffer('myString')
2016-04-04 13:44:00 +00:00
}));
2017-01-20 23:47:48 +00:00
smartchai_1.expect(localString).equal('myString');
2016-04-04 13:44:00 +00:00
});
});
2016-09-20 15:56:49 +00:00
describe('toFs()', function () {
it('should write a file to disk and return a promise', function (done) {
let localString = 'myString';
smartfile.memory.toFs(localString, path.join(process.cwd(), './test/temp/testMemToFs.txt')).then(done);
2016-04-04 13:44:00 +00:00
});
});
2016-09-20 15:56:49 +00:00
describe('toFsSync()', function () {
it('should write a file to disk and return true if successfull', function () {
let localString = 'myString';
smartfile.memory.toFsSync(localString, path.join(process.cwd(), './test/temp/testMemToFsSync.txt'));
2016-04-04 13:44:00 +00:00
});
});
});
2016-09-20 15:56:49 +00:00
describe('.remote', function () {
describe('.toString()', function () {
it('should load a remote file to a variable', function (done) {
this.timeout(5000);
2016-09-20 15:56:49 +00:00
smartfile.remote.toString('https://raw.githubusercontent.com/pushrocks/smartfile/master/test/mytest.txt').then(function (responseString) {
2017-01-20 23:47:48 +00:00
smartchai_1.expect(responseString).to.equal('Some TestString &&%$');
done();
});
});
2016-09-20 15:56:49 +00:00
it('should reject a Promise when the link is false', function (done) {
2016-04-02 21:03:18 +00:00
this.timeout(10000);
2016-09-20 15:56:49 +00:00
smartfile.remote.toString('https://push.rocks/doesnotexist.txt')
.then(function () {
2016-09-20 15:56:49 +00:00
throw new Error('this test should not be resolved');
}, function () {
done();
});
});
2016-03-14 02:50:14 +00:00
});
});
2016-02-03 11:52:09 +00:00
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSx3QkFBcUI7QUFDckIsMkNBQTBDO0FBQzFDLDZCQUE2QjtBQUU3Qix5Q0FBa0M7QUFFbEMsK0JBQThCO0FBRTlCLFFBQVEsQ0FBQyxXQUFXLEVBQUU7SUFDcEIsUUFBUSxDQUFDLEtBQUssRUFBRTtRQUNkLFFBQVEsQ0FBQyxpQkFBaUIsRUFBRTtZQUMxQixFQUFFLENBQUMsbUNBQW1DLEVBQUU7Z0JBQ3RDLGtCQUFNLENBQUMsU0FBUyxDQUFDLEVBQUUsQ0FBQyxjQUFjLENBQUMsb0JBQW9CLENBQUMsQ0FBQyxDQUFDLEVBQUUsQ0FBQyxFQUFFLENBQUMsSUFBSSxDQUFBO2dCQUNwRSxrQkFBTSxDQUFDLFNBQVMsQ0FBQyxFQUFFLENBQUMsY0FBYyxDQUFDLHNCQUFzQixDQUFDLENBQUMsQ0FBQyxFQUFFLENBQUMsS0FBSyxDQUFBO1lBQ3RFLENBQUMsQ0FBQyxDQUFBO1FBQ0osQ0FBQyxDQUFDLENBQUE7UUFDRixRQUFRLENBQUMsYUFBYSxFQUFFO1lBQ3RCLEVBQUUsQ0FBQyxpQ0FBaUMsRUFBRTtnQkFDcEMsa0JBQU0sQ0FBQyxTQUFTLENBQUMsRUFBRSxDQUFDLFVBQVUsQ0FBQyxvQkFBb0IsQ0FBQyxDQUFDLENBQUMsRUFBRSxDQUFDLEVBQUUsQ0FBQyxDQUFDLENBQUMsU0FBUyxDQUFDLENBQUE7Z0JBQ3hFLGtCQUFNLENBQUMsU0FBUyxD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