-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: added config feature description & tests
- Loading branch information
Showing
7 changed files
with
166 additions
and
61 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |