Skip to content

Commit 9da2a64

Browse files
authored
Merge pull request #2706 from rspec/fix-preview-paths
Fix preview paths
2 parents 4eaf3b1 + b84484b commit 9da2a64

File tree

4 files changed

+65
-20
lines changed

4 files changed

+65
-20
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
if Rails.autoloaders.respond_to?(:main)
1+
if Rails.autoloaders.respond_to?(:main) && Rails.autoloaders.main.respond_to?(:ignore)
22
Rails.autoloaders.main.ignore('lib/rails/generators/in_memory/model/model_generator.rb')
33
end

example_app_generator/spec/support/default_preview_path

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,18 @@ require_file_stub 'config/environment' do
3737
module ExampleApp
3838
class Application < Rails::Application
3939
config.eager_load = false
40+
if Rails::VERSION::STRING.to_f >= 7.0
41+
config.active_support.cache_format_version = 7.0
42+
end
4043

4144
# Don't care if the mailer can't send.
4245
config.action_mailer.raise_delivery_errors = false unless ENV['NO_ACTION_MAILER']
43-
4446
if ENV['CUSTOM_PREVIEW_PATH']
45-
config.action_mailer.preview_path = ENV['CUSTOM_PREVIEW_PATH']
47+
if Rails::VERSION::STRING.to_f >= 7.1
48+
config.action_mailer.preview_paths = [ENV['CUSTOM_PREVIEW_PATH']]
49+
else
50+
config.action_mailer.preview_path = ENV['CUSTOM_PREVIEW_PATH']
51+
end
4652
end
4753
if ENV['SHOW_PREVIEWS']
4854
config.action_mailer.show_previews = (ENV['SHOW_PREVIEWS'] == 'true')
@@ -59,13 +65,18 @@ require_file_stub 'config/environment' do
5965
Rails.application.initialize!
6066
end
6167

62-
exit if ENV['NO_ACTION_MAILER']
68+
exit(0) if ENV['NO_ACTION_MAILER']
6369
if ENV['DEFAULT_URL']
6470
puts ActionMailer::Base.default_url_options[:host]
6571
elsif defined?(::ActionMailer::Preview)
66-
puts Rails.application.config.action_mailer.preview_path
72+
if Rails::VERSION::STRING.start_with?('7.1')
73+
puts Rails.application.config.action_mailer.preview_paths
74+
else
75+
puts Rails.application.config.action_mailer.preview_path
76+
end
6777
end
6878

6979
# This will force the loading of ActionMailer settings to ensure we do not
7080
# accidentally set something we should not
7181
ActionMailer::Base.smtp_settings
82+
exit 0

example_app_generator/spec/verify_mailer_preview_path_spec.rb

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,17 @@ def as_commandline(ops)
1717

1818
def capture_exec(*ops)
1919
ops << { err: [:child, :out] }
20-
io = IO.popen(ops)
20+
lines = []
21+
22+
_process =
23+
IO.popen(ops) do |io|
24+
while (line = io.gets)
25+
lines << line
26+
end
27+
end
28+
2129
# Necessary to ignore warnings from Rails code base
22-
out = io.readlines
30+
out = lines
2331
.reject { |line| line =~ /warning: circular argument reference/ }
2432
.reject { |line| line =~ /Gem::Specification#rubyforge_project=/ }
2533
.reject { |line| line =~ /DEPRECATION WARNING/ }
@@ -30,12 +38,32 @@ def capture_exec(*ops)
3038
CaptureExec.new(out, $?.exitstatus)
3139
end
3240

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

37-
before do
38-
skip("Currently broken for unknown reasons")
47+
if Rails::VERSION::STRING.to_f >= 7.1
48+
let(:expected_custom_path) { "/custom/path\n#{::Rails.root}/test/mailers/previews" }
49+
let(:expected_rspec_path) { "#{::Rails.root}/spec/mailers/previews\n#{::Rails.root}/test/mailers/previews" }
50+
51+
def have_no_preview(opts = {})
52+
expected_io =
53+
if opts[:actually_blank]
54+
be_blank
55+
else
56+
"#{::Rails.root}/test/mailers/previews"
57+
end
58+
have_attributes(io: expected_io, exit_status: 0)
59+
end
60+
else
61+
let(:expected_custom_path) { '/custom/path' }
62+
let(:expected_rspec_path) { "#{::Rails.root}/spec/mailers/previews" }
63+
64+
def have_no_preview(_opts = {})
65+
have_attributes(io: be_blank, exit_status: 0)
66+
end
3967
end
4068

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

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

5783
it 'respects the setting from `show_previews`' do
@@ -69,7 +95,7 @@ def have_no_preview
6995
custom_env.merge('CUSTOM_PREVIEW_PATH' => '/custom/path'),
7096
exec_script
7197
)
72-
).to eq('/custom/path')
98+
).to eq(expected_custom_path)
7399
end
74100

75101
it 'allows initializers to set options' do
@@ -87,7 +113,7 @@ def have_no_preview
87113
custom_env.merge('NO_ACTION_MAILER' => 'true'),
88114
exec_script
89115
)
90-
).to have_no_preview
116+
).to have_no_preview(actually_blank: true)
91117
end
92118
end
93119

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

108134
it 'allows initializers to set options' do

lib/rspec-rails.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,18 @@ def config_preview_path?(options)
4747
end
4848
end
4949

50-
def config_default_preview_path(options)
51-
return unless options.preview_path.blank?
50+
if ::Rails::VERSION::STRING >= "7.1.0"
51+
def config_default_preview_path(options)
52+
return unless options.preview_paths.empty?
5253

53-
options.preview_path = "#{::Rails.root}/spec/mailers/previews"
54+
options.preview_paths << "#{::Rails.root}/spec/mailers/previews"
55+
end
56+
else
57+
def config_default_preview_path(options)
58+
return unless options.preview_path.blank?
59+
60+
options.preview_path = "#{::Rails.root}/spec/mailers/previews"
61+
end
5462
end
5563

5664
def supports_action_mailer_previews?(config)

0 commit comments

Comments
 (0)