smartfile/test/test.js

208 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");
let gulp = require("gulp");
let gFunction = require("gulp-function");
const path = require("path");
const should = require("should");
let vinyl = require("vinyl");
describe("smartfile".yellow, function () {
2016-06-23 16:31:55 +00:00
describe(".fs".yellow, function () {
2016-02-04 13:21:48 +00:00
describe(".fileExistsSync".yellow, function () {
it("should return an accurate boolean", function () {
2016-06-23 16:31:55 +00:00
(smartfile.fs.fileExistsSync("./test/mytest.json")).should.be.true();
(smartfile.fs.fileExistsSync("./test/notthere.json")).should.be.false();
2016-02-04 13:21:48 +00:00
});
});
describe(".fileExists".yellow, function () {
it("should return a working promise", function () {
2016-06-23 16:31:55 +00:00
(smartfile.fs.fileExists("./test/mytest.json")).should.be.Promise();
(smartfile.fs.fileExists("./test/mytest.json")).should.be.fulfilled();
(smartfile.fs.fileExists("./test/notthere.json")).should.not.be.fulfilled();
});
});
2016-06-23 16:39:02 +00:00
describe(".listFoldersSync()", function () {
2016-06-23 16:31:55 +00:00
it("should get the file type from a string", function () {
2016-06-23 16:39:02 +00:00
smartfile.fs.listFoldersSync("./test/").should.containDeep(["testfolder"]);
smartfile.fs.listFoldersSync("./test/").should.not.containDeep(["notExistentFolder"]);
2016-06-23 16:31:55 +00:00
});
});
2016-06-23 16:39:02 +00:00
describe(".listFolders()", function () {
2016-06-23 16:31:55 +00:00
it("should get the file type from a string", function (done) {
2016-06-23 16:39:02 +00:00
smartfile.fs.listFolders("./test/")
2016-06-23 16:31:55 +00:00
.then(function (folderArrayArg) {
folderArrayArg.should.containDeep(["testfolder"]);
folderArrayArg.should.not.containDeep(["notExistentFolder"]);
done();
});
2016-02-04 13:21:48 +00:00
});
});
2016-06-28 07:59:59 +00:00
describe(".listFilesSync()", function () {
it("should get the file type from a string", function () {
smartfile.fs.listFilesSync("./test/").should.containDeep(["mytest.json"]);
smartfile.fs.listFilesSync("./test/").should.not.containDeep(["notExistentFile"]);
smartfile.fs.listFilesSync("./test/", /mytest\.json/).should.containDeep(["mytest.json"]);
smartfile.fs.listFilesSync("./test/", /mytests.json/).should.not.containDeep(["mytest.json"]);
});
});
describe(".listFiles()", function () {
it("should get the file type from a string", function (done) {
smartfile.fs.listFiles("./test/")
.then(function (folderArrayArg) {
folderArrayArg.should.containDeep(["mytest.json"]);
folderArrayArg.should.not.containDeep(["notExistentFile"]);
done();
});
});
});
2016-06-30 23:37:48 +00:00
describe(".listFileTree()", function () {
it("should get a file tree", function (done) {
smartfile.fs.listFileTree(path.resolve("./test/"), "**/*.txt")
.then(function (folderArrayArg) {
folderArrayArg.should.containDeep(["testfolder/testfile1.txt"]);
folderArrayArg.should.not.containDeep(["mytest.json"]);
done();
});
});
});
describe(".copy()".yellow, function () {
it("should copy a directory", function () {
2016-06-23 16:31:55 +00:00
smartfile.fs.copy("./test/testfolder/", "./test/temp/");
});
it("should copy a file", function () {
2016-06-23 16:31:55 +00:00
smartfile.fs.copy("./test/mytest.yaml", "./test/temp/");
});
it("should copy a file and rename it", function () {
2016-06-23 16:31:55 +00:00
smartfile.fs.copy("./test/mytest.yaml", "./test/temp/mytestRenamed.yaml");
});
});
2016-03-20 23:28:29 +00:00
describe(".remove()", function () {
it("should remove an entire directory", function () {
});
2016-04-02 21:03:18 +00:00
it("should remove single files", function () {
});
2016-03-20 23:28:29 +00:00
});
});
2016-06-23 16:31:55 +00:00
describe(".interpreter", function () {
describe(".filetype()", function () {
it("should get the file type from a string", function () {
2016-06-23 16:31:55 +00:00
smartfile.interpreter.filetype("./somefolder/data.json").should.equal("json");
});
});
});
2016-06-23 16:31:55 +00:00
describe(".fs".yellow, function () {
2016-03-19 21:07:59 +00:00
describe("toGulpStreamSync() and toGulpDestSync", function () {
it("should produce a gulp stream", function (done) {
2016-06-23 16:31:55 +00:00
smartfile.fs.toGulpStreamSync("./test/my*")
.pipe(smartfile.fs.toGulpDestSync("./test/temp/"))
2016-03-19 21:07:59 +00:00
.pipe(gFunction(done, "atEnd"));
});
2016-03-14 02:50:14 +00:00
});
describe(".toObjectSync()".yellow, function () {
it("should read an " + ".yaml".blue + " file to an object", function () {
2016-07-17 15:34:15 +00:00
let testData = smartfile.fs.toObjectSync("./test/mytest.yaml");
testData.should.have.property("key1", "this works");
testData.should.have.property("key2", "this works too");
});
it("should state unknown file type for unknown file types", function () {
2016-07-17 15:34:15 +00:00
let testData = smartfile.fs.toObjectSync("./test/mytest.txt");
});
it("should read an " + ".json".blue + " file to an object", function () {
2016-07-17 15:34:15 +00:00
let testData = smartfile.fs.toObjectSync("./test/mytest.json");
testData.should.have.property("key1", "this works");
testData.should.have.property("key2", "this works too");
});
2016-03-14 02:50:14 +00:00
});
2016-03-19 21:07:59 +00:00
describe(".toStringSync()".yellow, function () {
it("should read a file to a string", function () {
2016-06-23 16:31:55 +00:00
should.equal(smartfile.fs.toStringSync("./test/mytest.txt"), "Some TestString &&%$");
2016-03-19 21:07:59 +00:00
});
});
describe(".toVinylSync".yellow, function () {
it("should read an " + ".json OR .yaml".blue + " file to an " + "vinyl file object".cyan, function () {
2016-07-17 15:34:15 +00:00
let testData = smartfile.fs.toVinylSync("./test/mytest.json");
(vinyl.isVinyl(testData)).should.be.true();
});
});
});
2016-04-04 13:44:00 +00:00
describe(".memory", function () {
describe(".toGulpStream()", function () {
it("should produce a valid gulp stream", function () {
2016-07-17 15:34:15 +00:00
let localArray = ["test1", "test2", "test3"];
2016-04-04 13:44:00 +00:00
smartfile.memory.toGulpStream(localArray)
.pipe(gulp.dest("./test/temp/"));
});
});
describe("toVinylFileSync()", function () {
it("should produce a vinylFile", function () {
2016-07-17 15:34:15 +00:00
let localString = "myString";
let localOptions = { filename: "vinylfile2", base: "/someDir" };
2016-04-04 13:44:00 +00:00
(smartfile.memory.toVinylFileSync(localString, localOptions) instanceof vinyl).should.be.true();
});
});
describe("toVinylArraySync()", function () {
it("should produce a an array of vinylfiles", function () {
2016-07-17 15:34:15 +00:00
let localStringArray = ["string1", "string2", "string3"];
let localOptions = { filename: "vinylfile2", base: "/someDir" };
let testResult = smartfile.memory.toVinylArraySync(localStringArray, localOptions);
2016-04-04 13:44:00 +00:00
testResult.should.be.Array();
(testResult.length === 3).should.be.true();
2016-07-17 15:34:15 +00:00
for (let myKey in testResult) {
2016-04-04 13:44:00 +00:00
(testResult[myKey] instanceof vinyl).should.be.true();
}
});
});
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-04-04 13:44:00 +00:00
base: "/",
path: "/test.txt",
contents: new Buffer("myString")
}));
localString.should.equal("myString");
});
});
describe("toFs()", function () {
it("should write a file to disk and return a promise", function (done) {
2016-07-17 15:34:15 +00:00
let localString = "myString";
2016-06-24 01:36:51 +00:00
smartfile.memory.toFs(localString, path.join(process.cwd(), "./test/temp/testMemToFs.txt")).then(done);
2016-04-04 13:44:00 +00:00
});
});
describe("toFsSync()", function () {
it("should write a file to disk and return true if successfull", function () {
2016-07-17 15:34:15 +00:00
let localString = "myString";
2016-06-24 01:36:51 +00:00
smartfile.memory.toFsSync(localString, path.join(process.cwd(), "./test/temp/testMemToFsSync.txt"));
2016-04-04 13:44:00 +00:00
});
});
});
describe(".remote", function () {
2016-03-19 21:07:59 +00:00
describe("toGulpStreamSync()", function () {
it("should produce a gulp stream", function (done) {
2016-09-18 10:26:43 +00:00
this.timeout(5000);
2016-03-19 21:07:59 +00:00
smartfile.remote.toGulpStreamSync("mytest.txt", "https://raw.githubusercontent.com/pushrocks/smartfile/master/test/")
2016-06-23 16:31:55 +00:00
.pipe(smartfile.fs.toGulpDestSync("./test/temp/"))
2016-03-19 21:07:59 +00:00
.pipe(gFunction(done, "atEnd"));
});
});
describe(".toString()", function () {
it("should load a remote file to a variable", function (done) {
this.timeout(5000);
smartfile.remote.toString("https://raw.githubusercontent.com/pushrocks/smartfile/master/test/mytest.txt")
.then(function (responseString) {
should.equal(responseString, "Some TestString &&%$");
done();
});
});
it("should reject a Promise when the link is false", function (done) {
2016-04-02 21:03:18 +00:00
this.timeout(10000);
smartfile.remote.toString("https://push.rocks/doesnotexist.txt")
.then(function () {
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 10:20:46 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFzQjtBQUN0QiwyQ0FBMkM7QUFFM0MsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLE1BQU0sQ0FBQyxDQUFDO0FBQzNCLElBQUksU0FBUyxHQUFHLE9BQU8sQ0FBQyxlQUFlLENBQUMsQ0FBQztBQUN6Qyw2QkFBOEI7QUFDOUIsaUNBQWtDO0FBQ2xDLElBQUksS0FBSyxHQUFHLE9BQU8sQ0FBQyxPQUFPLENBQUMsQ0FBQztBQUU3QixRQUFRLENBQUMsV0FBVyxDQUFDLE1BQU0sRUFBQztJQUN4QixRQUFRLENBQUMsS0FBSyxDQUFDLE1BQU0sRUFBQztRQUNsQixRQUFRLENBQUMsaUJBQWlCLENBQUMsTUFBTSxFQUFDO1lBQzlCLEVBQUUsQ0FBQyxtQ0FBbUMsRUFBQztnQkFDbkMsQ0FBQyxTQUFTLENBQUMsRUFBRSxDQUFDLGNBQWMsQ0FBQyxvQkFBb0IsQ0FBQyxDQUFDLENBQUMsTUFBTSxDQUFDLEVBQUUsQ0FBQyxJQUFJLEVBQUUsQ0FBQztnQkFDckUsQ0FBQyxTQUFTLENBQUMsRUFBRSxDQUFDLGNBQWMsQ0FBQyxzQkFBc0IsQ0FBQyxDQUFDLENBQUMsTUFBTSxDQUFDLEVBQUUsQ0FBQyxLQUFLLEVBQUUsQ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