Commit ea9ac0d2 by bergquist

chore(cli): improve unittests

parent 74625771
......@@ -11,7 +11,7 @@ type FakeIoUtil struct {
}
func (util *FakeIoUtil) Stat(path string) (os.FileInfo, error) {
return FakeFileInfo{IsDirectory: util.FakeIsDirectory}, nil
return &FakeFileInfo{IsDirectory: util.FakeIsDirectory}, nil
}
func (util *FakeIoUtil) RemoveAll(path string) error {
......@@ -30,7 +30,7 @@ type FakeFileInfo struct {
IsDirectory bool
}
func (ffi FakeFileInfo) IsDir() bool {
func (ffi *FakeFileInfo) IsDir() bool {
return ffi.IsDirectory
}
......
......@@ -7,22 +7,15 @@ import (
s "github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
)
var getPlugins func(path string) []m.InstalledPlugin
var ls_getPlugins func(path string) []m.InstalledPlugin = s.GetLocalPlugins
var GetStat m.IoUtil
func init() {
getPlugins = s.GetLocalPlugins
GetStat = s.IoUtil
}
func validateCommand(pluginDir string) error {
var validateLsCommmand = func(pluginDir string) error {
if pluginDir == "" {
return errors.New("missing path flag")
}
log.Info("plugindir: " + pluginDir + "\n")
pluginDirInfo, err := GetStat.Stat(pluginDir)
pluginDirInfo, err := s.IoHelper.Stat(pluginDir)
if err != nil {
return errors.New("missing path flag")
......@@ -37,11 +30,11 @@ func validateCommand(pluginDir string) error {
func lsCommand(c CommandLine) error {
pluginDir := c.GlobalString("path")
if err := validateCommand(pluginDir); err != nil {
if err := validateLsCommmand(pluginDir); err != nil {
return err
}
for _, plugin := range getPlugins(pluginDir) {
for _, plugin := range ls_getPlugins(pluginDir) {
log.Infof("plugin: %s @ %s \n", plugin.Name, plugin.Info.Version)
}
......
package commands
import (
"testing"
"errors"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/commands/commandstest"
s "github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestMissingPath(t *testing.T) {
Convey("Missing path", t, func() {
commandLine := &commandstest.FakeCommandLine{
CliArgs: []string{"ls"},
GlobalFlags: &commandstest.FakeFlagger{
Data: map[string]interface{}{
"path": "",
var org = validateLsCommmand
Convey("ls command", t, func() {
validateLsCommmand = org
Convey("Missing path", func() {
commandLine := &commandstest.FakeCommandLine{
CliArgs: []string{"ls"},
GlobalFlags: &commandstest.FakeFlagger{
Data: map[string]interface{}{
"path": "",
},
},
},
}
s.IoHelper = &commandstest.FakeIoUtil{}
}
s.IoHelper = &commandstest.FakeIoUtil{}
Convey("should return error", func() {
err := lsCommand(commandLine)
So(err, ShouldNotBeNil)
Convey("should return error", func() {
err := lsCommand(commandLine)
So(err, ShouldNotBeNil)
})
})
})
Convey("Path is not a directory", t, func() {
commandLine := &commandstest.FakeCommandLine{
CliArgs: []string{"ls"},
GlobalFlags: &commandstest.FakeFlagger{
Data: map[string]interface{}{
"path": "/var/lib/grafana/plugins",
Convey("Path is not a directory", func() {
commandLine := &commandstest.FakeCommandLine{
CliArgs: []string{"ls"},
GlobalFlags: &commandstest.FakeFlagger{
Data: map[string]interface{}{
"path": "/var/lib/grafana/plugins",
},
},
},
}
GetStat = &commandstest.FakeIoUtil{
FakeIsDirectory: false,
}
Convey("should return error", func() {
err := lsCommand(commandLine)
So(err, ShouldNotBeNil)
}
s.IoHelper = &commandstest.FakeIoUtil{
FakeIsDirectory: false,
}
Convey("should return error", func() {
err := lsCommand(commandLine)
So(err, ShouldNotBeNil)
})
})
Convey("can override validateLsCommand", func() {
commandLine := &commandstest.FakeCommandLine{
CliArgs: []string{"ls"},
GlobalFlags: &commandstest.FakeFlagger{
Data: map[string]interface{}{
"path": "/var/lib/grafana/plugins",
},
},
}
validateLsCommmand = func(pluginDir string) error {
return errors.New("dummie error")
}
Convey("should return error", func() {
err := lsCommand(commandLine)
So(err.Error(), ShouldEqual, "dummie error")
})
})
Convey("Validate that validateLsCommand is reset", func() {
commandLine := &commandstest.FakeCommandLine{
CliArgs: []string{"ls"},
GlobalFlags: &commandstest.FakeFlagger{
Data: map[string]interface{}{
"path": "/var/lib/grafana/plugins",
},
},
}
Convey("should return error", func() {
err := lsCommand(commandLine)
So(err.Error(), ShouldNotEqual, "dummie error")
})
})
})
}
package services
import (
m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
//m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
"io/ioutil"
"os"
)
var IoUtil m.IoUtil = IoUtilImp{}
//var IoUtils m.IoUtil = IoUtilImp{}
type IoUtilImp struct {
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment