mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 06:42:13 +08:00
chore(cli): improve unittests
This commit is contained in:
@ -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")
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user