statping/source/source_test.go

118 lines
2.4 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 (
dir string
requiredFiles = []string{
"css/style.css",
"css/style.css.gz",
"css/main.css",
"scss/main.scss",
"scss/base.scss",
"scss/forms.scss",
"scss/layout.scss",
"scss/mixin.scss",
"scss/mobile.scss",
"scss/variables.scss",
"js/bundle.js",
"js/main.chunk.js",
"js/polyfill.chunk.js",
"js/style.chunk.js",
"banner.png",
"favicon.ico",
"robots.txt",
}
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
}
func assetFiles(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))
assetFiles(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())
assetFiles(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)
assetFiles(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))
assetFiles(t, true)
2018-08-11 16:24:32 +00:00
}
2020-03-02 07:53:46 +00:00
func TestSaveAndCompileAsset(t *testing.T) {
scssData := "$bodycolor: #333; BODY { color: $bodycolor; }"
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
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)
assert.FileExists(t, dir+"/assets/css/main.css")
2020-03-02 07:53:46 +00:00
themeCSS, err := utils.OpenFile(dir + "/assets/css/main.css")
2020-03-02 07:53:46 +00:00
require.Nil(t, err)
2020-03-03 06:42:37 +00:00
assert.Contains(t, themeCSS, `color: #333;`)
2018-08-16 02:22:10 +00:00
}
func TestOpenAsset(t *testing.T) {
for _, f := range requiredFiles {
asset := OpenAsset(f)
assert.NotEmpty(t, asset)
}
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))
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")
}