Skip to content

Commit

Permalink
✨ Allow to set a success_debrief to debrief successful tasks
Browse files Browse the repository at this point in the history
marianosimone committed Nov 27, 2022
1 parent 28c8d2d commit 5495bbc
Showing 3 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/cli/ui/spinner.rb
Original file line number Diff line number Diff line change
@@ -61,7 +61,7 @@ class << self
#
# ==== Options
#
# * +:auto_debrief+ - Automatically debrief exceptions? Default to true
# * +:auto_debrief+ - Automatically debrief exceptions or through success_debrief? Default to true
#
# ==== Block
#
20 changes: 16 additions & 4 deletions lib/cli/ui/spinner/spin_group.rb
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ class SpinGroup
#
# ==== Options
#
# * +:auto_debrief+ - Automatically debrief exceptions? Default to true
# * +:auto_debrief+ - Automatically debrief exceptions or through success_debrief? Default to true
#
# ==== Example Usage
#
@@ -273,6 +273,16 @@ def failure_debrief(&block)
@failure_debrief = block
end

# Provide a debriefing for successful tasks
sig do
params(
block: T.proc.params(title: String, out: String, err: String).void,
).void
end
def success_debrief(&block)
@success_debrief = block
end

sig { returns(T::Boolean) }
def all_succeeded?
@m.synchronize do
@@ -286,13 +296,15 @@ def all_succeeded?
def debrief
@m.synchronize do
@tasks.each do |task|
next if task.success

title = task.title
e = task.exception
out = task.stdout
err = task.stderr

if task.success
next @success_debrief&.call(title, out, err)
end

e = task.exception
next @failure_debrief.call(title, e, out, err) if @failure_debrief

CLI::UI::Frame.open('Task Failed: ' + title, color: :red, timing: Time.new - @start) do
17 changes: 17 additions & 0 deletions test/cli/ui/spinner/spin_group_test.rb
Original file line number Diff line number Diff line change
@@ -33,6 +33,23 @@ def test_spin_group_auto_debrief_false

assert_equal('', err)
end

def test_spin_group_success_debrief
capture_io do
CLI::UI::StdoutRouter.ensure_activated

debriefer = ->(title, out, err) {}
sg = SpinGroup.new
sg.success_debrief(&debriefer)
debriefer.expects(:call).with('s', "Task output\n", '').once
sg.add('s') do
puts('Task output')
true
end

assert(sg.wait)
end
end
end
end
end

0 comments on commit 5495bbc

Please sign in to comment.