diff --git a/lib/ro_crate/reader.rb b/lib/ro_crate/reader.rb index a26e682..0eb5ba0 100644 --- a/lib/ro_crate/reader.rb +++ b/lib/ro_crate/reader.rb @@ -306,12 +306,16 @@ def self.extract_root_entity(entities) # @param source [String, ::File, Pathname] The location of the directory. # @return [Pathname, nil] The path to the root, or nil if not found. def self.detect_root_directory(source) - Pathname(source).find do |entry| + queue = [source] + until queue.empty? + entry = Pathname(queue.shift) if entry.file? name = entry.basename.to_s if name == ROCrate::Metadata::IDENTIFIER || name == ROCrate::Metadata::IDENTIFIER_1_0 return entry.parent end + elsif entry.directory? + queue += entry.children end end diff --git a/test/fixtures/multi_metadata_crate.crate.zip b/test/fixtures/multi_metadata_crate.crate.zip new file mode 100644 index 0000000..c2084e2 Binary files /dev/null and b/test/fixtures/multi_metadata_crate.crate.zip differ diff --git a/test/reader_test.rb b/test/reader_test.rb index 9ef0ac1..b3753c4 100644 --- a/test/reader_test.rb +++ b/test/reader_test.rb @@ -367,6 +367,12 @@ class ReaderTest < Test::Unit::TestCase assert_empty crate.data_entities end + test 'reads first metadata file it encounters' do + crate = ROCrate::Reader.read(fixture_file('multi_metadata_crate.crate.zip').path) + + assert_equal 'At the root', crate.name + end + private def check_exception(exception_class)