Make sure Cleaner doesn't remove dirs if they only contain subdirs

Closes #2204
This commit is contained in:
Renaud Martinet 2014-05-27 18:06:52 +02:00
parent a68fb9d0b6
commit 04baeefaa8
2 changed files with 51 additions and 1 deletions

View File

@ -49,7 +49,19 @@ module Jekyll
#
# Returns a Set with the directory paths
def new_dirs
new_files.map { |file| File.dirname(file) }.to_set
new_files.map { |file| parent_dirs(file) }.flatten.to_set
end
# Private: The list of parent directories of a given file
#
# Returns an Array with the directory paths
def parent_dirs(file)
parent_dir = File.dirname(file)
if parent_dir == site.dest
return []
else
return [parent_dir] + parent_dirs(parent_dir)
end
end
# Private: The list of existing files that will be replaced by a directory during build

38
test/test_cleaner.rb Normal file
View File

@ -0,0 +1,38 @@
require 'helper'
class TestCleaner < Test::Unit::TestCase
context "directory containing no files and non-empty directories" do
setup do
clear_dest
stub(Jekyll).configuration do
Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir})
end
FileUtils.mkdir_p(source_dir('no_files_inside/child_dir'))
FileUtils.touch(File.join(source_dir('no_files_inside/child_dir'), 'index.html'))
@site = Site.new(Jekyll.configuration)
@site.process
@cleaner = Site::Cleaner.new(@site)
@cleaner.cleanup!
end
teardown do
FileUtils.rm_rf(source_dir('no_files_inside'))
FileUtils.rm_rf(dest_dir('no_files_inside'))
end
should "keep the parent directory" do
assert File.exist?(dest_dir('no_files_inside'))
end
should "keep the child directory" do
assert File.exist?(dest_dir('no_files_inside/child_dir'))
end
should "keep the file" do
assert File.exist?(File.join(dest_dir('no_files_inside/child_dir'), 'index.html'))
end
end
end