Debian Codegraph @

resources/resource_transformers/cssjs/tailwindcss_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 "os"
18 "os/exec"
19 "runtime"
20 "strings"
21 "testing"
22
23 qt "github.com/frankban/quicktest"
24 "github.com/gohugoio/hugo/htesting"
25 "github.com/gohugoio/hugo/hugolib"
26 )
27
28 func TestTailwindV4Basic(t *testing.T) {
•29 if os.Getenv("AUTOPKGTEST_TMP") == "" {
30 t.Skip("Skip test as network is inaccessible during Debian package build")
31 }
32 if !(runtime.GOARCH == "amd64" || runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") {
33 t.Skipf("Skip test: prebuild of @parcel/watcher is available only for x64 (amd64), arm and arm64; see https://www.npmjs.com/search?q=%%40parcel%%2Fwatcher-linux")
34 }
35 if runtime.GOARCH == "arm" {
36 cmd := exec.Command("/usr/bin/go", "env", "GOARM")
37 out, err := cmd.Output()
38 if err != nil {
39 t.Skipf("Skip test: failed to check GOARM: %v: %v", cmd, err)
40 }
41 goarm := strings.TrimSpace(string(out))
42 if goarm == "5,softfloat" {
43 t.Skipf("Skip test: prebuild of @parcel/watcher is available for armhf but not for armel (GOARM=%v); see https://www.npmjs.com/search?q=%%40parcel%%2Fwatcher-linux", goarm)
44 }
45 }
46 if !htesting.IsCI() {
47 t.Skip("Skip long running test when running locally")
48 }
49
50 files := `
51 -- hugo.toml --
52 security.exec.allow = ['^go$', '^git$', '^node$', '^tailwindcss$']
53 -- package.json --
54 {
55 "license": "MIT",
56 "repository": {
57 "type": "git",
58 "url": "https://github.com/bep/hugo-starter-tailwind-basic.git"
59 },
60 "devDependencies": {
61 "@tailwindcss/cli": "^4.0.1",
62 "tailwindcss": "^4.0.1"
63 },
64 "name": "hugo-starter-tailwind-basic",
65 "version": "0.1.0"
66 }
67 -- assets/css/styles.css --
68 @import "tailwindcss";
69
70 @theme {
71 --font-family-display: "Satoshi", "sans-serif";
72
73 --breakpoint-3xl: 1920px;
74
75 --color-neon-pink: oklch(71.7% 0.25 360);
76 --color-neon-lime: oklch(91.5% 0.258 129);
77 --color-neon-cyan: oklch(91.3% 0.139 195.8);
78 }
79 -- layouts/home.html --
80 {{ $css := resources.Get "css/styles.css" | css.TailwindCSS }}
81 CSS: {{ $css.Content | safeCSS }}|
82 `
83
84 b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall(), hugolib.TestOptInfo())
85
86 b.AssertFileContent("public/index.html", "/*! tailwindcss v4.")
87 }
88
89 // See issue 15103.
90 func TestTailwindCSSImportContext(t *testing.T) {
•91 t.Skip("This test needs npm with internet access")
92
93 t.Parallel()
94 htesting.SkipSlowTestUnlessCI(t)
95
96 files := `
97 -- hugo.toml --
98 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
99 security.exec.allow = ['^go$', '^git$', '^node$', '^tailwindcss$']
100 -- assets/css/main.css --
101 @import "tailwindcss";
102
103 @import "foo.css";
104 @import "bar.css";
105 -- assets/css/foo.css --
106 .foo {color: orange;}
107 -- layouts/home.html --
108 {{ $foo := resources.FromString "foo.css" ".foo {color: blue;}" }}
109 {{ $bar := resources.FromString "bar.css" ".bar {color: green;}" }}
110 {{ $opts := dict "importContext" (slice $foo $bar) }}
111 {{ $css := resources.Get "css/main.css" | css.TailwindCSS $opts }}
112 CSS: {{ $css.Content | safeCSS }}|
113 -- package.json --
114 {
115 "devDependencies": {
116 "@tailwindcss/cli": "^4.1.7",
117 "tailwindcss": "^4.1.7"
118 }
119 }
120 `
121
122 b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall(), hugolib.TestOptInfo())
123
124 // foo.css resolves in the import context before the assets filesystem.
125 b.AssertFileContent(
126 "public/index.html",
127 ".foo {\n color: blue;\n }",
128 ".bar {\n color: green;\n }",
129 )
130 }
131
132 func TestTailwindCSSNoInlineImportsIssue13719(t *testing.T) {
•133 t.Skip("This tests needs internet access")
134 t.Parallel()
135 htesting.SkipSlowTestUnlessCI(t)
136
137 files := `
138 -- hugo.toml --
139 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
140 theme = 'my-theme'
141
142 security.exec.allow = ['^go$', '^git$', '^node$', '^tailwindcss$']
143
144 [[module.mounts]]
145 source = 'assets'
146 target = 'assets'
147
148 [[module.mounts]]
149 source = 'other'
150 target = 'assets/css'
151 -- assets/css/main.css --
152 @import "tailwindcss";
153
154 @import "colors/red.css";
155 @import "colors/blue.css";
156 @import "colors/purple.css";
157 -- assets/css/colors/red.css --
158 @import "green.css";
159
160 .red {color: red;}
161 -- assets/css/colors/green.css --
162 .green {color: green;}
163 -- themes/my-theme/assets/css/colors/blue.css --
164 .blue {color: blue;}
165 -- other/colors/purple.css --
166 .purple {color: purple;}
167 -- layouts/home.html --
168 {{ with (templates.Defer (dict "key" "global")) }}
169 {{ with resources.Get "css/main.css" }}
170 {{ $opts := dict "disableInlineImports" true }}
171 {{ with . | css.TailwindCSS $opts }}
172 <link rel="stylesheet" href="{{ .RelPermalink }}">
173 {{ end }}
174 {{ end }}
175 {{ end }}
176 -- package.json --
177 {
178 "devDependencies": {
179 "@tailwindcss/cli": "^4.1.7",
180 "tailwindcss": "^4.1.7"
181 }
182 }
183 `
184
185 b, err := hugolib.TestE(t, files, hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall(), hugolib.TestOptInfo())
186
187 b.Assert(err, qt.IsNotNil)
188 b.Assert(err.Error(), qt.Contains, "Can't resolve 'colors/red.css'")
189 b.Assert(err.Error(), qt.Contains, "You may want to set the 'disableInlineImports' option to false")
190 }
191
192 // This tests an internal feature used in the Hugo themes sites building to allow us to build with
193 // default security policy and still get a partial build even for themes using TailwindCSS.
194 func TestTailwindCSSIgnoreSecError(t *testing.T) {
•195 t.Skip("This test needs npm with internet access")
196 if !htesting.IsCI() {
197 t.Skip("Skip long running test when running locally")
198 }
199
200 files := `
201 -- hugo.toml --
202 [internalExternal]
203 ignoreTailwindCSSSecurityError = true
204 -- package.json --
205 {
206 "license": "MIT",
207 "repository": {
208 "type": "git",
209 "url": "https://github.com/bep/hugo-starter-tailwind-basic.git"
210 },
211 "devDependencies": {
212 "@tailwindcss/cli": "^4.0.1",
213 "tailwindcss": "^4.0.1"
214 },
215 "name": "hugo-starter-tailwind-basic",
216 "version": "0.1.0"
217 }
218 -- assets/css/styles.css --
219 @import "tailwindcss";
220
221 @theme {
222 --font-family-display: "Satoshi", "sans-serif";
223
224 --breakpoint-3xl: 1920px;
225
226 --color-neon-pink: oklch(71.7% 0.25 360);
227 --color-neon-lime: oklch(91.5% 0.258 129);
228 --color-neon-cyan: oklch(91.3% 0.139 195.8);
229 }
230 -- layouts/home.html --
231 {{ $css := resources.Get "css/styles.css" | css.TailwindCSS }}
232 CSS: {{ $css.Content | safeCSS }}|
233 `
234
235 b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall(), hugolib.TestOptInfo())
236
237 b.AssertFileContent("public/index.html", ".tailwindcss-security-error-ignored")
238 }