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

Respect RSpec default_path for generators #2508

Merged
merged 21 commits into from Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions Changelog.md
@@ -1,6 +1,10 @@
### Development
[Full Changelog](https://github.com/rspec/rspec-rails/compare/v6.0.0.rc1...main)

Enhancements:

* Generators now respects default path configuration option. (@vivekmiyani, #2508)

Breaking Changes:

* Change the order of `after_teardown` from `after` to `around` in system
Expand Down
6 changes: 5 additions & 1 deletion features/README.md
Expand Up @@ -34,11 +34,15 @@ without having to type RAILS_ENV=test.

Now you can run:

script/rails generate rspec:install
bundle exec rails generate rspec:install

This adds the spec directory and some skeleton files, including a .rspec
file.

You can also customize the default spec path with `--default-path` option:

bundle exec rails generate rspec:install --default-path behaviour

## Issues

The documentation for rspec-rails is a work in progress. We'll be adding
Expand Down
31 changes: 31 additions & 0 deletions features/generator_specs/channel_specs.feature
@@ -0,0 +1,31 @@
Feature: Channel generator spec

Scenario: Channel generator
When I run `bundle exec rails generate channel group`
Then the features should pass
Then the output should contain:
"""
invoke rspec
create spec/channels/group_channel_spec.rb
"""
Then the output should contain:
"""
create app/channels/group_channel.rb
"""

Scenario: Channel generator with customized `default-path`
Given a file named ".rspec" with:
"""
--default-path behaviour
"""
And I run `bundle exec rails generate channel group`
Then the features should pass
Then the output should contain:
"""
invoke rspec
create behaviour/channels/group_channel_spec.rb
"""
Then the output should contain:
"""
create app/channels/group_channel.rb
"""
37 changes: 37 additions & 0 deletions features/generator_specs/controller_specs.feature
@@ -0,0 +1,37 @@
Feature: Controller generator spec

Scenario: Controller generator
When I run `bundle exec rails generate controller posts`
Then the features should pass
Then the output should contain:
"""
create app/controllers/posts_controller.rb
invoke erb
create app/views/posts
invoke rspec
create spec/requests/posts_spec.rb
invoke helper
create app/helpers/posts_helper.rb
invoke rspec
create spec/helpers/posts_helper_spec.rb
"""

Scenario: Controller generator with customized `default-path`
Given a file named ".rspec" with:
"""
--default-path behaviour
"""
And I run `bundle exec rails generate controller posts`
Then the features should pass
Then the output should contain:
"""
create app/controllers/posts_controller.rb
invoke erb
create app/views/posts
invoke rspec
create behaviour/requests/posts_spec.rb
invoke helper
create app/helpers/posts_helper.rb
invoke rspec
create behaviour/helpers/posts_helper_spec.rb
"""
21 changes: 21 additions & 0 deletions features/generator_specs/feature_specs.feature
@@ -0,0 +1,21 @@
Feature: Feature generator spec

Scenario: Feature generator
When I run `bundle exec rails generate rspec:feature posts`
Then the features should pass
Then the output should contain:
"""
create spec/features/posts_spec.rb
"""

Scenario: Feature generator with customized `default-path`
Given a file named ".rspec" with:
"""
--default-path behaviour
"""
And I run `bundle exec rails generate rspec:feature posts`
Then the features should pass
Then the output should contain:
"""
create behaviour/features/posts_spec.rb
"""
17 changes: 17 additions & 0 deletions features/generator_specs/generator_specs.feature
Expand Up @@ -14,3 +14,20 @@ Feature: Generator spec
invoke rspec
create spec/generator/my_generators_generator_spec.rb
"""

Scenario: Use custom generator with customized `default-path`
Given a file named ".rspec" with:
"""
--default-path behaviour
"""
And I run `bundle exec rails generate generator my_generator`
Then the features should pass
Then the output should contain:
"""
create lib/generators/my_generator
create lib/generators/my_generator/my_generator_generator.rb
create lib/generators/my_generator/USAGE
create lib/generators/my_generator/templates
invoke rspec
create behaviour/generator/my_generators_generator_spec.rb
"""
vivekmiyani marked this conversation as resolved.
Show resolved Hide resolved
25 changes: 25 additions & 0 deletions features/generator_specs/helper_specs.feature
@@ -0,0 +1,25 @@
Feature: Helper generator spec

Scenario: Helper generator
When I run `bundle exec rails generate helper posts`
Then the features should pass
Then the output should contain:
"""
create app/helpers/posts_helper.rb
invoke rspec
create spec/helpers/posts_helper_spec.rb
"""

Scenario: Helper generator with customized `default-path`
Given a file named ".rspec" with:
"""
--default-path behaviour
"""
And I run `bundle exec rails generate helper posts`
Then the features should pass
Then the output should contain:
"""
create app/helpers/posts_helper.rb
invoke rspec
create behaviour/helpers/posts_helper_spec.rb
"""
21 changes: 21 additions & 0 deletions features/generator_specs/integration_specs.feature
@@ -0,0 +1,21 @@
Feature: Integration generator spec

Scenario: Integration generator
When I run `bundle exec rails generate rspec:integration posts`
Then the features should pass
Then the output should contain:
"""
create spec/requests/posts_spec.rb
"""

Scenario: Integration generator with customized `default-path`
Given a file named ".rspec" with:
"""
--default-path behaviour
"""
And I run `bundle exec rails generate rspec:integration posts`
Then the features should pass
Then the output should contain:
"""
create behaviour/requests/posts_spec.rb
"""
25 changes: 25 additions & 0 deletions features/generator_specs/job_specs.feature
@@ -0,0 +1,25 @@
Feature: Job generator spec

Scenario: Job generator
When I run `bundle exec rails generate job user`
Then the features should pass
Then the output should contain:
"""
invoke rspec
create spec/jobs/user_job_spec.rb
create app/jobs/user_job.rb
"""

Scenario: Job generator with customized `default-path`
Given a file named ".rspec" with:
"""
--default-path behaviour
"""
And I run `bundle exec rails generate job user`
Then the features should pass
Then the output should contain:
"""
invoke rspec
create behaviour/jobs/user_job_spec.rb
create app/jobs/user_job.rb
"""
25 changes: 25 additions & 0 deletions features/generator_specs/mailbox_specs.feature
@@ -0,0 +1,25 @@
Feature: Mailbox generator spec

Scenario: Mailbox generator
When I run `bundle exec rails generate mailbox forwards`
Then the features should pass
Then the output should contain:
"""
create app/mailboxes/forwards_mailbox.rb
invoke rspec
create spec/mailboxes/forwards_mailbox_spec.rb
"""

Scenario: Mailbox generator with customized `default-path`
Given a file named ".rspec" with:
"""
--default-path behaviour
"""
And I run `bundle exec rails generate mailbox forwards`
Then the features should pass
Then the output should contain:
"""
create app/mailboxes/forwards_mailbox.rb
invoke rspec
create behaviour/mailboxes/forwards_mailbox_spec.rb
"""
43 changes: 43 additions & 0 deletions features/generator_specs/mailer_specs.feature
@@ -0,0 +1,43 @@
Feature: Mailer generator spec

Scenario: Mailer generator
When I run `bundle exec rails generate mailer posts index show`
Then the features should pass
Then the output should contain:
"""
create app/mailers/posts_mailer.rb
invoke erb
create app/views/posts_mailer
create app/views/posts_mailer/index.text.erb
create app/views/posts_mailer/index.html.erb
create app/views/posts_mailer/show.text.erb
create app/views/posts_mailer/show.html.erb
invoke rspec
create spec/mailers/posts_spec.rb
create spec/fixtures/posts/index
create spec/fixtures/posts/show
create spec/mailers/previews/posts_preview.rb
"""

Scenario: Mailer generator with customized `default-path`
Given a file named ".rspec" with:
"""
--default-path behaviour
"""
And I run `bundle exec rails generate mailer posts index show`
Then the features should pass
Then the output should contain:
"""
create app/mailers/posts_mailer.rb
invoke erb
create app/views/posts_mailer
create app/views/posts_mailer/index.text.erb
create app/views/posts_mailer/index.html.erb
create app/views/posts_mailer/show.text.erb
create app/views/posts_mailer/show.html.erb
invoke rspec
create behaviour/mailers/posts_spec.rb
create behaviour/fixtures/posts/index
create behaviour/fixtures/posts/show
create behaviour/mailers/previews/posts_preview.rb
"""
21 changes: 21 additions & 0 deletions features/generator_specs/request_specs.feature
@@ -0,0 +1,21 @@
Feature: Request generator spec

Scenario: Request generator
When I run `bundle exec rails generate rspec:request posts`
Then the features should pass
Then the output should contain:
"""
create spec/requests/posts_spec.rb
"""

Scenario: Request generator with customized `default-path`
Given a file named ".rspec" with:
"""
--default-path behaviour
"""
And I run `bundle exec rails generate rspec:request posts`
Then the features should pass
Then the output should contain:
"""
create behaviour/requests/posts_spec.rb
"""
21 changes: 21 additions & 0 deletions features/generator_specs/system_specs.feature
@@ -0,0 +1,21 @@
Feature: System generator spec

Scenario: System generator
When I run `bundle exec rails generate rspec:system posts`
Then the features should pass
Then the output should contain:
"""
create spec/system/posts_spec.rb
"""

Scenario: System generator with customized `default-path`
Given a file named ".rspec" with:
"""
--default-path behaviour
"""
And I run `bundle exec rails generate rspec:system posts`
Then the features should pass
Then the output should contain:
"""
create behaviour/system/posts_spec.rb
"""
23 changes: 23 additions & 0 deletions features/generator_specs/view_specs.feature
@@ -0,0 +1,23 @@
Feature: View generator spec

Scenario: View generator
When I run `bundle exec rails generate rspec:view posts index`
Then the features should pass
Then the output should contain:
"""
create spec/views/posts
create spec/views/posts/index.html.erb_spec.rb
"""

Scenario: View generator with customized `default-path`
Given a file named ".rspec" with:
"""
--default-path behaviour
"""
And I run `bundle exec rails generate rspec:view posts index`
Then the features should pass
Then the output should contain:
"""
create behaviour/views/posts
create behaviour/views/posts/index.html.erb_spec.rb
"""
16 changes: 16 additions & 0 deletions lib/generators/rspec.rb
@@ -1,4 +1,5 @@
require 'rails/generators/named_base'
require 'rspec/core'
require 'rspec/rails/feature_check'

# @private
Expand All @@ -18,6 +19,21 @@ def self.source_root(path = nil)
@_rspec_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'rspec', generator_name, 'templates'))
end
end

# @private
# Load configuration from RSpec to ensure `--default-path` is set
def self.configuration
@configuration ||= begin
configuration = RSpec.configuration
options = RSpec::Core::ConfigurationOptions.new({})
options.configure(configuration)
configuration
end
end

def target_path(*paths)
File.join(self.class.configuration.default_path, *paths)
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/rspec/channel/channel_generator.rb
Expand Up @@ -5,7 +5,7 @@ module Generators
# @private
class ChannelGenerator < Base
def create_channel_spec
template 'channel_spec.rb.erb', File.join('spec/channels', class_path, "#{file_name}_channel_spec.rb")
template 'channel_spec.rb.erb', target_path('channels', class_path, "#{file_name}_channel_spec.rb")
end
end
end
Expand Down