From f037fecdd2da5a47aca44ac306352dfc6f47a6a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Hedenstr=C3=B6m?= <erik@hedenstroem.com> Date: Sat, 18 May 2024 19:45:07 +0000 Subject: [PATCH] Added support for strings and regexps in output --- .sonarlint/connectedMode.json | 4 ++ snok.go | 76 +++++++++++++++++++++-------------- snok_test.go | 10 ++--- 3 files changed, 54 insertions(+), 36 deletions(-) create mode 100644 .sonarlint/connectedMode.json diff --git a/.sonarlint/connectedMode.json b/.sonarlint/connectedMode.json new file mode 100644 index 0000000..46281cb --- /dev/null +++ b/.sonarlint/connectedMode.json @@ -0,0 +1,4 @@ +{ + "sonarQubeUri": "https://sonar.hedenstroem.com", + "projectKey": "go-snok" +} \ No newline at end of file diff --git a/snok.go b/snok.go index ccd27b9..7a1328a 100644 --- a/snok.go +++ b/snok.go @@ -23,8 +23,8 @@ import ( type test struct { Name string `json:"name,omitempty"` Args []string `json:"args,omitempty"` - Input *string `json:"input,omitempty"` // Todo: add support for URIs - Output *string `json:"output,omitempty"` // Todo: support regex and strings + Input *string `json:"input,omitempty"` // Todo: add support for URIs + Output *string `json:"output,omitempty"` ExpectError bool `json:"expectError,omitempty"` } @@ -75,18 +75,7 @@ func (ct *CommandTest) testCmd(t *testing.T, cmd *cobra.Command, args []string) args = append(args, cmd.Name()) containers, exists := cmd.Annotations["containers"] if exists { - for _, container := range strings.Split(containers, ",") { - key := strings.TrimFunc(container, unicode.IsSpace) - if c, ok := ct.containers[key]; ok { - if !c.running { - t.Run("[Start "+key+"]", func(_ *testing.T) { - err := c.startFn(ct, t) - require.NoError(t, err, "Failed to start container: %s", key) - }) - c.running = true - } - } - } + ct.startContainers(t, containers) } annotation, exists := cmd.Annotations["tests"] if exists { @@ -95,23 +84,7 @@ func (ct *CommandTest) testCmd(t *testing.T, cmd *cobra.Command, args []string) err := json.Unmarshal([]byte(annotation), &tests) require.NoError(t, err, "Malformed annotation: %s", annotation) for _, test := range tests { - t.Run(test.Name, func(t *testing.T) { - var input io.Reader - if test.Input != nil { - input = strings.NewReader(*test.Input) - } - output, err := ct.executeCmd(append(args[1:], test.Args...), input) - if test.ExpectError { - require.Error(t, err, "Expected error") - } else if err != nil { - ct.handleUnexpectedError(t, err, "Unexpected error") - } else if test.Output != nil { - output = strings.TrimRightFunc(output, unicode.IsSpace) - r, err := regexp.Compile(*test.Output) - require.NoError(t, err, "Malformed regexp: %s", *test.Output) - require.True(t, r.MatchString(output), "Expected output to match %s, got %s", *test.Output, output) - } - }) + ct.executeTest(t, test, args) } }) } @@ -126,6 +99,46 @@ func (ct *CommandTest) testCmd(t *testing.T, cmd *cobra.Command, args []string) } } +func (ct *CommandTest) startContainers(t *testing.T, containers string) { + for _, container := range strings.Split(containers, ",") { + key := strings.TrimFunc(container, unicode.IsSpace) + if c, ok := ct.containers[key]; ok { + if !c.running { + t.Run("[Start "+key+"]", func(_ *testing.T) { + err := c.startFn(ct, t) + require.NoError(t, err, "Failed to start container: %s", key) + }) + c.running = true + } + } + } +} + +func (ct *CommandTest) executeTest(t *testing.T, test test, args []string) { + t.Run(test.Name, func(t *testing.T) { + var input io.Reader + if test.Input != nil { + input = strings.NewReader(*test.Input) + } + output, err := ct.executeCmd(append(args[1:], test.Args...), input) + if test.ExpectError { + require.Error(t, err, "Expected error") + } else if err != nil { + ct.handleUnexpectedError(t, err, "Unexpected error") + } else if test.Output != nil { + output = strings.TrimRightFunc(output, unicode.IsSpace) + if strings.HasPrefix(*test.Output, "/") && strings.HasSuffix(*test.Output, "/") { + expr := (*test.Output)[1 : len(*test.Output)-1] + re, err := regexp.Compile(expr) + require.NoError(t, err, "Malformed regexp: %s", expr) + require.True(t, re.MatchString(output), "Expected output to match %s, got %s", expr, output) + } else { + require.Equal(t, *test.Output, output, "Expected output %s, got %s", *test.Output, output) + } + } + }) +} + func (ct *CommandTest) executeCmd(args []string, input io.Reader) (string, error) { osStdout := os.Stdout // keep backup of the real stdout @@ -152,6 +165,7 @@ func (ct *CommandTest) executeCmd(args []string, input io.Reader) (string, error wg.Wait() return output.String(), err + } func getOrder(cmd *cobra.Command) int { diff --git a/snok_test.go b/snok_test.go index 02050a5..b18c799 100644 --- a/snok_test.go +++ b/snok_test.go @@ -58,7 +58,7 @@ var rootCmd = &cobra.Command{ { "name": "Version", "args": ["--version"], - "output": "^test version \\d+\\.\\d+\\.\\d+$" + "output": "/^test version \\d+\\.\\d+\\.\\d+$/" } ]`, }, @@ -73,7 +73,7 @@ var envCmd = &cobra.Command{ { "name": "Check Container Message", "args": ["TEST_MESSAGE"], - "output": "^testing$" + "output": "testing" } ]`, }, @@ -95,18 +95,18 @@ var echoCmd = &cobra.Command{ { "name": "Echo", "args": ["hello","world"], - "output": "^hello world$" + "output": "hello world" }, { "name": "Echo Reverse", "args": ["-r","hello","world"], - "output": "^dlrow olleh$" + "output": "dlrow olleh" }, { "name": "Echo Input", "args": ["-r","The","rain"], "input": "in Spain falls mainly on the plain", - "output": "^nialp eht no ylniam sllaf niapS ni niar ehT$" + "output": "nialp eht no ylniam sllaf niapS ni niar ehT" } ]`, }, -- GitLab