Skip to content

Commit

Permalink
tests: added config feature description & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed Apr 10, 2019
1 parent 5ea3d32 commit f33a304
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 61 deletions.
31 changes: 0 additions & 31 deletions executor/executor.go

This file was deleted.

27 changes: 0 additions & 27 deletions executor/executor_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand All @@ -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=
Expand Down
91 changes: 91 additions & 0 deletions tests/config_test.go
Original file line number Diff line number Diff line change
@@ -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
}
51 changes: 51 additions & 0 deletions tests/executor/executor.go
Original file line number Diff line number Diff line change
@@ -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")
}
21 changes: 21 additions & 0 deletions tests/features/config.feature
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit f33a304

Please sign in to comment.