From 6ebb96243be87ae65385b0b33b511118e243031a Mon Sep 17 00:00:00 2001 From: Nicolas Brousse Date: Wed, 15 May 2019 19:41:45 +0200 Subject: [PATCH] Raise better error when mjml binary is not found --- lib/mjml.rb | 4 ++++ spec/mjml_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 spec/mjml_spec.rb diff --git a/lib/mjml.rb b/lib/mjml.rb index 281734a..ff1d163 100644 --- a/lib/mjml.rb +++ b/lib/mjml.rb @@ -5,6 +5,8 @@ # MJML library for ruby module MJML + class BinaryNotFound < StandardError; end + # Constants MIME_TYPE = 'text/mjml'.freeze EXTENSION = '.mjml'.freeze @@ -62,6 +64,8 @@ def self.extract_executable_version end match.nil? ? nil : match[1] + rescue Errno::ENOENT => _e + raise BinaryNotFound, "mjml binary not found for path '#{config.bin_path}'" end def self.logger diff --git a/spec/mjml_spec.rb b/spec/mjml_spec.rb new file mode 100644 index 0000000..1f523c9 --- /dev/null +++ b/spec/mjml_spec.rb @@ -0,0 +1,20 @@ +require_relative 'spec_helper' +require 'mjml' + +describe 'MJML' do + describe '#extract_executable_version' do + it 'should return the version if mjml is installed' do + MJML.extract_executable_version.must_be_instance_of String + end + + it 'should return false for unknown feature' do + bin_path = MJML.config.bin_path + MJML.config.bin_path = '/usr/bin/env mjml-not-installed' + + error = -> { MJML.extract_executable_version }.must_raise MJML::BinaryNotFound + error.message.must_match(/^mjml binary not found/) + + MJML.config.bin_path = bin_path + end + end +end