Skip to content

Conversation

@vivekmiyani
Copy link
Contributor

@vivekmiyanivivekmiyani commented Jun 21, 2021

Fixes#2507.

TODO

  • Replace hardcoded spec directory to default_path
  • Change log entry
  • Add feature test for each generator listed below:
    • channel
    • controller
    • feature
    • generator
    • helper
    • install
    • integration
    • job
    • mailbox
    • mailer
    • model
    • request
    • scaffold
    • system
    • view

@pirj
Copy link
Member

pirj commented Jun 21, 2021

Something failed

@vivekmiyani
Copy link
ContributorAuthor

Hii, I've updated the branch.

At the moment, rails installer does not support --default_path, So lib/generators/rspec/install/install_generator.rb remains untouched.
I think to change the default_path at the time of rails generate rspec:install, We need to do modify the .rspec file provided by rspec-core.

I'll work on it when get a chance.

@vivekmiyanivivekmiyani marked this pull request as ready for review June 24, 2021 16:41
@pirj
Copy link
Member

pirj commented Jun 24, 2021

Looks great, thank you. You may (completely optionally) add a feature to features/generator_specs/generator_specs.feature.

Just noticed that our Generators documentation is split into two, and the second one's name is misleading, Generator specs. It's a topic for another PR to bring them together, though.

@pirjpirj requested review from JonRowe and benoittgtJune 24, 2021 19:12
@vivekmiyani
Copy link
ContributorAuthor

You may (completely optionally) add a feature to features/generator_specs/generator_specs.feature.

@pirj Are we only going to test features/generator_specs/generator_specs.feature for different default_path and not others features. ?

Forgive me if I misunderstood anything 😅 .

@pirj
Copy link
Member

pirj commented Jun 25, 2021

Yes, just your fix. Those features are published as documentation, and are end-to-end tests. No worries if you get stuck with it. Altering the default spec path is a pretty rare case. Also, with your fix it now works intuitively, so maybe documentation for it is redundant. Leaving it up to you to decide.

@vivekmiyani
Copy link
ContributorAuthor

Okay I will update the PR. But while implementing the feature I am running into one issue.:

I thought that default_path will be initialized from .rspec OR spec_helper.rb at the time of running rails generate ... but that is not the case with actual project.

It looks the blocker is the ConfigurationOptions which was parsing options when I ran bundle exec rspec but not for rails generate ....

@vivekmiyani
Copy link
ContributorAuthor

We have to do something like, https://github.com/rspec/rspec-core/blob/main/lib/rspec/core/runner.rb#L132 (i.e. RSpec::Core::Runner#configure) is doing the same as we need here.

Any thoughts or suggestion?

@pirj
Copy link
Member

pirj commented Jun 26, 2021 via email

@pirj
Copy link
Member

pirj commented Jun 26, 2021

--default-path ... is not supported on RSpec.configuration, as it needs to be set before spec files are loaded

But https://github.com/rspec/rspec-core/blob/247d0a706ffa955718d600f3588aeb1e60cebde4/lib/rspec/core/configuration.rb#L35

# RSpec.configure do |c|# c.default_path = 'behavior'

and

 # Path to use if no path is provided to the `rspec` command (default: # `"spec"`). Allows you to just type `rspec` instead of `rspec spec` to # run all the examples in the `spec` directory. 

and what's more important:

# @note Other scripts invoking `rspec` indirectly will ignore this# setting.

which is exactly the case with generators.

Though .rspec should support setting the default path with:

--default-path behaviour 

Can you please confirm that defining the default path in .rspec works with this fix for generators?
Wondering if it gets loaded when you require 'rspec/coreas you've added. And whyspec/spec_helper.rbis not loaded, even though a typical.rspecfile defines--require spec/spec_helper.rb`. 🤔

If it works, I suggest you to write the feature test in such a way that the default path is set in the .rspec file, not spec/spec_helper.rb.

@pirj
Copy link
Member

pirj commented Jun 26, 2021

I think I have bad news. Options from files, .rspec etc are loaded by ConfigurationOptions that is exclusively called by Runner that is responsible for running specs, and only immediately before running specs.
spec/spec_helper.rb is required after that if .rspec has a --require spec_helper option.
So require 'rspec/core' only loads up the default Configuration with default_path set to spec. Alas.

@vivekmiyani
Copy link
ContributorAuthor

Yes that's what I exactly found. See my #2508 (comment) I've mentioned the line no. where ConfigurationOptions#configure updates the Configuration.

I will try to find more and will update the PR.

@JonRowe
Copy link
Member

I'd like to see a feature file demonstrating this behaviour so that its both documented and tested, the changes to the generator look fine but I'm not sure its sufficient to load rspec/core before accessing configuration, the config must have been loaded / set before being accessed.

@vivekmiyani
Copy link
ContributorAuthor

@JonRowe@pirj I will implement feature file.
but please check the ca907cf commit, I've added temporary workaround to parse the .rspec file (Extracted from the rspec/core/runner.rb).

Can we initialize ConfigurationOptions when Configuration initializes in rspec-core, So separate initialization of ConfigurationOptions can be removed.?

@pirj
Copy link
Member

pirj commented Jul 10, 2021

I believe with those discoveries, adding a feature test becomes a necessity.

@vivekmiyani
Copy link
ContributorAuthor

Hey,
I have added rspec install option:

# Install rspec at custom directory (this will append `--default-path` option to `.rspec` file) $ rails generate rspec:install --default-path behaviour

Also look at 5f7b640 which implements the feature you've asked for!!

@vivekmiyani
Copy link
ContributorAuthor

@pirj@JonRowe Do you want me to write the feature for all other generators (like: controller, model, etc.. etc..)?

I've only added feature for generator generator, as I mentioned above.

Copy link
Member

@pirjpirj left a comment

Choose a reason for hiding this comment

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

The code is copied over from rspec-core. It might be exposed as a public interface, but this may be postponed to a later version, and we can do this in rspec-core 4.0.1, and set the minimum dependent version to it.

Thank you!

@vivekmiyani
Copy link
ContributorAuthor

vivekmiyani commented Jul 18, 2021

@pirj Can we initialize ConfigurationOptions in Configuration constructor directly in rspec-core, So separate initialization of ConfigurationOptions can be completely removed from codebase.?

@pirj
Copy link
Member

pirj commented Jul 18, 2021

That's an option to consider, too. RSpec.configuration is memoized, though:

defself.configuration@configuration ||= RSpec::Core::Configuration.newend

and there could be cases when it's necessary to run a suite with different options, e.g. filters. It would make ConfigurationOptions class redundant, and some may make use of it. It could be done in a major rspec-core version, but we'd prefer not to do this in RSpec 4.

I don't feel that this code duplication is a big issue, it just hints that something can be optimized.

JonRowe
JonRowe previously requested changes Aug 12, 2021
Copy link
Member

@JonRoweJonRowe left a comment

Choose a reason for hiding this comment

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

Sorry for the delayed feedback, looking good but some more tweaks needed.

@pirjpirj requested a review from JonRoweSeptember 18, 2021 16:09
@pirjpirj dismissed JonRowe’s stale reviewSeptember 18, 2021 16:09

Code review notes addressed, proposed changes accepted

JonRowe
JonRowe previously requested changes Sep 20, 2021
Copy link
Member

@JonRoweJonRowe left a comment

Choose a reason for hiding this comment

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

This changes behaviour for all generators, yet is only tested against one generator. This needs the additional tests and scenarios.

@vivekmiyani
Copy link
ContributorAuthor

vivekmiyani commented Nov 21, 2021

Hello guys, Sorry for delayed response. I was super busy in some stuffs.

As @JonRowe asked, I am writing feature tests for below items (List grabbed from spec/generators/rspec/**/*_spec.rb):

  • channel
  • controller
  • feature
  • generator
  • helper
  • install
  • integration
  • job
  • mailbox
  • mailer
  • model
  • request
  • scaffold
  • system
  • view

EDIT: Added the entry of generator feature in TODO list.

@vivekmiyani
Copy link
ContributorAuthor

@pirj@JonRowe Check channel generator feature, is it correct OR any modification needed?
If it is correct I'll repeat for others.

@pirj
Copy link
Member

pirj commented Nov 21, 2021

channel generator feature, is it correct

Looks good to me 👍
Thanks for getting back to this.

@vivekmiyani
Copy link
ContributorAuthor

vivekmiyani commented Nov 28, 2021

Hey @pirj@JonRowe, Added more feature tests 🎉 .

Below are not implemented as I couldn't figure how to tackle them 🤕 :

  • Install generator feature, Because I' not able to run the installation using When I run 'bundle exec rails generate rspec:install' in feature, as our directory has already installed rspec structure.
  • Model generator feature, Because I am stuck at matching the migration timestamp output 🤔 . How to match it in Then the output should contain block?.
  • Scaffold generator feature, Same I am stuck at matching the migration timestamp output.

@JonRowe
Copy link
Member

@pirj can you review this?

@JonRoweJonRowe mentioned this pull request Jan 18, 2022
21 tasks
@vivekmiyani
Copy link
ContributorAuthor

@JonRowe@pirj Can you please review this?

@pirjpirj dismissed JonRowe’s stale reviewJuly 17, 2022 22:27

Requested changes done

@pirjpirj merged commit 7b7a86f into rspec:mainJul 17, 2022
@pirj
Copy link
Member

pirj commented Jul 17, 2022

Thank you!

@vivekmiyanivivekmiyani deleted the respect-default-path branch July 31, 2022 13:28
JonRowe added a commit that referenced this pull request Oct 10, 2022
Sign up for freeto 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.

rails generators not respecting RSpec's default_path

3 participants

@vivekmiyani@pirj@JonRowe