integration_test.go (6291B)
1 // Copyright 2021 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_test 15 16 import ( 17 "fmt" 18 "path/filepath" 19 "strings" 20 "testing" 21 22 jww "github.com/spf13/jwalterweatherman" 23 24 qt "github.com/frankban/quicktest" 25 "github.com/gohugoio/hugo/htesting" 26 "github.com/gohugoio/hugo/hugofs" 27 "github.com/gohugoio/hugo/hugolib" 28 ) 29 30 const postCSSIntegrationTestFiles = ` 31 -- assets/css/components/a.css -- 32 /* A comment. */ 33 /* Another comment. */ 34 class-in-a { 35 color: blue; 36 } 37 38 -- assets/css/components/all.css -- 39 @import "a.css"; 40 @import "b.css"; 41 -- assets/css/components/b.css -- 42 @import "a.css"; 43 44 class-in-b { 45 color: blue; 46 } 47 48 -- assets/css/styles.css -- 49 @tailwind base; 50 @tailwind components; 51 @tailwind utilities; 52 @import "components/all.css"; 53 h1 { 54 @apply text-2xl font-bold; 55 } 56 57 -- config.toml -- 58 disablekinds = ['taxonomy', 'term', 'page'] 59 baseURL = "https://example.com" 60 [build] 61 useResourceCacheWhen = 'never' 62 -- content/p1.md -- 63 -- data/hugo.toml -- 64 slogan = "Hugo Rocks!" 65 -- i18n/en.yaml -- 66 hello: 67 other: "Hello" 68 -- i18n/fr.yaml -- 69 hello: 70 other: "Bonjour" 71 -- layouts/index.html -- 72 {{ $options := dict "inlineImports" true }} 73 {{ $styles := resources.Get "css/styles.css" | resources.PostCSS $options }} 74 Styles RelPermalink: {{ $styles.RelPermalink }} 75 {{ $cssContent := $styles.Content }} 76 Styles Content: Len: {{ len $styles.Content }}| 77 -- package.json -- 78 { 79 "scripts": {}, 80 81 "devDependencies": { 82 "postcss-cli": "7.1.0", 83 "tailwindcss": "1.2.0" 84 } 85 } 86 -- postcss.config.js -- 87 console.error("Hugo Environment:", process.env.HUGO_ENVIRONMENT ); 88 // https://github.com/gohugoio/hugo/issues/7656 89 console.error("package.json:", process.env.HUGO_FILE_PACKAGE_JSON ); 90 console.error("PostCSS Config File:", process.env.HUGO_FILE_POSTCSS_CONFIG_JS ); 91 92 module.exports = { 93 plugins: [ 94 require('tailwindcss') 95 ] 96 } 97 98 ` 99 100 func TestTransformPostCSS(t *testing.T) { 101 if !htesting.IsCI() { 102 t.Skip("Skip long running test when running locally") 103 } 104 105 c := qt.New(t) 106 tempDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-integration-test") 107 c.Assert(err, qt.IsNil) 108 c.Cleanup(clean) 109 110 for _, s := range []string{"never", "always"} { 111 112 repl := strings.NewReplacer( 113 "https://example.com", 114 "https://example.com/foo", 115 "useResourceCacheWhen = 'never'", 116 fmt.Sprintf("useResourceCacheWhen = '%s'", s), 117 ) 118 119 files := repl.Replace(postCSSIntegrationTestFiles) 120 121 fmt.Println("===>", s, files) 122 123 b := hugolib.NewIntegrationTestBuilder( 124 hugolib.IntegrationTestConfig{ 125 T: c, 126 NeedsOsFS: true, 127 NeedsNpmInstall: true, 128 LogLevel: jww.LevelInfo, 129 WorkingDir: tempDir, 130 TxtarString: files, 131 }).Build() 132 133 b.AssertFileContent("public/index.html", ` 134 Styles RelPermalink: /foo/css/styles.css 135 Styles Content: Len: 770917| 136 `) 137 138 } 139 140 } 141 142 // 9880 143 func TestTransformPostCSSError(t *testing.T) { 144 if !htesting.IsCI() { 145 t.Skip("Skip long running test when running locally") 146 } 147 148 c := qt.New(t) 149 150 s, err := hugolib.NewIntegrationTestBuilder( 151 hugolib.IntegrationTestConfig{ 152 T: c, 153 NeedsOsFS: true, 154 NeedsNpmInstall: true, 155 TxtarString: strings.ReplaceAll(postCSSIntegrationTestFiles, "color: blue;", "@apply foo;"), // Syntax error 156 }).BuildE() 157 158 s.AssertIsFileError(err) 159 c.Assert(err.Error(), qt.Contains, "a.css:4:2") 160 161 } 162 163 // #9895 164 func TestTransformPostCSSImportError(t *testing.T) { 165 if !htesting.IsCI() { 166 t.Skip("Skip long running test when running locally") 167 } 168 169 c := qt.New(t) 170 171 s, err := hugolib.NewIntegrationTestBuilder( 172 hugolib.IntegrationTestConfig{ 173 T: c, 174 NeedsOsFS: true, 175 NeedsNpmInstall: true, 176 LogLevel: jww.LevelInfo, 177 TxtarString: strings.ReplaceAll(postCSSIntegrationTestFiles, `@import "components/all.css";`, `@import "components/doesnotexist.css";`), 178 }).BuildE() 179 180 s.AssertIsFileError(err) 181 c.Assert(err.Error(), qt.Contains, "styles.css:4:3") 182 c.Assert(err.Error(), qt.Contains, filepath.FromSlash(`failed to resolve CSS @import "css/components/doesnotexist.css"`)) 183 184 } 185 186 func TestTransformPostCSSImporSkipInlineImportsNotFound(t *testing.T) { 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.NewIntegrationTestBuilder( 197 hugolib.IntegrationTestConfig{ 198 T: c, 199 NeedsOsFS: true, 200 NeedsNpmInstall: true, 201 LogLevel: jww.LevelInfo, 202 TxtarString: files, 203 }).Build() 204 205 s.AssertFileContent("public/css/styles.css", `@import "components/doesnotexist.css";`) 206 207 } 208 209 // Issue 9787 210 func TestTransformPostCSSResourceCacheWithPathInBaseURL(t *testing.T) { 211 if !htesting.IsCI() { 212 t.Skip("Skip long running test when running locally") 213 } 214 215 c := qt.New(t) 216 tempDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-integration-test") 217 c.Assert(err, qt.IsNil) 218 c.Cleanup(clean) 219 220 for i := 0; i < 2; i++ { 221 files := postCSSIntegrationTestFiles 222 223 if i == 1 { 224 files = strings.ReplaceAll(files, "https://example.com", "https://example.com/foo") 225 files = strings.ReplaceAll(files, "useResourceCacheWhen = 'never'", " useResourceCacheWhen = 'always'") 226 } 227 228 b := hugolib.NewIntegrationTestBuilder( 229 hugolib.IntegrationTestConfig{ 230 T: c, 231 NeedsOsFS: true, 232 NeedsNpmInstall: true, 233 LogLevel: jww.LevelInfo, 234 TxtarString: files, 235 WorkingDir: tempDir, 236 }).Build() 237 238 b.AssertFileContent("public/index.html", ` 239 Styles Content: Len: 770917 240 `) 241 242 } 243 244 }