Skip to content

Commit

Permalink
made filesystem_* data sources handle periods in identifiers
Browse files Browse the repository at this point in the history
--HG--
branch : 3.0.x
  • Loading branch information
denisdefreyne committed Dec 22, 2009
1 parent bca7bb9 commit 9a31855
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 7 deletions.
8 changes: 4 additions & 4 deletions lib/nanoc3/data_sources/filesystem_combined.rb
Expand Up @@ -90,9 +90,9 @@ def items

# Get actual identifier
if filename =~ /\/index\.[^\/]+$/
identifier = filename.sub(/^content/, '').sub(/index\.[^\/]+$/, '') + '/'
identifier = filename.sub(/^content/, '').sub(/index\.[^\/\.]+$/, '') + '/'
else
identifier = filename.sub(/^content/, '').sub(/\.[^\/]+$/, '') + '/'
identifier = filename.sub(/^content/, '').sub(/\.[^\/\.]+$/, '') + '/'
end

# Get mtime
Expand All @@ -110,9 +110,9 @@ def layouts

# Get actual identifier
if filename =~ /\/index\.[^\/]+$/
identifier = filename.sub(/^layouts/, '').sub(/index\.[^\/]+$/, '') + '/'
identifier = filename.sub(/^layouts/, '').sub(/index\.[^\/\.]+$/, '') + '/'
else
identifier = filename.sub(/^layouts/, '').sub(/\.[^\/]+$/, '') + '/'
identifier = filename.sub(/^layouts/, '').sub(/\.[^\/\.]+$/, '') + '/'
end

# Get mtime
Expand Down
4 changes: 3 additions & 1 deletion lib/nanoc3/data_sources/filesystem_compact.rb
Expand Up @@ -208,7 +208,9 @@ def meta_filenames(base)
# '.rej' or '.bak')
def content_filename_for_meta_filename(meta_filename)
# Find all files
filenames = Dir[meta_filename.sub(/\.yaml$/, '.*')]
base_filename = File.basename(meta_filename, '.yaml')
dirname = File.dirname(meta_filename)
filenames = Dir.entries(dirname).select { |f| f =~ /#{base_filename}\.[^.]+$/ }.map { |f| "#{dirname}/#{f}" }

# Reject meta files
filenames.reject! { |f| f =~ /\.yaml$/ }
Expand Down
60 changes: 58 additions & 2 deletions test/data_sources/test_filesystem.rb
Expand Up @@ -72,6 +72,38 @@ def test_items
assert(items.any? { |a| a[:title] == 'Bar' })
end

def test_items_with_period_in_name
data_source = Nanoc3::DataSources::Filesystem.new(nil, nil, nil, nil)

# Create foo.css
FileUtils.mkdir_p('content/foo')
File.open('content/foo/foo.yaml', 'w') do |io|
io.write(YAML.dump({ 'title' => 'Foo' }))
end
File.open('content/foo/foo.css', 'w') do |io|
io.write('body.foo {}')
end

# Create foo.bar.css
FileUtils.mkdir_p('content/foo.bar')
File.open('content/foo.bar/foo.bar.yaml', 'w') do |io|
io.write(YAML.dump({ 'title' => 'Foo Bar' }))
end
File.open('content/foo.bar/foo.bar.css', 'w') do |io|
io.write('body.foobar {}')
end

# Load
items = data_source.items

# Check
assert_equal 2, items.size
assert_equal '/foo/', items[0].identifier
assert_equal 'Foo', items[0][:title]
assert_equal '/foo.bar/', items[1].identifier
assert_equal 'Foo Bar', items[1][:title]
end

def test_layouts
# Create data source
data_source = Nanoc3::DataSources::Filesystem.new(nil, nil, nil, nil)
Expand Down Expand Up @@ -143,8 +175,32 @@ def test_create_layout

# Test private methods

def test_meta_filenames
# TODO implement
def test_meta_filenames_good
# Create data sources
data_source = Nanoc3::DataSources::Filesystem.new(nil, nil, nil, nil)

# Create files
FileUtils.mkdir_p('foo')
File.open('foo/foo.yaml', 'w') { |io| io.write('foo') }
FileUtils.mkdir_p('foo.bar')
File.open('foo.bar/foo.bar.yaml', 'w') { |io| io.write('foo') }

# Check
assert_equal %w( ./foo/foo.yaml ./foo.bar/foo.bar.yaml ), data_source.send(:meta_filenames, '.')
end

def test_meta_filenames_bad
# Create data sources
data_source = Nanoc3::DataSources::Filesystem.new(nil, nil, nil, nil)

# Create files
FileUtils.mkdir_p('foo')
File.open('foo/dsafsdf.yaml', 'w') { |io| io.write('dagasfwegfwa') }

# Check
assert_raises(RuntimeError) do
data_source.send(:meta_filenames, '.')
end
end

def test_content_filename_for_dir_with_one_content_file
Expand Down
26 changes: 26 additions & 0 deletions test/data_sources/test_filesystem_combined.rb
Expand Up @@ -69,6 +69,32 @@ def test_items
assert(items.any? { |a| a[:title] == 'Bar' })
end

def test_items_with_period_in_name
data_source = Nanoc3::DataSources::FilesystemCombined.new(nil, nil, nil, nil)

FileUtils.mkdir_p('content/foo')

# Create bar.css
File.open('content/foo/bar.css', 'w') do |io|
io.write(YAML.dump({ 'title' => 'Foo' }) + "---\n" + 'body.foo {}')
end

# Create bar.baz.css
File.open('content/foo/bar.baz.css', 'w') do |io|
io.write(YAML.dump({ 'title' => 'Foo Bar' }) + "---\n" + 'body.foobar {}')
end

# Load
items = data_source.items.sort_by { |i| i[:title] }

# Check
assert_equal 2, items.size
assert_equal '/foo/bar/', items[0].identifier
assert_equal 'Foo', items[0][:title]
assert_equal '/foo/bar.baz/', items[1].identifier
assert_equal 'Foo Bar', items[1][:title]
end

def test_layouts
# Create data source
data_source = Nanoc3::DataSources::FilesystemCombined.new(nil, nil, nil, nil)
Expand Down
32 changes: 32 additions & 0 deletions test/data_sources/test_filesystem_compact.rb
Expand Up @@ -81,6 +81,38 @@ def test_items_with_non_index_names
assert_equal 'Foo', items[0][:title]
end

def test_items_with_period_in_name
data_source = Nanoc3::DataSources::FilesystemCompact.new(nil, nil, nil, nil)

FileUtils.mkdir_p('content/foo')

# Create bar.css
File.open('content/foo/bar.yaml', 'w') do |io|
io.write(YAML.dump({ 'title' => 'Foo' }))
end
File.open('content/foo/bar.css', 'w') do |io|
io.write('body{}')
end

# Create bar.baz.css
File.open('content/foo/bar.baz.yaml', 'w') do |io|
io.write(YAML.dump({ 'title' => 'Foo2' }))
end
File.open('content/foo/bar.baz.css', 'w') do |io|
io.write('body{}')
end

# Load
items = data_source.items.sort_by { |i| i[:title] }

# Check
assert_equal 2, items.size
assert_equal '/foo/bar/', items[0].identifier
assert_equal 'Foo', items[0][:title]
assert_equal '/foo/bar.baz/', items[1].identifier
assert_equal 'Foo2', items[1][:title]
end

def test_items_with_both_index_and_non_index_names
# Create data source
data_source = Nanoc3::DataSources::FilesystemCompact.new(nil, nil, nil, nil)
Expand Down

0 comments on commit 9a31855

Please sign in to comment.