From 84fcc8f825d895e52267de1c9e930b83f4880769 Mon Sep 17 00:00:00 2001 From: JPeer264 Date: Wed, 2 Nov 2016 23:30:14 +0100 Subject: [PATCH] updated specs and error handling --- lib/create.js | 12 ++++-------- lib/createSync.js | 13 ++----------- lib/toPath.js | 1 - test/create.spec.js | 29 +++++++++++++++++++++++++++++ test/createSync.spec.js | 8 ++++++++ 5 files changed, 43 insertions(+), 20 deletions(-) diff --git a/lib/create.js b/lib/create.js index 8836a43..2578a1f 100644 --- a/lib/create.js +++ b/lib/create.js @@ -27,18 +27,14 @@ function create (pathString, filename, content, callback) { } if (typeof content === 'object') { - if (!check('object', content)) { - return callback({ - message: 'JSON is not valid', - error: 'NOJSON' - }); - } - content = JSON.stringify(content); } if (typeof content !== 'string') { - return callback({err: 'Json string or object required'}); + return callback({ + message: 'JSON is not valid', + error: 'NOJSON' + }); } if (!check(content)) { diff --git a/lib/createSync.js b/lib/createSync.js index 3737b2c..b50ba6c 100644 --- a/lib/createSync.js +++ b/lib/createSync.js @@ -17,21 +17,12 @@ function createSync(pathString, filename, content) { content = typeof content === 'undefined' ? '{}' : content; if (typeof content === 'object') { - if (!check('object', content)) { - return false; - } - content = JSON.stringify(content); } - if (typeof content !== 'string') { - console.log('Error. Please enter a json string as content.'.bold.red); - return false; - } + if (typeof content !== 'string') return false; - if (!check(content)) { - return false; - } + if (!check(content)) return false; fs.ensureDirSync(pathString); fs.writeFileSync(combinePath, content, 'utf8'); diff --git a/lib/toPath.js b/lib/toPath.js index 59c7b26..368f974 100644 --- a/lib/toPath.js +++ b/lib/toPath.js @@ -1,6 +1,5 @@ 'use strict'; -var mkdirp = require('mkdirp'); var colors = require('colors'); var path = require('path'); var json = require('../index'); diff --git a/test/create.spec.js b/test/create.spec.js index ae9b31c..5ed98b8 100644 --- a/test/create.spec.js +++ b/test/create.spec.js @@ -32,6 +32,35 @@ describe('create.js', function () { }); }); + it('should generate an empty json file', function (done) { + base.create(testCwd, 'test.json', function (err) { + expect(err).to.not.exist; + + expect(fs.existsSync(path.join(testCwd, 'test.json'))).to.be.true; + + done(); + }); + }); + + it('should set the extname to json if it is not set to it', function (done) { + base.create(testCwd, 'test.jayson', '{"test": "string"}', function (err) { + expect(err).to.not.exist; + + expect(fs.existsSync(path.join(testCwd, 'test.json'))).to.be.true; + expect(fs.existsSync(path.join(testCwd, 'test.jayson'))).to.be.false; + + done(); + }); + }); + + it('should fail', function (done) { + base.create(testCwd, 'test.jayson', 123123, function (err) { + expect(err.error).to.equal('NOJSON'); + + done(); + }); + }); + it('should fail', function (done) { base.create(testCwd, 'test.json', '{"test"; "string"}', function (err) { expect(err.error).to.equal('NOJSON'); diff --git a/test/createSync.spec.js b/test/createSync.spec.js index 6ab307f..ce182e8 100644 --- a/test/createSync.spec.js +++ b/test/createSync.spec.js @@ -43,4 +43,12 @@ describe('createSync.js', function () { done(); }); + + it('should fail', function (done) { + var file = base.createSync(testCwd, 'test.json', 123123); + + expect(file).to.be.false; + + done(); + }); });