From e7606a9c8214afd8bc06b2f63e946a45e8577ed7 Mon Sep 17 00:00:00 2001 From: Philipp Rosenkranz Date: Mon, 15 Apr 2019 13:23:18 +0200 Subject: [PATCH] projects: added projects list feature description & test implementation --- tests/config_test.go | 91 ------------------- tests/executor/executor.go | 5 +- tests/feature_test.go | 150 ++++++++++++++++++++++++++++++++ tests/features/projects.feature | 45 ++++++++++ 4 files changed, 198 insertions(+), 93 deletions(-) delete mode 100644 tests/config_test.go create mode 100644 tests/feature_test.go create mode 100644 tests/features/projects.feature diff --git a/tests/config_test.go b/tests/config_test.go deleted file mode 100644 index 1011f15..0000000 --- a/tests/config_test.go +++ /dev/null @@ -1,91 +0,0 @@ -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 index f41c532..c53005a 100644 --- a/tests/executor/executor.go +++ b/tests/executor/executor.go @@ -37,8 +37,9 @@ func Executor(buildDirectory string) *Config { } } -func (c *Config) Execute(args ...string) ([]byte, error) { - cmd := exec.Command(c.ExecFullPath, args...) +func (c *Config) Execute(args string) ([]byte, error) { + subCmd := strings.Split(args, " ") + cmd := exec.Command(c.ExecFullPath, subCmd...) cmd.Dir = c.ExecDirectory return cmd.Output() } diff --git a/tests/feature_test.go b/tests/feature_test.go new file mode 100644 index 0000000..fdf314a --- /dev/null +++ b/tests/feature_test.go @@ -0,0 +1,150 @@ +package tests_test + +import ( + "errors" + "flag" + "fmt" + "github.com/DATA-DOG/godog" + "github.com/DATA-DOG/godog/colors" + "github.com/DATA-DOG/godog/gherkin" + "github.com/leanovate/mite-go/tests/executor" + "net/http" + "net/http/httptest" + "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) + s.Step(`^A local mock server is setup for the http method "([^"]*)" and path "([^"]*)" which returns:$`, c.aLocalMockServerIsSetupForTheHttpMethodAndPathWhichReturns) + s.Step(`^Mite is setup to connect to this mock server$`, c.miteIsSetupToConnectToThisMockServer) + s.Step(`^"([^"]*)" should return the following:$`, c.shouldReturnTheFollowing) +} + +var opt = godog.Options{ + Output: colors.Colored(os.Stdout), + Format: "progress", // can define default values +} + +type cmdTest struct { + executor *executor.Config + mockServer *httptest.Server +} + +func (c *cmdTest) reset(interface{}, error) { + err := c.executor.Clean() + if err != nil { + panic(err) + } + c.executor = executor.Executor(buildDirectory) + + if c.mockServer != nil { + c.mockServer.Close() + } +} + +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 { + output, err := c.executor.Execute(subCommand) + fmt.Println(string(output)) + return err +} + +func (c *cmdTest) shouldReturn(subCommand, output string) error { + stdout, err := c.executor.Execute(subCommand) + if err != nil { + return err + } + outputWithoutSpace := strings.TrimSpace(string(stdout)) + return assertEqual(output, outputWithoutSpace) +} + +func (c *cmdTest) aLocalMockServerIsSetupForTheHttpMethodAndPathWhichReturns(arg1, arg2 string, arg3 *gherkin.DocString) error { + handler := func(w http.ResponseWriter, r *http.Request) { + if r.Method != arg1 { + w.WriteHeader(400) + } + if r.URL.Path != arg2 { + w.WriteHeader(400) + } + + w.WriteHeader(200) + w.Header().Set("Content-Type", "application/json; charset=utf-8") + _, err := w.Write([]byte(arg3.Content)) + if err != nil { + panic(err) + } + } + handlerFunc := http.HandlerFunc(handler) + c.mockServer = httptest.NewServer(handlerFunc) + return nil +} + +func (c *cmdTest) miteIsSetupToConnectToThisMockServer() error { + err := c.iExecute("-c .mite.toml config api.key=foo") + if err != nil { + return err + } + err = c.iExecute(fmt.Sprintf("-c .mite.toml config api.url=%s", c.mockServer.URL)) + if err != nil { + return err + } + return nil +} + +func (c *cmdTest) shouldReturnTheFollowing(arg1 string, arg2 *gherkin.DocString) error { + actualOutput, err := c.executor.Execute(arg1) + if err != nil { + return err + } + return assertEqual(arg2.Content, string(actualOutput)) +} + +func assertEqual(expected, actual string) error { + if strings.Compare(expected, actual) != 0 { + return errors.New(fmt.Sprintf("expected: \n%s\n to equal: \n%s", expected, actual)) + } + return nil +} diff --git a/tests/features/projects.feature b/tests/features/projects.feature new file mode 100644 index 0000000..a11ac7b --- /dev/null +++ b/tests/features/projects.feature @@ -0,0 +1,45 @@ +Feature: projects + In order to use mite in a sane manner + As a mite user + I need to be able to list projects + + Scenario: list projects + Given A local mock server is setup for the http method "GET" and path "/projects.json" which returns: + """ + [ + { + "project": { + "id": 643, + "name": "Open-Source", + "note": "valvat, memento et all.", + "customer_id": 291, + "customer_name": "Yolk", + "budget": 0, + "budget_type": "minutes", + "hourly_rate": 6000, + "archived": false, + "active_hourly_rate": "hourly_rate", + "hourly_rates_per_service": [ + { + "service_id": 31272, + "hourly_rate": 4500 + }, + { + "service_id": 149228, + "hourly_rate": 5500 + } + ], + "created_at": "2011-08-17T12:06:57+02:00", + "updated_at": "2015-02-19T10:53:10+01:00" + } + } + ] + """ + And Mite is setup to connect to this mock server + Then "-c .mite.toml projects" should return the following: + """ + id name notes + -- ---- ----- + 643 Open-Source valvat, memento et all. + + """