Skip to content

Commit

Permalink
refactor: don't use FailNow and SkipNow (#2200)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly authored Dec 8, 2024
1 parent 366c1ac commit 0acd1bd
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 30 deletions.
12 changes: 0 additions & 12 deletions pkg/runner/failer/failer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var defaultFailer = New(false)

type Failer interface {
Fail(context.Context)
FailNow(context.Context)
}

type failer struct {
Expand All @@ -30,12 +29,6 @@ func (f failer) Fail(ctx context.Context) {
t.Fail()
}

func (f failer) FailNow(ctx context.Context) {
f.wait()
t := testing.FromContext(ctx)
t.FailNow()
}

func (f failer) wait() {
if f.pause {
fmt.Println("Failure detected, press ENTER to continue...")
Expand All @@ -55,8 +48,3 @@ func Fail(ctx context.Context) {
f := getFailerOrDefault(ctx)
f.Fail(ctx)
}

func FailNow(ctx context.Context) {
f := getFailerOrDefault(ctx)
f.FailNow(ctx)
}
14 changes: 9 additions & 5 deletions pkg/runner/processors/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
)

type StepProcessor interface {
Run(context.Context, namespacer.Namespacer, engine.Context)
Run(context.Context, namespacer.Namespacer, engine.Context) bool
}

func NewStepProcessor(
Expand All @@ -59,7 +59,7 @@ type stepProcessor struct {
basePath string
}

func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespacer, tc engine.Context) {
func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespacer, tc engine.Context) bool {
t := testing.FromContext(ctx)
report := &model.StepReport{
Name: p.step.Name,
Expand All @@ -86,7 +86,8 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
tc, err := setupContextAndBindings(ctx, tc, contextData, p.step.Bindings...)
if err != nil {
logging.Log(ctx, logging.Internal, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
failer.FailNow(ctx)
failer.Fail(ctx)
return true
}
cleaner := cleaner.New(tc.Timeouts().Cleanup.Duration, tc.DelayBeforeCleanup(), tc.DeletionPropagation())
t.Cleanup(func() {
Expand Down Expand Up @@ -195,22 +196,25 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
operations, err := p.tryOperation(operationTc.Compilers(), i, namespacer, operationTc.Bindings(), operation, cleaner)
if err != nil {
logger.Log(logging.Try, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
failer.FailNow(ctx)
failer.Fail(ctx)
return true
}
for _, operation := range operations {
outputs, err := operation.execute(ctx, operationTc, report)
if err != nil {
if continueOnError {
failer.Fail(ctx)
} else {
failer.FailNow(ctx)
failer.Fail(ctx)
return true
}
}
for k, v := range outputs {
tc = tc.WithBinding(ctx, k, v)
}
}
}
return false
}

func (p *stepProcessor) tryOperation(compilers compilers.Compilers, id int, namespacer namespacer.Namespacer, bindings apis.Bindings, handler v1alpha1.Operation, cleaner cleaner.CleanerCollector) ([]operation, error) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/runner/processors/step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func TestStepProcessor_Run(t *testing.T) {
basePath string
terminationGracePeriod *metav1.Duration
stepSpec v1alpha1.TestStep
want bool
expectedFail bool
skipped bool
}{{
name: "test with no handler",
client: &fake.FakeClient{},
Expand Down Expand Up @@ -799,7 +799,8 @@ func TestStepProcessor_Run(t *testing.T) {
Error: &config.Spec.Timeouts.Error,
Exec: &config.Spec.Timeouts.Exec,
})
stepProcessor.Run(ctx, tc.namespacer, tcontext)
got := stepProcessor.Run(ctx, tc.namespacer, tcontext)
assert.Equal(t, tc.want, got)
if tc.expectedFail {
assert.True(t, nt.FailedVar, "expected an error but got none")
} else {
Expand Down
22 changes: 15 additions & 7 deletions pkg/runner/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ func runTest(
if err != nil {
logging.Log(ctx, logging.Internal, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
tc.IncFailed()
failer.FailNow(ctx)
failer.Fail(ctx)
return
}
contextData := processors.ContextData{
BasePath: test.BasePath,
Expand All @@ -105,14 +106,17 @@ func runTest(
tc, err = processors.SetupContext(ctx, tc, contextData)
if err != nil {
logging.Log(ctx, logging.Internal, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
failer.FailNow(ctx)
failer.Fail(ctx)
return
}
// skip checks
if test.Test.Spec.Skip != nil && *test.Test.Spec.Skip {
t.SkipNow()
t.Skip()
return
}
if tc.FailFast() && tc.Failed() > 0 {
t.SkipNow()
t.Skip()
return
}
// setup cleaner
mainCleaner := cleaner.New(tc.Timeouts().Cleanup.Duration, nil, tc.DeletionPropagation())
Expand Down Expand Up @@ -169,7 +173,8 @@ func runTest(
nsTc, namespace, err := processors.SetupNamespace(ctx, tc, namespaceData)
if err != nil {
logging.Log(ctx, logging.Internal, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
failer.FailNow(ctx)
failer.Fail(ctx)
return
}
tc = nsTc
if namespace != nil {
Expand All @@ -183,7 +188,8 @@ func runTest(
tc, err = processors.SetupBindings(ctx, tc, test.Test.Spec.Bindings...)
if err != nil {
logging.Log(ctx, logging.Internal, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
failer.FailNow(ctx)
failer.Fail(ctx)
return
}
// run steps
for i, step := range test.Test.Spec.Steps {
Expand All @@ -197,6 +203,8 @@ func runTest(
}
tc := tc.WithBinding(ctx, "step", info)
processor := processors.NewStepProcessor(step, report, test.BasePath)
processor.Run(ctx, nspacer, tc)
if stop := processor.Run(ctx, nspacer, tc); stop {
return
}
}
}
5 changes: 3 additions & 2 deletions pkg/runner/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func runTests(ctx context.Context, t testing.TTest, clock clock.PassiveClock, ns
if err != nil {
logging.Log(ctx, logging.Internal, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
tc.IncFailed()
failer.FailNow(ctx)
failer.Fail(ctx)
return
}
tc = nsTc
if namespace != nil {
Expand All @@ -53,7 +54,7 @@ func runTests(ctx context.Context, t testing.TTest, clock clock.PassiveClock, ns
if err != nil {
logging.Log(ctx, logging.Internal, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
tc.IncFailed()
failer.FailNow(ctx)
failer.Fail(ctx)
} else {
testId := i + 1
if len(test.Test.Spec.Scenarios) == 0 {
Expand Down
2 changes: 0 additions & 2 deletions pkg/testing/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ type TTest interface {
Error(args ...any)
Errorf(format string, args ...any)
Fail()
FailNow()
Failed() bool
Fatal(args ...any)
Fatalf(format string, args ...any)
Expand All @@ -33,7 +32,6 @@ type TTest interface {
Run(name string, f func(t *T)) bool
Setenv(key, value string)
Skip(args ...any)
SkipNow()
Skipf(format string, args ...any)
Skipped() bool
TempDir() string
Expand Down

0 comments on commit 0acd1bd

Please sign in to comment.