Skip to content

Commit

Permalink
Do not break in verbose mode if using FileUtils with a frozen object
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jeremyevans committed Jul 31, 2019
1 parent 106ddaa commit 689cb9c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
18 changes: 6 additions & 12 deletions lib/fileutils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1602,13 +1602,13 @@ def fu_same?(a, b) #:nodoc:
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

Expand Down Expand Up @@ -1689,8 +1689,6 @@ def _do_nothing(*)end
#
module Verbose
include FileUtils
@fileutils_output = $stderr
@fileutils_label = ''
names = ::FileUtils.collect_method(:verbose)
names.each do |name|
module_eval(<<-EOS, __FILE__, __LINE__ + 1)
Expand All @@ -1714,8 +1712,6 @@ class << self
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)
Expand All @@ -1740,8 +1736,6 @@ class << self
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)
Expand Down
24 changes: 24 additions & 0 deletions test/fileutils/test_fileutils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_relative 'fileasserts'
require 'pathname'
require 'tmpdir'
require 'stringio'
require 'test/unit'

class TestFileUtils < Test::Unit::TestCase
Expand Down Expand Up @@ -1634,6 +1635,29 @@ def test_chdir
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
Expand Down

0 comments on commit 689cb9c

Please sign in to comment.