Skip to content

Commit efd4e6f

Browse files
committed
Respect RSpec default_path for generators #2508
1 parent 40192d2 commit efd4e6f

30 files changed

+379
-28
lines changed

Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
### Development
22
[Full Changelog](https://github.com/rspec/rspec-rails/compare/v6.0.0.rc1...6-0-maintenance)
33

4+
Enhancements:
5+
6+
* Generators now respects default path configuration option. (@vivekmiyani, #2508)
7+
48
Breaking Changes:
59

610
* Change the order of `after_teardown` from `after` to `around` in system

features/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ without having to type RAILS_ENV=test.
3535

3636
Now you can run:
3737

38-
script/rails generate rspec:install
38+
bundle exec rails generate rspec:install
3939

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

43+
You can also customize the default spec path with `--default-path` option:
44+
45+
bundle exec rails generate rspec:install --default-path behaviour
46+
4347
## Issues
4448

4549
The documentation for rspec-rails is a work in progress. We'll be adding
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Feature: Channel generator spec
2+
3+
Scenario: Channel generator
4+
When I run `bundle exec rails generate channel group`
5+
Then the features should pass
6+
Then the output should contain:
7+
"""
8+
invoke rspec
9+
create spec/channels/group_channel_spec.rb
10+
"""
11+
Then the output should contain:
12+
"""
13+
create app/channels/group_channel.rb
14+
"""
15+
16+
Scenario: Channel generator with customized `default-path`
17+
Given a file named ".rspec" with:
18+
"""
19+
--default-path behaviour
20+
"""
21+
And I run `bundle exec rails generate channel group`
22+
Then the features should pass
23+
Then the output should contain:
24+
"""
25+
invoke rspec
26+
create behaviour/channels/group_channel_spec.rb
27+
"""
28+
Then the output should contain:
29+
"""
30+
create app/channels/group_channel.rb
31+
"""
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Feature: Controller generator spec
2+
3+
Scenario: Controller generator
4+
When I run `bundle exec rails generate controller posts`
5+
Then the features should pass
6+
Then the output should contain:
7+
"""
8+
create app/controllers/posts_controller.rb
9+
invoke erb
10+
create app/views/posts
11+
invoke rspec
12+
create spec/requests/posts_spec.rb
13+
invoke helper
14+
create app/helpers/posts_helper.rb
15+
invoke rspec
16+
create spec/helpers/posts_helper_spec.rb
17+
"""
18+
19+
Scenario: Controller generator with customized `default-path`
20+
Given a file named ".rspec" with:
21+
"""
22+
--default-path behaviour
23+
"""
24+
And I run `bundle exec rails generate controller posts`
25+
Then the features should pass
26+
Then the output should contain:
27+
"""
28+
create app/controllers/posts_controller.rb
29+
invoke erb
30+
create app/views/posts
31+
invoke rspec
32+
create behaviour/requests/posts_spec.rb
33+
invoke helper
34+
create app/helpers/posts_helper.rb
35+
invoke rspec
36+
create behaviour/helpers/posts_helper_spec.rb
37+
"""
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Feature: Feature generator spec
2+
3+
Scenario: Feature generator
4+
When I run `bundle exec rails generate rspec:feature posts`
5+
Then the features should pass
6+
Then the output should contain:
7+
"""
8+
create spec/features/posts_spec.rb
9+
"""
10+
11+
Scenario: Feature generator with customized `default-path`
12+
Given a file named ".rspec" with:
13+
"""
14+
--default-path behaviour
15+
"""
16+
And I run `bundle exec rails generate rspec:feature posts`
17+
Then the features should pass
18+
Then the output should contain:
19+
"""
20+
create behaviour/features/posts_spec.rb
21+
"""

features/generator_specs/generator_specs.feature

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,20 @@ Feature: Generator spec
1414
invoke rspec
1515
create spec/generator/my_generators_generator_spec.rb
1616
"""
17+
18+
Scenario: Use custom generator with customized `default-path`
19+
Given a file named ".rspec" with:
20+
"""
21+
--default-path behaviour
22+
"""
23+
And I run `bundle exec rails generate generator my_generator`
24+
Then the features should pass
25+
Then the output should contain:
26+
"""
27+
create lib/generators/my_generator
28+
create lib/generators/my_generator/my_generator_generator.rb
29+
create lib/generators/my_generator/USAGE
30+
create lib/generators/my_generator/templates
31+
invoke rspec
32+
create behaviour/generator/my_generators_generator_spec.rb
33+
"""
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Feature: Helper generator spec
2+
3+
Scenario: Helper generator
4+
When I run `bundle exec rails generate helper posts`
5+
Then the features should pass
6+
Then the output should contain:
7+
"""
8+
create app/helpers/posts_helper.rb
9+
invoke rspec
10+
create spec/helpers/posts_helper_spec.rb
11+
"""
12+
13+
Scenario: Helper generator with customized `default-path`
14+
Given a file named ".rspec" with:
15+
"""
16+
--default-path behaviour
17+
"""
18+
And I run `bundle exec rails generate helper posts`
19+
Then the features should pass
20+
Then the output should contain:
21+
"""
22+
create app/helpers/posts_helper.rb
23+
invoke rspec
24+
create behaviour/helpers/posts_helper_spec.rb
25+
"""
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Feature: Integration generator spec
2+
3+
Scenario: Integration generator
4+
When I run `bundle exec rails generate rspec:integration posts`
5+
Then the features should pass
6+
Then the output should contain:
7+
"""
8+
create spec/requests/posts_spec.rb
9+
"""
10+
11+
Scenario: Integration generator with customized `default-path`
12+
Given a file named ".rspec" with:
13+
"""
14+
--default-path behaviour
15+
"""
16+
And I run `bundle exec rails generate rspec:integration posts`
17+
Then the features should pass
18+
Then the output should contain:
19+
"""
20+
create behaviour/requests/posts_spec.rb
21+
"""
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Feature: Job generator spec
2+
3+
Scenario: Job generator
4+
When I run `bundle exec rails generate job user`
5+
Then the features should pass
6+
Then the output should contain:
7+
"""
8+
invoke rspec
9+
create spec/jobs/user_job_spec.rb
10+
create app/jobs/user_job.rb
11+
"""
12+
13+
Scenario: Job generator with customized `default-path`
14+
Given a file named ".rspec" with:
15+
"""
16+
--default-path behaviour
17+
"""
18+
And I run `bundle exec rails generate job user`
19+
Then the features should pass
20+
Then the output should contain:
21+
"""
22+
invoke rspec
23+
create behaviour/jobs/user_job_spec.rb
24+
create app/jobs/user_job.rb
25+
"""
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Feature: Mailbox generator spec
2+
3+
Scenario: Mailbox generator
4+
When I run `bundle exec rails generate mailbox forwards`
5+
Then the features should pass
6+
Then the output should contain:
7+
"""
8+
create app/mailboxes/forwards_mailbox.rb
9+
invoke rspec
10+
create spec/mailboxes/forwards_mailbox_spec.rb
11+
"""
12+
13+
Scenario: Mailbox generator with customized `default-path`
14+
Given a file named ".rspec" with:
15+
"""
16+
--default-path behaviour
17+
"""
18+
And I run `bundle exec rails generate mailbox forwards`
19+
Then the features should pass
20+
Then the output should contain:
21+
"""
22+
create app/mailboxes/forwards_mailbox.rb
23+
invoke rspec
24+
create behaviour/mailboxes/forwards_mailbox_spec.rb
25+
"""
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Feature: Mailer generator spec
2+
3+
Scenario: Mailer generator
4+
When I run `bundle exec rails generate mailer posts index show`
5+
Then the features should pass
6+
Then the output should contain:
7+
"""
8+
create app/mailers/posts_mailer.rb
9+
invoke erb
10+
create app/views/posts_mailer
11+
create app/views/posts_mailer/index.text.erb
12+
create app/views/posts_mailer/index.html.erb
13+
create app/views/posts_mailer/show.text.erb
14+
create app/views/posts_mailer/show.html.erb
15+
invoke rspec
16+
create spec/mailers/posts_spec.rb
17+
create spec/fixtures/posts/index
18+
create spec/fixtures/posts/show
19+
create spec/mailers/previews/posts_preview.rb
20+
"""
21+
22+
Scenario: Mailer generator with customized `default-path`
23+
Given a file named ".rspec" with:
24+
"""
25+
--default-path behaviour
26+
"""
27+
And I run `bundle exec rails generate mailer posts index show`
28+
Then the features should pass
29+
Then the output should contain:
30+
"""
31+
create app/mailers/posts_mailer.rb
32+
invoke erb
33+
create app/views/posts_mailer
34+
create app/views/posts_mailer/index.text.erb
35+
create app/views/posts_mailer/index.html.erb
36+
create app/views/posts_mailer/show.text.erb
37+
create app/views/posts_mailer/show.html.erb
38+
invoke rspec
39+
create behaviour/mailers/posts_spec.rb
40+
create behaviour/fixtures/posts/index
41+
create behaviour/fixtures/posts/show
42+
create behaviour/mailers/previews/posts_preview.rb
43+
"""
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Feature: Request generator spec
2+
3+
Scenario: Request generator
4+
When I run `bundle exec rails generate rspec:request posts`
5+
Then the features should pass
6+
Then the output should contain:
7+
"""
8+
create spec/requests/posts_spec.rb
9+
"""
10+
11+
Scenario: Request generator with customized `default-path`
12+
Given a file named ".rspec" with:
13+
"""
14+
--default-path behaviour
15+
"""
16+
And I run `bundle exec rails generate rspec:request posts`
17+
Then the features should pass
18+
Then the output should contain:
19+
"""
20+
create behaviour/requests/posts_spec.rb
21+
"""
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Feature: System generator spec
2+
3+
Scenario: System generator
4+
When I run `bundle exec rails generate rspec:system posts`
5+
Then the features should pass
6+
Then the output should contain:
7+
"""
8+
create spec/system/posts_spec.rb
9+
"""
10+
11+
Scenario: System generator with customized `default-path`
12+
Given a file named ".rspec" with:
13+
"""
14+
--default-path behaviour
15+
"""
16+
And I run `bundle exec rails generate rspec:system posts`
17+
Then the features should pass
18+
Then the output should contain:
19+
"""
20+
create behaviour/system/posts_spec.rb
21+
"""
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Feature: View generator spec
2+
3+
Scenario: View generator
4+
When I run `bundle exec rails generate rspec:view posts index`
5+
Then the features should pass
6+
Then the output should contain:
7+
"""
8+
create spec/views/posts
9+
create spec/views/posts/index.html.erb_spec.rb
10+
"""
11+
12+
Scenario: View generator with customized `default-path`
13+
Given a file named ".rspec" with:
14+
"""
15+
--default-path behaviour
16+
"""
17+
And I run `bundle exec rails generate rspec:view posts index`
18+
Then the features should pass
19+
Then the output should contain:
20+
"""
21+
create behaviour/views/posts
22+
create behaviour/views/posts/index.html.erb_spec.rb
23+
"""

lib/generators/rspec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'rails/generators/named_base'
2+
require 'rspec/core'
23
require 'rspec/rails/feature_check'
34

45
# @private
@@ -18,6 +19,21 @@ def self.source_root(path = nil)
1819
@_rspec_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'rspec', generator_name, 'templates'))
1920
end
2021
end
22+
23+
# @private
24+
# Load configuration from RSpec to ensure `--default-path` is set
25+
def self.configuration
26+
@configuration ||= begin
27+
configuration = RSpec.configuration
28+
options = RSpec::Core::ConfigurationOptions.new({})
29+
options.configure(configuration)
30+
configuration
31+
end
32+
end
33+
34+
def target_path(*paths)
35+
File.join(self.class.configuration.default_path, *paths)
36+
end
2137
end
2238
end
2339
end

lib/generators/rspec/channel/channel_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Generators
55
# @private
66
class ChannelGenerator < Base
77
def create_channel_spec
8-
template 'channel_spec.rb.erb', File.join('spec/channels', class_path, "#{file_name}_channel_spec.rb")
8+
template 'channel_spec.rb.erb', target_path('channels', class_path, "#{file_name}_channel_spec.rb")
99
end
1010
end
1111
end

0 commit comments

Comments
 (0)