summaryrefslogtreecommitdiffstats
path: root/minifiers/minifiers_test.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2023-01-04 18:24:36 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2023-05-16 18:01:29 +0200
commit241b21b0fd34d91fccb2ce69874110dceae6f926 (patch)
treed4e0118eac7e9c42f065815447a70805f8d6ad3e /minifiers/minifiers_test.go
parent6aededf6b42011c3039f5f66487a89a8dd65e0e7 (diff)
Create a struct with all of Hugo's config options
Primary motivation is documentation, but it will also hopefully simplify the code. Also, * Lower case the default output format names; this is in line with the custom ones (map keys) and how it's treated all the places. This avoids doing `stringds.EqualFold` everywhere. Closes #10896 Closes #10620
Diffstat (limited to 'minifiers/minifiers_test.go')
-rw-r--r--minifiers/minifiers_test.go73
1 files changed, 34 insertions, 39 deletions
diff --git a/minifiers/minifiers_test.go b/minifiers/minifiers_test.go
index 1096ca2d1..4d5d9feb5 100644
--- a/minifiers/minifiers_test.go
+++ b/minifiers/minifiers_test.go
@@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-package minifiers
+package minifiers_test
import (
"bytes"
@@ -21,15 +21,17 @@ import (
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/config"
+ "github.com/gohugoio/hugo/config/testconfig"
"github.com/gohugoio/hugo/media"
+ "github.com/gohugoio/hugo/minifiers"
"github.com/gohugoio/hugo/output"
+ "github.com/spf13/afero"
"github.com/tdewolff/minify/v2/html"
)
func TestNew(t *testing.T) {
c := qt.New(t)
- v := config.NewWithTestDefaults()
- m, _ := New(media.DefaultTypes, output.DefaultFormats, v)
+ m, _ := minifiers.New(media.DefaultTypes, output.DefaultFormats, testconfig.GetTestConfig(afero.NewMemMapFs(), nil))
var rawJS string
var minJS string
@@ -46,26 +48,22 @@ func TestNew(t *testing.T) {
rawString string
expectedMinString string
}{
- {media.CSSType, " body { color: blue; } ", "body{color:blue}"},
- {media.RSSType, " <hello> Hugo! </hello> ", "<hello>Hugo!</hello>"}, // RSS should be handled as XML
- {media.JSONType, rawJSON, minJSON},
- {media.JavascriptType, rawJS, minJS},
+ {media.Builtin.CSSType, " body { color: blue; } ", "body{color:blue}"},
+ {media.Builtin.RSSType, " <hello> Hugo! </hello> ", "<hello>Hugo!</hello>"}, // RSS should be handled as XML
+ {media.Builtin.JSONType, rawJSON, minJSON},
+ {media.Builtin.JavascriptType, rawJS, minJS},
// JS Regex minifiers
- {media.Type{MainType: "application", SubType: "ecmascript"}, rawJS, minJS},
- {media.Type{MainType: "application", SubType: "javascript"}, rawJS, minJS},
- {media.Type{MainType: "application", SubType: "x-javascript"}, rawJS, minJS},
- {media.Type{MainType: "application", SubType: "x-ecmascript"}, rawJS, minJS},
- {media.Type{MainType: "text", SubType: "ecmascript"}, rawJS, minJS},
- {media.Type{MainType: "text", SubType: "javascript"}, rawJS, minJS},
- {media.Type{MainType: "text", SubType: "x-javascript"}, rawJS, minJS},
- {media.Type{MainType: "text", SubType: "x-ecmascript"}, rawJS, minJS},
+ {media.Type{Type: "application/ecmascript", MainType: "application", SubType: "ecmascript"}, rawJS, minJS},
+ {media.Type{Type: "application/javascript", MainType: "application", SubType: "javascript"}, rawJS, minJS},
+ {media.Type{Type: "application/x-javascript", MainType: "application", SubType: "x-javascript"}, rawJS, minJS},
+ {media.Type{Type: "application/x-ecmascript", MainType: "application", SubType: "x-ecmascript"}, rawJS, minJS},
+ {media.Type{Type: "text/ecmascript", MainType: "text", SubType: "ecmascript"}, rawJS, minJS},
+ {media.Type{Type: "application/javascript", MainType: "text", SubType: "javascript"}, rawJS, minJS},
// JSON Regex minifiers
- {media.Type{MainType: "application", SubType: "json"}, rawJSON, minJSON},
- {media.Type{MainType: "application", SubType: "x-json"}, rawJSON, minJSON},
- {media.Type{MainType: "application", SubType: "ld+json"}, rawJSON, minJSON},
- {media.Type{MainType: "text", SubType: "json"}, rawJSON, minJSON},
- {media.Type{MainType: "text", SubType: "x-json"}, rawJSON, minJSON},
- {media.Type{MainType: "text", SubType: "ld+json"}, rawJSON, minJSON},
+ {media.Type{Type: "application/json", MainType: "application", SubType: "json"}, rawJSON, minJSON},
+ {media.Type{Type: "application/x-json", MainType: "application", SubType: "x-json"}, rawJSON, minJSON},
+ {media.Type{Type: "application/ld+json", MainType: "application", SubType: "ld+json"}, rawJSON, minJSON},
+ {media.Type{Type: "application/json", MainType: "text", SubType: "json"}, rawJSON, minJSON},
} {
var b bytes.Buffer
@@ -76,7 +74,7 @@ func TestNew(t *testing.T) {
func TestConfigureMinify(t *testing.T) {
c := qt.New(t)
- v := config.NewWithTestDefaults()
+ v := config.New()
v.Set("minify", map[string]any{
"disablexml": true,
"tdewolff": map[string]any{
@@ -85,7 +83,7 @@ func TestConfigureMinify(t *testing.T) {
},
},
})
- m, _ := New(media.DefaultTypes, output.DefaultFormats, v)
+ m, _ := minifiers.New(media.DefaultTypes, output.DefaultFormats, testconfig.GetTestConfig(afero.NewMemMapFs(), v))
for _, test := range []struct {
tp media.Type
@@ -93,9 +91,9 @@ func TestConfigureMinify(t *testing.T) {
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> ", " <hello> Hugo! </hello> ", false}, // disable Xml minification
+ {media.Builtin.HTMLType, "<hello> Hugo! </hello>", "<hello> Hugo! </hello>", false}, // configured minifier
+ {media.Builtin.CSSType, " body { color: blue; } ", "body{color:blue}", false}, // default minifier
+ {media.Builtin.XMLType, " <hello> Hugo! </hello> ", " <hello> Hugo! </hello> ", false}, // disable Xml minification
} {
var b bytes.Buffer
if !test.errorExpected {
@@ -110,8 +108,7 @@ func TestConfigureMinify(t *testing.T) {
func TestJSONRoundTrip(t *testing.T) {
c := qt.New(t)
- v := config.NewWithTestDefaults()
- m, _ := New(media.DefaultTypes, output.DefaultFormats, v)
+ m, _ := minifiers.New(media.DefaultTypes, output.DefaultFormats, testconfig.GetTestConfig(nil, nil))
for _, test := range []string{`{
"glossary": {
@@ -140,7 +137,7 @@ func TestJSONRoundTrip(t *testing.T) {
m1 := make(map[string]any)
m2 := make(map[string]any)
c.Assert(json.Unmarshal([]byte(test), &m1), qt.IsNil)
- c.Assert(m.Minify(media.JSONType, &b, strings.NewReader(test)), qt.IsNil)
+ c.Assert(m.Minify(media.Builtin.JSONType, &b, strings.NewReader(test)), qt.IsNil)
c.Assert(json.Unmarshal(b.Bytes(), &m2), qt.IsNil)
c.Assert(m1, qt.DeepEquals, m2)
}
@@ -148,8 +145,8 @@ func TestJSONRoundTrip(t *testing.T) {
func TestBugs(t *testing.T) {
c := qt.New(t)
- v := config.NewWithTestDefaults()
- m, _ := New(media.DefaultTypes, output.DefaultFormats, v)
+ v := config.New()
+ m, _ := minifiers.New(media.DefaultTypes, output.DefaultFormats, testconfig.GetTestConfig(nil, v))
for _, test := range []struct {
tp media.Type
@@ -157,9 +154,9 @@ func TestBugs(t *testing.T) {
expectedMinString string
}{
// https://github.com/gohugoio/hugo/issues/5506
- {media.CSSType, " body { color: rgba(000, 000, 000, 0.7); }", "body{color:rgba(0,0,0,.7)}"},
+ {media.Builtin.CSSType, " body { color: rgba(000, 000, 000, 0.7); }", "body{color:rgba(0,0,0,.7)}"},
// https://github.com/gohugoio/hugo/issues/8332
- {media.HTMLType, "<i class='fas fa-tags fa-fw'></i> Tags", `<i class='fas fa-tags fa-fw'></i> Tags`},
+ {media.Builtin.HTMLType, "<i class='fas fa-tags fa-fw'></i> Tags", `<i class='fas fa-tags fa-fw'></i> Tags`},
} {
var b bytes.Buffer
@@ -171,7 +168,7 @@ func TestBugs(t *testing.T) {
// Renamed to Precision in v2.7.0. Check that we support both.
func TestDecodeConfigDecimalIsNowPrecision(t *testing.T) {
c := qt.New(t)
- v := config.NewWithTestDefaults()
+ v := config.New()
v.Set("minify", map[string]any{
"disablexml": true,
"tdewolff": map[string]any{
@@ -184,9 +181,8 @@ func TestDecodeConfigDecimalIsNowPrecision(t *testing.T) {
},
})
- conf, err := decodeConfig(v)
+ conf := testconfig.GetTestConfigs(nil, v).Base.Minify
- c.Assert(err, qt.IsNil)
c.Assert(conf.Tdewolff.CSS.Precision, qt.Equals, 3)
}
@@ -194,7 +190,7 @@ func TestDecodeConfigDecimalIsNowPrecision(t *testing.T) {
// Issue 9456
func TestDecodeConfigKeepWhitespace(t *testing.T) {
c := qt.New(t)
- v := config.NewWithTestDefaults()
+ v := config.New()
v.Set("minify", map[string]any{
"tdewolff": map[string]any{
"html": map[string]any{
@@ -203,9 +199,8 @@ func TestDecodeConfigKeepWhitespace(t *testing.T) {
},
})
- conf, err := decodeConfig(v)
+ conf := testconfig.GetTestConfigs(nil, v).Base.Minify
- c.Assert(err, qt.IsNil)
c.Assert(conf.Tdewolff.HTML, qt.DeepEquals,
html.Minifier{
KeepComments: false,