Skip to content

Commit 689cb9c

Browse files
committed
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.
1 parent 106ddaa commit 689cb9c

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

lib/fileutils.rb

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,13 +1602,13 @@ def fu_same?(a, b) #:nodoc:
16021602
end
16031603
private_module_function :fu_same?
16041604

1605-
@fileutils_output = $stderr
1606-
@fileutils_label = ''
1607-
16081605
def fu_output_message(msg) #:nodoc:
1609-
@fileutils_output ||= $stderr
1610-
@fileutils_label ||= ''
1611-
@fileutils_output.puts @fileutils_label + msg
1606+
output = @fileutils_output if defined?(@fileutils_output)
1607+
output ||= $stderr
1608+
if defined?(@fileutils_label)
1609+
msg = @fileutils_label + msg
1610+
end
1611+
output.puts msg
16121612
end
16131613
private_module_function :fu_output_message
16141614

@@ -1689,8 +1689,6 @@ def _do_nothing(*)end
16891689
#
16901690
module Verbose
16911691
include FileUtils
1692-
@fileutils_output = $stderr
1693-
@fileutils_label = ''
16941692
names = ::FileUtils.collect_method(:verbose)
16951693
names.each do |name|
16961694
module_eval(<<-EOS, __FILE__, __LINE__ + 1)
@@ -1714,8 +1712,6 @@ class << self
17141712
module NoWrite
17151713
include FileUtils
17161714
include LowMethods
1717-
@fileutils_output = $stderr
1718-
@fileutils_label = ''
17191715
names = ::FileUtils.collect_method(:noop)
17201716
names.each do |name|
17211717
module_eval(<<-EOS, __FILE__, __LINE__ + 1)
@@ -1740,8 +1736,6 @@ class << self
17401736
module DryRun
17411737
include FileUtils
17421738
include LowMethods
1743-
@fileutils_output = $stderr
1744-
@fileutils_label = ''
17451739
names = ::FileUtils.collect_method(:noop)
17461740
names.each do |name|
17471741
module_eval(<<-EOS, __FILE__, __LINE__ + 1)

test/fileutils/test_fileutils.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require_relative 'fileasserts'
77
require 'pathname'
88
require 'tmpdir'
9+
require 'stringio'
910
require 'test/unit'
1011

1112
class TestFileUtils < Test::Unit::TestCase
@@ -1634,6 +1635,29 @@ def test_chdir
16341635
check_singleton :chdir
16351636
end
16361637

1638+
def test_chdir_verbose
1639+
assert_output_lines(["cd .", "cd -"], FileUtils) do
1640+
FileUtils.chdir('.', verbose: true){}
1641+
end
1642+
end
1643+
1644+
def test_chdir_verbose_frozen
1645+
o = Object.new
1646+
o.extend(FileUtils)
1647+
o.singleton_class.send(:public, :chdir)
1648+
o.freeze
1649+
orig_stderr = $stderr
1650+
$stderr = StringIO.new
1651+
o.chdir('.', verbose: true){}
1652+
$stderr.rewind
1653+
assert_equal(<<-END, $stderr.read)
1654+
cd .
1655+
cd -
1656+
END
1657+
ensure
1658+
$stderr = orig_stderr if orig_stderr
1659+
end
1660+
16371661
def test_getwd
16381662
check_singleton :getwd
16391663
end

0 commit comments

Comments
 (0)