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.rbindex dff18ba1967..840aee77aba 100644--- a/lib/service/base.rb+++ b/lib/service/base.rb@@ -129,6 +129,10 @@ module Servicedef try(*exceptions, &block)steps << TryStep.new(exceptions, &block)end++ def only_if(name, &block)+ steps << OnlyIfStep.new(name, &block)+ endend# @!visibility private@@ -173,7 +177,7 @@ module Serviceenddef type- self.class.name.split("::").last.downcase.sub(/^(\w+)step$/, "\\1")+ self.class.name.split("::").last.underscore.sub(/^(\w+)_step$/, "\\1")enddef with_runtime@@ -346,6 +350,24 @@ module Serviceendend+ # @!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 privateclass OptionsStep < Stepdef run_stepdiff --git a/lib/service/steps_inspector.rb b/lib/service/steps_inspector.rbindex 8a102ac311e..7a830dd439c 100644--- a/lib/service/steps_inspector.rb+++ b/lib/service/steps_inspector.rb@@ -7,27 +7,36 @@class Service::StepsInspector# @!visibility privateclass Step- attr_reader :step, :result, :nesting_level+ COLORS = { red: 31, green: 32, white: 37, gray: 90 }++ attr_reader :step, :result, :nesting_level, :colordelegate :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: truealias 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 = colorenddef type- self.class.name.split("::").last.downcase+ self.class.name.split("::").last.underscoreendalias inspect_type type@@ -40,7 +49,7 @@ class Service::StepsInspectorenddef inspect- "#{" " * nesting_level}[#{inspect_type}] #{name}#{runtime} #{emoji}".rstrip+ "#{" " * nesting_level}\e[#{ansi_color}m[#{inspect_type}] #{name}#{runtime}\e[0m #{emoji}".rstripendprivate@@ -56,6 +65,7 @@ class Service::StepsInspectordef result_emojireturn "π₯" if raised_exception?+ return "βοΈ" if skipped?return "β" if failure?return "β " if success?""@@ -69,6 +79,13 @@ class Service::StepsInspectorreturn " <= 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]+ endend# @!visibility private@@ -100,11 +117,14 @@ class Service::StepsInspector# @!visibility privateclass Transaction < Stepdef 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 },+ ]enddef inspect- "#{" " * nesting_level}[#{inspect_type}]#{runtime}#{unexpected_result_emoji}"+ "#{" " * nesting_level}\e[#{ansi_color}m[#{inspect_type}]#{runtime}\e[0m#{unexpected_result_emoji}"endend@@ -126,7 +146,7 @@ class Service::StepsInspector# @!visibility privateclass Lock < Transactiondef inspect- "#{" " * nesting_level}[#{inspect_type}] #{name}#{runtime} #{emoji}".rstrip+ "#{" " * nesting_level}\e[#{ansi_color}m[#{inspect_type}] #{name}#{runtime}\e[0m #{emoji}".rstripenddef error@@ -134,6 +154,39 @@ class Service::StepsInspectorendend+ # @!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, :resultdef initialize(result)diff --git a/spec/lib/service/steps_inspector_spec.rb b/spec/lib/service/steps_inspector_spec.rbindex 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 doendtry { step :might_raise }+ only_if(:condition) { step :optional_step }step :final_stependsubject(:inspector) { described_class.new(result) }let(:parameter) { "present" }- let(:result) { DummyService.call(params: { parameter: parameter }) }+ let(:result) { DummyService.call(params: { parameter: }) }before doclass DummyService@@ -43,28 +44,34 @@ RSpec.describe Service::StepsInspector doin_transaction_step_1in_transaction_step_2might_raise+ condition+ optional_stepfinal_step].each { |name| define_method(name) { true } }endenddescribe "#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/, "")+ endcontext "when service runs without error" doit "outputs all the steps of the service" doexpect(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 βOUTPUTend@@ -84,10 +91,10 @@ RSpec.describe Service::StepsInspector doit "shows the failing step" doexpect(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)OUTPUTendend@@ -103,11 +110,11 @@ RSpec.describe Service::StepsInspector doit "shows the failing step" doexpect(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)OUTPUTendend@@ -117,12 +124,12 @@ RSpec.describe Service::StepsInspector doit "shows the failing step" doexpect(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)OUTPUTendend@@ -138,16 +145,16 @@ RSpec.describe Service::StepsInspector doit "shows the failing step" doexpect(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)OUTPUTendend@@ -163,18 +170,18 @@ RSpec.describe Service::StepsInspector doit "shows the failing step" doexpect(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)OUTPUTendend@@ -184,13 +191,41 @@ RSpec.describe Service::StepsInspector doit "shows the failing step" doexpect(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 βOUTPUTendend@@ -201,17 +236,19 @@ RSpec.describe Service::StepsInspector doit "adapts its output accordingly" doexpect(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 βOUTPUTendend@@ -228,11 +265,11 @@ RSpec.describe Service::StepsInspector doit "adapts its output accordingly" doexpect(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)OUTPUTendend@@ -367,15 +404,16 @@ RSpec.describe Service::StepsInspector dolet(: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 doProvided parameters: {"parameter"=>nil, "other_param"=>nil}OUTPUT+ )endendend