statping/cmd/cli_test.go

108 lines
2.7 KiB
Go
Raw Normal View History

package main
import (
2020-04-16 09:57:00 +00:00
"bytes"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/utils"
"github.com/stretchr/testify/assert"
2020-03-19 01:50:53 +00:00
"github.com/stretchr/testify/require"
2020-04-16 09:57:00 +00:00
"io/ioutil"
"testing"
)
var (
dir string
)
func init() {
2020-04-17 03:21:17 +00:00
utils.InitCLI()
}
2020-04-17 03:21:17 +00:00
func TestStatpingDirectory(t *testing.T) {
dir := utils.Directory
require.NotContains(t, dir, "/cmd")
require.NotEmpty(t, dir)
2020-04-17 03:21:17 +00:00
dir = utils.Params.GetString("STATPING_DIR")
require.NotContains(t, dir, "/cmd")
require.NotEmpty(t, dir)
}
2020-04-17 03:21:17 +00:00
func TestEnvCLI(t *testing.T) {
cmd := rootCmd
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetArgs([]string{"env"})
err := cmd.Execute()
2020-03-19 01:50:53 +00:00
require.Nil(t, err)
2020-04-17 03:21:17 +00:00
out, err := ioutil.ReadAll(b)
2020-03-19 01:50:53 +00:00
require.Nil(t, err)
2020-04-17 03:21:17 +00:00
assert.Contains(t, string(out), VERSION)
assert.Contains(t, utils.Directory, string(out))
assert.Contains(t, "SAMPLE_DATA=true", string(out))
}
func TestVersionCLI(t *testing.T) {
2020-04-16 09:57:00 +00:00
cmd := rootCmd
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetArgs([]string{"version"})
2020-04-17 03:21:17 +00:00
err := cmd.Execute()
require.Nil(t, err)
2020-04-16 09:57:00 +00:00
out, err := ioutil.ReadAll(b)
2020-04-17 03:21:17 +00:00
require.Nil(t, err)
assert.Contains(t, VERSION, string(out))
}
func TestAssetsCLI(t *testing.T) {
2020-04-16 09:57:00 +00:00
cmd := rootCmd
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetArgs([]string{"assets"})
2020-04-25 01:52:53 +00:00
cmd.Execute()
2020-04-16 09:57:00 +00:00
out, err := ioutil.ReadAll(b)
assert.Nil(t, err)
assert.Contains(t, string(out), VERSION)
2020-04-17 03:21:17 +00:00
assert.FileExists(t, utils.Directory+"/assets/css/main.css")
assert.FileExists(t, utils.Directory+"/assets/css/style.css")
assert.FileExists(t, utils.Directory+"/assets/css/vendor.css")
assert.FileExists(t, utils.Directory+"/assets/scss/base.scss")
assert.FileExists(t, utils.Directory+"/assets/scss/mobile.scss")
assert.FileExists(t, utils.Directory+"/assets/scss/variables.scss")
}
2020-04-17 03:21:17 +00:00
func TestHelpCLI(t *testing.T) {
cmd := rootCmd
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetArgs([]string{"help"})
err := cmd.Execute()
require.Nil(t, err)
out, err := ioutil.ReadAll(b)
require.Nil(t, err)
assert.Contains(t, string(out), VERSION)
}
2020-04-17 03:21:17 +00:00
func TestResetCLI(t *testing.T) {
err := utils.SaveFile(utils.Directory+"/statping.db", []byte("test data"))
require.Nil(t, err)
2020-04-17 03:21:17 +00:00
cmd := rootCmd
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetArgs([]string{"reset"})
err = cmd.Execute()
require.Nil(t, err)
out, err := ioutil.ReadAll(b)
require.Nil(t, err)
assert.Contains(t, string(out), VERSION)
2020-04-17 03:21:17 +00:00
assert.NoDirExists(t, utils.Directory+"/assets")
assert.NoDirExists(t, utils.Directory+"/logs")
assert.NoFileExists(t, utils.Directory+"/config.yml")
assert.NoFileExists(t, utils.Directory+"/statping.db")
assert.FileExists(t, utils.Directory+"/statping.db.backup")
2020-04-17 03:21:17 +00:00
err = utils.DeleteFile(utils.Directory + "/statping.db.backup")
require.Nil(t, err)
}