Skip to content

Commit

Permalink
#117 Add a check for a valid MRML binary
Browse files Browse the repository at this point in the history
  • Loading branch information
sighmon committed Jan 22, 2024
1 parent fbb4a2a commit 861ce8b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/mjml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def self.valid_mjml_binary
check_for_custom_mjml_binary ||
check_for_yarn_mjml_binary ||
check_for_npm_mjml_binary ||
check_for_global_mjml_binary
check_for_global_mjml_binary ||
check_for_mrml_binary

return @@valid_mjml_binary if @@valid_mjml_binary

Expand Down Expand Up @@ -102,6 +103,13 @@ def self.check_for_global_mjml_binary
return mjml_bin if mjml_bin.present? && check_version(mjml_bin)
end

def self.check_for_mrml_binary
MRML.present?
rescue NameError
Mjml.mjml_binary_error_string = 'Couldn\'t find MRML - did you add \'mrml\' to your Gemfile?'
false
end

def self.discover_mjml_bin
logger.warn('`Mjml.discover_mjml_bin` is deprecated and has no effect anymore! ' \
'Please use `Mjml.mjml_binary=` to set a custom MJML binary.')
Expand Down
16 changes: 16 additions & 0 deletions test/mjml_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,13 @@ class NotifierMailerTest < ActiveSupport::TestCase
before do
Mjml.mjml_binary = nil
Mjml.valid_mjml_binary = nil
Mjml.use_mrml = nil
end

after do
Mjml.mjml_binary = nil
Mjml.valid_mjml_binary = nil
Mjml.use_mrml = nil
end

it 'can be set to a custom value with mjml_binary if version is correct' do
Expand Down Expand Up @@ -156,5 +158,19 @@ class NotifierMailerTest < ActiveSupport::TestCase
assert(err.message.start_with?("MJML.mjml_binary is set to 'set by mjml_binary' " \
'but MJML-Rails could not validate that it is a valid MJML binary'))
end

it 'can use MRML and check for a valid binary' do
Mjml.use_mrml = true
Mjml.stubs(:check_for_custom_mjml_binary).returns(false)
Mjml.stubs(:check_for_yarn_mjml_binary).returns(false)
Mjml.stubs(:check_for_npm_mjml_binary).returns(false)
Mjml.stubs(:check_for_global_mjml_binary).returns(false)
expect(Mjml.valid_mjml_binary).must_equal(true)

Mjml.valid_mjml_binary = nil
MRML.stubs(:present?).raises(NameError)
assert_nil(Mjml.valid_mjml_binary)
expect(Mjml.mjml_binary_error_string).must_equal 'Couldn\'t find MRML - did you add \'mrml\' to your Gemfile?'
end
end
end

0 comments on commit 861ce8b

Please sign in to comment.