Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add `only_if` step to DRSF (#35247)

LoΓ―c Guitauta39785f3ddcf
diff --git a/lib/service/base.rb b/lib/service/base.rb
index dff18ba1967..840aee77aba 100644
--- a/lib/service/base.rb
+++ b/lib/service/base.rb
@@ -129,6 +129,10 @@ module Service
def try(*exceptions, &block)
steps << TryStep.new(exceptions, &block)
end
+
+ def only_if(name, &block)
+ steps << OnlyIfStep.new(name, &block)
+ end
end
# @!visibility private
@@ -173,7 +177,7 @@ module Service
end
def type
- self.class.name.split("::").last.downcase.sub(/^(\w+)step$/, "\\1")
+ self.class.name.split("::").last.underscore.sub(/^(\w+)_step$/, "\\1")
end
def with_runtime
@@ -346,6 +350,24 @@ module Service
end
end
+ # @!visibility private
+ class OnlyIfStep < Step
+ include StepsHelpers
+
+ attr_reader :steps
+
+ def initialize(name, &block)
+ super(name)
+ @steps = []
+ instance_exec(&block)
+ end
+
+ def run_step
+ return context[result_key][:skipped?] = true unless super
+ steps.each { |step| step.call(instance, context) }
+ end
+ end
+
# @!visibility private
class OptionsStep < Step
def run_step
diff --git a/lib/service/steps_inspector.rb b/lib/service/steps_inspector.rb
index 8a102ac311e..7a830dd439c 100644
--- a/lib/service/steps_inspector.rb
+++ b/lib/service/steps_inspector.rb
@@ -7,27 +7,36 @@
class Service::StepsInspector
# @!visibility private
class Step
- attr_reader :step, :result, :nesting_level
+ COLORS = { red: 31, green: 32, white: 37, gray: 90 }
+
+ attr_reader :step, :result, :nesting_level, :color
delegate :name, :result_key, to: :step
- delegate :failure?, :success?, :error, :raised_exception?, to: :step_result, allow_nil: true
+ delegate :failure?,
+ :success?,
+ :error,
+ :raised_exception?,
+ :skipped?,
+ to: :step_result,
+ allow_nil: true
alias error? failure?
- def self.for(step, result, nesting_level: 0)
+ def self.for(step, result, nesting_level: 0, color: nil)
class_name =
"#{module_parent_name}::#{step.class.name.split("::").last.sub(/^(\w+)Step$/, "\\1")}"
- class_name.constantize.new(step, result, nesting_level: nesting_level)
+ class_name.constantize.new(step, result, nesting_level:, color:)
end
- def initialize(step, result, nesting_level: 0)
+ def initialize(step, result, nesting_level: 0, color: nil)
@step = step
@result = result
@nesting_level = nesting_level
+ @color = color
end
def type
- self.class.name.split("::").last.downcase
+ self.class.name.split("::").last.underscore
end
alias inspect_type type
@@ -40,7 +49,7 @@ class Service::StepsInspector
end
def inspect
- "#{" " * nesting_level}[#{inspect_type}] #{name}#{runtime} #{emoji}".rstrip
+ "#{" " * nesting_level}\e[#{ansi_color}m[#{inspect_type}] #{name}#{runtime}\e[0m #{emoji}".rstrip
end
private
@@ -56,6 +65,7 @@ class Service::StepsInspector
def result_emoji
return "πŸ’₯" if raised_exception?
+ return "⏭️" if skipped?
return "❌" if failure?
return "βœ…" if success?
""
@@ -69,6 +79,13 @@ class Service::StepsInspector
return " <= expected to return true but got false instead" if error?
" <= expected to return false but got true instead"
end
+
+ def ansi_color
+ return color if color
+ return COLORS[:red] if failure?
+ return COLORS[:green] if success?
+ COLORS[:white]
+ end
end
# @!visibility private
@@ -100,11 +117,14 @@ class Service::StepsInspector
# @!visibility private
class Transaction < Step
def steps
- [self, *step.steps.map { Step.for(_1, result, nesting_level: nesting_level + 1).steps }]
+ [
+ self,
+ *step.steps.map { Step.for(_1, result, nesting_level: nesting_level + 1, color:).steps },
+ ]
end
def inspect
- "#{" " * nesting_level}[#{inspect_type}]#{runtime}#{unexpected_result_emoji}"
+ "#{" " * nesting_level}\e[#{ansi_color}m[#{inspect_type}]#{runtime}\e[0m#{unexpected_result_emoji}"
end
end
@@ -126,7 +146,7 @@ class Service::StepsInspector
# @!visibility private
class Lock < Transaction
def inspect
- "#{" " * nesting_level}[#{inspect_type}] #{name}#{runtime} #{emoji}".rstrip
+ "#{" " * nesting_level}\e[#{ansi_color}m[#{inspect_type}] #{name}#{runtime}\e[0m #{emoji}".rstrip
end
def error
@@ -134,6 +154,39 @@ class Service::StepsInspector
end
end
+ # @!visibility private
+ class OnlyIf < Step
+ def steps
+ [
+ self,
+ *step.steps.map do
+ Step.for(_1, result, nesting_level: nesting_level + 1, color: skipped_color).steps
+ end,
+ ]
+ end
+
+ def inspect
+ "#{" " * nesting_level}\e[#{ansi_color}m[#{inspect_type}] #{name}#{runtime}\e[0m #{emoji}#{explanation}".rstrip
+ end
+
+ private
+
+ def explanation
+ return unless skipped?
+ " (condition was not met)"
+ end
+
+ def ansi_color
+ return super unless skipped?
+ COLORS[:white]
+ end
+
+ def skipped_color
+ return unless skipped?
+ COLORS[:gray]
+ end
+ end
+
attr_reader :steps, :result
def initialize(result)
diff --git a/spec/lib/service/steps_inspector_spec.rb b/spec/lib/service/steps_inspector_spec.rb
index 67f33b6478f..873dfac3a20 100644
--- a/spec/lib/service/steps_inspector_spec.rb
+++ b/spec/lib/service/steps_inspector_spec.rb
@@ -27,13 +27,14 @@ RSpec.describe Service::StepsInspector do
end
try { step :might_raise }
+ only_if(:condition) { step :optional_step }
step :final_step
end
subject(:inspector) { described_class.new(result) }
let(:parameter) { "present" }
- let(:result) { DummyService.call(params: { parameter: parameter }) }
+ let(:result) { DummyService.call(params: { parameter: }) }
before do
class DummyService
@@ -43,28 +44,34 @@ RSpec.describe Service::StepsInspector do
in_transaction_step_1
in_transaction_step_2
might_raise
+ condition
+ optional_step
final_step
].each { |name| define_method(name) { true } }
end
end
describe "#execution_flow" do
- subject(:output) { inspector.execution_flow.strip.gsub(%r{ \(\d+\.\d+ ms\)}, "") }
+ subject(:output) do
+ inspector.execution_flow.strip.gsub(%r{ \(\d+\.\d+ ms\)}, "").gsub(/\e\[\d+(;\d+)?m/, "")
+ end
context "when service runs without error" do
it "outputs all the steps of the service" do
expect(output).to eq <<~OUTPUT.chomp
- [ 1/11] [options] default βœ…
- [ 2/11] [model] model βœ…
- [ 3/11] [policy] policy βœ…
- [ 4/11] [params] default βœ…
- [ 5/11] [lock] parameter:other_param βœ…
- [ 6/11] [transaction]
- [ 7/11] [step] in_transaction_step_1 βœ…
- [ 8/11] [step] in_transaction_step_2 βœ…
- [ 9/11] [try]
- [10/11] [step] might_raise βœ…
- [11/11] [step] final_step βœ…
+ [ 1/13] [options] default βœ…
+ [ 2/13] [model] model βœ…
+ [ 3/13] [policy] policy βœ…
+ [ 4/13] [params] default βœ…
+ [ 5/13] [lock] parameter:other_param βœ…
+ [ 6/13] [transaction]
+ [ 7/13] [step] in_transaction_step_1 βœ…
+ [ 8/13] [step] in_transaction_step_2 βœ…
+ [ 9/13] [try]
+ [10/13] [step] might_raise βœ…
+ [11/13] [only_if] condition βœ…
+ [12/13] [step] optional_step βœ…
+ [13/13] [step] final_step βœ…
OUTPUT
end
@@ -84,10 +91,10 @@ RSpec.describe Service::StepsInspector do
it "shows the failing step" do
expect(output).to eq <<~OUTPUT.chomp
- [ 1/11] [options] default βœ…
- [ 2/11] [model] model ❌
+ [ 1/13] [options] default βœ…
+ [ 2/13] [model] model ❌
- (9 more steps not shown as the execution flow was stopped before reaching them)
+ (11 more steps not shown as the execution flow was stopped before reaching them)
OUTPUT
end
end
@@ -103,11 +110,11 @@ RSpec.describe Service::StepsInspector do
it "shows the failing step" do
expect(output).to eq <<~OUTPUT.chomp
- [ 1/11] [options] default βœ…
- [ 2/11] [model] model βœ…
- [ 3/11] [policy] policy ❌
+ [ 1/13] [options] default βœ…
+ [ 2/13] [model] model βœ…
+ [ 3/13] [policy] policy ❌
- (8 more steps not shown as the execution flow was stopped before reaching them)
+ (10 more steps not shown as the execution flow was stopped before reaching them)
OUTPUT
end
end
@@ -117,12 +124,12 @@ RSpec.describe Service::StepsInspector do
it "shows the failing step" do
expect(output).to eq <<~OUTPUT.chomp
- [ 1/11] [options] default βœ…
- [ 2/11] [model] model βœ…
- [ 3/11] [policy] policy βœ…
- [ 4/11] [params] default ❌
+ [ 1/13] [options] default βœ…
+ [ 2/13] [model] model βœ…
+ [ 3/13] [policy] policy βœ…
+ [ 4/13] [params] default ❌
- (7 more steps not shown as the execution flow was stopped before reaching them)
+ (9 more steps not shown as the execution flow was stopped before reaching them)
OUTPUT
end
end
@@ -138,16 +145,16 @@ RSpec.describe Service::StepsInspector do
it "shows the failing step" do
expect(output).to eq <<~OUTPUT.chomp
- [ 1/11] [options] default βœ…
- [ 2/11] [model] model βœ…
- [ 3/11] [policy] policy βœ…
- [ 4/11] [params] default βœ…
- [ 5/11] [lock] parameter:other_param βœ…
- [ 6/11] [transaction]
- [ 7/11] [step] in_transaction_step_1 βœ…
- [ 8/11] [step] in_transaction_step_2 ❌
-
- (3 more steps not shown as the execution flow was stopped before reaching them)
+ [ 1/13] [options] default βœ…
+ [ 2/13] [model] model βœ…
+ [ 3/13] [policy] policy βœ…
+ [ 4/13] [params] default βœ…
+ [ 5/13] [lock] parameter:other_param βœ…
+ [ 6/13] [transaction]
+ [ 7/13] [step] in_transaction_step_1 βœ…
+ [ 8/13] [step] in_transaction_step_2 ❌
+
+ (5 more steps not shown as the execution flow was stopped before reaching them)
OUTPUT
end
end
@@ -163,18 +170,18 @@ RSpec.describe Service::StepsInspector do
it "shows the failing step" do
expect(output).to eq <<~OUTPUT.chomp
- [ 1/11] [options] default βœ…
- [ 2/11] [model] model βœ…
- [ 3/11] [policy] policy βœ…
- [ 4/11] [params] default βœ…
- [ 5/11] [lock] parameter:other_param βœ…
- [ 6/11] [transaction]
- [ 7/11] [step] in_transaction_step_1 βœ…
- [ 8/11] [step] in_transaction_step_2 βœ…
- [ 9/11] [try]
- [10/11] [step] might_raise πŸ’₯
-
- (1 more steps not shown as the execution flow was stopped before reaching them)
+ [ 1/13] [options] default βœ…
+ [ 2/13] [model] model βœ…
+ [ 3/13] [policy] policy βœ…
+ [ 4/13] [params] default βœ…
+ [ 5/13] [lock] parameter:other_param βœ…
+ [ 6/13] [transaction]
+ [ 7/13] [step] in_transaction_step_1 βœ…
+ [ 8/13] [step] in_transaction_step_2 βœ…
+ [ 9/13] [try]
+ [10/13] [step] might_raise πŸ’₯
+
+ (3 more steps not shown as the execution flow was stopped before reaching them)
OUTPUT
end
end
@@ -184,13 +191,41 @@ RSpec.describe Service::StepsInspector do
it "shows the failing step" do
expect(output).to eq <<~OUTPUT.chomp
- [ 1/11] [options] default βœ…
- [ 2/11] [model] model βœ…
- [ 3/11] [policy] policy βœ…
- [ 4/11] [params] default βœ…
- [ 5/11] [lock] parameter:other_param ❌
+ [ 1/13] [options] default βœ…
+ [ 2/13] [model] model βœ…
+ [ 3/13] [policy] policy βœ…
+ [ 4/13] [params] default βœ…
+ [ 5/13] [lock] parameter:other_param ❌
+
+ (8 more steps not shown as the execution flow was stopped before reaching them)
+ OUTPUT
+ end
+ end
- (6 more steps not shown as the execution flow was stopped before reaching them)
+ context "when the only_if step condition is not met" do
+ before do
+ class DummyService
+ def condition
+ false
+ end
+ end
+ end
+
+ it "shows the block was skipped" do
+ expect(output).to eq <<~OUTPUT.chomp
+ [ 1/13] [options] default βœ…
+ [ 2/13] [model] model βœ…
+ [ 3/13] [policy] policy βœ…
+ [ 4/13] [params] default βœ…
+ [ 5/13] [lock] parameter:other_param βœ…
+ [ 6/13] [transaction]
+ [ 7/13] [step] in_transaction_step_1 βœ…
+ [ 8/13] [step] in_transaction_step_2 βœ…
+ [ 9/13] [try]
+ [10/13] [step] might_raise βœ…
+ [11/13] [only_if] condition ⏭️ (condition was not met)
+ [12/13] [step] optional_step
+ [13/13] [step] final_step βœ…
OUTPUT
end
end
@@ -201,17 +236,19 @@ RSpec.describe Service::StepsInspector do
it "adapts its output accordingly" do
expect(output).to eq <<~OUTPUT.chomp
- [ 1/11] [options] default βœ…
- [ 2/11] [model] model βœ…
- [ 3/11] [policy] policy βœ… ⚠️ <= expected to return false but got true instead
- [ 4/11] [params] default βœ…
- [ 5/11] [lock] parameter:other_param βœ…
- [ 6/11] [transaction]
- [ 7/11] [step] in_transaction_step_1 βœ…
- [ 8/11] [step] in_transaction_step_2 βœ…
- [ 9/11] [try]
- [10/11] [step] might_raise βœ…
- [11/11] [step] final_step βœ…
+ [ 1/13] [options] default βœ…
+ [ 2/13] [model] model βœ…
+ [ 3/13] [policy] policy βœ… ⚠️ <= expected to return false but got true instead
+ [ 4/13] [params] default βœ…
+ [ 5/13] [lock] parameter:other_param βœ…
+ [ 6/13] [transaction]
+ [ 7/13] [step] in_transaction_step_1 βœ…
+ [ 8/13] [step] in_transaction_step_2 βœ…
+ [ 9/13] [try]
+ [10/13] [step] might_raise βœ…
+ [11/13] [only_if] condition βœ…
+ [12/13] [step] optional_step βœ…
+ [13/13] [step] final_step βœ…
OUTPUT
end
end
@@ -228,11 +265,11 @@ RSpec.describe Service::StepsInspector do
it "adapts its output accordingly" do
expect(output).to eq <<~OUTPUT.chomp
- [ 1/11] [options] default βœ…
- [ 2/11] [model] model βœ…
- [ 3/11] [policy] policy ❌ ⚠️ <= expected to return true but got false instead
+ [ 1/13] [options] default βœ…
+ [ 2/13] [model] model βœ…
+ [ 3/13] [policy] policy ❌ ⚠️ <= expected to return true but got false instead
- (8 more steps not shown as the execution flow was stopped before reaching them)
+ (10 more steps not shown as the execution flow was stopped before reaching them)
OUTPUT
end
end
@@ -367,15 +404,16 @@ RSpec.describe Service::StepsInspector do
let(:parameter) { nil }
it "outputs the service class name, the steps results and the specific error" do
- expect(inspector.inspect.gsub(%r{ \(\d+\.\d+ ms\)}, "")).to eq(<<~OUTPUT)
+ expect(inspector.inspect.gsub(%r{ \(\d+\.\d+ ms\)}, "").gsub(/\e\[\d+(;\d+)?m/, "")).to eq(
+ <<~OUTPUT,
Inspecting DummyService result object:
- [ 1/11] [options] default βœ…
- [ 2/11] [model] model βœ…
- [ 3/11] [policy] policy βœ…
- [ 4/11] [params] default ❌
+ [ 1/13] [options] default βœ…
+ [ 2/13] [model] model βœ…
+ [ 3/13] [policy] policy βœ…
+ [ 4/13] [params] default ❌
- (7 more steps not shown as the execution flow was stopped before reaching them)
+ (9 more steps not shown as the execution flow was stopped before reaching them)
Why it failed:
@@ -383,6 +421,7 @@ RSpec.describe Service::StepsInspector do
Provided parameters: {"parameter"=>nil, "other_param"=>nil}
OUTPUT
+ )
end
end
end