Show a helpful message when .all-contributorsrc doesn't exist. (#23)

* Show a helpful message when .all-contributorsrc doesn't exist.

* Adding unit tests.

* Better test descriptions.

* Just code improvements.
This commit is contained in:
fadc80 2016-10-17 16:11:33 -03:00 committed by Jeroen Engels
parent bae31e1f47
commit 190813c61e
3 changed files with 36 additions and 13 deletions

15
cli.js
View file

@ -2,7 +2,6 @@
/* eslint-disable no-console */
'use strict';
var fs = require('fs');
var path = require('path');
var yargs = require('yargs');
var inquirer = require('inquirer');
@ -31,11 +30,10 @@ var argv = yargs
.default('config', defaultRCFile)
.config('config', function (configPath) {
try {
return JSON.parse(fs.readFileSync(configPath, 'utf-8'));
return util.configFile.readConfig(configPath);
} catch (error) {
if (configPath !== defaultRCFile) {
console.error(error.message);
process.exit(1);
onError(error);
}
}
})
@ -80,17 +78,12 @@ function addContribution(argv, cb) {
function onError(error) {
if (error) {
return console.error(error);
console.error(error.message);
process.exit(1);
}
}
function promptForCommand(argv, cb) {
try {
fs.statSync(argv.config);
} catch (error) { // No config file --> first time using the command
return cb('init');
}
var questions = [{
type: 'list',
name: 'command',

View file

@ -4,7 +4,14 @@ var fs = require('fs');
var _ = require('lodash/fp');
function readConfig(configPath) {
return JSON.parse(fs.readFileSync(configPath, 'utf-8'));
try {
return JSON.parse(fs.readFileSync(configPath, 'utf-8'));
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error('Configuration file not found: ' + configPath);
}
throw error;
}
}
function writeConfig(configPath, content, cb) {
@ -12,7 +19,12 @@ function writeConfig(configPath, content, cb) {
}
function writeContributors(configPath, contributors, cb) {
var config = readConfig(configPath);
var config;
try {
config = readConfig(configPath);
} catch (error) {
return cb(error);
}
var content = _.assign(config, {contributors: contributors});
return writeConfig(configPath, content, cb);
}

View file

@ -0,0 +1,18 @@
import test from 'ava';
import configFile from './config-file.js';
const absentFile = './abc';
const expected = 'Configuration file not found: ' + absentFile;
test('Reading an absent configuration file throws a helpful error', t => {
t.throws(() => {
configFile.readConfig(absentFile);
}, expected);
});
test.cb('Writing contributors in an absent configuration file throws a helpful error', t => {
configFile.writeContributors(absentFile, [], error => {
t.is(error.message, expected);
t.end();
});
});