Skip to content

Commit 71eb415

Browse files
committed
Use BFS to detect ro-crate-metadata.json. Fixes #32
1 parent 21ae238 commit 71eb415

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

lib/ro_crate/reader.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,16 @@ def self.extract_root_entity(entities)
306306
# @param source [String, ::File, Pathname] The location of the directory.
307307
# @return [Pathname, nil] The path to the root, or nil if not found.
308308
def self.detect_root_directory(source)
309-
Pathname(source).find do |entry|
309+
queue = [source]
310+
until queue.empty?
311+
entry = Pathname(queue.shift)
310312
if entry.file?
311313
name = entry.basename.to_s
312314
if name == ROCrate::Metadata::IDENTIFIER || name == ROCrate::Metadata::IDENTIFIER_1_0
313315
return entry.parent
314316
end
317+
elsif entry.directory?
318+
queue += entry.children
315319
end
316320
end
317321

903 Bytes
Binary file not shown.

test/reader_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,12 @@ class ReaderTest < Test::Unit::TestCase
367367
assert_empty crate.data_entities
368368
end
369369

370+
test 'reads first metadata file it encounters' do
371+
crate = ROCrate::Reader.read(fixture_file('multi_metadata_crate.crate.zip').path)
372+
373+
assert_equal 'At the root', crate.name
374+
end
375+
370376
private
371377

372378
def check_exception(exception_class)

0 commit comments

Comments
 (0)