Skip to content

Commit

Permalink
Merge pull request #662 from BallAerospace/exception_dialog
Browse files Browse the repository at this point in the history
Fix exception dialog and partial pathing
  • Loading branch information
jmthomas authored Nov 20, 2017
2 parents 74cb36f + 94e2c6e commit 9be4e86
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
11 changes: 8 additions & 3 deletions lib/cosmos/config/config_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def error(message, usage = "", url = @url)

# Called by the ERB template to render a partial
def render(template_name, options = {})
raise Error.new(self, "Partial name '#{template_name}' must begin with an underscore.") if template_name[0] != '_'
raise Error.new(self, "Partial name '#{template_name}' must begin with an underscore.") if File.basename(template_name)[0] != '_'
b = binding
if options[:locals]
if RUBY_VERSION.split('.')[0..1].join.to_i >= 21
Expand All @@ -162,7 +162,12 @@ def render(template_name, options = {})
end
end
# Assume the file is there. If not we raise a pretty obvious error
ERB.new(File.read(File.join(File.dirname(@filename), template_name))).result(b)
if File.expand_path(template_name) == template_name # absolute path
path = template_name
else # relative to the current @filename
path = File.join(File.dirname(@filename), template_name)
end
ERB.new(File.read(path)).result(b)
end

# Processes a file and yields |config| to the given block
Expand All @@ -179,7 +184,7 @@ def parse_file(filename,
yield_non_keyword_lines = false,
remove_quotes = true,
&block)
raise "Configuration file #{filename} does not exist." unless filename && File.exist?(filename)
raise Error.new(self, "Configuration file #{filename} does not exist.") unless filename && File.exist?(filename)
@filename = filename

# Create a temp file where we write the ERB parsed output
Expand Down
25 changes: 20 additions & 5 deletions lib/cosmos/gui/dialogs/exception_dialog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,26 @@ def initialize(parent, exception, title = 'COSMOS Exception', exit_afterwards =
# by the ConfigParser
when ConfigParser::Error
# Substitute the html tags '<' and '>' and then replace newlines with html breaks
usage = exception.usage.gsub("<", "&#060;").gsub(">", "&#062;").gsub("\n", "<br/>")
message = exception.message.gsub("<", "&#060;").gsub(">", "&#062;").gsub("\n", "<br/>")
line = exception.keyword + ' ' + exception.parameters.join(' ').gsub("<", "&#060;").gsub(">", "&#062;").gsub("\n", "<br/>")
text = "Error at #{exception.filename}:#{exception.line_number}<br/><br/>#{line}<br/><br/>Usage: #{usage}<br/><br/>#{message}"
unless exception.url.nil?
if exception.usage && !exception.usage.empty?
usage = "Usage: #{exception.usage.gsub("<", "&#060;").gsub(">", "&#062;").gsub("\n", "<br/>")}<br/><br/>"
else
usage = ''
end
if exception.message && !exception.message.empty?
message = exception.message.gsub("<", "&#060;").gsub(">", "&#062;").gsub("\n", "<br/>")
else
message = ''
end
if exception.keyword && exception.parameters
line = exception.keyword + ' ' + exception.parameters.join(' ').gsub("<", "&#060;").gsub(">", "&#062;").gsub("\n", "<br/>") + "<br/><br/>"
else
line = ''
end
text = "#{line}#{usage}#{message}"
if exception.filename && exception.line_number
text = "Error at #{exception.filename}:#{exception.line_number}<br/><br/>#{text}"
end
if exception.url && !exception.url.empty?
text << "<br/><br/>For more information see <a href='#{exception.url}'>#{exception.url}</a>."
end
# FatalErrors are errors explicitly raised when a known fatal issue
Expand Down
41 changes: 39 additions & 2 deletions spec/config/config_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require 'cosmos'
require 'cosmos/config/config_parser'
require 'tempfile'
require 'tmpdir'

module Cosmos

Expand Down Expand Up @@ -76,10 +77,46 @@ module Cosmos
tf.unlink
end

it "allows ERB partials in subdirectories" do
Dir.mktmpdir("partial_dir") do |dir|
tf2 = Tempfile.new('_partial.txt', dir)
tf2.puts "SUBDIR"
tf2.close
tf = Tempfile.new('unittest')
# Grab the sub directory name plus filename
subdir_path = tf2.path().split('/')[-2..-1].join('/')
tf.puts "<%= render '#{subdir_path}' %>"
tf.close

@cp.parse_file(tf.path) do |keyword, params|
expect(keyword).to eql "SUBDIR"
end
tf.unlink
tf2.unlink
end
end

it "allows absolute paths to ERB partials" do
Dir.mktmpdir("partial_dir") do |dir|
tf2 = Tempfile.new('_partial.txt', dir)
tf2.puts "ABSOLUTE"
tf2.close
tf = Tempfile.new('unittest')
tf.puts "<%= render '#{tf2.path}' %>"
tf.close

@cp.parse_file(tf.path) do |keyword, params|
expect(keyword).to eql "ABSOLUTE"
end
tf.unlink
tf2.unlink
end
end

it "supports ERB partials via render" do
tf2 = Tempfile.new('_partial.txt')
tf2.puts '<% if output %>'
tf2.puts 'KEYWORD <%= id %> <%= desc %>'
tf2.puts 'RENDER <%= id %> <%= desc %>'
tf2.puts '<% end %>'
tf2.close

Expand All @@ -92,7 +129,7 @@ module Cosmos
yielded = false
@cp.parse_file(tf.path) do |keyword, params|
yielded = true
expect(keyword).to eql "KEYWORD"
expect(keyword).to eql "RENDER"
expect(params[0]).to eql "1"
expect(params[1]).to eql "Description"
end
Expand Down

0 comments on commit 9be4e86

Please sign in to comment.