Skip to content

Instantly share code, notes, and snippets.

@brandentimm
Created October 6, 2019 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandentimm/0fd788e1a527eb7b74d090a01c72ae3d to your computer and use it in GitHub Desktop.
Save brandentimm/0fd788e1a527eb7b74d090a01c72ae3d to your computer and use it in GitHub Desktop.
package feedback
import (
"os/exec"
"runtime"
"github.com/pkg/errors"
)
const issuesURL = "https://some-url"
type feedbackCmd struct {
path string
}
func (cmd *feedbackCmd) setPath(path string) {
cmd.path = path
}
func (cmd *feedbackCmd) run() error {
return exec.Command(cmd.path, issuesURL).Run()
}
func Feedback() error {
cmd := &feedbackCmd{}
err := getFeedback(runtime.GOOS, cmd)
if err != nil {
return err
}
return nil
}
type commander interface {
setPath(path string)
run() error
}
func getFeedback(os string, cmd commander) error {
var openCmds = map[string]string{
"linux": "xdg-open",
"darwin": "open",
}
if openCmd, ok := openCmds[os]; ok {
cmd.setPath(openCmd)
return cmd.run()
}
return errors.New("unsupported platform")
}
package feedback
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetFeedback(t *testing.T) {
t.Run("Test Linux", func(t *testing.T) {
cmd := &mockCommander{}
err := getFeedback("linux", cmd)
assert.Nil(t, err, "unexpected error creating command for linux")
assert.Equalf(t, cmd.path, "xdg-open", "expected xdg-open, got: %s", cmd.path)
})
t.Run("Test Mac OS", func(t *testing.T) {
cmd := &mockCommander{}
err := getFeedback("darwin", cmd)
assert.Nil(t, err, "unexpected error creating command for linux")
assert.Equalf(t, cmd.path, "open", "expected open, got: %s", cmd.path)
})
t.Run("Test unsupported operating system", func(t *testing.T) {
cmd := &mockCommander{}
err := getFeedback("freebsd", cmd)
assert.NotNil(t, err, "expected error getting feedback command for freebsd, got nil")
})
}
func TestFeedbackCmd_SetPath(t *testing.T) {
cmd := &feedbackCmd{}
cmd.setPath("foo")
assert.Equal(t, cmd.path, "foo", "command path set incorrectly")
}
func TestFeedbackCmd_Run(t *testing.T) {
t.Run("Test successful command invocation", func(t *testing.T) {
cmd := &feedbackCmd{path: "true"}
err := cmd.run()
assert.Nilf(t, err, "expected nil error response but got: %v", err)
})
t.Run("Test error handling on bogus command", func(t *testing.T) {
cmd := &feedbackCmd{path: "thisisnotgoingtowork"}
err := cmd.run()
assert.NotNil(t, err, "expected error from cmd.run but returned nil")
})
}
type mockCommander struct {
path string
}
func (cmd *mockCommander) run() error {
return nil
}
func (cmd *mockCommander) setPath(path string) {
cmd.path = path
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment