Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: Allow mocking the cwd in rule tester #12443

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 35 additions & 13 deletions lib/rule-tester/rule-tester.js
Expand Up @@ -120,7 +120,8 @@ const RuleTesterParameters = [
"filename",
"options",
"errors",
"output"
"output",
"cwd"
];

/*
Expand Down Expand Up @@ -337,7 +338,12 @@ class RuleTester {
* @type {Object}
*/
this.rules = {};
this.linter = new Linter();

/**
* Linter map, cwd -> Linter instance
* @type {Map<string | undefined, Linter>}
*/
this.linterMap = new Map();
}

/**
Expand Down Expand Up @@ -425,7 +431,8 @@ class RuleTester {
const testerConfig = this.testerConfig,
requiredScenarios = ["valid", "invalid"],
scenarioErrors = [],
linter = this.linter;
linterMap = this.linterMap,
rules = this.rules;

if (lodash.isNil(test) || typeof test !== "object") {
throw new TypeError(`Test Scenarios for rule ${ruleName} : Could not find test scenario object`);
Expand All @@ -443,20 +450,34 @@ class RuleTester {
].concat(scenarioErrors).join("\n"));
}

/**
* get linter from linterMap according to cwd
* @param {string} [cwd] cwd
* @returns {Linter} Linter instance
* @private
*/
function getLinter(cwd) {
if (!linterMap.has(cwd)) {
const linter = new Linter({ cwd });

linter.defineRule(ruleName, Object.assign({}, rule, {
linterMap.set(cwd, linter);
}
const linter = linterMap.get(cwd);

// Create a wrapper rule that freezes the `context` properties.
create(context) {
freezeDeeply(context.options);
freezeDeeply(context.settings);
freezeDeeply(context.parserOptions);
linter.defineRule(ruleName, Object.assign({}, rule, {

return (typeof rule === "function" ? rule : rule.create)(context);
}
}));
// Create a wrapper rule that freezes the `context` properties.
create(context) {
freezeDeeply(context.options);
freezeDeeply(context.settings);
freezeDeeply(context.parserOptions);

linter.defineRules(this.rules);
return (typeof rule === "function" ? rule : rule.create)(context);
}
}));
linter.defineRules(rules);
Copy link
Member

Choose a reason for hiding this comment

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

Thank you for the update!

Originally, the tester runs this process one time. Therefore, I think we can move these lines into if (!linterMap.has(cwd)) { ... } block.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the rules won't be set if the tester is ran multiple times.
Say

ruleTester.run('rule-a', ...);
ruleTester.run('rule-b', ...);

This is because linterMap.has(cwd) gives true at the second time.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I got it. Thank you for the explanation.

How about clearing linterMap at the top of the run() method?

My concern is low performance because getLinter() will be call on every test pattern. I know several rules that have +10k +5k test patterns.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do

return linter;
}

/**
* Run the rule for the given item
Expand All @@ -467,6 +488,7 @@ class RuleTester {
function runRuleForItem(item) {
let config = lodash.cloneDeep(testerConfig),
code, filename, output, beforeAST, afterAST;
const linter = getLinter(item.cwd);

if (typeof item === "string") {
code = item;
Expand Down
24 changes: 23 additions & 1 deletion tests/lib/rule-tester/rule-tester.js
Expand Up @@ -751,7 +751,15 @@ describe("RuleTester", () => {
});

it("should pass-through the parser to the rule", () => {
const spy = sinon.spy(ruleTester.linter, "verify");

// To generate the default linter
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: ["Eval(foo)"],
invalid: []
});

const linter = ruleTester.linterMap.get();
const spy = sinon.spy(linter, "verify");

ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: [
Expand Down Expand Up @@ -890,6 +898,20 @@ describe("RuleTester", () => {
}, /Property "env" is the wrong type./u);
});

it("should pass-through the cwd to the linter", () => {
ruleTester.run("some-random-rule", ctx => {

assert.strictEqual(ctx.getCwd(), "myCwd");
return {};
}, {
valid: [{
code: "var test = 'foo'",
cwd: "myCwd"
}],
invalid: []
});
});

it("should pass-through the tester config to the rule", () => {
ruleTester = new RuleTester({
globals: { test: true }
Expand Down