| Server IP : 68.183.124.220 / Your IP : 216.73.217.137 Web Server : Apache/2.4.18 (Ubuntu) System : Linux Sandbox-A 4.4.0-210-generic #242-Ubuntu SMP Fri Apr 16 09:57:56 UTC 2021 x86_64 User : gavin ( 1000) PHP Version : 7.0.33-0ubuntu0.16.04.16 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /home/gavin/workspace/happymandarin/node_modules/rcfinder/test/ |
Upload File : |
describe('RcFinder', function () {
var RcFinder = require('../');
var path = require('path');
var expect = require('expect.js');
var fs = require('fs');
var fixtures = {
root: path.resolve(__dirname, 'fixtures/foo/foo/foo/foo/'),
json: path.resolve(__dirname, 'fixtures/foo/foo/bar.json'),
text: path.resolve(__dirname, 'fixtures/foo/foo/.baz'),
checkCount: 4,
config: {
baz: 'bog'
}
};
it('looks for config files', function () {
var rcFinder = new RcFinder('bar.json');
var config = rcFinder.find(path.resolve(__dirname, 'fixtures/foo/foo/foo/foo/'));
expect(config).to.eql(fixtures.config);
});
it('can be run async by using a callback', function (done) {
var rcFinder = new RcFinder('bar.json');
var count = 0;
rcFinder.find(fixtures.root, function (err, config) {
expect(count).to.eql(1); // prove async
expect(config).to.eql(fixtures.config);
done();
});
count ++;
});
it('caches config objects', function () {
var count = 0;
var rcFinder = new RcFinder('bar.json', {
loader: function (path) {
count ++;
return JSON.parse(fs.readFileSync(path));
}
});
var config = rcFinder.find(fixtures.root);
expect(count).to.eql(1);
expect(config).to.eql(fixtures.config);
// it should only be loaded once
config = rcFinder.find(fixtures.root);
expect(count).to.eql(1);
expect(config).to.eql(fixtures.config);
});
it('caches config objects from async calls', function (done) {
var count = 0;
var rcFinder = new RcFinder('bar.json', {
loader: function (path, cb) {
count ++;
fs.readFile(path, function (err, file) {
var config;
if (!err) {
try {
config = JSON.parse(file);
} catch(e) {
err = cb(new Error(path + ' is not valid JSON: ' + e.message));
}
}
cb(err, config);
});
}
});
rcFinder.find(fixtures.root, function (err, config) {
expect(count).to.eql(1);
expect(config).to.eql(fixtures.config);
rcFinder.find(fixtures.root, function (err, config) {
expect(count).to.eql(1);
expect(config).to.eql(fixtures.config);
done();
});
});
});
it('throws an error when called without a callback by an async loader is in use', function () {
expect(function () {
var rcFinder = new RcFinder('bar.json', {
loader: 'async'
});
rcFinder.find(fixtures.root);
}).to.throwException();
});
it('properly caches sync lookups when a config file is not found', function () {
var count = 0;
var expectedCount = fixtures.checkCount;
var rcFinder = new RcFinder('bar.json', {
_syncCheck: function (path) {
count++;
return fs.existsSync(path);
}
});
expect(count).to.eql(0);
rcFinder.find(fixtures.root);
expect(count).to.eql(expectedCount);
rcFinder.find(fixtures.root);
// it should still equal previous count
expect(count).to.eql(expectedCount);
});
it('properly caches async lookups when a config file is not found', function () {
var count = 0;
var expectedCount = fixtures.checkCount;
var rcFinder = new RcFinder('bar.json', {
_asyncCheck: function (path, cb) {
count++;
fs.stat(path, function (err, exists) {
if (err && err.code !== 'ENOENT') return cb(err);
cb(void 0, !err);
});
}
});
expect(count).to.eql(0);
rcFinder.find(fixtures.root, function (err, config) {
expect(count).to.eql(expectedCount);
rcFinder.find(fixtures.root, function (err, config) {
// it should still equal previous count
expect(count).to.eql(expectedCount);
});
});
});
it('throws errors from loader when loading and calling synchronously', function() {
var rcFinder = new RcFinder('bar.json', {
loader: function(path) {
return JSON.parse('{not really json');
}
});
expect(function () {
var config = rcFinder.find(fixtures.root);
}).to.throwException('Unexpected token n');
});
it('propagates errors from loader when loading synchronously and calling async', function(done) {
var rcFinder = new RcFinder('bar.json', {
loader: function(path) {
return JSON.parse('{not really json');
}
});
rcFinder.find(fixtures.root, function(err, config) {
expect(err).to.be.an(Error);
expect(err.message).to.contain('Unexpected token n');
expect(config).to.be(undefined);
done();
});
});
it('propagates error from loader when loading asynchronously', function(done) {
var rcFinder = new RcFinder('bar.json', {
loader: function(path, callback) {
process.nextTick(function() {
var err, body;
try {
body = JSON.parse('{not really json');
} catch (e) {
err = e;
} finally {
callback(err, body);
}
});
}
});
rcFinder.find(fixtures.root, function(err, config) {
expect(err).to.be.an(Error);
expect(err.message).to.contain('Unexpected token n');
expect(config).to.be(undefined);
done();
});
});
});