mirror of
https://github.com/grafana/grafana.git
synced 2024-11-25 02:10:45 -06:00
chore(cli): improve unittests
This commit is contained in:
parent
746257710b
commit
ea9ac0d2d2
@ -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)
|
||||
}
|
||||
|
||||
|
@ -1,47 +1,90 @@
|
||||
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": "",
|
||||
},
|
||||
},
|
||||
}
|
||||
s.IoHelper = &commandstest.FakeIoUtil{}
|
||||
var org = validateLsCommmand
|
||||
|
||||
Convey("should return error", func() {
|
||||
err := lsCommand(commandLine)
|
||||
So(err, ShouldNotBeNil)
|
||||
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{}
|
||||
|
||||
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")
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -1,12 +1,12 @@
|
||||
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 {
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user