Skip to content

Commit

Permalink
Accept the object that has #to_path in Logger::LogDevice.new
Browse files Browse the repository at this point in the history
Because the `path` keyword in File.new will only accept objects that have `to_str`.
  • Loading branch information
Watson1978 committed Jan 11, 2025
1 parent 113b82a commit 08f9fb3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/logger/log_device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def fixup_mode(dev, filename)
else
def fixup_mode(dev, filename)
return dev if @binmode
filename = filename.respond_to?(:to_path) ? filename.to_path : filename
dev.autoclose = false
old_dev = dev
dev = File.new(dev.fileno, mode: MODE, path: filename)
Expand Down
15 changes: 15 additions & 0 deletions test/logger/test_logdevice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'logger'
require 'tempfile'
require 'tmpdir'
require 'pathname'

class TestLogDevice < Test::Unit::TestCase
class LogExcnRaiser
Expand Down Expand Up @@ -79,6 +80,20 @@ def test_initialize
File.unlink(tempfile)
tempfile.close(true)
end
# logfile object with Pathname object
tempfile = Tempfile.new("logger")
pathname = Pathname.new(tempfile.path)
logdev = d(pathname)
begin
logdev.write('world')
logfile = File.read(pathname)
assert_equal(1, logfile.split(/\n/).size)
assert_match(/^world$/, logfile)
assert_equal(pathname, logdev.filename)
ensure
logdev.close
tempfile.close(true)
end
end

def test_write
Expand Down

0 comments on commit 08f9fb3

Please sign in to comment.