Skip to content

Commit

Permalink
Improve handling of file: scheme considering windows and Unix style p…
Browse files Browse the repository at this point in the history
…aths.
  • Loading branch information
gkellogg committed Oct 8, 2024
1 parent 9f7b6a7 commit 586b5b8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
10 changes: 9 additions & 1 deletion lib/rdf/tabular/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,15 @@ def self.open(path, **options)
'Accept' => 'application/ld+json, application/json'
}
)
path = "file:#{path}" if RDF::URI(path).relative?
if RDF::URI(path).relative?
path = File.expand_path(path, __FILE__)
if Gem.win_platform?
# Expanded path starts with drive letter
"file:/" + File.expand_path(path, __FILE__)
else
path = "file:#{File.expand_path(path, __FILE__)}"
end
end
RDF::Util::File.open_file(path, **options) do |file|
self.new(file, **options.merge(base: path, filenames: path))
end
Expand Down
7 changes: 6 additions & 1 deletion lib/rdf/tabular/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ def initialize(input = $stdin, **options, &block)
@options[:base] ||= input.path if input.respond_to?(:path)
@options[:base] ||= input.filename if input.respond_to?(:filename)
if RDF::URI(@options[:base]).relative? && File.exist?(@options[:base].to_s)
file_uri = "file:" + File.expand_path(@options[:base])
file_uri = if Gem.win_platform?
# Expanded path starts with drive letter
"file:/" + File.expand_path(@options[:base], __FILE__)
else
"file:" + File.expand_path(@options[:base], __FILE__)
end
@options[:base] = RDF::URI(file_uri.to_s).normalize
end

Expand Down
4 changes: 2 additions & 2 deletions spec/metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@
context filename do
subject {RDF::Tabular::Metadata.open(filename, logger: logger)}
it {is_expected.to be_valid}
its(:filenames) {is_expected.to include("file:#{filename}")}
its(:filenames) {is_expected.to include("file:/#{filename.sub(/^\//, '')}")}
end
after(:each) do
expect(logger.to_s).not_to match(/ERROR|WARN/)
Expand Down Expand Up @@ -818,7 +818,7 @@
subject.validate
expect(logger.to_s).to be_empty
end
its(:filenames) {is_expected.to include("file:#{filename}")}
its(:filenames) {is_expected.to include("file:/#{filename.sub(/^\//, '')}")}
end
end
end
Expand Down

0 comments on commit 586b5b8

Please sign in to comment.