Skip to content

Commit

Permalink
Adding DAG testing draft
Browse files Browse the repository at this point in the history
  • Loading branch information
merschformann committed Mar 15, 2024
1 parent 3020e57 commit 1f83ad9
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions golden/dag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package golden

import "testing"

type DagTestCase struct {

Check warning on line 5 in golden/dag.go

View workflow job for this annotation

GitHub Actions / sdk-go-lint (./)

exported: exported type DagTestCase should have comment or be unexported (revive)

Check warning on line 5 in golden/dag.go

View workflow job for this annotation

GitHub Actions / sdk-go-lint (./)

exported: exported type DagTestCase should have comment or be unexported (revive)
name string
needs []string
config BashConfig
path string
}

// DagTest runs a set of test cases in topological order.
// Each test case is a BashTest, and the test cases are connected by their
// dependencies. If a test case has dependencies, it will only be run after all
// of its dependencies have been run.
//
// Sample usage:
//
// cases := []golden.DagTestCase{
// {
// name: "app-create",
// needs: []string{},
// config: BashConfig{ /**/ },
// path: "app-create",
// },
// {
// name: "app-push",
// needs: []string{"app-create"},
// config: BashConfig{ /**/ },
// path: "app-push",
// },
// }
// golden.DagTest(t, cases)
func DagTest(t *testing.T, cases []DagTestCase) {
open := cases
done := make(map[string]bool)

for len(open) > 0 {
// Pick the first case from the open list that has all its needs met.
var next DagTestCase
for _, c := range open {
ready := true
for _, need := range c.needs {
if !done[need] {
ready = false
break
}
}
if ready {
next = c
break
}
}

// If we didn't find a case to run, we have a cycle.
if next.name == "" {
t.Fatal("cycle detected")
}

// Run the case and mark it as done.
t.Run(next.name, func(t *testing.T) {
// Run the test case.
BashTest(t, next.path, next.config)
})
done[next.name] = true

// Remove the case from the open list.
for i, c := range open {
if c.name == next.name {
open = append(open[:i], open[i+1:]...)
break
}
}
}
}

0 comments on commit 1f83ad9

Please sign in to comment.