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

RSpec/SubjectStub. Add top level group callback #932

Merged
merged 4 commits into from
Jun 14, 2020

Conversation

andrykonchin
Copy link
Contributor

@andrykonchin andrykonchin commented Jun 12, 2020

It's a followup of #925 and it addresses the following issue #925 (comment)

Changes:

  • added module TopLevelGroup (similar to TopLevelDescribe)
  • refactored SubjectStub to avoid manual caching and rely on TopLevelGroup

The goal of TopLevelGroup is to handle all the possible example and shared groups in a spec file. It calls on_top_level_group callback for every top-level describe, context, feature, shared context... group.


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

@andrykonchin andrykonchin marked this pull request as draft June 12, 2020 20:21
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Even more cleaner than the previous implementation 💯

spec/rubocop/cop/rspec/subject_stub_spec.rb Outdated Show resolved Hide resolved
lib/rubocop/rspec/top_level_group.rb Outdated Show resolved Hide resolved
lib/rubocop/rspec/top_level_group.rb Outdated Show resolved Hide resolved
spec/rubocop/cop/rspec/subject_stub_spec.rb Outdated Show resolved Hide resolved
tags Outdated Show resolved Hide resolved
lib/rubocop/rspec/top_level_group.rb Outdated Show resolved Hide resolved
@andrykonchin andrykonchin marked this pull request as ready for review June 12, 2020 20:47
@andrykonchin andrykonchin requested a review from pirj June 13, 2020 09:58
@andrykonchin
Copy link
Contributor Author

@pirj Fixed all the issues

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Perfect! 😗 👌

end

def top_level_nodes
@top_level_nodes ||=
Copy link
Member

Choose a reason for hiding this comment

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

Minor: does it make sense to memoize it? It's only called once from a method that is also memoized.

@pirj pirj requested review from bquorning and Darhazer June 13, 2020 18:41
@andrykonchin andrykonchin requested a review from pirj June 13, 2020 20:10
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Perfect! Thank you.

top_level_nodes.select { |n| example_or_shared_group?(n) }
end

def top_level_nodes
Copy link
Member

Choose a reason for hiding this comment

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

At some point, we may want to DRY this between the different modules.
Also, top_level_describe seems to be a special case on top_level_group.

For now I'm fine with keeping it as it is

@pirj pirj merged commit 3030ba8 into rubocop:master Jun 14, 2020
@pirj
Copy link
Member

pirj commented Jun 14, 2020

Merging without consensus, since this change has been discussed in the past, and only affects one cop.

Thank you again, @andrykonchin !

@andrykonchin andrykonchin deleted the add-top-level-group-callback branch June 14, 2020 20:17
pirj added a commit that referenced this pull request Jul 23, 2020
Why?

 - it was slow #925 (comment)
 - it ignored non-describe top-level example groups #925 (comment)

`TopLevelGroup` is a modern replacement for `TopLevelDescribe`.

Examples how to migrate cops from TopLevelDescribe to TopLevelGroup:

 - #932
 - #977
@pirj pirj mentioned this pull request Jul 23, 2020
7 tasks
(ExampleGroups::ALL + SharedGroups::ALL).block_pattern

def on_block(node)
return unless respond_to?(:on_top_level_group)
Copy link
Member

@pirj pirj Jul 23, 2020

Choose a reason for hiding this comment

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

An afterthought.

I understand that this guard statement is on the very top to avoid a quite expensive check done in top_level_group?.

But is it really necessary? If a cop includes TopLevelGroup, that means it defines a on_top_level_group method? Otherwise why would it include TopLevelGroup?
Or am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TBH I just copied this approach from TopLevelDescribe module. I don't see any benefit in having such condition here.

@pirj pirj mentioned this pull request Aug 2, 2020
6 tasks
pirj added a commit that referenced this pull request Oct 22, 2020
Why?

 - it was slow #925 (comment)
 - it ignored non-describe top-level example groups #925 (comment)

`TopLevelGroup` is a modern replacement for `TopLevelDescribe`.

Examples how to migrate cops from TopLevelDescribe to TopLevelGroup:

 - #932
 - #977
pirj added a commit that referenced this pull request Oct 22, 2020
Why?

 - it was slow #925 (comment)
 - it ignored non-describe top-level example groups #925 (comment)

`TopLevelGroup` is a modern replacement for `TopLevelDescribe`.

Examples how to migrate cops from TopLevelDescribe to TopLevelGroup:

 - #932
 - #977
pirj added a commit that referenced this pull request Oct 22, 2020
Why?

 - it was slow #925 (comment)
 - it ignored non-describe top-level example groups #925 (comment)

`TopLevelGroup` is a modern replacement for `TopLevelDescribe`.

Examples how to migrate cops from TopLevelDescribe to TopLevelGroup:

 - #932
 - #977
pirj added a commit that referenced this pull request Nov 2, 2020
Why?

 - it was slow #925 (comment)
 - it ignored non-describe top-level example groups #925 (comment)

`TopLevelGroup` is a modern replacement for `TopLevelDescribe`.

Examples how to migrate cops from TopLevelDescribe to TopLevelGroup:

 - #932
 - #977
pirj added a commit to rubocop/rubocop-capybara that referenced this pull request Dec 29, 2022
Why?

 - it was slow rubocop/rubocop-rspec#925 (comment)
 - it ignored non-describe top-level example groups rubocop/rubocop-rspec#925 (comment)

`TopLevelGroup` is a modern replacement for `TopLevelDescribe`.

Examples how to migrate cops from TopLevelDescribe to TopLevelGroup:

 - rubocop/rubocop-rspec#932
 - rubocop/rubocop-rspec#977
ydah pushed a commit to rubocop/rubocop-factory_bot that referenced this pull request Apr 13, 2023
Why?

 - it was slow rubocop/rubocop-rspec#925 (comment)
 - it ignored non-describe top-level example groups rubocop/rubocop-rspec#925 (comment)

`TopLevelGroup` is a modern replacement for `TopLevelDescribe`.

Examples how to migrate cops from TopLevelDescribe to TopLevelGroup:

 - rubocop/rubocop-rspec#932
 - rubocop/rubocop-rspec#977
ydah pushed a commit to rubocop/rubocop-rspec_rails that referenced this pull request Mar 27, 2024
Why?

 - it was slow rubocop/rubocop-rspec#925 (comment)
 - it ignored non-describe top-level example groups rubocop/rubocop-rspec#925 (comment)

`TopLevelGroup` is a modern replacement for `TopLevelDescribe`.

Examples how to migrate cops from TopLevelDescribe to TopLevelGroup:

 - rubocop/rubocop-rspec#932
 - rubocop/rubocop-rspec#977
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants