Skip to content

Commit

Permalink
updated specs and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
JPeer264 committed Nov 2, 2016
1 parent 536cfa6 commit 84fcc8f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
12 changes: 4 additions & 8 deletions lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
13 changes: 2 additions & 11 deletions lib/createSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
1 change: 0 additions & 1 deletion lib/toPath.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var mkdirp = require('mkdirp');
var colors = require('colors');
var path = require('path');
var json = require('../index');
Expand Down
29 changes: 29 additions & 0 deletions test/create.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
8 changes: 8 additions & 0 deletions test/createSync.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit 84fcc8f

Please sign in to comment.