Skip to content

Commit

Permalink
Merge pull request #2706 from rspec/fix-preview-paths
Browse files Browse the repository at this point in the history
Fix preview paths
  • Loading branch information
JonRowe committed Nov 14, 2023
2 parents 4eaf3b1 + b84484b commit 9da2a64
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 20 deletions.
@@ -1,3 +1,3 @@
if Rails.autoloaders.respond_to?(:main)
if Rails.autoloaders.respond_to?(:main) && Rails.autoloaders.main.respond_to?(:ignore)
Rails.autoloaders.main.ignore('lib/rails/generators/in_memory/model/model_generator.rb')
end
19 changes: 15 additions & 4 deletions example_app_generator/spec/support/default_preview_path
Expand Up @@ -37,12 +37,18 @@ require_file_stub 'config/environment' do
module ExampleApp
class Application < Rails::Application
config.eager_load = false
if Rails::VERSION::STRING.to_f >= 7.0
config.active_support.cache_format_version = 7.0
end

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false unless ENV['NO_ACTION_MAILER']

if ENV['CUSTOM_PREVIEW_PATH']
config.action_mailer.preview_path = ENV['CUSTOM_PREVIEW_PATH']
if Rails::VERSION::STRING.to_f >= 7.1
config.action_mailer.preview_paths = [ENV['CUSTOM_PREVIEW_PATH']]
else
config.action_mailer.preview_path = ENV['CUSTOM_PREVIEW_PATH']
end
end
if ENV['SHOW_PREVIEWS']
config.action_mailer.show_previews = (ENV['SHOW_PREVIEWS'] == 'true')
Expand All @@ -59,13 +65,18 @@ require_file_stub 'config/environment' do
Rails.application.initialize!
end

exit if ENV['NO_ACTION_MAILER']
exit(0) if ENV['NO_ACTION_MAILER']
if ENV['DEFAULT_URL']
puts ActionMailer::Base.default_url_options[:host]
elsif defined?(::ActionMailer::Preview)
puts Rails.application.config.action_mailer.preview_path
if Rails::VERSION::STRING.start_with?('7.1')
puts Rails.application.config.action_mailer.preview_paths
else
puts Rails.application.config.action_mailer.preview_path
end
end

# This will force the loading of ActionMailer settings to ensure we do not
# accidentally set something we should not
ActionMailer::Base.smtp_settings
exit 0
50 changes: 38 additions & 12 deletions example_app_generator/spec/verify_mailer_preview_path_spec.rb
Expand Up @@ -17,9 +17,17 @@ def as_commandline(ops)

def capture_exec(*ops)
ops << { err: [:child, :out] }
io = IO.popen(ops)
lines = []

_process =
IO.popen(ops) do |io|
while (line = io.gets)
lines << line
end
end

# Necessary to ignore warnings from Rails code base
out = io.readlines
out = lines
.reject { |line| line =~ /warning: circular argument reference/ }
.reject { |line| line =~ /Gem::Specification#rubyforge_project=/ }
.reject { |line| line =~ /DEPRECATION WARNING/ }
Expand All @@ -30,12 +38,32 @@ def capture_exec(*ops)
CaptureExec.new(out, $?.exitstatus)
end

def have_no_preview
have_attributes(io: be_blank, exit_status: 0)
if ENV['RAILS_VERSION'] == 'main' && Rails::VERSION::STRING == "7.2.0.alpha"
before do
skip('This is broken on Rails main but is skipped for green builds of 7.1.x, please fix')
end
end

before do
skip("Currently broken for unknown reasons")
if Rails::VERSION::STRING.to_f >= 7.1
let(:expected_custom_path) { "/custom/path\n#{::Rails.root}/test/mailers/previews" }
let(:expected_rspec_path) { "#{::Rails.root}/spec/mailers/previews\n#{::Rails.root}/test/mailers/previews" }

def have_no_preview(opts = {})
expected_io =
if opts[:actually_blank]
be_blank
else
"#{::Rails.root}/test/mailers/previews"
end
have_attributes(io: expected_io, exit_status: 0)
end
else
let(:expected_custom_path) { '/custom/path' }
let(:expected_rspec_path) { "#{::Rails.root}/spec/mailers/previews" }

def have_no_preview(_opts = {})
have_attributes(io: be_blank, exit_status: 0)
end
end

let(:exec_script) {
Expand All @@ -49,9 +77,7 @@ def have_no_preview

it 'sets the preview path to the default rspec path' do
skip "this spec fails singularly on JRuby due to weird env things" if RUBY_ENGINE == "jruby"
expect(capture_exec(custom_env, exec_script)).to eq(
"#{::Rails.root}/spec/mailers/previews"
)
expect(capture_exec(custom_env, exec_script)).to eq(expected_rspec_path)
end

it 'respects the setting from `show_previews`' do
Expand All @@ -69,7 +95,7 @@ def have_no_preview
custom_env.merge('CUSTOM_PREVIEW_PATH' => '/custom/path'),
exec_script
)
).to eq('/custom/path')
).to eq(expected_custom_path)
end

it 'allows initializers to set options' do
Expand All @@ -87,7 +113,7 @@ def have_no_preview
custom_env.merge('NO_ACTION_MAILER' => 'true'),
exec_script
)
).to have_no_preview
).to have_no_preview(actually_blank: true)
end
end

Expand All @@ -102,7 +128,7 @@ def have_no_preview
it 'respects the setting from `show_previews`' do
expect(
capture_exec(custom_env.merge('SHOW_PREVIEWS' => 'true'), exec_script)
).to eq("#{::Rails.root}/spec/mailers/previews")
).to eq(expected_rspec_path)
end

it 'allows initializers to set options' do
Expand Down
14 changes: 11 additions & 3 deletions lib/rspec-rails.rb
Expand Up @@ -47,10 +47,18 @@ def config_preview_path?(options)
end
end

def config_default_preview_path(options)
return unless options.preview_path.blank?
if ::Rails::VERSION::STRING >= "7.1.0"
def config_default_preview_path(options)
return unless options.preview_paths.empty?

options.preview_path = "#{::Rails.root}/spec/mailers/previews"
options.preview_paths << "#{::Rails.root}/spec/mailers/previews"
end
else
def config_default_preview_path(options)
return unless options.preview_path.blank?

options.preview_path = "#{::Rails.root}/spec/mailers/previews"
end
end

def supports_action_mailer_previews?(config)
Expand Down

0 comments on commit 9da2a64

Please sign in to comment.