From 063c5f47056efa0fbcc4a8ea43288b578d78074f Mon Sep 17 00:00:00 2001 From: Jason Thomas Date: Mon, 20 Nov 2017 10:26:16 -0700 Subject: [PATCH 1/2] Fix exception dialog and partial pathing --- lib/cosmos/config/config_parser.rb | 11 ++++-- lib/cosmos/gui/dialogs/exception_dialog.rb | 25 +++++++++++--- spec/config/config_parser_spec.rb | 40 ++++++++++++++++++++-- 3 files changed, 66 insertions(+), 10 deletions(-) diff --git a/lib/cosmos/config/config_parser.rb b/lib/cosmos/config/config_parser.rb index fbd34e77c..c823a773d 100644 --- a/lib/cosmos/config/config_parser.rb +++ b/lib/cosmos/config/config_parser.rb @@ -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 @@ -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 @@ -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 diff --git a/lib/cosmos/gui/dialogs/exception_dialog.rb b/lib/cosmos/gui/dialogs/exception_dialog.rb index 49b4fc0ec..974ccbd01 100644 --- a/lib/cosmos/gui/dialogs/exception_dialog.rb +++ b/lib/cosmos/gui/dialogs/exception_dialog.rb @@ -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("<", "<").gsub(">", ">").gsub("\n", "
") - message = exception.message.gsub("<", "<").gsub(">", ">").gsub("\n", "
") - line = exception.keyword + ' ' + exception.parameters.join(' ').gsub("<", "<").gsub(">", ">").gsub("\n", "
") - text = "Error at #{exception.filename}:#{exception.line_number}

#{line}

Usage: #{usage}

#{message}" - unless exception.url.nil? + if exception.usage && !exception.usage.empty? + usage = "Usage: #{exception.usage.gsub("<", "<").gsub(">", ">").gsub("\n", "
")}

" + else + usage = '' + end + if exception.message && !exception.message.empty? + message = exception.message.gsub("<", "<").gsub(">", ">").gsub("\n", "
") + else + message = '' + end + if exception.keyword && exception.parameters + line = exception.keyword + ' ' + exception.parameters.join(' ').gsub("<", "<").gsub(">", ">").gsub("\n", "
") + "

" + else + line = '' + end + text = "#{line}#{usage}#{message}" + if exception.filename && exception.line_number + text = "Error at #{exception.filename}:#{exception.line_number}

#{text}" + end + if exception.url && !exception.url.empty? text << "

For more information see #{exception.url}." end # FatalErrors are errors explicitly raised when a known fatal issue diff --git a/spec/config/config_parser_spec.rb b/spec/config/config_parser_spec.rb index 6d9e5733c..c9c474a23 100644 --- a/spec/config/config_parser_spec.rb +++ b/spec/config/config_parser_spec.rb @@ -12,6 +12,7 @@ require 'cosmos' require 'cosmos/config/config_parser' require 'tempfile' +require 'tmpdir' module Cosmos @@ -76,10 +77,45 @@ 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') + tf.puts "<%= render '#{tf2.path.gsub(Dir.tmpdir, '')}' %>" + tf.puts "<%= render '#{tf2.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 @@ -92,7 +128,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 From 94e2c6e5a4168b9aa296686b10b5e864ceca878e Mon Sep 17 00:00:00 2001 From: Jason Thomas Date: Mon, 20 Nov 2017 13:18:31 -0700 Subject: [PATCH 2/2] Change how spec path is calculated --- spec/config/config_parser_spec.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/spec/config/config_parser_spec.rb b/spec/config/config_parser_spec.rb index c9c474a23..2eff1af3d 100644 --- a/spec/config/config_parser_spec.rb +++ b/spec/config/config_parser_spec.rb @@ -83,8 +83,9 @@ module Cosmos tf2.puts "SUBDIR" tf2.close tf = Tempfile.new('unittest') - tf.puts "<%= render '#{tf2.path.gsub(Dir.tmpdir, '')}' %>" - tf.puts "<%= render '#{tf2.path}' %>" + # 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|