From f974c5312d0f4239f79b3238766e7f7593c81475 Mon Sep 17 00:00:00 2001 From: Piotr Nestorow Date: Wed, 22 May 2024 15:36:58 +0200 Subject: [PATCH] Programmatic skip scenario feature (#2502) * Use local gauge-proto Signed-off-by: Piotr Nestorow * Handle skip scenario during execution Signed-off-by: Piotr Nestorow * Handle message when skip scenario Signed-off-by: Piotr Nestorow * Handle skip scenario message Signed-off-by: Piotr Nestorow * Golang version update Signed-off-by: Piotr Nestorow * Update using gauge-proto from git Signed-off-by: Piotr Nestorow --------- Signed-off-by: Piotr Nestorow --- execution/result/scenarioResult.go | 8 ++++++ execution/result/stepResult.go | 4 +++ execution/scenarioExecutor.go | 40 ++++++++++++++++++++++++++++-- execution/stepExecutor.go | 5 ++++ go.mod | 2 +- go.sum | 4 +-- 6 files changed, 58 insertions(+), 5 deletions(-) diff --git a/execution/result/scenarioResult.go b/execution/result/scenarioResult.go index 40fa5a28f..ec582df03 100644 --- a/execution/result/scenarioResult.go +++ b/execution/result/scenarioResult.go @@ -31,6 +31,14 @@ func (s ScenarioResult) GetFailed() bool { return s.ProtoScenario.GetExecutionStatus() == gauge_messages.ExecutionStatus_FAILED } +func (s ScenarioResult) SetSkippedScenario() { + s.ProtoScenario.ExecutionStatus = gauge_messages.ExecutionStatus_SKIPPED +} + +func (s ScenarioResult) GetSkippedScenario() bool { + return s.ProtoScenario.GetExecutionStatus() == gauge_messages.ExecutionStatus_SKIPPED +} + func (s ScenarioResult) AddItems(protoItems []*gauge_messages.ProtoItem) { s.ProtoScenario.ScenarioItems = append(s.ProtoScenario.ScenarioItems, protoItems...) } diff --git a/execution/result/stepResult.go b/execution/result/stepResult.go index 2983e61f6..f6165f201 100644 --- a/execution/result/stepResult.go +++ b/execution/result/stepResult.go @@ -56,6 +56,10 @@ func (s *StepResult) GetStepFailed() bool { return s.StepFailed } +func (s *StepResult) GetSkippedScenario() bool { + return s.ProtoStep.StepExecutionResult.ExecutionResult.GetSkipScenario() +} + // GetStackTrace returns the stacktrace for step failure func (s *StepResult) GetStackTrace() string { return s.ProtoStep.GetStepExecutionResult().GetExecutionResult().GetStackTrace() diff --git a/execution/scenarioExecutor.go b/execution/scenarioExecutor.go index 8eace7b48..e546eb144 100644 --- a/execution/scenarioExecutor.go +++ b/execution/scenarioExecutor.go @@ -73,18 +73,25 @@ func (e *scenarioExecutor) execute(i gauge.Item, r result.Result) { } e.notifyBeforeScenarioHook(scenarioResult) - if !scenarioResult.GetFailed() { + if !(scenarioResult.GetFailed() || scenarioResult.GetSkippedScenario()) { protoContexts := scenarioResult.ProtoScenario.GetContexts() protoScenItems := scenarioResult.ProtoScenario.GetScenarioItems() // context and steps are not appended together since sometime it cause the issue and the steps in step list and proto step list differs. // This is done to fix https://github.com/getgauge/gauge/issues/1629 if e.executeSteps(e.contexts, protoContexts, scenarioResult) { - e.executeSteps(scenario.Steps, protoScenItems, scenarioResult) + if !scenarioResult.GetSkippedScenario() { + e.executeSteps(scenario.Steps, protoScenItems, scenarioResult) + } } // teardowns are not appended to previous call to executeSteps to ensure they are run irrespective of context/step failure e.executeSteps(e.teardowns, scenarioResult.ProtoScenario.GetTearDownSteps(), scenarioResult) } + if scenarioResult.GetSkippedScenario() { + e.skippedScenarioUpdateErrMap(i, r) + setSkipInfoInResult(scenarioResult, scenario, e.errMap) + } + e.notifyAfterScenarioHook(scenarioResult) scenarioResult.UpdateExecutionTime() } @@ -123,6 +130,10 @@ func (e *scenarioExecutor) notifyBeforeScenarioHook(scenarioResult *result.Scena setScenarioFailure(e.currentExecutionInfo) handleHookFailure(scenarioResult, res, result.AddPreHook) } + if res.GetSkipScenario() { + scenarioResult.SetSkippedScenario() + scenarioResult.ProtoScenario.PreHookMessages = []string{res.ErrorMessage} + } message.ScenarioExecutionStartingRequest.ScenarioResult = gauge.ConvertToProtoScenarioResult(scenarioResult) e.pluginHandler.NotifyPlugins(message) } @@ -205,6 +216,11 @@ func (e *scenarioExecutor) executeSteps(steps []*gauge.Step, protoItems []*gauge return false } } + if scenarioResult.GetSkippedScenario() { + // The step execution resulted in SkipScenario. + // The rest of steps execution is skipped + break; + } } } return true @@ -222,6 +238,9 @@ func (e *scenarioExecutor) executeStep(step *gauge.Step, protoItem *gauge_messag se := &stepExecutor{runner: e.runner, pluginHandler: e.pluginHandler, currentExecutionInfo: e.currentExecutionInfo, stream: e.stream} res := se.executeStep(step, protoItem.GetStep()) protoItem.GetStep().StepExecutionResult = res.ProtoStepExecResult() + if res.ProtoStepExecResult().ExecutionResult.GetSkipScenario() { + scenarioResult.SetSkippedScenario() + } failed = res.GetFailed() recoverable = res.ProtoStepExecResult().GetExecutionResult().GetRecoverableError() } @@ -262,6 +281,11 @@ func (e *scenarioExecutor) executeConcept(item *gauge.Step, protoConcept *gauge_ return cptResult } + if scenarioResult.GetSkippedScenario() { + // The step execution resulted in SkipScenario. + // The rest of steps execution is skipped + break; + } } } cptResult.UpdateConceptExecResult() @@ -296,3 +320,15 @@ func setScenarioFailure(executionInfo *gauge_messages.ExecutionInfo) { setSpecFailure(executionInfo) executionInfo.CurrentScenario.IsFailed = true } + +func (e *scenarioExecutor) skippedScenarioUpdateErrMap(i gauge.Item, r result.Result) { + scenario := i.(*gauge.Scenario) + scenarioResult := r.(*result.ScenarioResult) + if len(scenarioResult.ProtoScenario.PreHookMessages) > 0 { + e.errMap.ScenarioErrs[scenario] = append([]error{errors.New(scenarioResult.ProtoScenario.PreHookMessages[0])}, e.errMap.ScenarioErrs[scenario]...) + scenarioResult.ProtoScenario.SkipErrors = scenarioResult.ProtoScenario.PreHookMessages + } else { + e.errMap.ScenarioErrs[scenario] = append([]error{errors.New(e.currentExecutionInfo.CurrentStep.ErrorMessage)}, e.errMap.ScenarioErrs[scenario]...) + scenarioResult.ProtoScenario.SkipErrors = []string{e.currentExecutionInfo.CurrentStep.ErrorMessage} + } +} diff --git a/execution/stepExecutor.go b/execution/stepExecutor.go index bd9d7b6fc..0010d472e 100644 --- a/execution/stepExecutor.go +++ b/execution/stepExecutor.go @@ -46,6 +46,9 @@ func (e *stepExecutor) executeStep(step *gauge.Step, protoStep *gauge_messages.P e.currentExecutionInfo.CurrentStep.StackTrace = stepExecutionStatus.GetStackTrace() setStepFailure(e.currentExecutionInfo) stepResult.SetStepFailure() + } else if stepResult.GetSkippedScenario() { + e.currentExecutionInfo.CurrentStep.ErrorMessage = stepExecutionStatus.GetErrorMessage() + e.currentExecutionInfo.CurrentStep.StackTrace = stepExecutionStatus.GetStackTrace() } stepResult.SetProtoExecResult(stepExecutionStatus) } @@ -95,3 +98,5 @@ func (e *stepExecutor) notifyAfterStepHook(stepResult *result.StepResult) { m.StepExecutionEndingRequest.StepResult = gauge.ConvertToProtoStepResult(stepResult) e.pluginHandler.NotifyPlugins(m) } + + diff --git a/go.mod b/go.mod index af0ddb011..51807b78a 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/daviddengcn/go-colortext v1.0.0 github.com/fsnotify/fsnotify v1.7.0 github.com/getgauge/common v0.0.0-20240331100109-225c78ec8f30 - github.com/getgauge/gauge-proto/go/gauge_messages v0.0.0-20240331094732-ac276d4db3b9 + github.com/getgauge/gauge-proto/go/gauge_messages v0.0.0-20240402072853-303b17b7c486 github.com/golang/protobuf v1.5.4 github.com/magiconair/properties v1.8.7 github.com/natefinch/lumberjack v2.0.0+incompatible diff --git a/go.sum b/go.sum index 770b14aa4..53a285c66 100644 --- a/go.sum +++ b/go.sum @@ -14,8 +14,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/getgauge/common v0.0.0-20240331100109-225c78ec8f30 h1:pGrxY3IZb/1wwlSUSKYplmQ9kEh4rRpcAvQYI2WcxX0= github.com/getgauge/common v0.0.0-20240331100109-225c78ec8f30/go.mod h1:q7UW1tDojJwCQPUHaE1ny71IZIDuKlsgh3Tyr5wAZDk= -github.com/getgauge/gauge-proto/go/gauge_messages v0.0.0-20240331094732-ac276d4db3b9 h1:g3Qhoj4ho3txgMcOJ3ivvEhfahaR6t8XTkf5K5M1VSA= -github.com/getgauge/gauge-proto/go/gauge_messages v0.0.0-20240331094732-ac276d4db3b9/go.mod h1:ZOT57PjvIqY31eGcwhj/LSi/K6ULBE1AhFcIMzWkmPg= +github.com/getgauge/gauge-proto/go/gauge_messages v0.0.0-20240402072853-303b17b7c486 h1:kb/Ey0fFX+EHPmFcOEZDa0s4bgsRpRF8iDJEcbZPgLU= +github.com/getgauge/gauge-proto/go/gauge_messages v0.0.0-20240402072853-303b17b7c486/go.mod h1:ZOT57PjvIqY31eGcwhj/LSi/K6ULBE1AhFcIMzWkmPg= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/golangplus/bytes v0.0.0-20160111154220-45c989fe5450/go.mod h1:Bk6SMAONeMXrxql8uvOKuAZSu8aM5RUGv+1C6IJaEho=