Skip to content

Commit

Permalink
Merge pull request #48 from davidovich/refactor-interfaces-add-coverage
Browse files Browse the repository at this point in the history
Refactor interfaces and add coverage
  • Loading branch information
davidovich authored Jul 5, 2019
2 parents 5ade9b1 + 3b9ebe3 commit f35d69c
Show file tree
Hide file tree
Showing 22 changed files with 455 additions and 215 deletions.
4 changes: 2 additions & 2 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
)

type completionCmdOpts struct {
driver summon.Interface
driver summon.Lister
cmd *cobra.Command
out io.Writer
}

func newCompletionCmd(driver summon.Interface) *cobra.Command {
func newCompletionCmd(driver summon.Lister) *cobra.Command {
cOpts := completionCmdOpts{
driver: driver,
}
Expand Down
25 changes: 25 additions & 0 deletions cmd/completion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"bytes"
"testing"

"github.com/davidovich/summon/pkg/summon"
"github.com/gobuffalo/packr/v2"
"github.com/stretchr/testify/assert"
)

func TestCompletionCommand(t *testing.T) {
box := packr.New("testCompletion", "testdata")

s, _ := summon.New(box)

cmd := newCompletionCmd(s)

b := &bytes.Buffer{}

cmd.SetOutput(b)
cmd.Execute()

assert.Contains(t, b.String(), "completion_summon.config.yaml")
}
4 changes: 2 additions & 2 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
)

type listCmdOpts struct {
driver summon.Interface
driver summon.ConfigurableLister
tree bool
out io.Writer
}

func newListCmd(driver summon.Interface) *cobra.Command {
func newListCmd(driver summon.ConfigurableLister) *cobra.Command {
listCmd := &listCmdOpts{
driver: driver,
}
Expand Down
49 changes: 49 additions & 0 deletions cmd/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cmd

import (
"bytes"
"strconv"
"testing"

"github.com/davidovich/summon/pkg/summon"
"github.com/gobuffalo/packr/v2"
"github.com/stretchr/testify/assert"
)

func TestListCmd(t *testing.T) {

box := packr.New("test box", "testdata")
tests := []struct {
name string
args []string
// roostCmd *cobra.Command
expected string
}{
{
name: "no-args",
expected: "json-for-template.json\nsummon.config.yaml",
},
{
name: "--tree",
args: []string{"--tree"},
expected: `testdata
├── json-for-template.json
└── summon.config.yaml`,
},
}

for i, tt := range tests {
t.Run(strconv.Itoa(i)+"_"+tt.name, func(t *testing.T) {
s, _ := summon.New(box)

cmd := newListCmd(s)
cmd.SetArgs(tt.args)

b := &bytes.Buffer{}
cmd.SetOut(b)
cmd.Execute()

assert.Contains(t, b.String(), tt.expected)
})
}
}
28 changes: 9 additions & 19 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"path/filepath"
"runtime/debug"

"github.com/gobuffalo/packr/v2"
"github.com/spf13/cobra"

"github.com/davidovich/summon/pkg/config"
Expand All @@ -19,17 +18,17 @@ import (
type mainCmd struct {
copyAll bool
dest string
driver summon.Interface
driver *summon.Driver
filename string
json string
jsonFile string
raw bool
out io.Writer
}

// CreateRoot creates the root command
func createRootCmd(driver summon.Interface) *cobra.Command {
cmdName := filepath.Base(os.Args[0])
// CreateRootCmd creates the root command
func CreateRootCmd(driver *summon.Driver, args []string) *cobra.Command {
cmdName := filepath.Base(args[0])
var showVersion bool

main := &mainCmd{
Expand All @@ -56,8 +55,7 @@ func createRootCmd(driver summon.Interface) *cobra.Command {
if showVersion {
v, ok := makeVersion()
if !ok {
fmt.Fprintln(cmd.OutOrStderr(), "Missing build info")
return nil
return fmt.Errorf("Missing build info")
}
enc := json.NewEncoder(main.out)
enc.SetIndent("", " ")
Expand All @@ -72,7 +70,7 @@ func createRootCmd(driver summon.Interface) *cobra.Command {
var j []byte
var err error
if main.jsonFile == "-" {
j, err = ioutil.ReadAll(os.Stdin)
j, err = ioutil.ReadAll(cmd.InOrStdin())
} else {
j, err = ioutil.ReadFile(main.jsonFile)
}
Expand Down Expand Up @@ -118,16 +116,6 @@ func (m *mainCmd) run() error {
return nil
}

// Execute is the main command entry point
func Execute(box *packr.Box) error {
s, err := summon.New(box)
if err != nil {
return err
}
rootCmd := createRootCmd(s)
return rootCmd.Execute()
}

type versionDesc struct {
Exe string `json:"exe,omitempty"`
Mod string `json:"mod"`
Expand All @@ -138,8 +126,10 @@ type versionInfo struct {
Lib versionDesc `json:"lib"`
}

var buildInfo = debug.ReadBuildInfo

func makeVersion() (v versionInfo, ok bool) {
bi, ok := debug.ReadBuildInfo()
bi, ok := buildInfo()
if !ok {
return v, false
}
Expand Down
Loading

0 comments on commit f35d69c

Please sign in to comment.