Class: RSpec::Support::StdErrSplitter

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/support/spec/stderr_splitter.rb

Instance Method Summary collapse

Constructor Details

#initialize(original) ⇒ StdErrSplitter

Returns a new instance of StdErrSplitter.



8
9
10
11
12
# File 'lib/rspec/support/spec/stderr_splitter.rb', line 8

def initialize(original)
  @orig_stderr    = original
  @output_tracker = ::StringIO.new
  @last_line = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



19
20
21
22
# File 'lib/rspec/support/spec/stderr_splitter.rb', line 19

def method_missing(name, *args, &block)
  @output_tracker.__send__(name, *args, &block) if @output_tracker.respond_to?(name)
  @orig_stderr.__send__(name, *args, &block)
end

Instance Method Details

#==(other) ⇒ Object



24
25
26
# File 'lib/rspec/support/spec/stderr_splitter.rb', line 24

def ==(other)
  @orig_stderr == other
end

#has_output?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/rspec/support/spec/stderr_splitter.rb', line 59

def has_output?
  !output.empty?
end

#outputObject



72
73
74
# File 'lib/rspec/support/spec/stderr_splitter.rb', line 72

def output
  @output_tracker.string
end

#reopen(*args) ⇒ Object



28
29
30
31
# File 'lib/rspec/support/spec/stderr_splitter.rb', line 28

def reopen(*args)
  reset!
  @orig_stderr.reopen(*args)
end

#reset!Object



63
64
65
# File 'lib/rspec/support/spec/stderr_splitter.rb', line 63

def reset!
  @output_tracker = ::StringIO.new
end

#to_ioObject

To work around JRuby error: can’t convert RSpec::Support::StdErrSplitter into String



35
36
37
# File 'lib/rspec/support/spec/stderr_splitter.rb', line 35

def to_io
  @orig_stderr.to_io
end

#verify_no_warnings!Object



67
68
69
70
# File 'lib/rspec/support/spec/stderr_splitter.rb', line 67

def verify_no_warnings!
  raise "Warnings were generated: #{output}" if has_output?
  reset!
end

#write(line) ⇒ Object

To work around JRuby error: TypeError: $stderr must have write method, RSpec::StdErrSplitter given



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rspec/support/spec/stderr_splitter.rb', line 41

def write(line)
  return if line =~ %r{^\S+/gems/\S+:\d+: warning:} # http://rubular.com/r/kqeUIZOfPG

  # Ruby 2.7.0 warnings from keyword arguments span multiple lines, extend check above
  # to look for the next line.
  return if @last_line =~ %r{^\S+/gems/\S+:\d+: warning:} &&
            line =~ %r{warning: The called method .* is defined here}

  # Ruby 2.7.0 complains about hashes used in place of keyword arguments
  # Aruba 0.14.2 uses this internally triggering that here
  return if line =~ %r{lib/ruby/2\.7\.0/fileutils\.rb:622: warning:}

  @orig_stderr.write(line)
  @output_tracker.write(line)
ensure
  @last_line = line
end