File: verify_mailer_preview_path_spec.rb

package info (click to toggle)
ruby-rspec-rails 7.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,796 kB
  • sloc: ruby: 11,068; sh: 198; makefile: 6
file content (194 lines) | stat: -rw-r--r-- 5,684 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
require 'rails_helper'
require 'rspec/rails/feature_check'

RSpec.describe 'Action Mailer railtie hook' do
  CaptureExec = Struct.new(:io, :exit_status) do
    def ==(str)
      io == str
    end
  end

  def as_commandline(ops)
    cmd, ops = ops.reverse
    ops ||= {}
    cmd_parts = ops.map { |k, v| "#{k}=#{v}" } << cmd << '2>&1'
    cmd_parts.join(' ')
  end

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

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

    # Necessary to ignore warnings from Rails code base
    out = lines
              .reject { |line| line =~ /warning: circular argument reference/ }
              .reject { |line| line =~ /Gem::Specification#rubyforge_project=/ }
              .reject { |line| line =~ /DEPRECATION WARNING/ }
              .reject { |line| line =~ /warning: previous/ }
              .reject { |line| line =~ /warning: already/ }
              .reject { |line| line =~ /but will no longer be part of the default gems / }
              .reject { |line| line =~ /You can add .* to your Gemfile/ }
              .join
              .chomp
    CaptureExec.new(out, $?.exitstatus)
  end

  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) {
    File.expand_path(File.join(__FILE__, '../support/default_preview_path'))
  }

  if RSpec::Rails::FeatureCheck.has_action_mailer_preview?
    context 'in the development environment' do
      let(:custom_env) { { 'RAILS_ENV' => rails_env } }
      let(:rails_env) { 'development' }

      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(expected_rspec_path)
      end

      it 'respects the setting from `show_previews`' do
        expect(
          capture_exec(
            custom_env.merge('SHOW_PREVIEWS' => 'false'),
            exec_script
          )
        ).to have_no_preview
      end

      it 'respects a custom `preview_path`' do
        expect(
          capture_exec(
            custom_env.merge('CUSTOM_PREVIEW_PATH' => '/custom/path'),
            exec_script
          )
        ).to eq(expected_custom_path)
      end

      it 'allows initializers to set options' do
        expect(
          capture_exec(
            custom_env.merge('DEFAULT_URL' => 'test-host'),
            exec_script
          )
        ).to eq('test-host')
      end

      it 'handles action mailer not being available' do
        expect(
          capture_exec(
            custom_env.merge('NO_ACTION_MAILER' => 'true'),
            exec_script
          )
        ).to have_no_preview(actually_blank: true)
      end
    end

    context 'in a non-development environment' do
      let(:custom_env) { { 'RAILS_ENV' => rails_env } }
      let(:rails_env) { 'test' }

      it 'does not set the preview path by default' do
        expect(capture_exec(custom_env, exec_script)).to have_no_preview
      end

      it 'respects the setting from `show_previews`' do
        expect(
          capture_exec(custom_env.merge('SHOW_PREVIEWS' => 'true'), exec_script)
        ).to eq(expected_rspec_path)
      end

      it 'allows initializers to set options' do
        expect(
          capture_exec(
            custom_env.merge('DEFAULT_URL' => 'test-host'),
            exec_script
          )
        ).to eq('test-host')
      end
    end
  else
    context 'in the development environment' do
      let(:custom_env) { { 'RAILS_ENV' => rails_env } }
      let(:rails_env) { 'development' }

      it 'handles no action mailer preview' do
        expect(capture_exec(custom_env, exec_script)).to have_no_preview
      end

      it 'allows initializers to set options' do
        expect(
          capture_exec(
            custom_env.merge('DEFAULT_URL' => 'test-host'),
            exec_script
          )
        ).to eq('test-host')
      end

      it 'handles action mailer not being available' do
        expect(
          capture_exec(
            custom_env.merge('NO_ACTION_MAILER' => 'true'),
            exec_script
          )
        ).to have_no_preview
      end
    end

    context 'in a non-development environment' do
      let(:custom_env) { { 'RAILS_ENV' => rails_env } }
      let(:rails_env) { 'test' }

      it 'handles no action mailer preview' do
        expect(capture_exec(custom_env, exec_script)).to have_no_preview
      end

      it 'allows initializers to set options' do
        expect(
          capture_exec(
            custom_env.merge('DEFAULT_URL' => 'test-host'),
            exec_script
          )
        ).to eq('test-host')
      end

      it 'handles action mailer not being available' do
        expect(
          capture_exec(
            custom_env.merge('NO_ACTION_MAILER' => 'true'),
            exec_script
          )
        ).to have_no_preview
      end
    end
  end
end