Module: RSpec::Risky::Probe::OutputProbe

Defined in:
lib/rspec/risky/probe/fd_capture.rb,
lib/rspec/risky/probe/output_probe.rb,
lib/rspec/risky/probe/recording_io.rb,
lib/rspec/risky/probe/output_report.rb

Defined Under Namespace

Modules: KernelWarnPatch, LoggerPatch, OutputMatcherPatch Classes: FdCapture, RecordingIO, Report, State, StreamStats

Constant Summary collapse

THREAD_KEY =
:__rspec_risky_output_probe_state
SUPPRESS_KEY =
:__rspec_risky_output_probe_suppressed

Class Method Summary collapse

Class Method Details

.capture_suppressed?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/rspec/risky/probe/output_probe.rb', line 115

def capture_suppressed?
  Thread.current[SUPPRESS_KEY].to_i.positive?
end

.finish(state) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/rspec/risky/probe/output_probe.rb', line 97

def finish(state)
  return unless state

  state.fd_captures.reverse_each(&:finish)
  $stdout = state.original_stdout if state.stdout_proxy
  $stderr = state.original_stderr if state.stderr_proxy
  Thread.current[THREAD_KEY] = state.previous_state

  state.report
end

.installObject



59
60
61
62
63
64
65
66
67
# File 'lib/rspec/risky/probe/output_probe.rb', line 59

def install
  return if @installed

  ::RSpec::Matchers::BuiltIn::Output.prepend(OutputMatcherPatch)
  ::Kernel.prepend(KernelWarnPatch)
  install_logger_patch

  @installed = true
end

.record_logger(logger, message, progname, locations) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/rspec/risky/probe/output_probe.rb', line 123

def record_logger(logger, message, progname, locations)
  state = current
  return unless state

  path = logger_path(logger)
  return unless state.rule_config.captures_logger?(path)

  sample = message || progname || "logger write"
  state.report.record(:logger, sample.to_s, locations)
end

.start(rule_config) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rspec/risky/probe/output_probe.rb', line 69

def start(rule_config)
  report = Report.new
  state = State.new(
    report: report,
    original_stdout: $stdout,
    original_stderr: $stderr,
    allow_warn: rule_config.allow_warn,
    fd_captures: [],
    rule_config: rule_config,
    previous_state: current
  )

  if rule_config.strict
    start_fd_captures(state, rule_config, report)
  elsif rule_config.captures?(:stdout)
    state.stdout_proxy = RecordingIO.new(:stdout, $stdout, report, passthrough: rule_config.passthrough)
  end
  if !rule_config.strict && rule_config.captures?(:stderr)
    state.stderr_proxy = RecordingIO.new(:stderr, $stderr, report, passthrough: rule_config.passthrough)
  end

  $stdout = state.stdout_proxy if state.stdout_proxy
  $stderr = state.stderr_proxy if state.stderr_proxy
  Thread.current[THREAD_KEY] = state

  state
end

.suppress_warn?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/rspec/risky/probe/output_probe.rb', line 119

def suppress_warn?
  current&.allow_warn
end

.with_capture_suppressedObject



108
109
110
111
112
113
# File 'lib/rspec/risky/probe/output_probe.rb', line 108

def with_capture_suppressed
  Thread.current[SUPPRESS_KEY] = Thread.current[SUPPRESS_KEY].to_i + 1
  yield
ensure
  Thread.current[SUPPRESS_KEY] -= 1
end