From 5c7a2a3cd2ec78166ee6cb3f8939732917cf286a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Hedenstro=CC=88m?= <erik@hedenstroem.com> Date: Mon, 20 May 2024 01:01:59 +0200 Subject: [PATCH] Added args to handlers. Added common handlers. --- helpers.go | 28 ++++++++++++++++++++++++++++ snok.go | 13 ++++++++----- snok_test.go | 6 ++++-- types.go | 7 ++++++- 4 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 helpers.go diff --git a/helpers.go b/helpers.go new file mode 100644 index 0000000..adc5d2d --- /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 5400ba5..c7397fe 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 cccc774..2170acd 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 c64efe6..b35aefa 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. -- GitLab