Skip to content

Commit b6c4e8d

Browse files
Add AuthenticationGenerator for users factory creation (#542)
* Add AuthenticationGenerator for users factory creation This change addresses an issue where running the `factory_bot` generator could result in a `factory_bot [not found]` error. The problem occurred due to missing generator definitions and template files. With this update, the authentication generator is properly defined and includes the necessary templates, ensuring that the `users.rb` factory file is generated regardless of whether the project is using RSpec (`spec/factories`) or Minitest (`test/factories`). This guarantees consistent behavior and avoids generator errors in environments without a `spec` directory. Previously, projects without a `spec` directory or with incomplete generator/template setup would fail when invoking `factory_bot` generators, making it impossible to scaffold the necessary factory files automatically. - Added the missing `factory_bot:authentication` generator definition. - Included `users.rb` template under `templates` directory. - Implemented logic to detect the test framework and place the generated factory file in the correct directory (`spec/factories` or `test/factories`). - Ensured generator runs successfully regardless of the presence of a `spec` directory. * Inherit AuthenticationGenerator from ModelGenerator --------- Co-authored-by: Rodrigo Toledo <[email protected]>
1 parent 364bd89 commit b6c4e8d

File tree

6 files changed

+125
-62
lines changed

6 files changed

+125
-62
lines changed

Gemfile.lock

Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,38 @@ PATH
88
GEM
99
remote: https://rubygems.org/
1010
specs:
11-
actionpack (8.0.2)
12-
actionview (= 8.0.2)
13-
activesupport (= 8.0.2)
11+
actionpack (8.0.2.1)
12+
actionview (= 8.0.2.1)
13+
activesupport (= 8.0.2.1)
1414
nokogiri (>= 1.8.5)
1515
rack (>= 2.2.4)
1616
rack-session (>= 1.0.1)
1717
rack-test (>= 0.6.3)
1818
rails-dom-testing (~> 2.2)
1919
rails-html-sanitizer (~> 1.6)
2020
useragent (~> 0.16)
21-
actionview (8.0.2)
22-
activesupport (= 8.0.2)
21+
actionview (8.0.2.1)
22+
activesupport (= 8.0.2.1)
2323
builder (~> 3.1)
2424
erubi (~> 1.11)
2525
rails-dom-testing (~> 2.2)
2626
rails-html-sanitizer (~> 1.6)
27-
activejob (8.0.2)
28-
activesupport (= 8.0.2)
27+
activejob (8.0.2.1)
28+
activesupport (= 8.0.2.1)
2929
globalid (>= 0.3.6)
30-
activemodel (8.0.2)
31-
activesupport (= 8.0.2)
32-
activerecord (8.0.2)
33-
activemodel (= 8.0.2)
34-
activesupport (= 8.0.2)
30+
activemodel (8.0.2.1)
31+
activesupport (= 8.0.2.1)
32+
activerecord (8.0.2.1)
33+
activemodel (= 8.0.2.1)
34+
activesupport (= 8.0.2.1)
3535
timeout (>= 0.4.0)
36-
activestorage (8.0.2)
37-
actionpack (= 8.0.2)
38-
activejob (= 8.0.2)
39-
activerecord (= 8.0.2)
40-
activesupport (= 8.0.2)
36+
activestorage (8.0.2.1)
37+
actionpack (= 8.0.2.1)
38+
activejob (= 8.0.2.1)
39+
activerecord (= 8.0.2.1)
40+
activesupport (= 8.0.2.1)
4141
marcel (~> 1.0)
42-
activesupport (8.0.2)
42+
activesupport (8.0.2.1)
4343
base64
4444
benchmark (>= 0.3)
4545
bigdecimal
@@ -56,90 +56,91 @@ GEM
5656
bundler
5757
rake
5858
thor (>= 0.14.0)
59-
aruba (2.3.0)
59+
aruba (2.3.1)
6060
bundler (>= 1.17, < 3.0)
6161
contracts (>= 0.16.0, < 0.18.0)
62-
cucumber (>= 8.0, < 10.0)
62+
cucumber (>= 8.0, < 11.0)
6363
rspec-expectations (~> 3.4)
6464
thor (~> 1.0)
6565
ast (2.4.3)
6666
base64 (0.3.0)
6767
benchmark (0.4.1)
68-
bigdecimal (3.2.2)
69-
bigdecimal (3.2.2-java)
68+
bigdecimal (3.2.3)
69+
bigdecimal (3.2.3-java)
7070
builder (3.3.0)
7171
concurrent-ruby (1.3.5)
72-
connection_pool (2.5.3)
72+
connection_pool (2.5.4)
7373
contracts (0.17.2)
7474
crass (1.0.6)
75-
cucumber (9.2.1)
75+
cucumber (10.1.0)
76+
base64 (~> 0.2)
7677
builder (~> 3.2)
7778
cucumber-ci-environment (> 9, < 11)
78-
cucumber-core (> 13, < 14)
79-
cucumber-cucumber-expressions (~> 17.0)
80-
cucumber-gherkin (> 24, < 28)
79+
cucumber-core (> 15, < 17)
80+
cucumber-cucumber-expressions (> 17, < 19)
8181
cucumber-html-formatter (> 20.3, < 22)
82-
cucumber-messages (> 19, < 25)
8382
diff-lcs (~> 1.5)
83+
logger (~> 1.6)
8484
mini_mime (~> 1.1)
8585
multi_test (~> 1.1)
86-
sys-uname (~> 1.2)
86+
sys-uname (~> 1.3)
8787
cucumber-ci-environment (10.0.1)
88-
cucumber-core (13.0.3)
89-
cucumber-gherkin (>= 27, < 28)
90-
cucumber-messages (>= 20, < 23)
88+
cucumber-core (15.2.1)
89+
cucumber-gherkin (> 27, < 33)
90+
cucumber-messages (> 26, < 30)
9191
cucumber-tag-expressions (> 5, < 7)
92-
cucumber-cucumber-expressions (17.1.0)
92+
cucumber-cucumber-expressions (18.0.1)
9393
bigdecimal
94-
cucumber-gherkin (27.0.0)
95-
cucumber-messages (>= 19.1.4, < 23)
96-
cucumber-html-formatter (21.10.1)
94+
cucumber-gherkin (32.2.0)
95+
cucumber-messages (> 25, < 28)
96+
cucumber-html-formatter (21.14.0)
9797
cucumber-messages (> 19, < 28)
98-
cucumber-messages (22.0.0)
98+
cucumber-messages (27.2.0)
9999
cucumber-tag-expressions (6.1.2)
100100
date (3.4.1)
101101
date (3.4.1-java)
102102
diff-lcs (1.6.2)
103103
drb (2.2.3)
104-
erb (5.0.1)
105-
erb (5.0.1-java)
104+
erb (5.0.2)
105+
erb (5.0.2-java)
106106
erubi (1.13.1)
107-
factory_bot (6.5.4)
107+
factory_bot (6.5.5)
108108
activesupport (>= 6.1.0)
109109
ffi (1.17.2)
110110
ffi (1.17.2-java)
111111
globalid (1.2.1)
112112
activesupport (>= 6.1)
113113
i18n (1.14.7)
114114
concurrent-ruby (~> 1.0)
115-
io-console (0.8.0)
116-
io-console (0.8.0-java)
115+
io-console (0.8.1)
116+
io-console (0.8.1-java)
117117
irb (1.15.2)
118118
pp (>= 0.6.0)
119119
rdoc (>= 4.0.0)
120120
reline (>= 0.4.2)
121121
jar-dependencies (0.5.5)
122-
json (2.12.2)
123-
json (2.12.2-java)
122+
json (2.13.2)
123+
json (2.13.2-java)
124124
language_server-protocol (3.17.0.5)
125125
lint_roller (1.1.0)
126126
logger (1.7.0)
127127
loofah (2.24.1)
128128
crass (~> 1.0.2)
129129
nokogiri (>= 1.12.0)
130130
marcel (1.0.4)
131+
memoist3 (1.0.0)
131132
mini_mime (1.1.5)
132133
mini_portile2 (2.8.9)
133134
minitest (5.25.5)
134135
multi_test (1.1.0)
135136
mutex_m (0.3.0)
136-
nokogiri (1.18.8)
137+
nokogiri (1.18.9)
137138
mini_portile2 (~> 2.8.2)
138139
racc (~> 1.4)
139-
nokogiri (1.18.8-java)
140+
nokogiri (1.18.9-java)
140141
racc (~> 1.4)
141142
parallel (1.27.0)
142-
parser (3.3.8.0)
143+
parser (3.3.9.0)
143144
ast (~> 2.4.1)
144145
racc
145146
pp (0.6.2)
@@ -154,7 +155,7 @@ GEM
154155
jar-dependencies (>= 0.1.7)
155156
racc (1.8.1)
156157
racc (1.8.1-java)
157-
rack (3.1.16)
158+
rack (3.2.1)
158159
rack-session (2.1.1)
159160
base64 (>= 0.1.0)
160161
rack (>= 3.0.0)
@@ -169,39 +170,39 @@ GEM
169170
rails-html-sanitizer (1.6.2)
170171
loofah (~> 2.21)
171172
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
172-
railties (8.0.2)
173-
actionpack (= 8.0.2)
174-
activesupport (= 8.0.2)
173+
railties (8.0.2.1)
174+
actionpack (= 8.0.2.1)
175+
activesupport (= 8.0.2.1)
175176
irb (~> 1.13)
176177
rackup (>= 1.0.0)
177178
rake (>= 12.2)
178179
thor (~> 1.0, >= 1.2.2)
179180
zeitwerk (~> 2.6)
180181
rainbow (3.1.1)
181182
rake (13.3.0)
182-
rdoc (6.14.0)
183+
rdoc (6.14.2)
183184
erb
184185
psych (>= 4.0.0)
185-
regexp_parser (2.10.0)
186-
reline (0.6.1)
186+
regexp_parser (2.11.2)
187+
reline (0.6.2)
187188
io-console (~> 0.5)
188-
rspec-core (3.13.4)
189+
rspec-core (3.13.5)
189190
rspec-support (~> 3.13.0)
190191
rspec-expectations (3.13.5)
191192
diff-lcs (>= 1.2.0, < 2.0)
192193
rspec-support (~> 3.13.0)
193194
rspec-mocks (3.13.5)
194195
diff-lcs (>= 1.2.0, < 2.0)
195196
rspec-support (~> 3.13.0)
196-
rspec-rails (8.0.0)
197+
rspec-rails (8.0.2)
197198
actionpack (>= 7.2)
198199
activesupport (>= 7.2)
199200
railties (>= 7.2)
200201
rspec-core (~> 3.13)
201202
rspec-expectations (~> 3.13)
202203
rspec-mocks (~> 3.13)
203204
rspec-support (~> 3.13)
204-
rspec-support (3.13.4)
205+
rspec-support (3.13.5)
205206
rubocop (1.75.8)
206207
json (~> 2.3)
207208
language_server-protocol (~> 3.17.0.2)
@@ -213,7 +214,7 @@ GEM
213214
rubocop-ast (>= 1.44.0, < 2.0)
214215
ruby-progressbar (~> 1.7)
215216
unicode-display_width (>= 2.4.0, < 4.0)
216-
rubocop-ast (1.45.1)
217+
rubocop-ast (1.46.0)
217218
parser (>= 3.3.7.2)
218219
prism (~> 1.4)
219220
rubocop-performance (1.25.0)
@@ -222,7 +223,7 @@ GEM
222223
rubocop-ast (>= 1.38.0, < 2.0)
223224
ruby-progressbar (1.13.0)
224225
securerandom (0.4.1)
225-
sqlite3 (2.7.0)
226+
sqlite3 (2.7.3)
226227
mini_portile2 (~> 2.8.0)
227228
standard (1.50.0)
228229
language_server-protocol (~> 3.17.0.2)
@@ -237,13 +238,14 @@ GEM
237238
lint_roller (~> 1.1)
238239
rubocop-performance (~> 1.25.0)
239240
stringio (3.1.7)
240-
sys-uname (1.3.1)
241+
sys-uname (1.4.1)
241242
ffi (~> 1.1)
242-
thor (1.3.2)
243+
memoist3 (~> 1.0.0)
244+
thor (1.4.0)
243245
timeout (0.4.3)
244246
tzinfo (2.0.6)
245247
concurrent-ruby (~> 1.0)
246-
unicode-display_width (3.1.4)
248+
unicode-display_width (3.1.5)
247249
unicode-emoji (~> 4.0, >= 4.0.4)
248250
unicode-emoji (4.0.4)
249251
uri (1.0.3)

features/generators.feature

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,21 @@ Feature:
9090
And I run `bundle exec rails generate model User name:string age:integer` with a clean environment
9191
Then the output should not contain "test/factories/users.rb"
9292
And the output should contain "test/fixtures/users.yml"
93+
94+
95+
@rails_8
96+
Scenario: The factory_bot_rails authentication generator, coupled with rspec-rails, creates a user factory file
97+
When I add "rspec-rails" as a dependency
98+
And I run `bundle install --verbose` with a clean environment
99+
Then the output should contain "rspec-rails"
100+
And I run `bundle exec rails generate authentication` with a clean environment
101+
Then the output should contain "spec/factories/users.rb"
102+
And the file "spec/factories/users.rb" should contain exactly:
103+
"""
104+
FactoryBot.define do
105+
factory :user do
106+
email_address { "[email protected]" }
107+
password { "password" }
108+
end
109+
end
110+
"""

features/support/env.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111
setup_aruba
1212
end
1313

14+
Before("@rails_8") do
15+
rails_version = Gem::Version.new(
16+
Bundler.load.specs.find { |spec| spec.name == "rails" }&.version.to_s
17+
)
18+
required_version = Gem::Version.new("8.0.0")
19+
20+
if rails_version < required_version
21+
skip_this_scenario("Requires Rails 8.0 or higher (current: #{rails_version})")
22+
end
23+
end
24+
1425
if RUBY_PLATFORM == "java"
1526
Aruba.configure do |config|
1627
config.before_cmd do

gemfiles/rails8.0.gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ source "https://rubygems.org"
44

55
gem "appraisal"
66
gem "aruba"
7+
gem "bcrypt", "~> 3.1.7"
78
gem "brakeman"
89
gem "byebug"
910
gem "cucumber"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require "generators/factory_bot"
2+
require "generators/factory_bot/model/model_generator"
3+
require "factory_bot_rails"
4+
5+
module FactoryBot
6+
module Generators
7+
FixedAttribute = Struct.new(:name, :default)
8+
9+
class AuthenticationGenerator < ModelGenerator
10+
source_paths << File.join(File.dirname(__FILE__), "../model/templates")
11+
12+
private
13+
14+
def attributes
15+
[
16+
FixedAttribute.new(:email_address, "[email protected]"),
17+
FixedAttribute.new(:password, "password")
18+
]
19+
end
20+
end
21+
end
22+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
FactoryBot.define do
4+
factory :user do
5+
password { "password123" }
6+
password_confirmation { "password123" }
7+
email_address { "user#{SecureRandom.hex(3)}@example.com" }
8+
end
9+
end

0 commit comments

Comments
 (0)