Debian Codegraph @

resources/resource_transformers/cssjs/postcss_integration_test.go

raw ◀ ▶
1 Debian patch touch this file
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_test
15
16 import (
17 "fmt"
18 "os"
19 "path/filepath"
20 "runtime"
21 "strings"
22 "testing"
23
24 qt "github.com/frankban/quicktest"
25 "github.com/gohugoio/hugo/common/herrors"
26 "github.com/gohugoio/hugo/htesting"
27 "github.com/gohugoio/hugo/hugofs"
28 "github.com/gohugoio/hugo/hugolib"
29 )
30
31 const postCSSIntegrationTestFiles = `
32 -- assets/css/components/a.css --
33 /* A comment. */
34 /* Another comment. */
35 class-in-a {
36 color: blue;
37 }
38
39 -- assets/css/components/all.css --
40 @import "a.css";
41 @import "b.css";
42 -- assets/css/components/b.css --
43 @import "a.css";
44
45 class-in-b {
46 color: blue;
47 }
48
49 -- assets/css/styles.css --
50 @tailwind base;
51 @tailwind components;
52 @tailwind utilities;
53 @import "components/all.css";
54 h1 {
55 @apply text-2xl font-bold;
56 }
57
58 -- hugo.toml --
59 disablekinds = ['taxonomy', 'term', 'page']
60 baseURL = "https://example.com"
61 [build]
62 useResourceCacheWhen = 'never'
63 -- content/p1.md --
64 -- data/hugo.toml --
65 slogan = "Hugo Rocks!"
66 -- i18n/en.yaml --
67 hello:
68 other: "Hello"
69 -- i18n/fr.yaml --
70 hello:
71 other: "Bonjour"
72 -- layouts/home.html --
73 {{ $options := dict "inlineImports" true }}
74 {{ $styles := resources.Get "css/styles.css" | css.PostCSS $options }}
75 Styles RelPermalink: {{ $styles.RelPermalink }}
76 {{ $cssContent := $styles.Content }}
77 Styles Content: Len: {{ len $styles.Content }}|
78 -- package.json --
79 {
80 "scripts": {},
81
82 "devDependencies": {
83 "postcss-cli": "7.1.0",
84 "tailwindcss": "1.2.0"
85 }
86 }
87 -- postcss.config.js --
88 console.error("Hugo Environment:", process.env.HUGO_ENVIRONMENT );
89 console.error("Hugo PublishDir:", process.env.HUGO_PUBLISHDIR );
90 // https://github.com/gohugoio/hugo/issues/7656
91 console.error("package.json:", process.env.HUGO_FILE_PACKAGE_JSON );
92 console.error("PostCSS Config File:", process.env.HUGO_FILE_POSTCSS_CONFIG_JS );
93
94 module.exports = {
95 plugins: [
96 require('tailwindcss')
97 ]
98 }
99
100 `
101
102 func TestTransformPostCSS(t *testing.T) {
•103 if os.Getenv("AUTOPKGTEST_TMP") == "" {
104 t.Skip("Skip test as network is inaccessible during Debian package build")
105 }
106 if !htesting.IsCI() {
107 t.Skip("Skip long running test when running locally")
108 }
109
110 c := qt.New(t)
111 tempDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-integration-test")
112 c.Assert(err, qt.IsNil)
113 c.Cleanup(clean)
114
115 for _, s := range []string{"never", "always"} {
116
117 repl := strings.NewReplacer(
118 "https://example.com",
119 "https://example.com/foo",
120 "useResourceCacheWhen = 'never'",
121 fmt.Sprintf("useResourceCacheWhen = '%s'", s),
122 )
123
124 files := repl.Replace(postCSSIntegrationTestFiles)
125
126 b := hugolib.Test(c, files, hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall(), hugolib.TestOptInfo(), hugolib.TestOptWithConfig(func(cfg *hugolib.IntegrationTestConfig) {
127 cfg.WorkingDir = tempDir
128 }))
129
130 b.AssertFileContent("public/index.html", `
131 Styles RelPermalink: /foo/css/styles.css
132 Styles Content: Len: 770917|
133 `)
134
135 if s == "never" {
136 b.AssertLogContains("Hugo Environment: production")
137 b.AssertLogContains("Hugo PublishDir: " + filepath.Join(tempDir, "public"))
138 }
139 }
140 }
141
142 // 9880
143 func TestTransformPostCSSError(t *testing.T) {
•144 if os.Getenv("AUTOPKGTEST_TMP") == "" {
145 t.Skip("Skip test as network is inaccessible during Debian package build")
146 }
147 if !htesting.IsCI() {
148 t.Skip("Skip long running test when running locally")
149 }
150
151 if runtime.GOOS == "windows" {
152 // TODO(bep) This has started to fail on Windows with Go 1.19 on GitHub Actions for some mysterious reason.
153 t.Skip("Skip on Windows")
154 }
155
156 c := qt.New(t)
157
158 b, err := hugolib.TestE(c, strings.ReplaceAll(postCSSIntegrationTestFiles, "color: blue;", "@apply foo;"), hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall())
159
160 ferrs := herrors.UnwrapFileErrors(err)
161 b.Assert(len(ferrs), qt.Equals, 2)
162 b.Assert(err.Error(), qt.Contains, "a.css:4:2")
163 }
164
165 // #9895
166 func TestTransformPostCSSImportError(t *testing.T) {
•167 if os.Getenv("AUTOPKGTEST_TMP") == "" {
168 t.Skip("Skip test as network is inaccessible during Debian package build")
169 }
170 if !htesting.IsCI() {
171 t.Skip("Skip long running test when running locally")
172 }
173
174 c := qt.New(t)
175
176 _, err := hugolib.TestE(c, strings.ReplaceAll(postCSSIntegrationTestFiles, `@import "components/all.css";`, `@import "components/doesnotexist.css";`), hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall(), hugolib.TestOptInfo())
177 ferrs := herrors.UnwrapFileErrors(err)
178 c.Assert(len(ferrs), qt.Equals, 2)
179 c.Assert(err.Error(), qt.Contains, "styles.css:4:3")
180 c.Assert(err.Error(), qt.Contains, filepath.FromSlash(`failed to resolve CSS @import "/css/components/doesnotexist.css"`))
181 }
182
183 func TestTransformPostCSSImporSkipInlineImportsNotFound(t *testing.T) {
•184 if os.Getenv("AUTOPKGTEST_TMP") == "" {
185 t.Skip("Skip test as network is inaccessible during Debian package build")
186 }
187 if !htesting.IsCI() {
188 t.Skip("Skip long running test when running locally")
189 }
190
191 c := qt.New(t)
192
193 files := strings.ReplaceAll(postCSSIntegrationTestFiles, `@import "components/all.css";`, `@import "components/doesnotexist.css";`)
194 files = strings.ReplaceAll(files, `{{ $options := dict "inlineImports" true }}`, `{{ $options := dict "inlineImports" true "skipInlineImportsNotFound" true }}`)
195
196 s := hugolib.Test(c, files, hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall(), hugolib.TestOptInfo())
197
198 s.AssertFileContent("public/css/styles.css", `@import "components/doesnotexist.css";`)
199 }
200
201 // Issue 9787
202 func TestTransformPostCSSResourceCacheWithPathInBaseURL(t *testing.T) {
•203 if os.Getenv("AUTOPKGTEST_TMP") == "" {
204 t.Skip("Skip test as network is inaccessible during Debian package build")
205 }
206 if !htesting.IsCI() {
207 t.Skip("Skip long running test when running locally")
208 }
209
210 c := qt.New(t)
211 tempDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-integration-test")
212 c.Assert(err, qt.IsNil)
213 c.Cleanup(clean)
214
215 for i := range 2 {
216 files := postCSSIntegrationTestFiles
217
218 if i == 1 {
219 files = strings.ReplaceAll(files, "https://example.com", "https://example.com/foo")
220 files = strings.ReplaceAll(files, "useResourceCacheWhen = 'never'", " useResourceCacheWhen = 'always'")
221 }
222
223 b := hugolib.Test(c, files, hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall(), hugolib.TestOptInfo(), hugolib.TestOptWithConfig(func(cfg *hugolib.IntegrationTestConfig) {
224 cfg.WorkingDir = tempDir
225 }))
226
227 b.AssertFileContent("public/index.html", `
228 Styles Content: Len: 770917
229 `)
230
231 }
232 }
233
234 // See Issue 15039.
235 // See Issue 15040.
236 func TestTransformPostCSSConfigResolution(t *testing.T) {
•237 t.Skip("This tests needs internet access")
238
239 if !htesting.IsCI() {
240 t.Skip("Skip long running test when running locally")
241 }
242
243 files := `
244 -- hugo.toml --
245 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
246 [[module.imports]]
247 path = "github.com/bep/hugo-mod-nop"
248 -- assets/css/styles.css --
249 body { color: red }
250 -- layouts/home.html --
251 {{ $styles := resources.Get "css/styles.css" | css.PostCSS }}
252 RelPermalink: {{ $styles.RelPermalink }}|HasBody: {{ in $styles.Content "color:" }}|
253 -- package.json --
254 {
255 "devDependencies": {
256 "postcss-cli": "11.0.0"
257 }
258 }
259 -- go.mod --
260 module github.com/example/project
261
262 go 1.26
263
264 replace github.com/bep/hugo-mod-nop => ../external-module
265 -- ../external-module/go.mod --
266 module github.com/bep/hugo-mod-nop
267
268 go 1.26
269 -- ../external-module/CONFIG_FILE_NAME --
270 CONFIG_FILE_CONTENT
271 `
272
273 tests := []struct {
274 name string
275 configFileName string
276 configFileContent string
277 }{
278 {
279 name: "mjs in module",
280 configFileName: "postcss.config.mjs",
281 configFileContent: "export default {};\n",
282 },
283 {
284 name: "cjs in module",
285 configFileName: "postcss.config.cjs",
286 configFileContent: "module.exports = {};\n",
287 },
288 {
289 name: "js in module",
290 configFileName: "postcss.config.js",
291 configFileContent: "module.exports = {};\n",
292 },
293 }
294
295 for _, tt := range tests {
296 t.Run(tt.name, func(t *testing.T) {
297 c := qt.New(t)
298 rootDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-integration-test")
299 c.Assert(err, qt.IsNil)
300 c.Cleanup(clean)
301
302 projectDir := filepath.Join(rootDir, "project")
303 moduleDir := filepath.Join(rootDir, "external-module")
304 c.Assert(os.MkdirAll(projectDir, 0o755), qt.IsNil)
305 c.Assert(os.MkdirAll(moduleDir, 0o755), qt.IsNil)
306
307 f := strings.ReplaceAll(files, "CONFIG_FILE_NAME", tt.configFileName)
308 f = strings.ReplaceAll(f, "CONFIG_FILE_CONTENT", tt.configFileContent)
309
310 b := hugolib.Test(c, f,
311 hugolib.TestOptWithConfig(func(cfg *hugolib.IntegrationTestConfig) {
312 cfg.WorkingDir = projectDir
313 cfg.NeedsOsFS = true
314 cfg.NeedsNpmInstall = true
315 }),
316 hugolib.TestOptInfo(),
317 )
318
319 b.AssertFileContent("public/index.html",
320 "RelPermalink: /css/styles.css|HasBody: true|",
321 )
322 b.AssertLogContains(tt.configFileName)
323 })
324 }
325 }
326
327 // See Issue 13987.
328 func TestTransformPostCSSESMConfigInModule(t *testing.T) {
•329 if os.Getenv("AUTOPKGTEST_TMP") == "" {
330 t.Skip("Skip test as network is inaccessible during Debian package build")
331 }
332 if !htesting.IsCI() {
333 t.Skip("Skip long running test when running locally")
334 }
335
336 c := qt.New(t)
337 // Use htesting.CreateTempDir to get canonical paths on macOS
338 // (/private/var/...); Node's --permission model rejects the symlinked
339 // /var/folders/... form when crossing the project boundary.
340 rootDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-integration-test")
341 c.Assert(err, qt.IsNil)
342 c.Cleanup(clean)
343
344 projectDir := filepath.Join(rootDir, "project")
345 moduleDir := filepath.Join(rootDir, "external-module")
346 c.Assert(os.MkdirAll(projectDir, 0o755), qt.IsNil)
347 c.Assert(os.MkdirAll(moduleDir, 0o755), qt.IsNil)
348
349 files := `
350 -- hugo.toml --
351 disableKinds = ['taxonomy', 'term', 'page']
352 baseURL = "https://example.com"
353 [[module.imports]]
354 path = "github.com/bep/hugo-mod-nop"
355 -- assets/css/styles.css --
356 body { color: red }
357 -- layouts/home.html --
358 {{ $styles := resources.Get "css/styles.css" | css.PostCSS }}
359 RelPermalink: {{ $styles.RelPermalink }}|HasBody: {{ in $styles.Content "color:" }}|
360 -- content/_index.md --
361 ---
362 title: home
363 ---
364 -- package.json --
365 {
366 "devDependencies": {
367 "postcss-cli": "11.0.0",
368 "postcss-import": "16.0.0"
369 }
370 }
371 -- go.mod --
372 module github.com/example/project
373
374 go 1.20
375
376 replace github.com/bep/hugo-mod-nop => ../external-module
377 -- ../external-module/go.mod --
378 module github.com/bep/hugo-mod-nop
379
380 go 1.20
381 -- ../external-module/postcss.config.js --
382 import postcssImport from "postcss-import";
383 export default { plugins: [postcssImport()] };
384
385 `
386
387 b := hugolib.Test(c, files,
388 hugolib.TestOptWithConfig(func(cfg *hugolib.IntegrationTestConfig) {
389 cfg.WorkingDir = projectDir
390 cfg.NeedsOsFS = true
391 cfg.NeedsNpmInstall = true
392 }),
393 )
394
395 b.AssertFileContent("public/index.html",
396 "RelPermalink: /css/styles.css|HasBody: true|",
397 )
398 }
399
400 // Netlify stores its node_modules cache in the same tree as the Hugo file
401 // cache, so Node's resolver can walk up from a module's postcss.config.js and
402 // hit a node_modules outside the permission allow-list, aborting with
403 // ERR_ACCESS_DENIED instead of falling through to NODE_PATH. The restricted
404 // postcss-import below (an ancestor of the external module, never installed by
405 // us) forces that walk to fail; the build must still succeed by resolving the
406 // real postcss-import via NODE_PATH.
407 //
408 // See issue 15041.
409 func TestTransformPostCSSESMConfigAccessDenied(t *testing.T) {
•410 t.Skip("This tests needs internet access")
411
412 if !htesting.IsCI() {
413 t.Skip("Skip long running test when running locally")
414 }
415
416 c := qt.New(t)
417 // Use htesting.CreateTempDir to get canonical paths on macOS
418 // (/private/var/...); Node's --permission model rejects the symlinked
419 // /var/folders/... form when crossing the project boundary.
420 rootDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-integration-test")
421 c.Assert(err, qt.IsNil)
422 c.Cleanup(clean)
423
424 projectDir := filepath.Join(rootDir, "project")
425 moduleDir := filepath.Join(rootDir, "external-module")
426 c.Assert(os.MkdirAll(projectDir, 0o755), qt.IsNil)
427 c.Assert(os.MkdirAll(moduleDir, 0o755), qt.IsNil)
428
429 files := `
430 -- hugo.toml --
431 disableKinds = ['taxonomy', 'term', 'page']
432 baseURL = "https://example.com"
433 [[module.imports]]
434 path = "github.com/bep/hugo-mod-nop"
435 -- assets/css/styles.css --
436 body { color: red }
437 -- layouts/home.html --
438 {{ $styles := resources.Get "css/styles.css" | css.PostCSS }}
439 RelPermalink: {{ $styles.RelPermalink }}|HasBody: {{ in $styles.Content "color:" }}|
440 -- content/_index.md --
441 ---
442 title: home
443 ---
444 -- package.json --
445 {
446 "devDependencies": {
447 "postcss-cli": "11.0.0",
448 "postcss-import": "16.0.0"
449 }
450 }
451 -- go.mod --
452 module github.com/example/project
453
454 go 1.20
455
456 replace github.com/bep/hugo-mod-nop => ../external-module
457 -- ../node_modules/postcss-import/package.json --
458 { "name": "postcss-import", "version": "0.0.0-RESTRICTED", "main": "index.js" }
459 -- ../external-module/go.mod --
460 module github.com/bep/hugo-mod-nop
461
462 go 1.20
463 -- ../external-module/postcss.config.js --
464 import postcssImport from "postcss-import";
465 export default { plugins: [postcssImport()] };
466
467 `
468
469 b := hugolib.Test(c, files,
470 hugolib.TestOptWithConfig(func(cfg *hugolib.IntegrationTestConfig) {
471 cfg.WorkingDir = projectDir
472 cfg.NeedsOsFS = true
473 cfg.NeedsNpmInstall = true
474 }),
475 )
476
477 b.AssertFileContent("public/index.html",
478 "RelPermalink: /css/styles.css|HasBody: true|",
479 )
480 }