Bladeren bron

chore(cli): improve unittests

bergquist 9 jaren geleden
bovenliggende
commit
ea9ac0d2d2

+ 2 - 2
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) {
 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 {
 func (util *FakeIoUtil) RemoveAll(path string) error {
@@ -30,7 +30,7 @@ type FakeFileInfo struct {
 	IsDirectory bool
 	IsDirectory bool
 }
 }
 
 
-func (ffi FakeFileInfo) IsDir() bool {
+func (ffi *FakeFileInfo) IsDir() bool {
 	return ffi.IsDirectory
 	return ffi.IsDirectory
 }
 }
 
 

+ 5 - 12
pkg/cmd/grafana-cli/commands/ls_command.go

@@ -7,22 +7,15 @@ import (
 	s "github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
 	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 == "" {
 	if pluginDir == "" {
 		return errors.New("missing path flag")
 		return errors.New("missing path flag")
 	}
 	}
 
 
 	log.Info("plugindir: " + pluginDir + "\n")
 	log.Info("plugindir: " + pluginDir + "\n")
-	pluginDirInfo, err := GetStat.Stat(pluginDir)
+	pluginDirInfo, err := s.IoHelper.Stat(pluginDir)
 
 
 	if err != nil {
 	if err != nil {
 		return errors.New("missing path flag")
 		return errors.New("missing path flag")
@@ -37,11 +30,11 @@ func validateCommand(pluginDir string) error {
 
 
 func lsCommand(c CommandLine) error {
 func lsCommand(c CommandLine) error {
 	pluginDir := c.GlobalString("path")
 	pluginDir := c.GlobalString("path")
-	if err := validateCommand(pluginDir); err != nil {
+	if err := validateLsCommmand(pluginDir); err != nil {
 		return err
 		return err
 	}
 	}
 
 
-	for _, plugin := range getPlugins(pluginDir) {
+	for _, plugin := range ls_getPlugins(pluginDir) {
 		log.Infof("plugin: %s @ %s \n", plugin.Name, plugin.Info.Version)
 		log.Infof("plugin: %s @ %s \n", plugin.Name, plugin.Info.Version)
 	}
 	}
 
 

+ 73 - 30
pkg/cmd/grafana-cli/commands/ls_command_test.go

@@ -1,47 +1,90 @@
 package commands
 package commands
 
 
 import (
 import (
-	"testing"
-
+	"errors"
 	"github.com/grafana/grafana/pkg/cmd/grafana-cli/commands/commandstest"
 	"github.com/grafana/grafana/pkg/cmd/grafana-cli/commands/commandstest"
 	s "github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
 	s "github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
 	. "github.com/smartystreets/goconvey/convey"
 	. "github.com/smartystreets/goconvey/convey"
+	"testing"
 )
 )
 
 
 func TestMissingPath(t *testing.T) {
 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")
+			})
 		})
 		})
 	})
 	})
 }
 }

+ 2 - 2
pkg/cmd/grafana-cli/services/io_util.go

@@ -1,12 +1,12 @@
 package services
 package services
 
 
 import (
 import (
-	m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
+	//m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
 	"io/ioutil"
 	"io/ioutil"
 	"os"
 	"os"
 )
 )
 
 
-var IoUtil m.IoUtil = IoUtilImp{}
+//var IoUtils m.IoUtil = IoUtilImp{}
 
 
 type IoUtilImp struct {
 type IoUtilImp struct {
 }
 }