Skip to content

Commit

Permalink
refactor(cmd): Add Support for Stateful Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jshlbrd committed Oct 3, 2024
1 parent dd96fa3 commit c55ac9e
Showing 1 changed file with 34 additions and 38 deletions.
72 changes: 34 additions & 38 deletions cmd/substation/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,16 @@ func test(ctx context.Context, file string, cfg customConfig) error {

var failedFile bool // Tracks if any test in a file failed.
for _, test := range cfg.Tests {
// setup creates the test environment.
// cnd asserts that the test is successful.
cnd, err := condition.New(ctx, test.Condition)
if err != nil {
fmt.Printf("FAIL\t%s\t[test error]\n", file)

//nolint:nilerr // errors should not disrupt the test.
return nil
}

// setup creates the test messages that are tested.
setup, err := substation.New(ctx, substation.Config{
Transforms: test.Transforms,
})
Expand All @@ -88,6 +97,17 @@ func test(ctx context.Context, file string, cfg customConfig) error {
return nil
}

// tester contains the config that will be tested.
// This has to be done for each test to ensure
// that there is no state shared between tests.
tester, err := substation.New(ctx, cfg.Config)
if err != nil {
fmt.Printf("?\t%s\t[config error]\n", file)

//nolint:nilerr // errors should not disrupt the test.
return nil
}

sMsgs, err := setup.Transform(ctx, message.New().AsControl())
if err != nil {
fmt.Printf("?\t%s\t[test error]\n", file)
Expand All @@ -96,60 +116,36 @@ func test(ctx context.Context, file string, cfg customConfig) error {
return nil
}

cnd, err := condition.New(ctx, test.Condition)
tMsgs, err := tester.Transform(ctx, sMsgs...)
if err != nil {
fmt.Printf("FAIL\t%s\t[test error]\n", file)
fmt.Printf("?\t%s\t[config error]\n", file)

//nolint:nilerr // errors should not disrupt the test.
return nil
}

for _, msg := range sMsgs {
for _, msg := range tMsgs {
// Skip control messages because they contain no data.
if msg.IsControl() {
continue
}

// tester contains the config that will be tested.
// This has to be done for every message to ensure
// that there is no state shared between tests.
tester, err := substation.New(ctx, cfg.Config)
ok, err := cnd.Condition(ctx, msg)
if err != nil {
fmt.Printf("?\t%s\t[config error]\n", file)
fmt.Printf("?\t%s\t[test error]\n", file)

//nolint:nilerr // errors should not disrupt the test.
return nil
}

tMsgs, err := tester.Transform(ctx, msg)
if err != nil {
fmt.Printf("?\t%s\t[config error]\n", file)

//nolint:nilerr // errors should not disrupt the test.
return nil
}

for _, msg := range tMsgs {
if msg.IsControl() {
continue
}
if !ok {
fmt.Printf("%s\n%s\n%s\n",
fmt.Sprintf("--- FAIL: %s", test.Name),
fmt.Sprintf(" message:\t%s", msg),
fmt.Sprintf(" condition:\t%s", cnd),
)

ok, err := cnd.Condition(ctx, msg)
if err != nil {
fmt.Printf("?\t%s\t[test error]\n", file)

//nolint:nilerr // errors should not disrupt the test.
return nil
}

if !ok {
fmt.Printf("%s\n%s\n%s\n",
fmt.Sprintf("--- FAIL: %s", test.Name),
fmt.Sprintf(" message:\t%s", msg),
fmt.Sprintf(" condition:\t%s", cnd),
)

failedFile = true
}
failedFile = true
}
}
}
Expand Down

0 comments on commit c55ac9e

Please sign in to comment.