github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/resources/resource_transformers/postcss/integration_test.go (about) 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/hugolib" 27 ) 28 29 func TestTransformPostCSS(t *testing.T) { 30 if !htesting.IsCI() { 31 t.Skip("Skip long running test when running locally") 32 } 33 34 c := qt.New(t) 35 36 files := ` 37 -- assets/css/components/a.css -- 38 class-in-a { 39 color: blue; 40 } 41 42 -- assets/css/components/all.css -- 43 @import "a.css"; 44 @import "b.css"; 45 -- assets/css/components/b.css -- 46 @import "a.css"; 47 48 class-in-b { 49 color: blue; 50 } 51 52 -- assets/css/styles.css -- 53 @tailwind base; 54 @tailwind components; 55 @tailwind utilities; 56 @import "components/all.css"; 57 h1 { 58 @apply text-2xl font-bold; 59 } 60 61 -- config.toml -- 62 disablekinds = ['taxonomy', 'term', 'page'] 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/index.html -- 73 {{ $options := dict "inlineImports" true }} 74 {{ $styles := resources.Get "css/styles.css" | resources.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 // https://github.com/gohugoio/hugo/issues/7656 90 console.error("package.json:", process.env.HUGO_FILE_PACKAGE_JSON ); 91 console.error("PostCSS Config File:", process.env.HUGO_FILE_POSTCSS_CONFIG_JS ); 92 93 module.exports = { 94 plugins: [ 95 require('tailwindcss') 96 ] 97 } 98 99 ` 100 101 c.Run("Success", func(c *qt.C) { 102 b := hugolib.NewIntegrationTestBuilder( 103 hugolib.IntegrationTestConfig{ 104 T: c, 105 NeedsOsFS: true, 106 NeedsNpmInstall: true, 107 LogLevel: jww.LevelInfo, 108 TxtarString: files, 109 }).Build() 110 111 b.AssertLogContains("Hugo Environment: production") 112 b.AssertLogContains(filepath.FromSlash(fmt.Sprintf("PostCSS Config File: %s/postcss.config.js", b.Cfg.WorkingDir))) 113 b.AssertLogContains(filepath.FromSlash(fmt.Sprintf("package.json: %s/package.json", b.Cfg.WorkingDir))) 114 115 b.AssertFileContent("public/index.html", ` 116 Styles RelPermalink: /css/styles.css 117 Styles Content: Len: 770875| 118 `) 119 }) 120 121 c.Run("Error", func(c *qt.C) { 122 s, err := hugolib.NewIntegrationTestBuilder( 123 hugolib.IntegrationTestConfig{ 124 T: c, 125 NeedsOsFS: true, 126 NeedsNpmInstall: true, 127 TxtarString: strings.ReplaceAll(files, "color: blue;", "@apply foo;"), // Syntax error 128 }).BuildE() 129 s.AssertIsFileError(err) 130 }) 131 } 132 133 // bookmark2 134 func TestIntegrationTestTemplate(t *testing.T) { 135 c := qt.New(t) 136 137 files := `` 138 139 b := hugolib.NewIntegrationTestBuilder( 140 hugolib.IntegrationTestConfig{ 141 T: c, 142 NeedsOsFS: false, 143 NeedsNpmInstall: false, 144 TxtarString: files, 145 }).Build() 146 147 b.Assert(true, qt.IsTrue) 148 }