diff --git a/helpers.go b/helpers.go new file mode 100644 index 0000000000000000000000000000000000000000..adc5d2d8e472cae569ad2052733837b22486b0a6 --- /dev/null +++ b/helpers.go @@ -0,0 +1,28 @@ +package snok + +import ( + "os" + "sort" + "testing" + + "github.com/stretchr/testify/require" +) + +func PrintEnv(tests *Tests, t *testing.T, args []string) (err error) { + envs := os.Environ() + sort.Strings(envs) + for _, env := range envs { + t.Log(env) + } + return +} + +func TempFile(tests *Tests, t *testing.T, args []string) (err error) { + file, err := os.CreateTemp("", "test_") + require.NoError(t, err) + t.Cleanup(func() { + os.Remove(file.Name()) + }) + t.Setenv(args[0], file.Name()) + return +} diff --git a/snok.go b/snok.go index 5400ba5aa4d7f5b2e60776b87d2775134ff14052..c7397fe9706a86253ac3bee591869c6496bd1861 100644 --- a/snok.go +++ b/snok.go @@ -61,14 +61,16 @@ func (ct *Tests) Accept(log testcontainers.Log) { ct.t.Logf("%s", log.Content) } -func (tests *Tests) AddHelper(name string, fn func(*Tests, *testing.T) error, needs ...string) { - tests.graph.AddNode(&helper{ +func (tests *Tests) AddHelper(name string, fn func(*Tests, *testing.T, []string) error, needs ...string) *helper { + helper := &helper{ node: node{ Name: name, Needs: needs, }, fn: fn, - }) + } + tests.graph.AddNode(helper) + return helper } func (tests *Tests) addTests(t *testing.T, cmd *cobra.Command, cmds []string) { @@ -96,7 +98,7 @@ func (tests *Tests) calculateExecutionOrder(t *testing.T) []graph.Node { if node, ok := n.(Node); ok { for _, id := range node.DependencyIDs() { dependency, new := tests.graph.NodeWithID(id) - require.False(t, new, "Unknown dependency for %s", node) + require.False(t, new, "'%s' has invalid needs", node) tests.graph.SetEdge(simple.Edge{F: dependency, T: node}) } } @@ -114,7 +116,8 @@ func (tests *Tests) Run(t *testing.T) { nodes := tests.calculateExecutionOrder(t) for _, node := range nodes { if helper, ok := node.(*helper); ok { - err := helper.fn(tests, t) + fmt.Println("Running helper: ", helper.Name) + err := helper.fn(tests, t, helper.args) require.NoError(t, err, "Failed to execute helper: %s", helper) } if test, ok := node.(*test); ok { diff --git a/snok_test.go b/snok_test.go index cccc774f587a384bc90c4710acb7a8244dac9bd1..2170acd7da8c46918d4e527f0f7c096cdf064d8a 100644 --- a/snok_test.go +++ b/snok_test.go @@ -30,6 +30,8 @@ func TestMain(m *testing.M) { echoCmd.Flags().BoolP("reverse", "r", false, "reverse the output") tests = NewTestSuite(rootCmd) tests.unexpectedErr = mockUnexpectedError + tests.AddHelper("PrintEnv", PrintEnv) + tests.AddHelper("TempFile", TempFile).SetArgs("TEMP_FILE") tests.AddHelper("TestContainer", mockContainer) tests.AddHelper("PatchURL", patchTestInput, "TestContainer") os.Exit(m.Run()) @@ -39,7 +41,7 @@ func TestCmds(t *testing.T) { tests.Run(t) } -func mockContainer(tests *Tests, t *testing.T) error { +func mockContainer(tests *Tests, t *testing.T, args []string) error { if os.Getenv("MOCKSERVER_URL") == "" { ctx := context.Background() opts := []testcontainers.ContainerCustomizer{ @@ -61,7 +63,7 @@ func mockContainer(tests *Tests, t *testing.T) error { return nil } -func patchTestInput(tests *Tests, t *testing.T) error { +func patchTestInput(tests *Tests, t *testing.T, args []string) error { _, err := tests.GetTest("TestContainer") require.Error(t, err) _, err = tests.GetTest("Does not exist") diff --git a/types.go b/types.go index c64efe60f256d1ddb15c5ae880c691d65bd417a0..b35aefa332f065abc5a4374f0cb2f22e3738fe72 100644 --- a/types.go +++ b/types.go @@ -53,7 +53,12 @@ func (n *node) String() string { // helper node in the graph. type helper struct { node - fn func(*Tests, *testing.T) error + fn func(*Tests, *testing.T, []string) error + args []string +} + +func (h *helper) SetArgs(args ...string) { + h.args = args } // test node in the graph.