Skip to content

Commit

Permalink
Support file_fixture in Factory definitions
Browse files Browse the repository at this point in the history
Related to [factory_bot#1282][]

[rails/rails#45606][] has been merged and is likely to be released as
part of Rails 7.1.

With that addition, the path toward resolving [factory_bot#1282][]
becomes more clear. If factories can pass along [Pathname][] instances
to attachment attributes, Active Support will handle the rest.

Instances of `ActiveSupport::TestCase` provide a [file_fixture][] helper
to construct a `Pathname` instance based on the path defined by
`ActiveSupport::TestCase.file_fixture_path` (relative to the Rails root
directory).

[factory_bot#1282]: thoughtbot/factory_bot#1282 (comment)
[rails/rails#45606]: rails/rails#45606
[Pathname]: https://docs.ruby-lang.org/en/master/Pathname.html
[file_fixture]: https://api.rubyonrails.org/classes/ActiveSupport/Testing/FileFixtures.html#method-i-file_fixture
  • Loading branch information
seanpdoyle committed Jan 10, 2025
1 parent 346e3c7 commit 61baf5f
Show file tree
Hide file tree
Showing 14 changed files with 310 additions and 170 deletions.
242 changes: 140 additions & 102 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,128 +8,148 @@ PATH
GEM
remote: https://rubygems.org/
specs:
actionpack (7.1.2)
actionview (= 7.1.2)
activesupport (= 7.1.2)
actionpack (7.2.2)
actionview (= 7.2.2)
activesupport (= 7.2.2)
nokogiri (>= 1.8.5)
racc
rack (>= 2.2.4)
rack (>= 2.2.4, < 3.2)
rack-session (>= 1.0.1)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
actionview (7.1.2)
activesupport (= 7.1.2)
useragent (~> 0.16)
actionview (7.2.2)
activesupport (= 7.2.2)
builder (~> 3.1)
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
activemodel (7.1.2)
activesupport (= 7.1.2)
activerecord (7.1.2)
activemodel (= 7.1.2)
activesupport (= 7.1.2)
activejob (7.2.2)
activesupport (= 7.2.2)
globalid (>= 0.3.6)
activemodel (7.2.2)
activesupport (= 7.2.2)
activerecord (7.2.2)
activemodel (= 7.2.2)
activesupport (= 7.2.2)
timeout (>= 0.4.0)
activesupport (7.1.2)
activestorage (7.2.2)
actionpack (= 7.2.2)
activejob (= 7.2.2)
activerecord (= 7.2.2)
activesupport (= 7.2.2)
marcel (~> 1.0)
activesupport (7.2.2)
base64
benchmark (>= 0.3)
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
concurrent-ruby (~> 1.0, >= 1.3.1)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
logger (>= 1.4.2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
appraisal (2.5.0)
bundler
rake
thor (>= 0.14.0)
aruba (2.1.0)
aruba (2.2.0)
bundler (>= 1.17, < 3.0)
childprocess (>= 2.0, < 5.0)
contracts (>= 0.16.0, < 0.18.0)
cucumber (>= 4.0, < 9.0)
cucumber (>= 8.0, < 10.0)
rspec-expectations (~> 3.4)
thor (~> 1.0)
ast (2.4.2)
base64 (0.2.0)
benchmark (0.3.0)
bigdecimal (3.1.8)
bigdecimal (3.1.8-java)
builder (3.3.0)
childprocess (4.1.0)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
contracts (0.17)
contracts (0.17.2)
crass (1.0.6)
cucumber (8.0.0)
builder (~> 3.2, >= 3.2.4)
cucumber-ci-environment (~> 9.0, >= 9.0.4)
cucumber-core (~> 11.0, >= 11.0.0)
cucumber-cucumber-expressions (~> 15.1, >= 15.1.1)
cucumber-gherkin (~> 23.0, >= 23.0.1)
cucumber-html-formatter (~> 19.1, >= 19.1.0)
cucumber-messages (~> 18.0, >= 18.0.0)
diff-lcs (~> 1.5, >= 1.5.0)
mime-types (~> 3.4, >= 3.4.1)
multi_test (~> 1.1, >= 1.1.0)
sys-uname (~> 1.2, >= 1.2.2)
cucumber-ci-environment (9.1.0)
cucumber-core (11.0.0)
cucumber-gherkin (~> 23.0, >= 23.0.1)
cucumber-messages (~> 18.0, >= 18.0.0)
cucumber-tag-expressions (~> 4.1, >= 4.1.0)
cucumber-cucumber-expressions (15.2.0)
cucumber-gherkin (23.0.1)
cucumber-messages (~> 18.0, >= 18.0.0)
cucumber-html-formatter (19.2.0)
cucumber-messages (~> 18.0, >= 18.0.0)
cucumber-messages (18.0.0)
cucumber-tag-expressions (4.1.0)
cucumber (9.2.0)
builder (~> 3.2)
cucumber-ci-environment (> 9, < 11)
cucumber-core (> 13, < 14)
cucumber-cucumber-expressions (~> 17.0)
cucumber-gherkin (> 24, < 28)
cucumber-html-formatter (> 20.3, < 22)
cucumber-messages (> 19, < 25)
diff-lcs (~> 1.5)
mini_mime (~> 1.1)
multi_test (~> 1.1)
sys-uname (~> 1.2)
cucumber-ci-environment (10.0.1)
cucumber-core (13.0.3)
cucumber-gherkin (>= 27, < 28)
cucumber-messages (>= 20, < 23)
cucumber-tag-expressions (> 5, < 7)
cucumber-cucumber-expressions (17.1.0)
bigdecimal
cucumber-gherkin (27.0.0)
cucumber-messages (>= 19.1.4, < 23)
cucumber-html-formatter (21.7.0)
cucumber-messages (> 19, < 27)
cucumber-messages (22.0.0)
cucumber-tag-expressions (6.1.1)
diff-lcs (1.5.1)
drb (2.2.1)
erubi (1.13.0)
factory_bot (6.5.0)
activesupport (>= 5.0.0)
ffi (1.15.5)
ffi (1.15.5-java)
i18n (1.14.5)
ffi (1.17.0-aarch64-linux-gnu)
ffi (1.17.0-aarch64-linux-musl)
ffi (1.17.0-arm-linux-gnu)
ffi (1.17.0-arm-linux-musl)
ffi (1.17.0-arm64-darwin)
ffi (1.17.0-x86-linux-gnu)
ffi (1.17.0-x86-linux-musl)
ffi (1.17.0-x86_64-darwin)
ffi (1.17.0-x86_64-linux-gnu)
ffi (1.17.0-x86_64-linux-musl)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
io-console (0.7.2)
io-console (0.7.2-java)
irb (1.14.0)
irb (1.14.1)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
jar-dependencies (0.4.1)
json (2.7.2)
json (2.7.2-java)
json (2.7.5)
language_server-protocol (3.17.0.3)
lint_roller (1.1.0)
loofah (2.22.0)
logger (1.6.1)
loofah (2.23.1)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
mime-types (3.4.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2023.0218.1)
mini_portile2 (2.8.7)
marcel (1.0.4)
mini_mime (1.1.5)
minitest (5.25.1)
multi_test (1.1.0)
mutex_m (0.2.0)
nokogiri (1.16.7)
mini_portile2 (~> 2.8.2)
nokogiri (1.16.7-aarch64-linux)
racc (~> 1.4)
nokogiri (1.16.7-arm-linux)
racc (~> 1.4)
nokogiri (1.16.7-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.7-java)
nokogiri (1.16.7-x86-linux)
racc (~> 1.4)
nokogiri (1.16.7-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.16.7-x86_64-linux)
racc (~> 1.4)
parallel (1.26.3)
parser (3.3.4.2)
parser (3.3.5.1)
ast (~> 2.4.1)
racc
psych (5.1.2)
stringio
psych (5.1.2-java)
jar-dependencies (>= 0.1.7)
racc (1.8.1)
racc (1.8.1-java)
rack (3.1.7)
rack (3.1.8)
rack-session (2.0.0)
rack (>= 3.0.0)
rack-test (2.1.0)
Expand All @@ -144,10 +164,10 @@ GEM
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
railties (7.1.2)
actionpack (= 7.1.2)
activesupport (= 7.1.2)
irb
railties (7.2.2)
actionpack (= 7.2.2)
activesupport (= 7.2.2)
irb (~> 1.13)
rackup (>= 1.0.0)
rake (>= 12.2)
thor (~> 1.0, >= 1.2.2)
Expand All @@ -157,77 +177,95 @@ GEM
rdoc (6.7.0)
psych (>= 4.0.0)
regexp_parser (2.9.2)
reline (0.5.9)
reline (0.5.10)
io-console (~> 0.5)
rexml (3.3.6)
strscan
rspec-core (3.13.0)
rspec-core (3.13.2)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.2)
rspec-expectations (3.13.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.1)
rspec-mocks (3.13.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-rails (6.1.4)
actionpack (>= 6.1)
activesupport (>= 6.1)
railties (>= 6.1)
rspec-rails (7.0.1)
actionpack (>= 7.0)
activesupport (>= 7.0)
railties (>= 7.0)
rspec-core (~> 3.13)
rspec-expectations (~> 3.13)
rspec-mocks (~> 3.13)
rspec-support (~> 3.13)
rspec-support (3.13.1)
rubocop (1.65.1)
rubocop (1.66.1)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 2.4, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-ast (>= 1.32.2, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.32.1)
rubocop-ast (1.33.0)
parser (>= 3.3.1.0)
rubocop-performance (1.21.1)
rubocop-performance (1.22.1)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (1.13.0)
sqlite3 (1.6.2)
mini_portile2 (~> 2.8.0)
standard (1.40.0)
securerandom (0.3.1)
sqlite3 (2.2.0-aarch64-linux-gnu)
sqlite3 (2.2.0-aarch64-linux-musl)
sqlite3 (2.2.0-arm-linux-gnu)
sqlite3 (2.2.0-arm-linux-musl)
sqlite3 (2.2.0-arm64-darwin)
sqlite3 (2.2.0-x86-linux-gnu)
sqlite3 (2.2.0-x86-linux-musl)
sqlite3 (2.2.0-x86_64-darwin)
sqlite3 (2.2.0-x86_64-linux-gnu)
sqlite3 (2.2.0-x86_64-linux-musl)
standard (1.41.1)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.0)
rubocop (~> 1.65.0)
rubocop (~> 1.66.0)
standard-custom (~> 1.0.0)
standard-performance (~> 1.4)
standard-performance (~> 1.5)
standard-custom (1.0.2)
lint_roller (~> 1.0)
rubocop (~> 1.50)
standard-performance (1.4.0)
standard-performance (1.5.0)
lint_roller (~> 1.1)
rubocop-performance (~> 1.21.0)
rubocop-performance (~> 1.22.0)
stringio (3.1.1)
strscan (3.1.0)
strscan (3.1.0-java)
sys-uname (1.2.3)
sys-uname (1.3.0)
ffi (~> 1.1)
thor (1.3.2)
timeout (0.4.1)
timeout (0.4.2)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
webrick (1.8.1)
zeitwerk (2.6.17)
unicode-display_width (2.6.0)
useragent (0.16.10)
webrick (1.8.2)
zeitwerk (2.7.1)

PLATFORMS
java
ruby
aarch64-linux
aarch64-linux-gnu
aarch64-linux-musl
arm-linux
arm-linux-gnu
arm-linux-musl
arm64-darwin
x86-linux
x86-linux-gnu
x86-linux-musl
x86_64-darwin
x86_64-linux
x86_64-linux-gnu
x86_64-linux-musl

DEPENDENCIES
activerecord (>= 5.0.0)
activestorage (>= 5.0.0)
appraisal
aruba
cucumber
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ using an empty array:
config.factory_bot.definition_file_paths = []
```

### File Fixture Support

Factories have access to [ActiveSupport::Testing::FileFixtures#file_fixture][]
helper to read files from tests.

To disable file fixture support, set `file_fixture_support = false`:

```rb
config.factory_bot.file_fixture_support = false
```

[ActiveSupport::Testing::FileFixtures#file_fixture]: https://api.rubyonrails.org/classes/ActiveSupport/Testing/FileFixtures.html#method-i-file_fixture

### Generators

Including factory\_bot\_rails in the development group of your Gemfile
Expand Down
5 changes: 4 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "bundler/setup"
require "cucumber/rake/task"
require "rspec/core/rake_task"
require "minitest/test_task"
require "standard/rake"

Bundler::GemHelper.install_tasks name: "factory_bot_rails"
Expand All @@ -12,5 +13,7 @@ end

RSpec::Core::RakeTask.new(:spec)

Minitest::TestTask.create

desc "Run the test suite and standard"
task default: %w[spec cucumber standard]
task default: %w[spec test cucumber standard]
Loading

0 comments on commit 61baf5f

Please sign in to comment.