ref(cmd/draft): add test for config flag on init

This commit is contained in:
Michelle Noorali 2018-05-08 11:05:25 -04:00
Родитель 43460de1ad
Коммит 3caecf0d0f
4 изменённых файлов: 77 добавлений и 8 удалений

Просмотреть файл

@ -57,14 +57,9 @@ func newInitCmd(out io.Writer, in io.Reader) *cobra.Command {
// runInit initializes local config and installs Draft to Kubernetes Cluster
func (i *initCmd) run() error {
pluginOverrides := []plugin.Builtin{}
repoOverrides := []repo.Builtin{}
if i.configFile != "" {
var err error
pluginOverrides, repoOverrides, err = parseConfigFile(i.configFile)
if err != nil {
return fmt.Errorf("Could not parse config file: %s", err)
}
pluginOverrides, repoOverrides, err := i.parseConfig()
if err != nil {
return err
}
if !i.dryRun {
@ -77,6 +72,20 @@ func (i *initCmd) run() error {
return nil
}
func (i *initCmd) parseConfig() ([]plugin.Builtin, []repo.Builtin, error) {
pluginOverrides := []plugin.Builtin{}
repoOverrides := []repo.Builtin{}
if i.configFile != "" {
var err error
pluginOverrides, repoOverrides, err = parseConfigFile(i.configFile)
if err != nil {
return pluginOverrides, repoOverrides, fmt.Errorf("Could not parse config file: %s", err)
}
}
return pluginOverrides, repoOverrides, nil
}
func (i *initCmd) setupDraftHome(plugins []plugin.Builtin, repos []repo.Builtin) error {
ensureFuncs := []func() error{
i.ensureDirectories,

48
cmd/draft/init_test.go Normal file
Просмотреть файл

@ -0,0 +1,48 @@
package main
import (
"io/ioutil"
"testing"
"github.com/Azure/draft/pkg/draft/draftpath"
)
func TestParseConfig(t *testing.T) {
testCases := []struct {
configFile string
expectErr bool
pluginCount int
repoCount int
}{
{"", false, 0, 0},
{"testdata/init/configFile.toml", false, 1, 1},
{"testdata/init/malformedConfigFile.toml", true, 0, 0},
{"testdata/init/missingConfigFile.toml", true, 0, 0},
}
for _, tc := range testCases {
resetEnvVars := unsetEnvVars()
tempHome, teardown := tempDir(t, "draft-init")
defer func() {
teardown()
resetEnvVars()
}()
cmd := &initCmd{
home: draftpath.Home(tempHome),
out: ioutil.Discard,
configFile: tc.configFile,
}
plugins, repos, err := cmd.parseConfig()
if err != nil && !tc.expectErr {
t.Errorf("Not expecting error but got error: %v", err)
}
if len(plugins) != tc.pluginCount {
t.Errorf("Expected %v plugins, got %#v", tc.pluginCount, len(plugins))
}
if len(repos) != tc.repoCount {
t.Errorf("Expected %v pack repos, got %#v", tc.repoCount, len(repos))
}
}
}

8
cmd/draft/testdata/init/configFile.toml поставляемый Normal file
Просмотреть файл

@ -0,0 +1,8 @@
[[plugin]]
name = "plugin1"
url = "a url"
version = "1.0.0"
[[repo]]
name = "pack-repo"
url = "some url"

4
cmd/draft/testdata/init/malformedConfigFile.toml поставляемый Normal file
Просмотреть файл

@ -0,0 +1,4 @@
something random
[[nothing]]
nonsense here