Skip to content

Commit

Permalink
Resolve symlinks in static data source
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Apr 7, 2013
1 parent e5d30ef commit 4a55c0c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/nanoc/data_sources/static.rb
Expand Up @@ -26,24 +26,21 @@ module Nanoc::DataSources
# exclude them from the Blogging helper's atom feed generator, among other
# things.
class Static < Nanoc::DataSource

identifier :static

def items
# Get prefix
prefix = config[:prefix] || 'static'

# Get all files under prefix dir
filenames = Dir[prefix + '/**/*'].select { |f| File.file?(f) }

# Convert filenames to items
filenames.map do |filename|
self.all_files_in(prefix).map do |filename|
attributes = {
:extension => File.extname(filename)[1..-1],
:filename => filename,
}
attributes[:is_hidden] = true unless config[:hide_items] == false
identifier = filename[(prefix.length+1)..-1] + '/'

mtime = File.mtime(filename)
checksum = Pathname.new(filename).checksum

Expand All @@ -56,5 +53,12 @@ def items
end
end

protected

def all_files_in(dir_name)
Nanoc::Extra::FilesystemTools.all_files_in(dir_name)
end

end

end
28 changes: 28 additions & 0 deletions test/data_sources/test_static.rb
Expand Up @@ -13,6 +13,34 @@ def new_data_source(params=nil)
data_source
end

def test_items_with_symlinks
# Create data source
data_source = new_data_source(:prefix => 'foo')

# Create sample files
FileUtils.mkdir_p('foo')
FileUtils.mkdir_p('foo-outside-1')
FileUtils.mkdir_p('foo-outside-2')
File.open('foo/a.png', 'w') { |io| io.write("random binary data") }
File.open('foo-outside-1/b.png', 'w') { |io| io.write("more binary data") }
File.open('foo-outside-2/c.png', 'w') { |io| io.write("yet more binary data") }

# Create symlinks
File.symlink('../foo-outside-1', 'foo/1')
File.symlink('../foo-outside-2/c.png', 'foo/c.png')

# Check all files
expected_filenames = [ 'foo/a.png', 'foo/1/b.png', 'foo/c.png' ].sort
actual_filenames = Nanoc::Extra::FilesystemTools.all_files_in('foo').sort
assert_equal expected_filenames, actual_filenames

# Check items
items = data_source.send(:items).sort_by { |i| i.identifier }
actual_item_identifiers = items.map { |i| i.identifier }.sort
expected_item_identifiers = %w( /a.png/ /1/b.png/ /c.png/ ).sort
assert_equal expected_item_identifiers, actual_item_identifiers
end

def test_items
# Create data source
data_source = new_data_source(:prefix => 'foo')
Expand Down

0 comments on commit 4a55c0c

Please sign in to comment.