From 37c2da5062b6f64ce3e6bf372aa8b28e6bed7ff3 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Sat, 1 Mar 2014 15:31:48 -0500 Subject: [PATCH] Properly clean path for Windows machines which is *nix-compliant. --- lib/jekyll.rb | 17 +++++++---------- test/test_path_sanitization.rb | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 10 deletions(-) create mode 100644 test/test_path_sanitization.rb diff --git a/lib/jekyll.rb b/lib/jekyll.rb index 7b661048..6c6fafa4 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -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 diff --git a/test/test_path_sanitization.rb b/test/test_path_sanitization.rb new file mode 100644 index 00000000..8d753808 --- /dev/null +++ b/test/test_path_sanitization.rb @@ -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