Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add portability to command invocation unit tests #3758

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions server/core/runtime/post_workflow_hook_runner_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runtime_test

import (
"fmt"
"strings"
"testing"

Expand All @@ -18,6 +19,8 @@ func TestPostWorkflowHookRunner_Run(t *testing.T) {

defaultShell := "sh"
defaultShellArgs := "-c"
defautShellCommandNotFoundErrorFormat := commandNotFoundErrorFormat(defaultShell)
defaultUnterminatedStringError := unterminatedStringError(defaultShell, defaultShellArgs)

cases := []struct {
Command string
Expand Down Expand Up @@ -63,7 +66,7 @@ func TestPostWorkflowHookRunner_Run(t *testing.T) {
Command: "echo 'a",
Shell: defaultShell,
ShellArgs: defaultShellArgs,
ExpOut: "sh: 1: Syntax error: Unterminated quoted string\r\n",
ExpOut: defaultUnterminatedStringError,
ExpErr: "exit status 2: running \"sh -c echo 'a\" in",
ExpDescription: "",
},
Expand All @@ -79,7 +82,7 @@ func TestPostWorkflowHookRunner_Run(t *testing.T) {
Command: "lkjlkj",
Shell: defaultShell,
ShellArgs: defaultShellArgs,
ExpOut: "sh: 1: lkjlkj: not found\r\n",
ExpOut: fmt.Sprintf(defautShellCommandNotFoundErrorFormat, "lkjlkj"),
ExpErr: "exit status 127: running \"sh -c lkjlkj\" in",
ExpDescription: "",
},
Expand Down
27 changes: 25 additions & 2 deletions server/core/runtime/pre_workflow_hook_runner_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package runtime_test

import (
"fmt"
goruntime "runtime"
"strings"
"testing"

Expand All @@ -14,10 +16,31 @@ import (
. "github.com/runatlantis/atlantis/testing"
)

func commandNotFoundErrorFormat(shell string) string {
// TODO: Add more GOOSs. Also I haven't done too much testing
// maybe the output here depends on other factors as well
if goruntime.GOOS == "darwin" {
return fmt.Sprintf("%s: %%s: command not found\r\n", shell)
}
return fmt.Sprintf("%s: 1: %%s: not found\r\n", shell)

}

func unterminatedStringError(shell, shellArgs string) string {
// TODO: Add more GOOSs. Also I haven't done too much testing
// maybe the output here depends on other factors as well
if goruntime.GOOS == "darwin" {
return fmt.Sprintf("%s: %s: line 0: unexpected EOF while looking for matching `''\r\n%s: %s: line 1: syntax error: unexpected end of file\r\n", shell, shellArgs, shell, shellArgs)
}
return fmt.Sprintf("%s: 1: Syntax error: Unterminated quoted string\r\n", shell)
}

func TestPreWorkflowHookRunner_Run(t *testing.T) {

defaultShell := "sh"
defaultShellArgs := "-c"
defautShellCommandNotFoundErrorFormat := commandNotFoundErrorFormat(defaultShell)
defaultUnterminatedStringError := unterminatedStringError(defaultShell, defaultShellArgs)

cases := []struct {
Command string
Expand Down Expand Up @@ -63,7 +86,7 @@ func TestPreWorkflowHookRunner_Run(t *testing.T) {
Command: "echo 'a",
Shell: defaultShell,
ShellArgs: defaultShellArgs,
ExpOut: "sh: 1: Syntax error: Unterminated quoted string\r\n",
ExpOut: defaultUnterminatedStringError,
ExpErr: "exit status 2: running \"sh -c echo 'a\" in",
ExpDescription: "",
},
Expand All @@ -79,7 +102,7 @@ func TestPreWorkflowHookRunner_Run(t *testing.T) {
Command: "lkjlkj",
Shell: defaultShell,
ShellArgs: defaultShellArgs,
ExpOut: "sh: 1: lkjlkj: not found\r\n",
ExpOut: fmt.Sprintf(defautShellCommandNotFoundErrorFormat, "lkjlkj"),
ExpErr: "exit status 127: running \"sh -c lkjlkj\" in",
ExpDescription: "",
},
Expand Down