From f33a304061715fc8be32bb2958caf1661f6abb95 Mon Sep 17 00:00:00 2001 From: Philipp Rosenkranz Date: Wed, 10 Apr 2019 17:10:05 +0200 Subject: [PATCH] tests: added config feature description & tests --- executor/executor.go | 31 ------------ executor/executor_test.go | 27 ----------- go.mod | 2 +- go.sum | 4 +- tests/config_test.go | 91 +++++++++++++++++++++++++++++++++++ tests/executor/executor.go | 51 ++++++++++++++++++++ tests/features/config.feature | 21 ++++++++ 7 files changed, 166 insertions(+), 61 deletions(-) delete mode 100644 executor/executor.go delete mode 100644 executor/executor_test.go create mode 100644 tests/config_test.go create mode 100644 tests/executor/executor.go create mode 100644 tests/features/config.feature diff --git a/executor/executor.go b/executor/executor.go deleted file mode 100644 index 90c49e9..0000000 --- a/executor/executor.go +++ /dev/null @@ -1,31 +0,0 @@ -package executor - -import ( - "fmt" - "github.com/progrium/go-shell" - "os/exec" -) - -type Config struct { - execFullPath string - buildDirectory string -} - -func Executor(execFullPath, buildDirectory string) *Config { - shell.Shell = []string{"/bin/bash", "-c"} - shell.Run() - defer shell.ErrExit() - - shell.Run(fmt.Sprintf("pushd %s; go build -o %s .; popd", buildDirectory, execFullPath)) - - return &Config{ - execFullPath: execFullPath, - buildDirectory: buildDirectory, - } -} - -func (c *Config) Execute(args ...string) ([]byte, error) { - fmt.Println(c.execFullPath) - cmd := exec.Command(c.execFullPath, args...) - return cmd.CombinedOutput() -} diff --git a/executor/executor_test.go b/executor/executor_test.go deleted file mode 100644 index 446c49c..0000000 --- a/executor/executor_test.go +++ /dev/null @@ -1,27 +0,0 @@ -package executor - -import ( - "fmt" - "github.com/stretchr/testify/assert" - "io/ioutil" - "path/filepath" - "testing" -) - -func TestCliBdd(t *testing.T) { - // given - tmpDir, err := ioutil.TempDir("", "clibdd") - assert.Nil(t, err) - efp, err := filepath.Abs(filepath.Join(tmpDir, "mite")) - assert.Nil(t, err) - bd, err := filepath.Abs("..") - assert.Nil(t, err) - config := Executor(efp, bd) - - // when - stdOutAndErr, err := config.Execute("-c", "testdata/.mite.toml", "config") - - // then - assert.Nil(t, err) - fmt.Println(string(stdOutAndErr)) -} diff --git a/go.mod b/go.mod index 7b9b81b..8c85166 100644 --- a/go.mod +++ b/go.mod @@ -2,10 +2,10 @@ module github.com/leanovate/mite-go require ( github.com/BurntSushi/toml v0.3.1 // indirect + github.com/DATA-DOG/godog v0.7.13 github.com/cheynewallace/tabby v1.1.0 github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/mitchellh/go-homedir v1.1.0 - github.com/progrium/go-shell v0.0.0-20181023041501-104b11941186 github.com/spf13/cobra v0.0.3 github.com/spf13/viper v1.3.1 github.com/stretchr/testify v1.3.0 diff --git a/go.sum b/go.sum index 02984a0..529ca8a 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/DATA-DOG/godog v0.7.13 h1:JmgpKcra7Vf3yzI9vPsWyoQRx13tyKziHtXWDCUUgok= +github.com/DATA-DOG/godog v0.7.13/go.mod h1:z2OZ6a3X0/YAKVqLfVzYBwFt3j6uSt3Xrqa7XTtcQE0= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= github.com/cheynewallace/tabby v1.1.0 h1:XtG/ZanoIvNZHfe0cClhWLzD/16GGF9UD7mMdWwYnCQ= github.com/cheynewallace/tabby v1.1.0/go.mod h1:Pba/6cUL8uYqvOc9RkyvFbHGrQ9wShyrn6/S/1OYVys= @@ -25,8 +27,6 @@ github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181 github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/progrium/go-shell v0.0.0-20181023041501-104b11941186 h1:MhWIYlyv3SL+7kBBp1R/sPqLNKoWa5BSuMzy5FVkLTU= -github.com/progrium/go-shell v0.0.0-20181023041501-104b11941186/go.mod h1:8ElsifciDrOoyOeBTsa7GpKzmxWo0AtZ/5mjoNxzKnQ= github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8= diff --git a/tests/config_test.go b/tests/config_test.go new file mode 100644 index 0000000..1011f15 --- /dev/null +++ b/tests/config_test.go @@ -0,0 +1,91 @@ +package tests_test + +import ( + "errors" + "flag" + "github.com/DATA-DOG/godog" + "github.com/DATA-DOG/godog/colors" + "github.com/leanovate/mite-go/tests/executor" + "os" + "path/filepath" + "strings" + "testing" +) + +const buildDirectory = "../" + +func init() { + godog.BindFlags("godog.", flag.CommandLine, &opt) +} + +func TestMain(m *testing.M) { + flag.Parse() + opt.Paths = flag.Args() + + status := godog.RunWithOptions("godogs", func(s *godog.Suite) { + FeatureContext(s) + }, opt) + + if st := m.Run(); st > status { + status = st + } + os.Exit(status) +} + +func FeatureContext(s *godog.Suite) { + c := cmdTest{ + executor: executor.Executor(buildDirectory), + } + + s.AfterScenario(c.reset) + s.AfterSuite(func() { + err := c.executor.Clean() + if err != nil { + panic(err) + } + }) + s.Step(`^an empty config file called "([^"]*)"$`, c.anEmptyConfigFileCalled) + s.Step(`^I execute "([^"]*)"$`, c.iExecute) + s.Step(`^"([^"]*)" should return "([^"]*)"$`, c.shouldReturn) +} + +var opt = godog.Options{ + Output: colors.Colored(os.Stdout), + Format: "progress", // can define default values +} + +type cmdTest struct { + executor *executor.Config +} + +func (c *cmdTest) reset(interface{}, error) { + err := c.executor.Clean() + if err != nil { + panic(err) + } + c.executor = executor.Executor(buildDirectory) +} + +func (c *cmdTest) anEmptyConfigFileCalled(arg1 string) error { + _, err := os.Stat(filepath.Join(c.executor.ExecDirectory, arg1)) + if os.IsNotExist(err) { + return nil + } + return err +} + +func (c *cmdTest) iExecute(subCommand string) error { + subCmd := strings.Split(subCommand, " ") + _, err := c.executor.Execute(subCmd...) + return err +} + +func (c *cmdTest) shouldReturn(subCommand, output string) error { + subCmd := strings.Split(subCommand, " ") + stdout, err := c.executor.Execute(subCmd...) + outputWithoutSpace := strings.TrimSpace(string(stdout)) + if strings.Compare(outputWithoutSpace, output) != 0 { + return errors.New("output is not expected output") + } + return err +} diff --git a/tests/executor/executor.go b/tests/executor/executor.go new file mode 100644 index 0000000..f41c532 --- /dev/null +++ b/tests/executor/executor.go @@ -0,0 +1,51 @@ +package executor + +import ( + "errors" + "fmt" + "io/ioutil" + "os" + "os/exec" + "path/filepath" + "strings" +) + +type Config struct { + ExecDirectory string + ExecFullPath string + buildDirectory string +} + +func Executor(buildDirectory string) *Config { + execDirectory, err := ioutil.TempDir("", "executor") + if err != nil { + panic(err) + } + execFullPath := filepath.Join(execDirectory, "mite") + + args := []string{"bash", "-c", fmt.Sprintf("pushd %s; go build -o %s .; popd", buildDirectory, execFullPath)} + cmd := exec.Command("/usr/bin/env", args...) + _, err = cmd.Output() + if err != nil { + panic(err) + } + + return &Config{ + ExecDirectory: execDirectory, + ExecFullPath: execFullPath, + buildDirectory: buildDirectory, + } +} + +func (c *Config) Execute(args ...string) ([]byte, error) { + cmd := exec.Command(c.ExecFullPath, args...) + cmd.Dir = c.ExecDirectory + return cmd.Output() +} + +func (c *Config) Clean() error { + if !strings.HasPrefix("/tmp/", c.ExecDirectory) { + return os.RemoveAll(c.ExecDirectory) + } + return errors.New("tried to remove file without prefix /tmp") +} diff --git a/tests/features/config.feature b/tests/features/config.feature new file mode 100644 index 0000000..65fb5b9 --- /dev/null +++ b/tests/features/config.feature @@ -0,0 +1,21 @@ +Feature: config + In order to use mite in a sane manner + As a mite user + I need to be able to set defaults in a config + + Scenario: Write & read api key + Given an empty config file called ".mite.toml" + When I execute "-c .mite.toml config api.key=foo" + Then "-c .mite.toml config api.key" should return "foo" + Scenario: Write & read api url + Given an empty config file called ".mite.toml" + When I execute "-c .mite.toml config api.url=http://foo.invalid" + Then "-c .mite.toml config api.url" should return "http://foo.invalid" + Scenario: Write & read default projectId + Given an empty config file called ".mite.toml" + When I execute "-c .mite.toml config projectId=12345" + Then "-c .mite.toml config projectId" should return "12345" + Scenario: Write & read default serviceId + Given an empty config file called ".mite.toml" + When I execute "-c .mite.toml config serviceId=54321" + Then "-c .mite.toml config serviceId" should return "54321"