diff --git a/pkg/cmd/grafana-cli/commands/commandstest/fake_ioutil.go b/pkg/cmd/grafana-cli/commands/commandstest/fake_ioutil.go index e6715a5ea77..6c8f6bdb57c 100644 --- a/pkg/cmd/grafana-cli/commands/commandstest/fake_ioutil.go +++ b/pkg/cmd/grafana-cli/commands/commandstest/fake_ioutil.go @@ -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 } diff --git a/pkg/cmd/grafana-cli/commands/ls_command.go b/pkg/cmd/grafana-cli/commands/ls_command.go index b12213799cc..f863bc4b72b 100644 --- a/pkg/cmd/grafana-cli/commands/ls_command.go +++ b/pkg/cmd/grafana-cli/commands/ls_command.go @@ -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) } diff --git a/pkg/cmd/grafana-cli/commands/ls_command_test.go b/pkg/cmd/grafana-cli/commands/ls_command_test.go index 55553c4fd99..fa49375234d 100644 --- a/pkg/cmd/grafana-cli/commands/ls_command_test.go +++ b/pkg/cmd/grafana-cli/commands/ls_command_test.go @@ -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") + }) }) }) } diff --git a/pkg/cmd/grafana-cli/services/io_util.go b/pkg/cmd/grafana-cli/services/io_util.go index c3be33af199..07629a655d8 100644 --- a/pkg/cmd/grafana-cli/services/io_util.go +++ b/pkg/cmd/grafana-cli/services/io_util.go @@ -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 { }