Skip to content

Commit

Permalink
fix(core): rules do not run when told not to run; closes #102
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed Jan 7, 2021
1 parent c540d85 commit fd7fd63
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 31 deletions.
54 changes: 31 additions & 23 deletions packages/core/src/observable.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,29 +117,6 @@ function toReport(opts = {}) {
);
}

/**
*
* @param {object} [config] - Raw rule configuration
* @returns {import('rxjs').OperatorFunction<import('@report-toolkit/inspector/src/rule').RuleDefinition,import('@report-toolkit/inspector/src/rule-config').RuleConfig>}
*/
function toRuleConfig(config = {}) {
const ruleIdsCount = _.getOr(0, 'rules.length', config);
return ruleDefs =>
ruleDefs.pipe(
pipeIf(
!ruleIdsCount,
debug(() => 'enabling all rules by default')
),
pipeIf(
ruleIdsCount,
filter(({id}) => Boolean(_.get(id, config.rules)))
),
map(ruleDefinition =>
inspector.createRule(ruleDefinition).toRuleConfig(config)
)
);
}

/**
*
* @param {Partial<DiffOptions>} [opts]
Expand All @@ -163,6 +140,37 @@ function toReportDiff(opts = {}) {
);
}

/**
*
* @param {object} [config] - Raw rule configuration
* @ignore
* @returns {import('rxjs').OperatorFunction<import('@report-toolkit/inspector/src/rule').RuleDefinition,import('@report-toolkit/inspector/src/rule-config').RuleConfig>}
*/
export function toRuleConfig(config = {}) {
const ruleIdsCount = _.pipe(
_.values,
_.map(Boolean),
_.sum
)(config.rules || {});
return ruleDefs =>
ruleDefs.pipe(
pipeIf(
!ruleIdsCount,
debug(() => 'enabling all rules by default')
),
pipeIf(
ruleIdsCount,
filter(({id}) => {
console.error(id, _.get(id, config.rules));
return Boolean(_.get(id, config.rules));
})
),
map(ruleDefinition =>
inspector.createRule(ruleDefinition).toRuleConfig(config)
)
);
}

/**
* Returns the difference between two reports.
*
Expand Down
52 changes: 44 additions & 8 deletions packages/core/test/observable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ const REPORT_002_FILEPATH = require.resolve(
);

describe('@report-toolkit/core:observable', function () {
let sandbox;
/**
* @type {import('../src/observable')}
*/
let core;
let subject;

const msg001 = {
filepath: REPORT_001_FILEPATH,
Expand All @@ -43,17 +41,22 @@ describe('@report-toolkit/core:observable', function () {

let stubs;

let createRuleStub;

beforeEach(function () {
sandbox = sinon.createSandbox();
createRuleStub = sinon
.stub()
.returns({toRuleConfig: sinon.stub().returns({some: 'config'})});

stubs = {
'@report-toolkit/inspector': {
inspectReports: sandbox
inspectReports: sinon
.stub()
.returnsOperatorFunction(switchMapTo([msg001, msg002]))
.returnsOperatorFunction(switchMapTo([msg001, msg002])),
createRule: createRuleStub
},
'@report-toolkit/diff': {
diffReports: sandbox.stub().returnsOperatorFunction(
diffReports: sinon.stub().returnsOperatorFunction(
switchMapTo([
{
oldValue: 45164,
Expand All @@ -73,7 +76,7 @@ describe('@report-toolkit/core:observable', function () {
},
'@report-toolkit/common': {
config: {
parseConfig: sandbox.stub().returnsOperatorFunction(),
parseConfig: sinon.stub().returnsOperatorFunction(),
BUILTIN_CONFIGS: config.BUILTIN_CONFIGS
}
}
Expand All @@ -83,7 +86,7 @@ describe('@report-toolkit/core:observable', function () {
});

afterEach(function () {
sandbox.restore();
sinon.restore();
});

describe('function', function () {
Expand Down Expand Up @@ -134,6 +137,8 @@ describe('@report-toolkit/core:observable', function () {
});

describe('inspect()', function () {
/** @type {typeof core.inspect} */
let subject;
beforeEach(function () {
subject = core.inspect;
});
Expand Down Expand Up @@ -180,6 +185,9 @@ describe('@report-toolkit/core:observable', function () {
});

describe('use()', function () {
/** @type {typeof core.use} */
let subject;
/** @type {string} */
let pluginPath;

beforeEach(function () {
Expand Down Expand Up @@ -215,6 +223,7 @@ describe('@report-toolkit/core:observable', function () {

describe('isPluginRegistered()', function () {
let pluginPath;
/** @type {typeof core.isPluginRegistered} */
let subject;

beforeEach(function () {
Expand All @@ -240,6 +249,7 @@ describe('@report-toolkit/core:observable', function () {
});

describe('loadConfig()', function () {
/** @type {typeof core.loadConfig} */
let subject;
let cwd;

Expand Down Expand Up @@ -279,6 +289,7 @@ describe('@report-toolkit/core:observable', function () {
});

describe('fromTransformerChain()', function () {
/** @type {typeof core.fromTransformerChain} */
let subject;

beforeEach(function () {
Expand Down Expand Up @@ -337,6 +348,7 @@ describe('@report-toolkit/core:observable', function () {
});

describe('transform()', function () {
/** @type {typeof core.transform} */
let subject;

beforeEach(function () {
Expand All @@ -351,5 +363,29 @@ describe('@report-toolkit/core:observable', function () {
);
});
});

describe('toRuleConfig()', function () {
/** @type {typeof core.toRuleConfig} */
let subject;

beforeEach(function () {
subject = core.toRuleConfig;
});

it('should generate a rule config for only enabled rules', async function () {
const ruleDefs = [
{id: 'foo', inspect: sinon.stub()},
{id: 'bar', inspect: sinon.stub()}
];

await of(...ruleDefs)
.pipe(subject({rules: {foo: true, bar: false}}))
.toPromise();

expect(createRuleStub, 'to have no calls satisfying', [
ruleDefs[1]
]).and('was called once');
});
});
});
});

0 comments on commit fd7fd63

Please sign in to comment.