inline_imports_test.go - hugo - [fork] hugo port for 9front
HTML git clone https://git.drkhsh.at/hugo.git
DIR Log
DIR Files
DIR Refs
DIR Submodules
DIR README
DIR LICENSE
---
inline_imports_test.go (4337B)
---
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 "regexp"
18 "strings"
19 "testing"
20
21 "github.com/gohugoio/hugo/common/loggers"
22 "github.com/gohugoio/hugo/htesting/hqt"
23 "github.com/gohugoio/hugo/identity"
24
25 "github.com/gohugoio/hugo/helpers"
26
27 "github.com/spf13/afero"
28
29 qt "github.com/frankban/quicktest"
30 )
31
32 // Issue 6166
33 func TestDecodeOptions(t *testing.T) {
34 c := qt.New(t)
35 opts1, err := decodePostCSSOptions(map[string]any{
36 "no-map": true,
37 })
38
39 c.Assert(err, qt.IsNil)
40 c.Assert(opts1.NoMap, qt.Equals, true)
41
42 opts2, err := decodePostCSSOptions(map[string]any{
43 "noMap": true,
44 })
45
46 c.Assert(err, qt.IsNil)
47 c.Assert(opts2.NoMap, qt.Equals, true)
48 }
49
50 func TestShouldImport(t *testing.T) {
51 c := qt.New(t)
52 var imp *importResolver
53
54 for _, test := range []struct {
55 input string
56 expect bool
57 }{
58 {input: `@import "navigation.css";`, expect: true},
59 {input: `@import "navigation.css"; /* Using a string */`, expect: true},
60 {input: `@import "navigation.css"`, expect: true},
61 {input: `@import 'navigation.css';`, expect: true},
62 {input: `@import url("navigation.css");`, expect: false},
63 {input: `@import url('https://fonts.googleapis.com/css?family=Open+Sans:400,400i,800,800i&display=swap');`, expect: false},
64 {input: `@import "printstyle.css" print;`, expect: false},
65 } {
66 c.Assert(imp.shouldImport(test.input), qt.Equals, test.expect)
67 }
68 }
69
70 func TestShouldImportExcludes(t *testing.T) {
71 c := qt.New(t)
72 var imp *importResolver
73
74 c.Assert(imp.shouldImport(`@import "navigation.css";`), qt.Equals, true)
75 c.Assert(imp.shouldImport(`@import "tailwindcss";`), qt.Equals, false)
76 c.Assert(imp.shouldImport(`@import "tailwindcss.css";`), qt.Equals, true)
77 c.Assert(imp.shouldImport(`@import "tailwindcss/preflight";`), qt.Equals, false)
78 }
79
80 func TestImportResolver(t *testing.T) {
81 c := qt.New(t)
82 fs := afero.NewMemMapFs()
83
84 writeFile := func(name, content string) {
85 c.Assert(afero.WriteFile(fs, name, []byte(content), 0o777), qt.IsNil)
86 }
87
88 writeFile("a.css", `@import "b.css";
89 @import "c.css";
90 A_STYLE1
91 A_STYLE2
92 `)
93
94 writeFile("b.css", `B_STYLE`)
95 writeFile("c.css", "@import \"d.css\"\nC_STYLE")
96 writeFile("d.css", "@import \"a.css\"\n\nD_STYLE")
97 writeFile("e.css", "E_STYLE")
98
99 mainStyles := strings.NewReader(`@import "a.css";
100 @import "b.css";
101 LOCAL_STYLE
102 @import "c.css";
103 @import "e.css";`)
104
105 imp := newImportResolver(
106 mainStyles,
107 "styles.css",
108 InlineImports{},
109 fs, loggers.NewDefault(),
110 identity.NopManager,
111 )
112
113 r, err := imp.resolve()
114 c.Assert(err, qt.IsNil)
115 rs := helpers.ReaderToString(r)
116 result := regexp.MustCompile(`\n+`).ReplaceAllString(rs, "\n")
117
118 c.Assert(result, hqt.IsSameString, `B_STYLE
119 D_STYLE
120 C_STYLE
121 A_STYLE1
122 A_STYLE2
123 LOCAL_STYLE
124 E_STYLE`)
125
126 dline := imp.linemap[3]
127 c.Assert(dline, qt.DeepEquals, fileOffset{
128 Offset: 1,
129 Filename: "d.css",
130 })
131 }
132
133 func BenchmarkImportResolver(b *testing.B) {
134 c := qt.New(b)
135 fs := afero.NewMemMapFs()
136
137 writeFile := func(name, content string) {
138 c.Assert(afero.WriteFile(fs, name, []byte(content), 0o777), qt.IsNil)
139 }
140
141 writeFile("a.css", `@import "b.css";
142 @import "c.css";
143 A_STYLE1
144 A_STYLE2
145 `)
146
147 writeFile("b.css", `B_STYLE`)
148 writeFile("c.css", "@import \"d.css\"\nC_STYLE"+strings.Repeat("\nSTYLE", 12))
149 writeFile("d.css", "@import \"a.css\"\n\nD_STYLE"+strings.Repeat("\nSTYLE", 55))
150 writeFile("e.css", "E_STYLE")
151
152 mainStyles := `@import "a.css";
153 @import "b.css";
154 LOCAL_STYLE
155 @import "c.css";
156 @import "e.css";
157 @import "missing.css";`
158
159 logger := loggers.NewDefault()
160
161 for i := 0; i < b.N; i++ {
162 b.StopTimer()
163 imp := newImportResolver(
164 strings.NewReader(mainStyles),
165 "styles.css",
166 InlineImports{},
167 fs, logger,
168 identity.NopManager,
169 )
170
171 b.StartTimer()
172
173 _, err := imp.resolve()
174 if err != nil {
175 b.Fatal(err)
176 }
177
178 }
179 }