diff --git a/helpers.go b/helpers.go index adc5d2d8e472cae569ad2052733837b22486b0a6..7354dc7dcf3967847a0bb540d0460c5f7e8272e1 100644 --- a/helpers.go +++ b/helpers.go @@ -23,6 +23,7 @@ func TempFile(tests *Tests, t *testing.T, args []string) (err error) { t.Cleanup(func() { os.Remove(file.Name()) }) + t.Logf("TempFile: %s=%s", args[0], file.Name()) t.Setenv(args[0], file.Name()) return } diff --git a/snok.go b/snok.go index c7397fe9706a86253ac3bee591869c6496bd1861..756ab2fdb696eb5e44f2ffb8e8bf782cbd39a603 100644 --- a/snok.go +++ b/snok.go @@ -48,12 +48,12 @@ func NewTestSuite(cmd *cobra.Command) *Tests { func (ct *Tests) GetTest(name string) (*test, error) { node, new := ct.graph.NodeWithID(hash(name)) if new { - return nil, fmt.Errorf("Undefined test: %s", name) + return nil, fmt.Errorf("undefined test: %s", name) } if test, ok := node.(*test); ok { return test, nil } - return nil, fmt.Errorf("Node type is not test: %s", name) + return nil, fmt.Errorf("node type is not test: %s", name) } func (ct *Tests) Accept(log testcontainers.Log) { @@ -99,6 +99,7 @@ func (tests *Tests) calculateExecutionOrder(t *testing.T) []graph.Node { for _, id := range node.DependencyIDs() { dependency, new := tests.graph.NodeWithID(id) require.False(t, new, "'%s' has invalid needs", node) + dependency.(Node).SetNeeded(true) tests.graph.SetEdge(simple.Edge{F: dependency, T: node}) } } @@ -115,8 +116,7 @@ func (tests *Tests) Run(t *testing.T) { tests.addTests(t, tests.RootCmd, []string{}) nodes := tests.calculateExecutionOrder(t) for _, node := range nodes { - if helper, ok := node.(*helper); ok { - fmt.Println("Running helper: ", helper.Name) + if helper, ok := node.(*helper); ok && helper.needed { err := helper.fn(tests, t, helper.args) require.NoError(t, err, "Failed to execute helper: %s", helper) } diff --git a/snok_test.go b/snok_test.go index 2170acd7da8c46918d4e527f0f7c096cdf064d8a..2625dfc195cd644739e45b09d459981c22781900 100644 --- a/snok_test.go +++ b/snok_test.go @@ -86,6 +86,7 @@ var rootCmd = &cobra.Command{ "tests": `[ { "name": "Version", + "needs": ["PrintEnv","TempFile"], "args": ["--version"], "output": "/^test version \\d+\\.\\d+\\.\\d+$/" }, diff --git a/types.go b/types.go index b35aefa332f065abc5a4374f0cb2f22e3738fe72..bd5597f8967b6f94139e4454e9c45b8cc70e7107 100644 --- a/types.go +++ b/types.go @@ -19,15 +19,18 @@ func hash(s string) int64 { // integer IDs of nodes that it depends on. type Node interface { graph.Node + // Needed sets the needed flag to true or false. + SetNeeded(bool) // DependencyIDs returns a list of graph-unique integer ID for each dependency. DependencyIDs() []int64 } // node in the graph. helper and test inherit from this. type node struct { - id *int64 - Name string `json:"name,omitempty"` - Needs []string `json:"needs,omitempty"` + id *int64 + Name string `json:"name,omitempty"` + Needs []string `json:"needs,omitempty"` + needed bool } func (n *node) ID() int64 { @@ -38,6 +41,10 @@ func (n *node) ID() int64 { return *n.id } +func (n *node) SetNeeded(needed bool) { + n.needed = needed +} + func (n *node) DependencyIDs() []int64 { needs := make([]int64, len(n.Needs)) for i, need := range n.Needs {