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

Conversation

fa93hws
Copy link
Contributor

@fa93hws fa93hws commented Oct 16, 2019

What is the purpose of this pull request? (put an "X" next to item)

[x] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[x] Add something to the core
[ ] Other, please explain:

What changes did you make? (Give an overview)
#12389 adds the cwd to the CLIEngine and the Linter.
However it's not possible to mock it in the ruleTester.
This PR aims to solve this issue.

Is there anything you'd like reviewers to focus on?
N/A

B.T.W. Does that need to go through the RFC process?

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Oct 16, 2019
@platinumazure platinumazure requested review from mysticatea and kaicataldo and removed request for mysticatea October 17, 2019 14:25
Copy link
Member

@platinumazure platinumazure left a comment

Choose a reason for hiding this comment

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

This looks good to me. Thanks for contributing!

I have requested reviews from the team members that merged the original cwd PR, just to make sure this looks good from their side as well.

@platinumazure platinumazure added core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Oct 17, 2019
@platinumazure
Copy link
Member

Also, I don't see a strong need to send this through the RFC process-- if anything, this could be seen as an addition to the previous RFC. But other team members may disagree.

@mysticatea
Copy link
Member

Thank you for your contribution!

I'm not sure if we need RFC for testers. I'm OK to advance this without RFCs.

I'd like to see if we can add cwd option to each test pattern, like filename option.

@fa93hws
Copy link
Contributor Author

fa93hws commented Oct 18, 2019

I'd like to see if we can add cwd option to each test pattern, like filename option.

Should be possible, I will have a try.

@fa93hws
Copy link
Contributor Author

fa93hws commented Oct 18, 2019

I'd like to see if we can add cwd option to each test pattern, like filename option.

Hi, it seems that I may need to modify the ConfigData to implement this change.
Does that SGTY?
Because there is a settings field in the ConfigData which I believe is a representation of the .eslintrc file.

If cwd is added to the ConfigData, it might not be a bad idea for it to live in the settings which means it will no longer be a specific configuration for the CLIEngine but for the whole eslint?

@mysticatea
Copy link
Member

mysticatea commented Oct 18, 2019

No. ConfigData is the form of .eslintrc.* files.

I had imaged that the tester re-creates linter instance for different cwds.

@fa93hws
Copy link
Contributor Author

fa93hws commented Oct 18, 2019

No. ConfigData is the form of .eslintrc.* files.

I had imaged that the tester re-creates linter instance for different cwds.

Cool, I understand you concern about recreating the linter instance during the test.
I was thinking removing the cwd from the CLIEngine and have that in the .eslintrc.
But then I realize that the CLIEngine can have the option even though it's in the .eslintrc.🤦‍♂️

I will add the cwd property to providedConfig then.

@mysticatea
Copy link
Member

Sorry, my previous comment may not be clear.

I'm imaging that we can realize cwd option in each test pattern by recreating the linter instance for each cwd values. So probably the this.linter property becomes a map, from cwd to the linter instance.

I don't think that the change of ConfigData is good. It's a core enhancement that is too big to do in this PR.

@fa93hws
Copy link
Contributor Author

fa93hws commented Oct 18, 2019

Sorry, my previous comment may not be clear.

I'm imaging that we can realize cwd option in each test pattern by recreating the linter instance for each cwd values. So probably the this.linter property becomes a map, from cwd to the linter instance.

I don't think that the change of ConfigData is good. It's a core enhancement that is too big to do in this PR.

Oh I see.
Agree, the change to ConfigData is a bit too large without RFC.

@kaicataldo
Copy link
Member

I'm assuming @platinumazure supports this, but it would be great if you could leave a 👍. We'll need one more 👍 and a champion to mark this as accepted.

Copy link
Member

@mysticatea mysticatea left a comment

Choose a reason for hiding this comment

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

I'd be a champion.

However, my stance is this comment; it's better if we can configure the cwd in each pattern. For example:

new RuleTester().run("my-rule", rule, {
    valie: [
        { code: "", cwd: "", filename: "", ... }
    ],
    invalie: [
        { code: "", errors: [], cwd: "", filename: "", ... }
    ],
})

@mysticatea mysticatea self-assigned this Dec 23, 2019
@mysticatea mysticatea added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Dec 23, 2019
@nzakas
Copy link
Member

nzakas commented Jun 11, 2020

@fa93hws do you plan on continuing to work on this?

@fa93hws
Copy link
Contributor Author

fa93hws commented Jun 12, 2020

Ah, sorry I forget about this PR. Will take a look later.

@nzakas
Copy link
Member

nzakas commented Jun 12, 2020

Thanks!

@fa93hws fa93hws force-pushed the eric/allow-cwd-in-rule-tester branch from 9db291e to 902e55f Compare June 20, 2020 06:03
@fa93hws fa93hws requested a review from mysticatea June 20, 2020 06:06
Comment on lines 450 to 478
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

Comment on lines 420 to 422
* linterMapDevOnly?: Map<string | undefined, Linter>
* }} test The collection of tests to run.
* test.linterMapDevOnly is for testing purpose
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is one unit test that need access to linter to verify the parser.
Can't think of a better way to inject the linter...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's doable to have linterMap to be a class property.
But I think it would be better not to do so, because it's used in run method only.
An additional method parameter is better than a class property in my point of view.

Copy link
Member

Choose a reason for hiding this comment

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

Removing the instance field sounds nice.
I like using Symbol("linterMapDevOnly") in that case. Our public API doesn't contain the symbol and we use it only in tests.

Copy link
Member

Choose a reason for hiding this comment

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

@fa93hws fa93hws requested a review from mysticatea June 21, 2020 06:50
@fa93hws fa93hws force-pushed the eric/allow-cwd-in-rule-tester branch from d44700f to 78a1393 Compare June 21, 2020 07:50
@nzakas
Copy link
Member

nzakas commented Jul 7, 2020

@mysticatea have your concerns been addressed?

@kaicataldo
Copy link
Member

@mysticatea One more friendly ping.

jarrodldavis added a commit to jarrodldavis/eslint-plugin-tailwindcss that referenced this pull request Nov 2, 2020
allow mocking `cwd` in `RuleTester` test cases.

Patch based on this pull request:
eslint/eslint#12443
@nzakas
Copy link
Member

nzakas commented Oct 9, 2021

Closing due to age.

@nzakas nzakas closed this Oct 9, 2021
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Apr 8, 2022
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Apr 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants