statping/source/source_test.go

111 lines
2.3 KiB
Go
Raw Normal View History

2018-08-11 16:24:32 +00:00
package source
import (
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/utils"
2018-08-11 16:24:32 +00:00
"github.com/stretchr/testify/assert"
2020-02-19 04:07:22 +00:00
"github.com/stretchr/testify/require"
2018-08-11 16:24:32 +00:00
"testing"
)
var (
2020-08-03 10:35:48 +00:00
dir string
2018-08-11 16:24:32 +00:00
)
func init() {
dir = utils.Directory
utils.InitLogs()
Assets()
2019-12-30 03:34:49 +00:00
utils.DeleteDirectory(dir + "/assets")
2020-04-17 07:21:59 +00:00
dir = utils.Params.GetString("STATPING_DIR")
2018-08-11 16:24:32 +00:00
}
2020-08-03 10:35:48 +00:00
func assertFiles(t *testing.T, exist bool) {
for _, f := range RequiredFiles {
if exist {
assert.FileExists(t, dir+"/assets/"+f)
} else {
assert.NoFileExists(t, dir+"/assets/"+f)
}
}
}
2018-08-11 16:24:32 +00:00
func TestCore_UsingAssets(t *testing.T) {
2018-08-16 20:55:30 +00:00
assert.False(t, UsingAssets(dir))
2020-08-03 10:35:48 +00:00
assertFiles(t, false)
2018-08-11 16:24:32 +00:00
}
func TestCreateAssets(t *testing.T) {
2020-07-30 23:14:41 +00:00
assert.Nil(t, CreateAllAssets(dir))
2018-08-16 20:55:30 +00:00
assert.True(t, UsingAssets(dir))
assert.Nil(t, CompileSASS())
2020-08-03 10:35:48 +00:00
assertFiles(t, true)
2020-02-19 04:07:22 +00:00
}
2018-08-11 16:24:32 +00:00
2020-07-30 23:14:41 +00:00
func TestCopyAllToPublic(t *testing.T) {
err := CopyAllToPublic(TmplBox)
require.Nil(t, err)
2020-08-03 10:35:48 +00:00
assertFiles(t, true)
2020-07-30 23:14:41 +00:00
}
2020-03-02 07:53:46 +00:00
2018-08-11 16:24:32 +00:00
func TestCompileSASS(t *testing.T) {
err := CompileSASS()
require.Nil(t, err)
2018-08-16 20:55:30 +00:00
assert.True(t, UsingAssets(dir))
2020-08-03 10:35:48 +00:00
assertFiles(t, true)
2018-08-11 16:24:32 +00:00
}
2020-03-02 07:53:46 +00:00
func TestSaveAndCompileAsset(t *testing.T) {
2020-08-03 10:35:48 +00:00
vars := OpenAsset("scss/variables.scss")
vars += "$testingcolor: #b1b2b3;"
err := SaveAsset([]byte(vars), "scss/variables.scss")
require.Nil(t, err)
assert.FileExists(t, dir+"/assets/scss/variables.scss")
2020-03-02 07:53:46 +00:00
2020-08-03 10:35:48 +00:00
scssData := OpenAsset("scss/base.scss")
scssData += "BODY { color: $testingcolor; }"
err = SaveAsset([]byte(scssData), "scss/base.scss")
2020-03-02 07:53:46 +00:00
require.Nil(t, err)
assert.FileExists(t, dir+"/assets/scss/base.scss")
2020-03-02 07:53:46 +00:00
2020-08-03 10:35:48 +00:00
asset := OpenAsset("scss/variables.scss")
assert.NotEmpty(t, asset)
assert.Equal(t, vars, asset)
asset = OpenAsset("scss/base.scss")
2020-03-02 07:53:46 +00:00
assert.NotEmpty(t, asset)
assert.Equal(t, scssData, asset)
err = CompileSASS()
2020-03-02 07:53:46 +00:00
require.Nil(t, err)
2020-08-03 10:35:48 +00:00
assertFiles(t, true)
2020-03-02 07:53:46 +00:00
2020-08-06 07:58:00 +00:00
themeCSS, err := utils.OpenFile(dir + "/assets/css/index.css")
2020-03-02 07:53:46 +00:00
require.Nil(t, err)
2020-08-03 10:35:48 +00:00
assert.Contains(t, themeCSS, `color: #b1b2b3;`)
2018-08-16 02:22:10 +00:00
}
func TestOpenAsset(t *testing.T) {
2020-08-03 10:35:48 +00:00
for _, f := range RequiredFiles {
assert.FileExists(t, dir+"/assets/"+f)
assert.NotEmpty(t, OpenAsset(f))
}
2018-08-16 02:22:10 +00:00
}
2018-08-11 16:24:32 +00:00
func TestDeleteAssets(t *testing.T) {
2020-02-19 04:07:22 +00:00
assert.True(t, UsingAssets(dir))
2020-03-03 06:42:37 +00:00
assert.Nil(t, DeleteAllAssets(dir))
2018-08-16 20:55:30 +00:00
assert.False(t, UsingAssets(dir))
2020-08-03 10:35:48 +00:00
assertFiles(t, false)
2018-08-16 02:22:10 +00:00
}
func ExampleSaveAsset() {
data := []byte("alert('helloooo')")
2020-02-19 04:07:22 +00:00
SaveAsset(data, "js/test.js")
}
func ExampleOpenAsset() {
2020-02-19 04:07:22 +00:00
OpenAsset("js/main.js")
}