Skip to content

Expand file_fixture_path against Rails.root#2902

Open
hammadxcm wants to merge 1 commit into
rspec:mainfrom
hammadxcm:fix-2682-file-fixture-path-absolute
Open

Expand file_fixture_path against Rails.root#2902
hammadxcm wants to merge 1 commit into
rspec:mainfrom
hammadxcm:fix-2682-file-fixture-path-absolute

Conversation

@hammadxcm

Copy link
Copy Markdown
Contributor

Fixes #2682.

Problem

The configured default file_fixture_path is the relative string 'spec/fixtures/files' (lib/rspec/rails/configuration.rb:84). When applied to example groups via FileFixtureSupport, it stays relative. But ActiveSupport::TestCase (and downstream consumers such as ActionMailer's read_fixture) treat file_fixture_path as absolute — they hand it directly to Pathname.new / File.join expecting a rooted path.

Issue reporter @jasonkarns hit this while fixing read_fixture and noted that all Rails-side defaults for file_fixture_path are absolute (either via File.expand_path(__dir__) or Rails.root.join(...)).

Fix

Expand the configured value against Rails.root in FileFixtureSupport.included before assigning it to the example group and to ActiveStorage::FixtureSet. File.expand_path is a no-op on already-absolute paths, so users who override the config with an absolute path see no behavioural change.

The user-facing config default (config.file_fixture_path = 'spec/fixtures/files') is intentionally left as the relative form — that's the conventional value people write in RSpec.configure and matches what documentation references. Only the value that gets exposed to example groups becomes absolute.

Tests

New spec spec/rspec/rails/file_fixture_support_spec.rb covers three cases:

  1. A relative configured path gets expanded against Rails.root.
  2. An absolute configured path passes through unchanged.
  3. ActiveStorage::FixtureSet.file_fixture_path receives the expanded value.

Test plan

  • bundle exec rspec spec/rspec/rails/file_fixture_support_spec.rb — 3/3 pass
  • bundle exec rspec spec/rspec/rails/configuration_spec.rb spec/rspec/rails/fixture_support_spec.rb — 67/67 pass
  • bundle exec rubocop on changed files — clean

The configured `file_fixture_path` default is the relative string
`spec/fixtures/files`. When this is assigned to example groups it stays
relative, but `ActiveSupport::TestCase` and downstream consumers (e.g.
ActionMailer's `read_fixture`) expect an absolute path.

Expand the configured value against `Rails.root` in `FileFixtureSupport`
before assigning it to example groups and `ActiveStorage::FixtureSet`.
`File.expand_path` is a no-op on already-absolute paths, so user-supplied
absolute overrides pass through unchanged.

Fixes rspec#2682.
Sign up for free to 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.

default file_fixture_path is relative but is absolute in rails itself

1 participant