Skip to content

Commit

Permalink
feat: add config to disable or silence root require check
Browse files Browse the repository at this point in the history
closes #158
  • Loading branch information
shadowgate15 committed Feb 21, 2022
1 parent 6a6473b commit 30fd2c0
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class Bree extends EventEmitter {
// Set this to `false` to prevent requiring a root directory of jobs
// (e.g. if your jobs are not all in one directory)
root: resolve('jobs'),
// Set this to `true` to silence root check error log
silenceRootCheckError: false,
// Set this to `false` to prevent requiring a root directory of jobs
doRootCheck: true,
// Default timeout for jobs
// (set this to `false` if you do not wish for a default timeout to be set)
timeout: 0,
Expand Down Expand Up @@ -191,12 +195,15 @@ class Bree extends EventEmitter {
//
if (
this.config.root &&
this.config.doRootCheck &&
(!Array.isArray(this.config.jobs) || this.config.jobs.length === 0)
) {
try {
this.config.jobs = require(this.config.root);
} catch (err) {
this.config.logger.error(err);
if (!this.config.silenceRootCheckError) {
this.config.logger.error(err);
}
}
}

Expand Down
70 changes: 69 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,22 @@ test('preset and override is set by cronValidate config', (t) => {
t.is(bree.config.cronValidate.override.test, 'works');
});

test('throws if jobs is not an array', (t) => {
test('throws if jobs is not an array and logs MODULE_NOT_FOUND error by default', (t) => {
t.plan(2);

const logger = {
info: () => {},
error: (err) => {
t.is(err.code, 'MODULE_NOT_FOUND');
}
};

t.throws(
() =>
new Bree({
jobs: null,
...baseConfig,
logger,
root: path.join(__dirname, 'noIndexJobs')
}),
{
Expand All @@ -84,6 +94,64 @@ test('throws if jobs is not an array', (t) => {
);
});

test('logs MODULE_NOT_FOUND error if array is empty', (t) => {
t.plan(2);

const logger = {
info: () => {},
error: (err) => {
t.is(err.code, 'MODULE_NOT_FOUND');
}
};

const bree = new Bree({
jobs: [],
...baseConfig,
logger,
root: path.join(__dirname, 'noIndexJobs')
});

t.true(bree instanceof Bree);
});

test('does not log MODULE_NOT_FOUND error if silenceRootCheckError is false', (t) => {
const logger = {
info: () => {},
error: () => {
t.fail();
}
};

const bree = new Bree({
jobs: [],
...baseConfig,
logger,
root: path.join(__dirname, 'noIndexJobs'),
silenceRootCheckError: true
});

t.true(bree instanceof Bree);
});

test('does not log MODULE_NOT_FOUND error if doRootCheck is false', (t) => {
const logger = {
info: () => {},
error: () => {
t.fail();
}
};

const bree = new Bree({
jobs: [],
...baseConfig,
logger,
root: path.join(__dirname, 'noIndexJobs'),
doRootCheck: false
});

t.true(bree instanceof Bree);
});

test('throws during constructor if job-validator throws', (t) => {
t.throws(
() =>
Expand Down

1 comment on commit 30fd2c0

@theahmadzai
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

Please sign in to comment.