mutineer 1.8.2 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +611 -0
- data/README.md +84 -49
- data/lib/mutineer/changed_lines.rb +2 -1
- data/lib/mutineer/child_cause.rb +149 -0
- data/lib/mutineer/child_stdout.rb +12 -1
- data/lib/mutineer/cli.rb +536 -58
- data/lib/mutineer/command_redact.rb +38 -0
- data/lib/mutineer/config.rb +112 -6
- data/lib/mutineer/coverage_map.rb +1345 -156
- data/lib/mutineer/daemon_backend.rb +40 -17
- data/lib/mutineer/daemon_client.rb +63 -15
- data/lib/mutineer/daemon_server.rb +75 -5
- data/lib/mutineer/database_urls.rb +189 -0
- data/lib/mutineer/external_backend.rb +47 -1
- data/lib/mutineer/fs_path.rb +125 -0
- data/lib/mutineer/isolation.rb +374 -20
- data/lib/mutineer/job_plan.rb +349 -39
- data/lib/mutineer/minitest_integration/file_order.rb +56 -0
- data/lib/mutineer/minitest_integration/in_test_database_error.rb +243 -19
- data/lib/mutineer/minitest_integration.rb +26 -58
- data/lib/mutineer/mutant_id.rb +4 -1
- data/lib/mutineer/mutation.rb +6 -1
- data/lib/mutineer/mutator_registry.rb +6 -3
- data/lib/mutineer/mutators/collection_method.rb +195 -6
- data/lib/mutineer/mutators/literal_mutation.rb +33 -3
- data/lib/mutineer/mutators/range_literal.rb +78 -29
- data/lib/mutineer/mutators/rescue_body.rb +126 -0
- data/lib/mutineer/mutators/return_nil.rb +23 -6
- data/lib/mutineer/output_filter.rb +451 -0
- data/lib/mutineer/pairing.rb +463 -31
- data/lib/mutineer/parser.rb +14 -0
- data/lib/mutineer/project_path.rb +15 -8
- data/lib/mutineer/rails_worker_db.rb +356 -6
- data/lib/mutineer/reach_probe.rb +229 -0
- data/lib/mutineer/redaction.rb +264 -32
- data/lib/mutineer/reload_guards.rb +460 -39
- data/lib/mutineer/reporter.rb +465 -57
- data/lib/mutineer/result.rb +6 -7
- data/lib/mutineer/run_seed.rb +16 -0
- data/lib/mutineer/runner.rb +175 -51
- data/lib/mutineer/source_bytes.rb +187 -0
- data/lib/mutineer/statement_lines.rb +108 -14
- data/lib/mutineer/test_runners/rspec.rb +118 -10
- data/lib/mutineer/test_runners/rspec_seed.rb +151 -0
- data/lib/mutineer/test_runners.rb +3 -2
- data/lib/mutineer/triage.rb +55 -10
- data/lib/mutineer/version.rb +1 -1
- data/lib/mutineer.rb +1 -0
- data/skills/mutineer/SKILL.md +12 -4
- metadata +12 -1
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "redaction"
|
|
4
|
+
|
|
5
|
+
module Mutineer
|
|
6
|
+
# Copies the process's stdout and stderr through {Redaction.text}. A fork
|
|
7
|
+
# child and a subprocess inherit the pipe. The child starts its own copier
|
|
8
|
+
# so a DATABASE_URL it sets is hidden (#341). Before a fork child execs, its
|
|
9
|
+
# copier stops, so the program writes to the parent's pipe and the parent
|
|
10
|
+
# redacts it with the parent's environment. A program that would run with
|
|
11
|
+
# a DATABASE_URL the parent does not know writes to the null device. A
|
|
12
|
+
# `--test-command` Ruby process restores the captured descriptors before
|
|
13
|
+
# exec, and the next Ruby loads its own copier (#457).
|
|
14
|
+
#
|
|
15
|
+
# Stdlib only, so the app-side daemon can load this file. A chunk with no
|
|
16
|
+
# password is copied unchanged, including its bytes.
|
|
17
|
+
module OutputFilter
|
|
18
|
+
# Seconds to wait for a last buffered chunk after {uninstall} closes this
|
|
19
|
+
# process's write end. A grandchild that still holds the pipe does not
|
|
20
|
+
# extend the wait. Bytes already read are written before the copier
|
|
21
|
+
# returns; a slow reader blocks that write, and the bytes are not dropped.
|
|
22
|
+
QUIET_POLL = 0.05
|
|
23
|
+
|
|
24
|
+
# Runs {after_fork!} in the child. Kernel#fork, Kernel.fork, and
|
|
25
|
+
# Process.fork are separate, so all three are wrapped.
|
|
26
|
+
module ForkHook
|
|
27
|
+
# @return [Integer, nil] the child pid in the parent; nil in the child
|
|
28
|
+
# when no block is given.
|
|
29
|
+
def fork(*)
|
|
30
|
+
if block_given?
|
|
31
|
+
super do
|
|
32
|
+
OutputFilter.after_fork!
|
|
33
|
+
yield
|
|
34
|
+
end
|
|
35
|
+
else
|
|
36
|
+
pid = super
|
|
37
|
+
OutputFilter.after_fork! if pid.nil?
|
|
38
|
+
pid
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Stops a fork child's copier before exec. The copier thread and its read
|
|
44
|
+
# end do not survive exec, so the program would write to a pipe nobody
|
|
45
|
+
# reads. Kernel#exec, Kernel.exec, and Process.exec are separate, so all
|
|
46
|
+
# three are wrapped.
|
|
47
|
+
module ExecHook
|
|
48
|
+
# @return [void] does not return when exec succeeds.
|
|
49
|
+
def exec(*args)
|
|
50
|
+
OutputFilter.before_exec(args)
|
|
51
|
+
super
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Drops a spawned program's output when its env hash sets a DATABASE_URL
|
|
56
|
+
# this process's copier does not know. A URL set in ENV first is known:
|
|
57
|
+
# the copier reads ENV. Process has no `system`, so it gets {SpawnHook}
|
|
58
|
+
# only.
|
|
59
|
+
module SpawnHook
|
|
60
|
+
# @return [Integer] the child pid.
|
|
61
|
+
def spawn(*args)
|
|
62
|
+
super(*OutputFilter.spawn_args(args))
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# The `system` form of {SpawnHook}, for Kernel#system and Kernel.system.
|
|
67
|
+
module SystemHook
|
|
68
|
+
# @return [Boolean, nil] as Kernel#system.
|
|
69
|
+
def system(*args)
|
|
70
|
+
super(*OutputFilter.spawn_args(args))
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Flushes the copier before exit!. exit! skips at_exit, and a forked
|
|
75
|
+
# test exits that way.
|
|
76
|
+
module ExitHook
|
|
77
|
+
# @param status [Boolean, Integer]
|
|
78
|
+
# @return [void]
|
|
79
|
+
def exit!(status = true)
|
|
80
|
+
OutputFilter.uninstall
|
|
81
|
+
super
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
module_function
|
|
86
|
+
|
|
87
|
+
# True while the descriptors point at the pipe.
|
|
88
|
+
#
|
|
89
|
+
# @return [Boolean]
|
|
90
|
+
def installed?
|
|
91
|
+
@installed == true
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Point each named stream at a pipe and start the copier. A second call
|
|
95
|
+
# does nothing and returns false, so the caller that turned it on is the
|
|
96
|
+
# one that turns it off. `streams` is `:stdout`, `:stderr`, or both.
|
|
97
|
+
# Returns :failed without leaving the descriptors redirected when they
|
|
98
|
+
# cannot be moved (a closed stream). Returns :already when a copier is
|
|
99
|
+
# already running, so the caller that started it stays the one that
|
|
100
|
+
# stops it.
|
|
101
|
+
#
|
|
102
|
+
# @param streams [Array<Symbol>]
|
|
103
|
+
# @return [Symbol] :started, :already, or :failed.
|
|
104
|
+
def install(streams: %i[stdout stderr])
|
|
105
|
+
return :already if installed?
|
|
106
|
+
|
|
107
|
+
names = Array(streams)
|
|
108
|
+
saved = {}
|
|
109
|
+
stdio = {}
|
|
110
|
+
readers = {}
|
|
111
|
+
names.each do |name|
|
|
112
|
+
io = stream_io(name)
|
|
113
|
+
copy = io.dup
|
|
114
|
+
copy.sync = true
|
|
115
|
+
read, write = IO.pipe
|
|
116
|
+
write.sync = true
|
|
117
|
+
read.binmode
|
|
118
|
+
read.close_on_exec = true
|
|
119
|
+
io.reopen(write)
|
|
120
|
+
io.sync = true
|
|
121
|
+
write.close
|
|
122
|
+
saved[name] = copy
|
|
123
|
+
stdio[name] = io
|
|
124
|
+
readers[read] = name
|
|
125
|
+
end
|
|
126
|
+
@streams = names
|
|
127
|
+
@saved = saved
|
|
128
|
+
@stdio = stdio
|
|
129
|
+
@readers = readers
|
|
130
|
+
@stopping = false
|
|
131
|
+
@installed = true
|
|
132
|
+
# What the process that reads this pipe knows, for {before_exec}.
|
|
133
|
+
@install_url = ENV["DATABASE_URL"].to_s
|
|
134
|
+
hook_fork
|
|
135
|
+
hook_exit!
|
|
136
|
+
@thread = Thread.new { drain } # rubocop:disable ThreadSafety/NewThread
|
|
137
|
+
@thread.report_on_exception = false
|
|
138
|
+
hook_exit
|
|
139
|
+
:started
|
|
140
|
+
rescue StandardError
|
|
141
|
+
restore(stdio || {}, saved || {})
|
|
142
|
+
readers&.each_key { |io| io.close unless io.closed? }
|
|
143
|
+
:failed
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# Put the descriptors back and write every byte already queued. Does
|
|
147
|
+
# nothing when {install} is not on. Safe to call twice. Does not kill the
|
|
148
|
+
# copier: a slow stdout must still receive the report. A grandchild that
|
|
149
|
+
# holds the pipe does not add a wait past {QUIET_POLL}, because the
|
|
150
|
+
# copier stops reading once the pipe is empty.
|
|
151
|
+
#
|
|
152
|
+
# @return [void]
|
|
153
|
+
def uninstall
|
|
154
|
+
return unless installed?
|
|
155
|
+
|
|
156
|
+
@stopping = true
|
|
157
|
+
saved = @saved || {}
|
|
158
|
+
stdio = @stdio || {}
|
|
159
|
+
stdio.each do |name, io|
|
|
160
|
+
copy = saved[name]
|
|
161
|
+
io.reopen(copy) if copy && !copy.closed? && !io.closed?
|
|
162
|
+
end
|
|
163
|
+
@thread&.join
|
|
164
|
+
saved.each_value { |io| io.close unless io.closed? }
|
|
165
|
+
(@readers || {}).each_key { |io| io.close unless io.closed? }
|
|
166
|
+
@installed = false
|
|
167
|
+
@thread = nil
|
|
168
|
+
@stopping = false
|
|
169
|
+
@saved = {}
|
|
170
|
+
@stdio = {}
|
|
171
|
+
@readers = {}
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# Start a copier in a fork child. The parent's thread was not copied.
|
|
175
|
+
# This process redacts with its own environment, then writes to the
|
|
176
|
+
# parent's pipe. The parent's read ends are closed so this process does
|
|
177
|
+
# not steal the parent's bytes.
|
|
178
|
+
#
|
|
179
|
+
# @return [void]
|
|
180
|
+
def after_fork!
|
|
181
|
+
return unless installed?
|
|
182
|
+
|
|
183
|
+
names = @streams
|
|
184
|
+
reads = (@readers || {}).keys.dup
|
|
185
|
+
@fork_child = true
|
|
186
|
+
# The parent's copier redacts with this URL, read before child code runs.
|
|
187
|
+
@parent_url = ENV["DATABASE_URL"].to_s
|
|
188
|
+
@installed = false
|
|
189
|
+
@thread = nil
|
|
190
|
+
@stopping = false
|
|
191
|
+
@hooked = false
|
|
192
|
+
@readers = {}
|
|
193
|
+
@saved = {}
|
|
194
|
+
@stdio = {}
|
|
195
|
+
reads.each { |io| io.close unless io.closed? }
|
|
196
|
+
silence_stdio unless install(streams: names || [:stderr]) == :started
|
|
197
|
+
rescue StandardError
|
|
198
|
+
silence_stdio
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# In a fork child, put the parent's pipe back on stdout and stderr and
|
|
202
|
+
# flush this copier. When the program would run with a DATABASE_URL the
|
|
203
|
+
# parent does not know, its output goes to the null device instead: the
|
|
204
|
+
# parent could not hide that password. A `--test-command` Ruby process
|
|
205
|
+
# also puts the captured descriptors back, so the program that replaces
|
|
206
|
+
# it does not write to a pipe whose reader died. The next Ruby loads its
|
|
207
|
+
# own copier from RUBYOPT (#457). The same null-device rule holds there
|
|
208
|
+
# when the program would run with a URL the tool did not know at start:
|
|
209
|
+
# a program that is not Ruby has no copier.
|
|
210
|
+
#
|
|
211
|
+
# @param args [Array] the arguments given to exec; a leading Hash is its
|
|
212
|
+
# environment.
|
|
213
|
+
# @return [void]
|
|
214
|
+
def before_exec(args = [])
|
|
215
|
+
if @fork_child
|
|
216
|
+
known = known_url?(args, @parent_url)
|
|
217
|
+
uninstall
|
|
218
|
+
silence_stdio unless known
|
|
219
|
+
return
|
|
220
|
+
end
|
|
221
|
+
return unless installed? && command_ruby?
|
|
222
|
+
|
|
223
|
+
# The next Ruby loads its own copier. A program that is not Ruby does not,
|
|
224
|
+
# so drop its output when it would run with a URL nobody here knows.
|
|
225
|
+
known = known_url?(args, @install_url)
|
|
226
|
+
uninstall
|
|
227
|
+
silence_stdio unless known
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
# True when this process was started as a `--test-command` Ruby, which
|
|
231
|
+
# loads the copier from RUBYOPT.
|
|
232
|
+
#
|
|
233
|
+
# @return [Boolean]
|
|
234
|
+
def command_ruby?
|
|
235
|
+
ENV["RUBYOPT"].to_s.include?("command_redact.rb")
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
# `args` for spawn or system, with stdout and stderr sent to the null
|
|
239
|
+
# device when the program's env hash sets a DATABASE_URL this process
|
|
240
|
+
# does not know. The caller's own redirects of stdout and stderr are
|
|
241
|
+
# dropped too: a target such as `err: :out` or `err: STDOUT` would still
|
|
242
|
+
# reach this pipe, and Ruby refuses a stream given twice.
|
|
243
|
+
#
|
|
244
|
+
# @param args [Array] the arguments given to spawn or system.
|
|
245
|
+
# @return [Array]
|
|
246
|
+
def spawn_args(args)
|
|
247
|
+
return args if !installed? || known_url?(args, ENV.fetch("DATABASE_URL", nil))
|
|
248
|
+
|
|
249
|
+
options = args.size > 1 && args.last.is_a?(Hash) ? args.last : {}
|
|
250
|
+
kept = options.reject { |key, _| Array(key).intersect?([:out, :err, 1, 2, STDOUT, STDERR]) }
|
|
251
|
+
command = options.empty? ? args : args[0...-1]
|
|
252
|
+
command + [kept.merge(out: File::NULL, err: File::NULL)]
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# True when the program `args` start would run with no DATABASE_URL,
|
|
256
|
+
# with `current`, or with a dotenv URL redaction already knows. The env
|
|
257
|
+
# hash wins; ENV counts only when `unsetenv_others` does not clear it.
|
|
258
|
+
#
|
|
259
|
+
# @param args [Array] exec, spawn, or system arguments.
|
|
260
|
+
# @param current [String, nil] the URL the redacting copier knows.
|
|
261
|
+
# @return [Boolean]
|
|
262
|
+
def known_url?(args, current)
|
|
263
|
+
env = args.first.is_a?(Hash) ? args.first : {}
|
|
264
|
+
options = args.size > 1 && args.last.is_a?(Hash) ? args.last : {}
|
|
265
|
+
inherited = options[:unsetenv_others] ? nil : ENV.fetch("DATABASE_URL", nil)
|
|
266
|
+
url = (env.key?("DATABASE_URL") ? env["DATABASE_URL"] : inherited).to_s
|
|
267
|
+
url.empty? || url == current.to_s || Redaction.extra_urls.include?(url)
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
# Point stdout and stderr at the null device. Used when this fork cannot
|
|
271
|
+
# start its own copier: the parent's pipe would otherwise publish a
|
|
272
|
+
# password this process set and the parent does not know.
|
|
273
|
+
#
|
|
274
|
+
# @return [void]
|
|
275
|
+
def silence_stdio
|
|
276
|
+
File.open(File::NULL, "w") do |null|
|
|
277
|
+
STDOUT.reopen(null) unless STDOUT.closed?
|
|
278
|
+
STDERR.reopen(null) unless STDERR.closed?
|
|
279
|
+
end
|
|
280
|
+
rescue StandardError
|
|
281
|
+
nil
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
# The real stream for `name`.
|
|
285
|
+
#
|
|
286
|
+
# @param name [Symbol] `:stdout` or `:stderr`.
|
|
287
|
+
# @return [IO]
|
|
288
|
+
def stream_io(name)
|
|
289
|
+
name == :stdout ? STDOUT : STDERR
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
# Register the process-exit flush once. `exit!` skips at_exit; {ExitHook}
|
|
293
|
+
# covers that.
|
|
294
|
+
#
|
|
295
|
+
# @return [void]
|
|
296
|
+
def hook_exit
|
|
297
|
+
return if @hooked
|
|
298
|
+
|
|
299
|
+
@hooked = true
|
|
300
|
+
at_exit { uninstall }
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
# Wrap fork, exec, spawn, and system once per process.
|
|
304
|
+
#
|
|
305
|
+
# @return [void]
|
|
306
|
+
def hook_fork
|
|
307
|
+
return if @fork_hooked
|
|
308
|
+
|
|
309
|
+
@fork_hooked = true
|
|
310
|
+
[Kernel, Kernel.singleton_class, Process.singleton_class].each do |target|
|
|
311
|
+
target.prepend(ForkHook)
|
|
312
|
+
target.prepend(ExecHook)
|
|
313
|
+
target.prepend(SpawnHook)
|
|
314
|
+
end
|
|
315
|
+
Kernel.prepend(SystemHook)
|
|
316
|
+
Kernel.singleton_class.prepend(SystemHook)
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
# @return [void]
|
|
320
|
+
def hook_exit!
|
|
321
|
+
return if @exit_hooked
|
|
322
|
+
|
|
323
|
+
@exit_hooked = true
|
|
324
|
+
Kernel.prepend(ExitHook)
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
# Read both pipes until they close, or until {uninstall} has closed this
|
|
328
|
+
# process's write ends and the pipes are empty. A password that contains
|
|
329
|
+
# a newline stays in the buffer until the whole form has arrived.
|
|
330
|
+
#
|
|
331
|
+
# @return [void]
|
|
332
|
+
def drain
|
|
333
|
+
pending = Hash.new { |buffer, io| buffer[io] = +"".b }
|
|
334
|
+
open = (@readers || {}).keys
|
|
335
|
+
until open.empty?
|
|
336
|
+
if @stopping
|
|
337
|
+
drain_ready(open, pending)
|
|
338
|
+
break
|
|
339
|
+
end
|
|
340
|
+
ready, = IO.select(open, nil, nil, QUIET_POLL)
|
|
341
|
+
next unless ready
|
|
342
|
+
|
|
343
|
+
ready.each do |io|
|
|
344
|
+
chunk = io.read_nonblock(4096, exception: false)
|
|
345
|
+
if chunk.nil?
|
|
346
|
+
flush_pending(io, pending, flush: true)
|
|
347
|
+
open.delete(io)
|
|
348
|
+
io.close unless io.closed?
|
|
349
|
+
next
|
|
350
|
+
end
|
|
351
|
+
next if chunk == :wait_readable
|
|
352
|
+
|
|
353
|
+
pending[io] << chunk.b
|
|
354
|
+
# Nothing new can be emitted before a newline, so a long line is not
|
|
355
|
+
# rescanned on every read.
|
|
356
|
+
flush_pending(io, pending, flush: false) if chunk.include?("\n")
|
|
357
|
+
end
|
|
358
|
+
end
|
|
359
|
+
open.each { |io| flush_pending(io, pending, flush: true) }
|
|
360
|
+
rescue StandardError
|
|
361
|
+
nil
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
# Read what is already buffered, plus one quiet poll, then stop. Used
|
|
365
|
+
# after {uninstall} closes this process's write ends.
|
|
366
|
+
#
|
|
367
|
+
# @param open [Array<IO>]
|
|
368
|
+
# @param pending [Hash{IO => String}]
|
|
369
|
+
# @return [void]
|
|
370
|
+
def drain_ready(open, pending)
|
|
371
|
+
open.each do |io|
|
|
372
|
+
loop do
|
|
373
|
+
chunk = io.read_nonblock(4096, exception: false)
|
|
374
|
+
if chunk == :wait_readable
|
|
375
|
+
break unless io.wait_readable(QUIET_POLL)
|
|
376
|
+
|
|
377
|
+
next
|
|
378
|
+
end
|
|
379
|
+
break if chunk.nil?
|
|
380
|
+
|
|
381
|
+
pending[io] << chunk.b
|
|
382
|
+
end
|
|
383
|
+
flush_pending(io, pending, flush: true)
|
|
384
|
+
end
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
# Redact and write the part of `pending[io]` that is safe to emit.
|
|
388
|
+
#
|
|
389
|
+
# @param io [IO]
|
|
390
|
+
# @param pending [Hash{IO => String}]
|
|
391
|
+
# @param flush [Boolean]
|
|
392
|
+
# @return [void]
|
|
393
|
+
def flush_pending(io, pending, flush:)
|
|
394
|
+
ready, pending[io] = Redaction.take_redactable(pending[io], flush: flush)
|
|
395
|
+
emit(@readers[io], ready)
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
# Write `chunk` to the original stream, redacted. The original bytes are
|
|
399
|
+
# kept when redaction changes nothing, so a report with no password stays
|
|
400
|
+
# byte for byte. An error writes {Redaction::FAILED}, never the raw chunk.
|
|
401
|
+
#
|
|
402
|
+
# @param stream [Symbol, nil]
|
|
403
|
+
# @param chunk [String] raw bytes. May contain newlines.
|
|
404
|
+
# @return [void]
|
|
405
|
+
def emit(stream, chunk)
|
|
406
|
+
return if stream.nil? || chunk.nil? || chunk.empty?
|
|
407
|
+
|
|
408
|
+
text = chunk.dup.force_encoding(Encoding::UTF_8)
|
|
409
|
+
clean = text.dup
|
|
410
|
+
clean.scrub! unless clean.valid_encoding?
|
|
411
|
+
redacted = Redaction.text(clean)
|
|
412
|
+
# Unchanged text keeps the original bytes, including invalid ones.
|
|
413
|
+
# A replaced secret is the redacted UTF-8 string.
|
|
414
|
+
payload = redacted == clean ? chunk : redacted.b
|
|
415
|
+
write_out(stream, payload)
|
|
416
|
+
rescue StandardError
|
|
417
|
+
write_out(stream, Redaction::FAILED.b)
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
# Write to `$stdout` or `$stderr` when the caller replaced that variable
|
|
421
|
+
# (a test capture). Otherwise write to the descriptor saved before
|
|
422
|
+
# {install}, never back into the pipe.
|
|
423
|
+
#
|
|
424
|
+
# @param stream [Symbol]
|
|
425
|
+
# @param data [String]
|
|
426
|
+
# @return [void]
|
|
427
|
+
def write_out(stream, data)
|
|
428
|
+
current = stream == :stdout ? $stdout : $stderr
|
|
429
|
+
pipe = @stdio[stream]
|
|
430
|
+
target = current.equal?(pipe) ? @saved[stream] : current
|
|
431
|
+
target&.write(data)
|
|
432
|
+
rescue Errno::EPIPE, IOError
|
|
433
|
+
nil
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
# Put any streams this failed {install} already moved back where they were.
|
|
437
|
+
#
|
|
438
|
+
# @param stdio [Hash{Symbol => IO}]
|
|
439
|
+
# @param saved [Hash{Symbol => IO}]
|
|
440
|
+
# @return [void]
|
|
441
|
+
def restore(stdio, saved)
|
|
442
|
+
stdio.each do |name, io|
|
|
443
|
+
copy = saved[name]
|
|
444
|
+
io.reopen(copy) if copy && !copy.closed? && !io.closed?
|
|
445
|
+
end
|
|
446
|
+
saved.each_value { |io| io.close unless io.closed? }
|
|
447
|
+
rescue StandardError
|
|
448
|
+
nil
|
|
449
|
+
end
|
|
450
|
+
end
|
|
451
|
+
end
|