github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/resources/resource_transformers/postcss/postcss_test.go (about) 1 // Copyright 2020 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package postcss 15 16 import ( 17 "regexp" 18 "strings" 19 "testing" 20 21 "github.com/gohugoio/hugo/htesting/hqt" 22 23 "github.com/gohugoio/hugo/common/loggers" 24 "github.com/gohugoio/hugo/helpers" 25 26 "github.com/spf13/afero" 27 28 qt "github.com/frankban/quicktest" 29 ) 30 31 // Issue 6166 32 func TestDecodeOptions(t *testing.T) { 33 c := qt.New(t) 34 opts1, err := DecodeOptions(map[string]interface{}{ 35 "no-map": true, 36 }) 37 38 c.Assert(err, qt.IsNil) 39 c.Assert(opts1.NoMap, qt.Equals, true) 40 41 opts2, err := DecodeOptions(map[string]interface{}{ 42 "noMap": true, 43 }) 44 45 c.Assert(err, qt.IsNil) 46 c.Assert(opts2.NoMap, qt.Equals, true) 47 } 48 49 func TestShouldImport(t *testing.T) { 50 c := qt.New(t) 51 var imp *importResolver 52 53 for _, test := range []struct { 54 input string 55 expect bool 56 }{ 57 {input: `@import "navigation.css";`, expect: true}, 58 {input: `@import "navigation.css"; /* Using a string */`, expect: true}, 59 {input: `@import "navigation.css"`, expect: true}, 60 {input: `@import 'navigation.css';`, expect: true}, 61 {input: `@import url("navigation.css");`, expect: false}, 62 {input: `@import url('https://fonts.googleapis.com/css?family=Open+Sans:400,400i,800,800i&display=swap');`, expect: false}, 63 } { 64 c.Assert(imp.shouldImport(test.input), qt.Equals, test.expect) 65 } 66 } 67 68 func TestImportResolver(t *testing.T) { 69 c := qt.New(t) 70 fs := afero.NewMemMapFs() 71 72 writeFile := func(name, content string) { 73 c.Assert(afero.WriteFile(fs, name, []byte(content), 0777), qt.IsNil) 74 } 75 76 writeFile("a.css", `@import "b.css"; 77 @import "c.css"; 78 A_STYLE1 79 A_STYLE2 80 `) 81 82 writeFile("b.css", `B_STYLE`) 83 writeFile("c.css", "@import \"d.css\"\nC_STYLE") 84 writeFile("d.css", "@import \"a.css\"\n\nD_STYLE") 85 writeFile("e.css", "E_STYLE") 86 87 mainStyles := strings.NewReader(`@import "a.css"; 88 @import "b.css"; 89 LOCAL_STYLE 90 @import "c.css"; 91 @import "e.css"; 92 @import "missing.css";`) 93 94 imp := newImportResolver( 95 mainStyles, 96 "styles.css", 97 fs, loggers.NewErrorLogger(), 98 ) 99 100 r, err := imp.resolve() 101 c.Assert(err, qt.IsNil) 102 rs := helpers.ReaderToString(r) 103 result := regexp.MustCompile(`\n+`).ReplaceAllString(rs, "\n") 104 105 c.Assert(result, hqt.IsSameString, `B_STYLE 106 D_STYLE 107 C_STYLE 108 A_STYLE1 109 A_STYLE2 110 LOCAL_STYLE 111 E_STYLE 112 @import "missing.css";`) 113 114 dline := imp.linemap[3] 115 c.Assert(dline, qt.DeepEquals, fileOffset{ 116 Offset: 1, 117 Filename: "d.css", 118 }) 119 } 120 121 func BenchmarkImportResolver(b *testing.B) { 122 c := qt.New(b) 123 fs := afero.NewMemMapFs() 124 125 writeFile := func(name, content string) { 126 c.Assert(afero.WriteFile(fs, name, []byte(content), 0777), qt.IsNil) 127 } 128 129 writeFile("a.css", `@import "b.css"; 130 @import "c.css"; 131 A_STYLE1 132 A_STYLE2 133 `) 134 135 writeFile("b.css", `B_STYLE`) 136 writeFile("c.css", "@import \"d.css\"\nC_STYLE"+strings.Repeat("\nSTYLE", 12)) 137 writeFile("d.css", "@import \"a.css\"\n\nD_STYLE"+strings.Repeat("\nSTYLE", 55)) 138 writeFile("e.css", "E_STYLE") 139 140 mainStyles := `@import "a.css"; 141 @import "b.css"; 142 LOCAL_STYLE 143 @import "c.css"; 144 @import "e.css"; 145 @import "missing.css";` 146 147 logger := loggers.NewErrorLogger() 148 149 for i := 0; i < b.N; i++ { 150 b.StopTimer() 151 imp := newImportResolver( 152 strings.NewReader(mainStyles), 153 "styles.css", 154 fs, logger, 155 ) 156 157 b.StartTimer() 158 159 _, err := imp.resolve() 160 if err != nil { 161 b.Fatal(err) 162 } 163 164 } 165 }