[ruby/fileutils] Do not break in verbose mode if using FileUtils with a frozen object

If FileUtils is included into another object, and verbose mode is
used, a FrozenError is currently raised unless the object has the
@fileutils_output and @fileutils_label instance variables.

This fixes things so that it does not attempt to set the instance
variables, but it still uses them if they are present.

https://github.com/ruby/fileutils/commit/689cb9c56a
This commit is contained in:
Jeremy Evans 2019-07-31 12:57:21 -07:00 committed by Hiroshi SHIBATA
parent 02cd420505
commit 9494ef8b2d
No known key found for this signature in database
GPG Key ID: F9CF13417264FAC2
2 changed files with 30 additions and 12 deletions

View File

@ -1608,13 +1608,13 @@ module FileUtils
end
private_module_function :fu_same?
@fileutils_output = $stderr
@fileutils_label = ''
def fu_output_message(msg) #:nodoc:
@fileutils_output ||= $stderr
@fileutils_label ||= ''
@fileutils_output.puts @fileutils_label + msg
output = @fileutils_output if defined?(@fileutils_output)
output ||= $stderr
if defined?(@fileutils_label)
msg = @fileutils_label + msg
end
output.puts msg
end
private_module_function :fu_output_message
@ -1695,8 +1695,6 @@ module FileUtils
#
module Verbose
include FileUtils
@fileutils_output = $stderr
@fileutils_label = ''
names = ::FileUtils.collect_method(:verbose)
names.each do |name|
module_eval(<<-EOS, __FILE__, __LINE__ + 1)
@ -1720,8 +1718,6 @@ module FileUtils
module NoWrite
include FileUtils
include LowMethods
@fileutils_output = $stderr
@fileutils_label = ''
names = ::FileUtils.collect_method(:noop)
names.each do |name|
module_eval(<<-EOS, __FILE__, __LINE__ + 1)
@ -1746,8 +1742,6 @@ module FileUtils
module DryRun
include FileUtils
include LowMethods
@fileutils_output = $stderr
@fileutils_label = ''
names = ::FileUtils.collect_method(:noop)
names.each do |name|
module_eval(<<-EOS, __FILE__, __LINE__ + 1)

View File

@ -6,6 +6,7 @@ require 'etc'
require_relative 'fileasserts'
require 'pathname'
require 'tmpdir'
require 'stringio'
require 'test/unit'
class TestFileUtils < Test::Unit::TestCase
@ -1673,6 +1674,29 @@ class TestFileUtils < Test::Unit::TestCase
check_singleton :chdir
end
def test_chdir_verbose
assert_output_lines(["cd .", "cd -"], FileUtils) do
FileUtils.chdir('.', verbose: true){}
end
end
def test_chdir_verbose_frozen
o = Object.new
o.extend(FileUtils)
o.singleton_class.send(:public, :chdir)
o.freeze
orig_stderr = $stderr
$stderr = StringIO.new
o.chdir('.', verbose: true){}
$stderr.rewind
assert_equal(<<-END, $stderr.read)
cd .
cd -
END
ensure
$stderr = orig_stderr if orig_stderr
end
def test_getwd
check_singleton :getwd
end