smartfile/test/test.js

206 lines
21 KiB
JavaScript
Raw Normal View History

2016-04-04 13:44:00 +00:00
"use strict";
require("typings-test");
2016-07-17 15:34:15 +00:00
const smartfile = require("../dist/index");
2016-09-20 15:56:49 +00:00
let gulp = require('gulp');
let gFunction = require('gulp-function');
2016-07-17 15:34:15 +00:00
const path = require("path");
const should = require("should");
2016-09-20 15:56:49 +00:00
let vinyl = require('vinyl');
describe('smartfile'.yellow, function () {
describe('.fs'.yellow, function () {
describe('.fileExistsSync'.yellow, function () {
it('should return an accurate boolean', function () {
should(smartfile.fs.fileExistsSync('./test/mytest.json')).be.true();
should(smartfile.fs.fileExistsSync('./test/notthere.json')).be.false();
2016-02-04 13:21:48 +00:00
});
});
2016-09-20 15:56:49 +00:00
describe('.fileExists'.yellow, function () {
it('should return a working promise', function () {
should(smartfile.fs.fileExists('./test/mytest.json')).be.Promise();
should(smartfile.fs.fileExists('./test/mytest.json')).be.fulfilled();
should(smartfile.fs.fileExists('./test/notthere.json')).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 () {
should(smartfile.fs.listFoldersSync('./test/')).containDeep(['testfolder']);
should(smartfile.fs.listFoldersSync('./test/')).not.containDeep(['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) {
2016-09-20 15:56:49 +00:00
should(folderArrayArg).containDeep(['testfolder']);
should(folderArrayArg).not.containDeep(['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 () {
should(smartfile.fs.listFilesSync('./test/')).containDeep(['mytest.json']);
should(smartfile.fs.listFilesSync('./test/')).not.containDeep(['notExistentFile']);
should(smartfile.fs.listFilesSync('./test/', /mytest\.json/)).containDeep(['mytest.json']);
should(smartfile.fs.listFilesSync('./test/', /mytests.json/)).not.containDeep(['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) {
2016-09-20 15:56:49 +00:00
should(folderArrayArg).containDeep(['mytest.json']);
should(folderArrayArg).not.containDeep(['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) {
2016-09-20 15:56:49 +00:00
should(folderArrayArg).containDeep(['testfolder/testfile1.txt']);
should(folderArrayArg).not.containDeep(['mytest.json']);
2016-06-30 23:37:48 +00:00
done();
});
});
});
2016-09-20 15:56:49 +00:00
describe('.copy()'.yellow, function () {
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-20 15:56:49 +00:00
it('should remove single files', function () {
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 () {
should(smartfile.interpreter.filetype('./somefolder/data.json')).equal('json');
});
});
});
2016-09-20 15:56:49 +00:00
describe('.fs'.yellow, function () {
describe('toGulpStreamSync() and toGulpDestSync', function () {
it('should produce a gulp stream', function (done) {
smartfile.fs.toGulpStreamSync('./test/my*')
.pipe(smartfile.fs.toGulpDestSync('./test/temp/'))
.pipe(gFunction(done, 'atEnd'));
});
2016-03-14 02:50:14 +00:00
});
2016-09-20 15:56:49 +00:00
describe('.toObjectSync()'.yellow, function () {
it('should read an ' + '.yaml'.blue + ' file to an object', function () {
let testData = smartfile.fs.toObjectSync('./test/mytest.yaml');
should(testData).have.property('key1', 'this works');
should(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');
});
2016-09-20 15:56:49 +00:00
it('should read an ' + '.json'.blue + ' file to an object', function () {
let testData = smartfile.fs.toObjectSync('./test/mytest.json');
should(testData).have.property('key1', 'this works');
should(testData).have.property('key2', 'this works too');
});
2016-03-14 02:50:14 +00:00
});
2016-09-20 15:56:49 +00:00
describe('.toStringSync()'.yellow, function () {
it('should read a file to a string', function () {
should.equal(smartfile.fs.toStringSync('./test/mytest.txt'), 'Some TestString &&%$');
2016-03-19 21:07:59 +00:00
});
});
2016-09-20 15:56:49 +00:00
describe('.toVinylSync'.yellow, function () {
it('should read an ' + '.json OR .yaml'.blue + ' file to an ' + 'vinyl file object'.cyan, function () {
let testData = smartfile.fs.toVinylSync('./test/mytest.json');
should(vinyl.isVinyl(testData)).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-04-04 13:44:00 +00:00
smartfile.memory.toGulpStream(localArray)
2016-09-20 15:56:49 +00:00
.pipe(gulp.dest('./test/temp/'));
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' };
should(smartfile.memory.toVinylFileSync(localString, localOptions) instanceof vinyl).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);
2016-09-20 15:56:49 +00:00
should(testResult).be.Array();
should(testResult.length === 3).be.true();
2016-07-17 15:34:15 +00:00
for (let myKey in testResult) {
2016-09-20 15:56:49 +00:00
should(testResult[myKey] instanceof vinyl).be.true();
2016-04-04 13:44:00 +00:00
}
});
});
2016-09-20 15:56:49 +00:00
describe('toStringSync()', function () {
it('should produce a String from vinyl file', function () {
2016-07-17 15:34:15 +00:00
let localString = smartfile.memory.toStringSync(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
}));
2016-09-20 15:56:49 +00:00
should(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('toGulpStreamSync()', function () {
it('should produce a gulp stream', function (done) {
2016-09-18 10:26:43 +00:00
this.timeout(5000);
2016-09-20 15:56:49 +00:00
smartfile.remote.toGulpStreamSync('mytest.txt', 'https://raw.githubusercontent.com/pushrocks/smartfile/master/test/').pipe(smartfile.fs.toGulpDestSync('./test/temp/'))
.pipe(gFunction(done, 'atEnd'));
2016-03-19 21:07:59 +00:00
});
});
2016-09-20 15:56:49 +00:00
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) {
should.equal(responseString, '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
});
2016-09-20 15:56:49 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUNyQiwyQ0FBMEM7QUFFMUMsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLE1BQU0sQ0FBQyxDQUFBO0FBQzFCLElBQUksU0FBUyxHQUFHLE9BQU8sQ0FBQyxlQUFlLENBQUMsQ0FBQTtBQUN4Qyw2QkFBNkI7QUFDN0IsaUNBQWdDO0FBQ2hDLElBQUksS0FBSyxHQUFHLE9BQU8sQ0FBQyxPQUFPLENBQUMsQ0FBQTtBQUU1QixRQUFRLENBQUMsV0FBVyxDQUFDLE1BQU0sRUFBRTtJQUN6QixRQUFRLENBQUMsS0FBSyxDQUFDLE1BQU0sRUFBRTtRQUNuQixRQUFRLENBQUMsaUJBQWlCLENBQUMsTUFBTSxFQUFFO1lBQy9CLEVBQUUsQ0FBQyxtQ0FBbUMsRUFBRTtnQkFDcE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