| 1 |
// Copyright 2024 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 cssjs |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"regexp" |
| 19 |
"strings" |
| 20 |
"testing" |
| 21 |
|
| 22 |
"github.com/gohugoio/hugo/common/loggers" |
| 23 |
"github.com/gohugoio/hugo/htesting/hqt" |
| 24 |
"github.com/gohugoio/hugo/identity" |
| 25 |
"github.com/gohugoio/hugo/resources/resource" |
| 26 |
|
| 27 |
"github.com/gohugoio/hugo/helpers" |
| 28 |
|
| 29 |
"github.com/spf13/afero" |
| 30 |
"github.com/spf13/cast" |
| 31 |
|
| 32 |
qt "github.com/frankban/quicktest" |
| 33 |
) |
| 34 |
|
| 35 |
// Issue 6166 |
| 36 |
func TestDecodeOptions(t *testing.T) { |
| 37 |
c := qt.New(t) |
| 38 |
opts1, err := decodePostCSSOptions(map[string]any{ |
| 39 |
"no-map": true, |
| 40 |
}) |
| 41 |
|
| 42 |
c.Assert(err, qt.IsNil) |
| 43 |
c.Assert(opts1.NoMap, qt.Equals, true) |
| 44 |
|
| 45 |
opts2, err := decodePostCSSOptions(map[string]any{ |
| 46 |
"noMap": true, |
| 47 |
}) |
| 48 |
|
| 49 |
c.Assert(err, qt.IsNil) |
| 50 |
c.Assert(opts2.NoMap, qt.Equals, true) |
| 51 |
} |
| 52 |
|
| 53 |
func TestShouldImport(t *testing.T) { |
| 54 |
c := qt.New(t) |
| 55 |
var imp *importResolver |
| 56 |
|
| 57 |
for _, test := range []struct { |
| 58 |
input string |
| 59 |
expect bool |
| 60 |
}{ |
| 61 |
{input: `@import "navigation.css";`, expect: true}, |
| 62 |
{input: `@import "navigation.css"; /* Using a string */`, expect: true}, |
| 63 |
{input: `@import "navigation.css"`, expect: true}, |
| 64 |
{input: `@import 'navigation.css';`, expect: true}, |
| 65 |
{input: `@import url("navigation.css");`, expect: false}, |
| 66 |
{input: `@import url('https://fonts.googleapis.com/css?family=Open+Sans:400,400i,800,800i&display=swap');`, expect: false}, |
| 67 |
{input: `@import "printstyle.css" print;`, expect: false}, |
| 68 |
} { |
| 69 |
c.Assert(imp.shouldImport(test.input), qt.Equals, test.expect) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
func TestShouldImportExcludes(t *testing.T) { |
| 74 |
c := qt.New(t) |
| 75 |
var imp *importResolver |
| 76 |
|
| 77 |
c.Assert(imp.shouldImport(`@import "navigation.css";`), qt.Equals, true) |
| 78 |
c.Assert(imp.shouldImport(`@import "tailwindcss";`), qt.Equals, false) |
| 79 |
c.Assert(imp.shouldImport(`@import "tailwindcss.css";`), qt.Equals, true) |
| 80 |
c.Assert(imp.shouldImport(`@import "tailwindcss/preflight";`), qt.Equals, false) |
| 81 |
} |
| 82 |
|
| 83 |
func TestImportResolver(t *testing.T) { |
| 84 |
c := qt.New(t) |
| 85 |
fs := afero.NewMemMapFs() |
| 86 |
|
| 87 |
writeFile := func(name, content string) { |
| 88 |
c.Assert(afero.WriteFile(fs, name, []byte(content), 0o777), qt.IsNil) |
| 89 |
} |
| 90 |
|
| 91 |
writeFile("a.css", `@import "b.css"; |
| 92 |
@import "c.css"; |
| 93 |
A_STYLE1 |
| 94 |
A_STYLE2 |
| 95 |
`) |
| 96 |
|
| 97 |
writeFile("b.css", `B_STYLE`) |
| 98 |
writeFile("c.css", "@import \"d.css\"\nC_STYLE") |
| 99 |
writeFile("d.css", "@import \"a.css\"\n\nD_STYLE") |
| 100 |
writeFile("e.css", "E_STYLE") |
| 101 |
|
| 102 |
mainStyles := strings.NewReader(`@import "a.css"; |
| 103 |
@import "b.css"; |
| 104 |
LOCAL_STYLE |
| 105 |
@import "c.css"; |
| 106 |
@import "e.css";`) |
| 107 |
|
| 108 |
imp := newImportResolver( |
| 109 |
t.Context(), |
| 110 |
mainStyles, |
| 111 |
"styles.css", |
| 112 |
InlineImports{}, |
| 113 |
fs, loggers.NewDefault(), |
| 114 |
identity.NopManager, |
| 115 |
) |
| 116 |
|
| 117 |
r, err := imp.resolve() |
| 118 |
c.Assert(err, qt.IsNil) |
| 119 |
rs := helpers.ReaderToString(r) |
| 120 |
result := regexp.MustCompile(`\n+`).ReplaceAllString(rs, "\n") |
| 121 |
|
| 122 |
c.Assert(result, hqt.IsSameString, `B_STYLE |
| 123 |
D_STYLE |
| 124 |
C_STYLE |
| 125 |
A_STYLE1 |
| 126 |
A_STYLE2 |
| 127 |
LOCAL_STYLE |
| 128 |
E_STYLE`) |
| 129 |
|
| 130 |
dline := imp.linemap[3] |
| 131 |
c.Assert(dline, qt.DeepEquals, fileOffset{ |
| 132 |
Offset: 1, |
| 133 |
Filename: "d.css", |
| 134 |
}) |
| 135 |
} |
| 136 |
|
| 137 |
func TestImportResolverImportContext(t *testing.T) { |
| 138 |
c := qt.New(t) |
| 139 |
fs := afero.NewMemMapFs() |
| 140 |
|
| 141 |
writeFile := func(name, content string) { |
| 142 |
c.Assert(afero.WriteFile(fs, name, []byte(content), 0o777), qt.IsNil) |
| 143 |
} |
| 144 |
|
| 145 |
// Loses to the import context entry with the same name. |
| 146 |
writeFile("css/a.css", "A_STYLE_FS") |
| 147 |
writeFile("css/b.css", "B_STYLE_FS") |
| 148 |
|
| 149 |
importContext := testImportContext{ |
| 150 |
"a.css": "@import \"c.css\";\nA_STYLE", |
| 151 |
"c.css": "C_STYLE", |
| 152 |
"css/d.css": "D_STYLE", |
| 153 |
} |
| 154 |
|
| 155 |
mainStyles := strings.NewReader(`@import "a.css"; |
| 156 |
@import "b.css"; |
| 157 |
@import "./d.css"; |
| 158 |
LOCAL_STYLE`) |
| 159 |
|
| 160 |
imp := newImportResolver( |
| 161 |
t.Context(), |
| 162 |
mainStyles, |
| 163 |
"css/styles.css", |
| 164 |
InlineImports{ImportContext: importContext}, |
| 165 |
fs, loggers.NewDefault(), |
| 166 |
identity.NopManager, |
| 167 |
) |
| 168 |
|
| 169 |
r, err := imp.resolve() |
| 170 |
c.Assert(err, qt.IsNil) |
| 171 |
rs := helpers.ReaderToString(r) |
| 172 |
result := regexp.MustCompile(`\n+`).ReplaceAllString(rs, "\n") |
| 173 |
|
| 174 |
c.Assert(result, hqt.IsSameString, `C_STYLE |
| 175 |
A_STYLE |
| 176 |
B_STYLE_FS |
| 177 |
D_STYLE |
| 178 |
LOCAL_STYLE`) |
| 179 |
} |
| 180 |
|
| 181 |
type testImportContext map[string]string |
| 182 |
|
| 183 |
func (g testImportContext) Get(name any) resource.Resource { |
| 184 |
s := cast.ToString(name) |
| 185 |
if content, found := g[s]; found { |
| 186 |
return testImportContextResource{name: s, content: content} |
| 187 |
} |
| 188 |
return nil |
| 189 |
} |
| 190 |
|
| 191 |
type testImportContextResource struct { |
| 192 |
resource.Resource |
| 193 |
name string |
| 194 |
content string |
| 195 |
} |
| 196 |
|
| 197 |
func (r testImportContextResource) Name() string { return r.name } |
| 198 |
|
| 199 |
func (r testImportContextResource) Content(context.Context) (any, error) { return r.content, nil } |
| 200 |
|
| 201 |
func BenchmarkImportResolver(b *testing.B) { |
| 202 |
c := qt.New(b) |
| 203 |
fs := afero.NewMemMapFs() |
| 204 |
|
| 205 |
writeFile := func(name, content string) { |
| 206 |
c.Assert(afero.WriteFile(fs, name, []byte(content), 0o777), qt.IsNil) |
| 207 |
} |
| 208 |
|
| 209 |
writeFile("a.css", `@import "b.css"; |
| 210 |
@import "c.css"; |
| 211 |
A_STYLE1 |
| 212 |
A_STYLE2 |
| 213 |
`) |
| 214 |
|
| 215 |
writeFile("b.css", `B_STYLE`) |
| 216 |
writeFile("c.css", "@import \"d.css\"\nC_STYLE"+strings.Repeat("\nSTYLE", 12)) |
| 217 |
writeFile("d.css", "@import \"a.css\"\n\nD_STYLE"+strings.Repeat("\nSTYLE", 55)) |
| 218 |
writeFile("e.css", "E_STYLE") |
| 219 |
|
| 220 |
mainStyles := `@import "a.css"; |
| 221 |
@import "b.css"; |
| 222 |
LOCAL_STYLE |
| 223 |
@import "c.css"; |
| 224 |
@import "e.css"; |
| 225 |
@import "missing.css";` |
| 226 |
|
| 227 |
logger := loggers.NewDefault() |
| 228 |
|
| 229 |
for b.Loop() { |
| 230 |
b.StopTimer() |
| 231 |
imp := newImportResolver( |
| 232 |
b.Context(), |
| 233 |
strings.NewReader(mainStyles), |
| 234 |
"styles.css", |
| 235 |
InlineImports{}, |
| 236 |
fs, logger, |
| 237 |
identity.NopManager, |
| 238 |
) |
| 239 |
|
| 240 |
b.StartTimer() |
| 241 |
|
| 242 |
_, err := imp.resolve() |
| 243 |
if err != nil { |
| 244 |
b.Fatal(err) |
| 245 |
} |
| 246 |
|
| 247 |
} |
| 248 |
} |