Properly clean path for Windows machines which is *nix-compliant.

This commit is contained in:
Parker Moore 2014-03-01 15:31:48 -05:00
parent 48d5f79d9a
commit 37c2da5062
2 changed files with 21 additions and 10 deletions

View File

@ -106,19 +106,16 @@ module Jekyll
#
# Returns the root of the filesystem as a Pathname
def self.fs_root
@fs_root ||= traverse_up(Pathname.new(Dir.pwd))
@fs_root ||= "/"
end
def self.sanitized_path(base_directory, questionable_path)
clean_path = File.expand_path(questionable_path, fs_root)
clean_path.sub(/\A[\w]:\\\\/, '')
File.join(base_directory, clean_path)
end
private
def self.traverse_up(pathname)
return pathname if pathname.parent.eql?(pathname)
traverse_up(pathname.parent)
clean_path.gsub!(/\/\w\:\//, '/')
unless clean_path.start_with?(base_directory)
File.join(base_directory, clean_path)
else
clean_path
end
end
end

View File

@ -0,0 +1,14 @@
require 'helper'
class TestPathSanitization < Test::Unit::TestCase
context "on Windows with absolute source" do
setup do
@source = "C:/Users/xmr/Desktop/mpc-hc.org"
@dest = "./_site/"
stub(Dir).pwd { "C:/Users/xmr/Desktop/mpc-hc.org" }
end
should "strip drive name from path" do
assert_equal "C:/Users/xmr/Desktop/mpc-hc.org/_site", Jekyll.sanitized_path(@source, @dest)
end
end
end