Skip to content

Commit

Permalink
postcss: Move integration test to its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Feb 9, 2022
1 parent 94f10cf commit c4aaf1d
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 198 deletions.
198 changes: 0 additions & 198 deletions hugolib/resource_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package hugolib

import (
"bytes"
"fmt"
"io"
"io/ioutil"
Expand All @@ -27,19 +26,10 @@ import (
"testing"
"time"

"github.com/gohugoio/hugo/config"

jww "github.com/spf13/jwalterweatherman"

"github.com/gohugoio/hugo/common/herrors"

"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/htesting"

qt "github.com/frankban/quicktest"

"github.com/gohugoio/hugo/hugofs"

"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/scss"
)
Expand Down Expand Up @@ -713,194 +703,6 @@ JSON: {{ $json.RelPermalink }}: {{ $json.Content }}
"JSONS: 2", "/jsons/data1.json: json1 content")
}

func TestResourceChainPostCSS(t *testing.T) {
if !htesting.IsCI() {
t.Skip("skip (relative) long running modules test when running locally")
}

wd, _ := os.Getwd()
defer func() {
os.Chdir(wd)
}()

c := qt.New(t)

packageJSON := `{
"scripts": {},
"devDependencies": {
"postcss-cli": "7.1.0",
"tailwindcss": "1.2.0"
}
}
`

postcssConfig := `
console.error("Hugo Environment:", process.env.HUGO_ENVIRONMENT );
// https://github.com/gohugoio/hugo/issues/7656
console.error("package.json:", process.env.HUGO_FILE_PACKAGE_JSON );
console.error("PostCSS Config File:", process.env.HUGO_FILE_POSTCSS_CONFIG_JS );
module.exports = {
plugins: [
require('tailwindcss')
]
}
`

tailwindCss := `
@tailwind base;
@tailwind components;
@tailwind utilities;
@import "components/all.css";
h1 {
@apply text-2xl font-bold;
}
`

workDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-test-postcss")
c.Assert(err, qt.IsNil)
defer clean()

var logBuf bytes.Buffer

newTestBuilder := func(v config.Provider) *sitesBuilder {
v.Set("workingDir", workDir)
v.Set("disableKinds", []string{"taxonomy", "term", "page"})
logger := loggers.NewBasicLoggerForWriter(jww.LevelInfo, &logBuf)
b := newTestSitesBuilder(t).WithLogger(logger)
// Need to use OS fs for this.
b.Fs = hugofs.NewDefault(v)
b.WithWorkingDir(workDir)
b.WithViper(v)

b.WithContent("p1.md", "")
b.WithTemplates("index.html", `
{{ $options := dict "inlineImports" true }}
{{ $styles := resources.Get "css/styles.css" | resources.PostCSS $options }}
Styles RelPermalink: {{ $styles.RelPermalink }}
{{ $cssContent := $styles.Content }}
Styles Content: Len: {{ len $styles.Content }}|
`)

return b
}

b := newTestBuilder(config.New())

cssDir := filepath.Join(workDir, "assets", "css", "components")
b.Assert(os.MkdirAll(cssDir, 0777), qt.IsNil)

b.WithSourceFile("assets/css/styles.css", tailwindCss)
b.WithSourceFile("assets/css/components/all.css", `
@import "a.css";
@import "b.css";
`, "assets/css/components/a.css", `
class-in-a {
color: blue;
}
`, "assets/css/components/b.css", `
@import "a.css";
class-in-b {
color: blue;
}
`)

b.WithSourceFile("package.json", packageJSON)
b.WithSourceFile("postcss.config.js", postcssConfig)

b.Assert(os.Chdir(workDir), qt.IsNil)
cmd := b.NpmInstall()
err = cmd.Run()
b.Assert(err, qt.IsNil)
b.Build(BuildCfg{})

// Make sure Node sees this.
b.Assert(logBuf.String(), qt.Contains, "Hugo Environment: production")
b.Assert(logBuf.String(), qt.Contains, filepath.FromSlash(fmt.Sprintf("PostCSS Config File: %s/postcss.config.js", workDir)))
b.Assert(logBuf.String(), qt.Contains, filepath.FromSlash(fmt.Sprintf("package.json: %s/package.json", workDir)))

b.AssertFileContent("public/index.html", `
Styles RelPermalink: /css/styles.css
Styles Content: Len: 770878|
`)

assertCss := func(b *sitesBuilder) {
content := b.FileContent("public/css/styles.css")

b.Assert(strings.Contains(content, "class-in-a"), qt.Equals, true)
b.Assert(strings.Contains(content, "class-in-b"), qt.Equals, true)
}

assertCss(b)

build := func(s string, shouldFail bool) error {
b.Assert(os.RemoveAll(filepath.Join(workDir, "public")), qt.IsNil)

v := config.New()
v.Set("build", map[string]interface{}{
"useResourceCacheWhen": s,
})

b = newTestBuilder(v)

b.Assert(os.RemoveAll(filepath.Join(workDir, "public")), qt.IsNil)

err := b.BuildE(BuildCfg{})
if shouldFail {
b.Assert(err, qt.Not(qt.IsNil))
} else {
b.Assert(err, qt.IsNil)
assertCss(b)
}

return err
}

build("always", false)
build("fallback", false)

// Introduce a syntax error in an import
b.WithSourceFile("assets/css/components/b.css", `@import "a.css";
class-in-b {
@apply asdf;
}
`)

err = build("never", true)

err = herrors.UnwrapErrorWithFileContext(err)
_, ok := err.(*herrors.ErrorWithFileContext)
b.Assert(ok, qt.Equals, true)

// TODO(bep) for some reason, we have starting to get
// execute of template failed: template: index.html:5:25
// on CI (GitHub action).
// b.Assert(fe.Position().LineNumber, qt.Equals, 5)
// b.Assert(fe.Error(), qt.Contains, filepath.Join(workDir, "assets/css/components/b.css:4:1"))

// Remove PostCSS
b.Assert(os.RemoveAll(filepath.Join(workDir, "node_modules")), qt.IsNil)

build("always", false)
build("fallback", false)
build("never", true)

// Remove cache
b.Assert(os.RemoveAll(filepath.Join(workDir, "resources")), qt.IsNil)

build("always", true)
build("fallback", true)
build("never", true)
}

func TestResourceMinifyDisabled(t *testing.T) {
t.Parallel()

Expand Down
148 changes: 148 additions & 0 deletions resources/resource_transformers/postcss/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright 2021 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package postcss_test

import (
"fmt"
"path/filepath"
"strings"
"testing"

jww "github.com/spf13/jwalterweatherman"

qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/htesting"
"github.com/gohugoio/hugo/hugolib"
)

func TestTransformPostCSS(t *testing.T) {
if !htesting.IsCI() {
t.Skip("Skip long running test when running locally")
}

c := qt.New(t)

files := `
-- assets/css/components/a.css --
class-in-a {
color: blue;
}
-- assets/css/components/all.css --
@import "a.css";
@import "b.css";
-- assets/css/components/b.css --
@import "a.css";
class-in-b {
color: blue;
}
-- assets/css/styles.css --
@tailwind base;
@tailwind components;
@tailwind utilities;
@import "components/all.css";
h1 {
@apply text-2xl font-bold;
}
-- config.toml --
disablekinds = ['taxonomy', 'term', 'page']
-- content/p1.md --
-- data/hugo.toml --
slogan = "Hugo Rocks!"
-- i18n/en.yaml --
hello:
other: "Hello"
-- i18n/fr.yaml --
hello:
other: "Bonjour"
-- layouts/index.html --
{{ $options := dict "inlineImports" true }}
{{ $styles := resources.Get "css/styles.css" | resources.PostCSS $options }}
Styles RelPermalink: {{ $styles.RelPermalink }}
{{ $cssContent := $styles.Content }}
Styles Content: Len: {{ len $styles.Content }}|
-- package.json --
{
"scripts": {},
"devDependencies": {
"postcss-cli": "7.1.0",
"tailwindcss": "1.2.0"
}
}
-- postcss.config.js --
console.error("Hugo Environment:", process.env.HUGO_ENVIRONMENT );
// https://github.com/gohugoio/hugo/issues/7656
console.error("package.json:", process.env.HUGO_FILE_PACKAGE_JSON );
console.error("PostCSS Config File:", process.env.HUGO_FILE_POSTCSS_CONFIG_JS );
module.exports = {
plugins: [
require('tailwindcss')
]
}
`

c.Run("Success", func(c *qt.C) {
b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: c,
NeedsOsFS: true,
NeedsNpmInstall: true,
LogLevel: jww.LevelInfo,
TxtarString: files,
}).Build()

b.AssertLogContains("Hugo Environment: production")
b.AssertLogContains(filepath.FromSlash(fmt.Sprintf("PostCSS Config File: %s/postcss.config.js", b.Cfg.WorkingDir)))
b.AssertLogContains(filepath.FromSlash(fmt.Sprintf("package.json: %s/package.json", b.Cfg.WorkingDir)))

b.AssertFileContent("public/index.html", `
Styles RelPermalink: /css/styles.css
Styles Content: Len: 770875|
`)
})

c.Run("Error", func(c *qt.C) {
s, err := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: c,
NeedsOsFS: true,
NeedsNpmInstall: true,
TxtarString: strings.ReplaceAll(files, "color: blue;", "@apply foo;"), // Syntax error
}).BuildE()
s.AssertIsFileError(err)
})
}

// bookmark2
func TestIntegrationTestTemplate(t *testing.T) {
c := qt.New(t)

files := ``

b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: c,
NeedsOsFS: false,
NeedsNpmInstall: false,
TxtarString: files,
}).Build()

b.Assert(true, qt.IsTrue)
}

0 comments on commit c4aaf1d

Please sign in to comment.