Add test for print_stdout: :debug.

This commit is contained in:
Markus Reiter 2023-10-10 03:39:42 +02:00
parent 5d87da135a
commit 4b7aed84fa
No known key found for this signature in database
GPG Key ID: 245293B51702655B
4 changed files with 53 additions and 7 deletions

View File

@ -33,27 +33,33 @@ module Context
@verbose = verbose @verbose = verbose
end end
sig { returns(T::Boolean) }
def debug? def debug?
@debug == true @debug == true
end end
sig { returns(T::Boolean) }
def quiet? def quiet?
@quiet == true @quiet == true
end end
sig { returns(T::Boolean) }
def verbose? def verbose?
@verbose == true @verbose == true
end end
end end
sig { returns(T::Boolean) }
def debug? def debug?
Context.current.debug? Context.current.debug?
end end
sig { returns(T::Boolean) }
def quiet? def quiet?
Context.current.quiet? Context.current.quiet?
end end
sig { returns(T::Boolean) }
def verbose? def verbose?
Context.current.verbose? Context.current.verbose?
end end
@ -69,8 +75,10 @@ module Context
Thread.current[:context] = new_context Thread.current[:context] = new_context
begin
yield yield
ensure ensure
Thread.current[:context] = old_context Thread.current[:context] = old_context
end end
end
end end

View File

@ -244,9 +244,13 @@ class SystemCommand
write_input_to(raw_stdin) write_input_to(raw_stdin)
raw_stdin.close_write raw_stdin.close_write
thread_context = Context.current
thread_ready_queue = Queue.new thread_ready_queue = Queue.new
thread_done_queue = Queue.new thread_done_queue = Queue.new
line_thread = Thread.new do line_thread = Thread.new do
# Ensure the new thread inherits the current context.
Context.current = thread_context
Thread.handle_interrupt(ProcessTerminatedInterrupt => :never) do Thread.handle_interrupt(ProcessTerminatedInterrupt => :never) do
thread_ready_queue << true thread_ready_queue << true
each_line_from [raw_stdout, raw_stderr], &block each_line_from [raw_stdout, raw_stderr], &block

View File

@ -109,6 +109,8 @@ RSpec.configure do |config|
config.include(FileUtils) config.include(FileUtils)
config.include(Context)
config.include(RuboCop::RSpec::ExpectOffense) config.include(RuboCop::RSpec::ExpectOffense)
config.include(Test::Helper::Cask) config.include(Test::Helper::Cask)
@ -236,6 +238,7 @@ RSpec.configure do |config|
example.example.set_exception(e) example.example.set_exception(e)
ensure ensure
ENV.replace(@__env) ENV.replace(@__env)
Context.current = Context::ContextStruct.new
$stdout.reopen(@__stdout) $stdout.reopen(@__stdout)
$stderr.reopen(@__stderr) $stderr.reopen(@__stderr)

View File

@ -166,7 +166,7 @@ describe SystemCommand do
include_examples("it returns '1 2 3 4 5 6'") include_examples("it returns '1 2 3 4 5 6'")
end end
context "with print_stdout" do context "with `print_stdout: true`" do
before do before do
options.merge!(print_stdout: true) options.merge!(print_stdout: true)
end end
@ -180,7 +180,38 @@ describe SystemCommand do
include_examples("it returns '1 2 3 4 5 6'") include_examples("it returns '1 2 3 4 5 6'")
end end
context "without print_stderr" do context "with `print_stdout: :debug`" do
before do
options.merge!(print_stdout: :debug)
end
it "echoes only STDERR output" do
expect { described_class.run(command, **options) }
.to output("2\n4\n6\n").to_stderr
.and not_to_output.to_stdout
end
context "when `debug?` is true" do
let(:options) do
{ args: [
"-c",
"for i in $(seq 1 2 5); do echo $i; sleep 1; echo $(($i + 1)) >&2; done",
] }
end
it "echoes the command and all output to STDERR when `debug?` is true" do
with_context debug: true do
expect { described_class.run(command, **options) }
.to output(/\A.*#{Regexp.escape(command)}.*\n1\n2\n3\n4\n5\n6\n\Z/).to_stderr
.and not_to_output.to_stdout
end
end
end
include_examples("it returns '1 2 3 4 5 6'")
end
context "with `print_stderr: false`" do
before do before do
options.merge!(print_stderr: false) options.merge!(print_stderr: false)
end end
@ -188,13 +219,13 @@ describe SystemCommand do
it "echoes nothing" do it "echoes nothing" do
expect do expect do
described_class.run(command, **options) described_class.run(command, **options)
end.to output("").to_stdout end.not_to output.to_stdout
end end
include_examples("it returns '1 2 3 4 5 6'") include_examples("it returns '1 2 3 4 5 6'")
end end
context "with print_stdout but without print_stderr" do context "with `print_stdout: true` and `print_stderr: false`" do
before do before do
options.merge!(print_stdout: true, print_stderr: false) options.merge!(print_stdout: true, print_stderr: false)
end end