Skip to content

Commit

Permalink
Merge pull request #36 from ResearchObject/fix-multi-metadata-bug
Browse files Browse the repository at this point in the history
Use BFS to detect `ro-crate-metadata.json`
  • Loading branch information
fbacall authored Sep 13, 2024
2 parents 34ef086 + 71eb415 commit 58b6585
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/ro_crate/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Binary file added test/fixtures/multi_metadata_crate.crate.zip
Binary file not shown.
6 changes: 6 additions & 0 deletions test/reader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 58b6585

Please sign in to comment.