From 04832374cabfb02c0bd903391c49d8030ae5ff1d Mon Sep 17 00:00:00 2001 From: softwaredevelop <61334390+softwaredevelop@users.noreply.github.com> Date: Mon, 12 Aug 2024 19:21:27 +0200 Subject: [PATCH] test: Add unit test for Shellcheck function in main_test.go --- shellcheck/main_test.go | 67 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 shellcheck/main_test.go diff --git a/shellcheck/main_test.go b/shellcheck/main_test.go new file mode 100644 index 0000000..9c45707 --- /dev/null +++ b/shellcheck/main_test.go @@ -0,0 +1,67 @@ +package main_test + +import ( + "context" + "flag" + "os" + "testing" + + "dagger.io/dagger" + "github.com/stretchr/testify/require" +) + +var c *dagger.Client + +func TestMain(m *testing.M) { + flag.Parse() + + ctx := context.Background() + + c, _ = dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr)) + defer c.Close() + + code := m.Run() + defer c.Close() + os.Exit(code) +} + +func Test_Shellcheck(t *testing.T) { + t.Parallel() + ctx := context.Background() + + t.Run("Test_mounted_host_directory_check", func(t *testing.T) { + t.Parallel() + container := base() + require.NotNil(t, container) + + _, err := container. + WithMountedDirectory("/tmp", c.Host().Directory("./test/testdata")). + WithWorkdir("/tmp"). + WithExec([]string{"sh", "-c", "find . -type f -name '*.sh' -print0 | xargs -0 shellcheck"}). + Stdout(ctx) + require.Error(t, err) + // require.Contains(t, err.Error(), "SC2283") + errorIDs := []string{"SC2283", "SC2154", "SC2086"} + for _, id := range errorIDs { + require.Contains(t, err.Error(), id) + } + }) + t.Run("Test_shellcheck_version", func(t *testing.T) { + t.Parallel() + container := base() + require.NotNil(t, container) + + out, err := container. + WithExec([]string{"shellcheck", "--version"}). + Stdout(ctx) + require.NoError(t, err) + require.Contains(t, out, "version") + }) +} + +func base() *dagger.Container { + return c. + Container(). + From("koalaman/shellcheck-alpine:latest"). + WithoutEntrypoint() +}