1
0
mirror of https://github.com/meineerde/rackstash.git synced 2025-10-17 14:01:01 +00:00

Handle plain absolute Windows paths gracefully for adapter creation

A absolute Windows path such as `C:/path.to/file.log` is a valid URL
with schema `C`. If a user specifies such a path, they likely want to
create a File adapter in this case. As such, we fallback from
`Adapter.adapter_by_uri_scheme` and create File adapter instance via
`Adapter.adapter_by_type`.
This commit is contained in:
Holger Just 2020-07-17 15:42:42 +02:00
parent 65bf5ae252
commit 7e54d70270
3 changed files with 21 additions and 3 deletions

View File

@ -112,6 +112,8 @@ module Rackstash
return unless uri.scheme
adapter_class = adapter_schemes.fetch(uri.scheme) {
# `uri` might look like a windows path, e.g. C:/path/to/file.log
return if ('a'..'z').include?(uri.scheme.downcase)
raise ArgumentError, "No log adapter found for URI #{uri}"
}

View File

@ -118,7 +118,12 @@ module Rackstash
# @param rotate (see #rotate=)
# @param lock (see #lock=)
def initialize(path, auto_reopen: true, rotate: nil, lock: false)
@base_path = ::File.expand_path(path).freeze
if path.is_a?(String) && path =~ %r{\A/?[a-z]:}i
path = path[1..-1] if path.start_with?('/')
@base_path = path.dup.freeze
else
@base_path = ::File.expand_path(path).freeze
end
self.auto_reopen = auto_reopen
self.rotate = rotate

View File

@ -35,8 +35,19 @@ RSpec.describe Rackstash::Adapter::File do
it 'sets the base_path from the URI path' do
expect(described_class.from_uri("file:#{logfile.path}").base_path)
.to eql logfile.path
expect(described_class.from_uri("file://#{logfile.path}").base_path)
.to eql logfile.path
if Gem.win_platform?
expect(logfile.path).to match /\A[a-z]:/i
# For an absolute Windows path to be correctly identified in a URI, we
# need to add a third slash (since the drive letter doesn't have one on
# its own)
expect(described_class.from_uri("file:///#{logfile.path}").base_path)
.to eql logfile.path
else
expect(described_class.from_uri("file://#{logfile.path}").base_path)
.to eql logfile.path
end
end
it 'sets optional attributes' do