Add minify config

Fixes #6750
Updates #6892
This commit is contained in:
SatowTakeshi
2020-02-29 18:44:05 +09:00
committed by Bjørn Erik Pedersen
parent 99958f90fe
commit 574c2959b8
12 changed files with 346 additions and 47 deletions

View File

@@ -23,11 +23,13 @@ import (
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/output"
"github.com/spf13/viper"
)
func TestNew(t *testing.T) {
c := qt.New(t)
m := New(media.DefaultTypes, output.DefaultFormats)
v := viper.New()
m, _ := New(media.DefaultTypes, output.DefaultFormats, v)
var rawJS string
var minJS string
@@ -73,9 +75,44 @@ func TestNew(t *testing.T) {
}
func TestConfiguredMinify(t *testing.T) {
c := qt.New(t)
v := viper.New()
v.Set("minifiers", map[string]interface{}{
"enablexml": false,
"tdewolff": map[string]interface{}{
"html": map[string]interface{}{
"keepwhitespace": true,
},
},
})
m, _ := New(media.DefaultTypes, output.DefaultFormats, v)
for _, test := range []struct {
tp media.Type
rawString string
expectedMinString string
errorExpected bool
}{
{media.HTMLType, "<hello> Hugo! </hello>", "<hello> Hugo! </hello>", false}, // configured minifier
{media.CSSType, " body { color: blue; } ", "body{color:blue}", false}, // default minifier
{media.XMLType, " <hello> Hugo! </hello> ", "", true}, // disable Xml minificatin
} {
var b bytes.Buffer
if !test.errorExpected {
c.Assert(m.Minify(test.tp, &b, strings.NewReader(test.rawString)), qt.IsNil)
c.Assert(b.String(), qt.Equals, test.expectedMinString)
} else {
err := m.Minify(test.tp, &b, strings.NewReader(test.rawString))
c.Assert(err, qt.ErrorMatches, "minifier does not exist for mimetype")
}
}
}
func TestJSONRoundTrip(t *testing.T) {
c := qt.New(t)
m := New(media.DefaultTypes, output.DefaultFormats)
v := viper.New()
m, _ := New(media.DefaultTypes, output.DefaultFormats, v)
for _, test := range []string{`{
"glossary": {
@@ -113,7 +150,8 @@ func TestJSONRoundTrip(t *testing.T) {
func TestBugs(t *testing.T) {
c := qt.New(t)
m := New(media.DefaultTypes, output.DefaultFormats)
v := viper.New()
m, _ := New(media.DefaultTypes, output.DefaultFormats, v)
for _, test := range []struct {
tp media.Type