Skip to content

Commit

Permalink
Collection#glob also takes account of its configured #ext (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
benpickles authored Sep 23, 2024
1 parent b1a7f91 commit 305c10a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 50 deletions.
7 changes: 3 additions & 4 deletions lib/decant/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ def initialize(dir:, ext: nil)
end

def entries
glob("**/*#{ext}")
glob('**/*')
end

def find(path)
pattern = "#{path}#{ext}"
def find(pattern)
glob(pattern).first
end

def glob(pattern)
dir.glob(pattern).select { |path| path.file? }
dir.glob("#{pattern}#{ext}").select { |path| path.file? }
end

def slug_for(path)
Expand Down
118 changes: 72 additions & 46 deletions spec/decant/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,62 +135,88 @@
end

describe '#glob' do
let(:collection) { described_class.new(dir: tmpdir, ext: 'md') }
let(:collection) { described_class.new(dir: tmpdir, ext: ext) }

before do
file('a/b/c/d')
file('a/b/c/d.md')
file('a/b/c/d.txt')
file('a/b/d')
file('a/b/c/de.md')
file('a/b/d.md')
file('a/d.mdn')
file('a/b/d.mdn')
file('a/b/de.md')
file('a/b/d.txt')
file('a/d/')
file('d')
file('d.foo')
file('d.md')
file('de/')
file('d.txt')
file('de.md')
end

it 'returns files ignoring #ext and otherwise behaves like a standard Dir.glob' do
expect(collection.glob('d')).to contain_exactly(
file_path('d'),
)
expect(collection.glob('d.*')).to contain_exactly(
file_path('d.foo'),
file_path('d.md'),
)
expect(collection.glob('d*')).to contain_exactly(
file_path('d'),
file_path('d.foo'),
file_path('d.md'),
)
expect(collection.glob('a/*/d*')).to contain_exactly(
file_path('a/b/d'),
file_path('a/b/d.md'),
)
expect(collection.glob('a/**/d*')).to contain_exactly(
file_path('a/b/c/d'),
file_path('a/b/c/d.md'),
file_path('a/b/c/d.txt'),
file_path('a/b/d'),
file_path('a/b/d.md'),
file_path('a/d.mdn'),
)
expect(collection.glob('**/d.md')).to contain_exactly(
file_path('a/b/c/d.md'),
file_path('a/b/d.md'),
file_path('d.md'),
)
expect(collection.glob('**/d.md*')).to contain_exactly(
file_path('a/b/c/d.md'),
file_path('a/b/d.md'),
file_path('a/d.mdn'),
file_path('d.md'),
)
expect(collection.glob('a/b/**/*.{md,txt}')).to contain_exactly(
file_path('a/b/c/d.md'),
file_path('a/b/c/d.txt'),
file_path('a/b/d.md'),
)
context 'when #ext is defined' do
let(:ext) { '.md' }

it 'combines the supplied pattern with the configured #ext and returns only files' do
expect(collection.glob('d')).to contain_exactly(
file_path('d.md'),
)
expect(collection.glob('d*')).to contain_exactly(
file_path('d.md'),
file_path('de.md'),
)
expect(collection.glob('a/*/d*')).to contain_exactly(
file_path('a/b/d.md'),
file_path('a/b/de.md'),
)
expect(collection.glob('a/**/d*')).to contain_exactly(
file_path('a/b/c/d.md'),
file_path('a/b/c/de.md'),
file_path('a/b/d.md'),
file_path('a/b/de.md'),
)
expect(collection.glob('**/d')).to contain_exactly(
file_path('a/b/c/d.md'),
file_path('a/b/d.md'),
file_path('d.md'),
)
end
end

context 'when #ext is more complicated' do
let(:ext) { '.{md,txt}' }

it 'also works' do
expect(collection.glob('**/d')).to contain_exactly(
file_path('a/b/c/d.md'),
file_path('a/b/c/d.txt'),
file_path('a/b/d.md'),
file_path('a/b/d.txt'),
file_path('d.md'),
file_path('d.txt'),
)
end
end

context 'when #ext is not defined' do
let(:ext) { nil }

it 'requires you to pass your own #ext if wanted' do
expect(collection.glob('d')).to contain_exactly(
file_path('d'),
)
expect(collection.glob('d.md')).to contain_exactly(
file_path('d.md'),
)
expect(collection.glob('**/d')).to contain_exactly(
file_path('a/b/c/d'),
file_path('d'),
)
expect(collection.glob('**/d.md')).to contain_exactly(
file_path('a/b/c/d.md'),
file_path('a/b/d.md'),
file_path('d.md'),
)
end
end
end

Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module FileHelpers
def file(path, content = nil)
full_path = file_path(path)

raise "a file already exists at #{path}" if full_path.exist?

if path.end_with?('/')
FileUtils.mkdir_p(full_path)
else
Expand Down

0 comments on commit 305c10a

Please sign in to comment.