From 88ef34ac4cb34b3d6a79de4bbe1446e4afc2fd8c Mon Sep 17 00:00:00 2001 From: EhyehAsherEhyeh Date: Sun, 10 Mar 2013 19:57:50 +0100 Subject: [PATCH] Fix for Issue 16 "Wrong::Config.read_here_or_higher can't handle absolute paths". Checks whether path is absolute or relative simply by calling File.file? on the path. --- lib/wrong/config.rb | 11 +++++++++-- test/chunk_test.rb | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/wrong/config.rb b/lib/wrong/config.rb index 9ac0058..13244b3 100644 --- a/lib/wrong/config.rb +++ b/lib/wrong/config.rb @@ -1,4 +1,3 @@ - module Wrong def self.load_config @@ -25,7 +24,7 @@ class ConfigError < RuntimeError end def self.read_here_or_higher(file, dir = ".") - File.read "#{dir}/#{file}" + File.read get_file_path(file, dir) rescue Errno::ENOENT, Errno::EACCES => e # we may be in a chdir underneath where the file is, so move up one level and try again parent = "#{dir}/..".gsub(/^(\.\/)*/, '') @@ -35,6 +34,14 @@ def self.read_here_or_higher(file, dir = ".") read_here_or_higher(file, parent) end + def self.get_file_path(file, dir) + if File.file?(file) + file + else + "#{dir}/#{file}" + end + end + def initialize(string = nil) self[:aliases] = {:assert => [:assert], :deny => [:deny]} if string diff --git a/test/chunk_test.rb b/test/chunk_test.rb index 643b87d..deb92ca 100644 --- a/test/chunk_test.rb +++ b/test/chunk_test.rb @@ -19,6 +19,20 @@ end end + describe "#read_source_file" do + it "handles absolute file paths" do + chunk = Wrong::Chunk.new(nil, 0) + f = File.new "abs_file_path_test_file.tmp", "w" + abs_path = File.absolute_path(f.path) + assert(chunk.read_source_file(abs_path).is_a? String) + end + it "handles relative file paths" do + chunk = Wrong::Chunk.new(nil, 0) + rel_path = './chunk_test.rb' + assert(chunk.read_source_file(rel_path).is_a? String) + end + end + describe "line numbers" do before do @chunk = Wrong::Chunk.new("foo.rb", 10)