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

Add ScatteredSetup cop #254

Merged
merged 2 commits into from
Jan 15, 2017
Merged

Add ScatteredSetup cop #254

merged 2 commits into from
Jan 15, 2017

Conversation

backus
Copy link
Collaborator

@backus backus commented Dec 20, 2016

You should only use max one hook type per example group.

# bad
describe Foo do
  before { setup1 }
  before { setup2 }
end

# good
describe Foo do
  before do
    setup1
    setup2
  end
end

@backus backus force-pushed the feature/SeparatedHooks branch 3 times, most recently from b24343d to 2e9e933 Compare December 20, 2016 02:30
@backus
Copy link
Collaborator Author

backus commented Dec 20, 2016

@andyw8 or @bquorning what do you think of this cop? If you agree with its intent can you review the code and merge if you approve?

@bquorning
Copy link
Collaborator

bquorning commented Dec 20, 2016

I like it!

How about checking eventual arguments? Calling before, before(:each) and before(:example) in the same context, we should recommend merging them. Not so if calling once with :each and once with :all.

@backus
Copy link
Collaborator Author

backus commented Dec 20, 2016

Good catch that is definitely necessary

@bquorning
Copy link
Collaborator

bquorning commented Dec 20, 2016

As I recall, valid arguments are :suite, :context/:all, and :example/:each/no argument.

And as you suggested in #252 (comment), adding these to the language definition sounds really useful.

@andyw8
Copy link
Collaborator

andyw8 commented Dec 20, 2016

Nice idea. Just ran it over a large codebase with some very long tests and it seemed to be accurate. This currently generates warnings though:

describe do
  context do
    before(:all) do
    end

    before(:each) do
    end

    after(:each) do
    end

    after(:all) do
    end
  end
end

As @bquorning says, I think we should allow :each and :all in the same context.

@backus backus force-pushed the feature/SeparatedHooks branch 2 times, most recently from 704c4ea to 76b10a4 Compare December 26, 2016 19:46
@backus backus force-pushed the feature/SeparatedHooks branch 7 times, most recently from 4caebed to 2e30fc3 Compare January 12, 2017 09:59
@backus
Copy link
Collaborator Author

backus commented Jan 12, 2017

I've cleaned up this PR and addressed the functional behavior feedback here. I haven't extracted the different hook scope names to the language yet because I didn't want to further expand this PR. @bquorning @andyw8 lmk what you think

when :context, :all then :context
when :suite then :suite
else
raise "Unrecognized scope #{scope_argument.to_a.first}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Worth defining a rubocop-rspec error for this rather than a plain RuntimeError?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated

@andyw8
Copy link
Collaborator

andyw8 commented Jan 12, 2017

LGTM

@backus
Copy link
Collaborator Author

backus commented Jan 12, 2017

@andyw8 updated to extract that error class.

@backus backus force-pushed the feature/SeparatedHooks branch 2 times, most recently from 93d1413 to e6b3a61 Compare January 12, 2017 19:22
@backus
Copy link
Collaborator Author

backus commented Jan 12, 2017

Side note / question @andyw8 @bquorning. I've got these node wrapper that I've also refactored a bit in this PR and I'm struggling to name it well. Right now I've got RuboCop::RSpec::Interface which is an abstract class that wraps a RuboCop::Node. The concrete classes inheriting from Interface are RuboCop::RSpec::ExampleGroup, RuboCop::RSpec::Example, and RuboCop::RSpec::Hook. They're useful because they let me centralize destructuring of RSpec methods that are commonly worked with in these cops. For example, here I can do hook.scope # => :each and hook.name # => :before and for examples I can do things like example.docstring.

Anyways, what is a good name for RuboCop::RSpec::Interface? I tried "Construct" before but that doesn't feel right. Interface is better but its too loaded of a CS term IMO. Basically, I want a nice concise way of saying RSpecDSLNodeWrapper 😆

@backus
Copy link
Collaborator Author

backus commented Jan 12, 2017

Actually I removed the error. It is a slim chance anyone runs into it but if they do it shouldn't raise an error because they are doing something weird

@backus backus force-pushed the feature/SeparatedHooks branch 2 times, most recently from 2148ffd to 624402b Compare January 13, 2017 20:36
@backus backus merged commit 25fadd5 into master Jan 15, 2017
@backus backus deleted the feature/SeparatedHooks branch January 15, 2017 05:18
markburns pushed a commit to markburns/rubocop-rails-ddd that referenced this pull request Nov 7, 2017
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