tocss: Port integration tests to their package

This commit is contained in:
Bjørn Erik Pedersen 2022-02-09 14:04:57 +01:00
parent 39f69ca7f0
commit d22f7795c7
3 changed files with 346 additions and 330 deletions

View file

@ -29,8 +29,6 @@ import (
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/dartsass"
jww "github.com/spf13/jwalterweatherman"
"github.com/gohugoio/hugo/common/herrors"
@ -46,334 +44,6 @@ import (
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/scss"
)
func TestSCSSWithIncludePaths(t *testing.T) {
c := qt.New(t)
for _, test := range []struct {
name string
supports func() bool
}{
{"libsass", func() bool { return scss.Supports() }},
{"dartsass", func() bool { return dartsass.Supports() }},
} {
c.Run(test.name, func(c *qt.C) {
if !test.supports() {
c.Skip(fmt.Sprintf("Skip %s", test.name))
}
workDir, clean, err := htesting.CreateTempDir(hugofs.Os, fmt.Sprintf("hugo-scss-include-%s", test.name))
c.Assert(err, qt.IsNil)
defer clean()
v := config.New()
v.Set("workingDir", workDir)
b := newTestSitesBuilder(c).WithLogger(loggers.NewErrorLogger())
// Need to use OS fs for this.
b.Fs = hugofs.NewDefault(v)
b.WithWorkingDir(workDir)
b.WithViper(v)
fooDir := filepath.Join(workDir, "node_modules", "foo")
scssDir := filepath.Join(workDir, "assets", "scss")
c.Assert(os.MkdirAll(fooDir, 0777), qt.IsNil)
c.Assert(os.MkdirAll(filepath.Join(workDir, "content", "sect"), 0777), qt.IsNil)
c.Assert(os.MkdirAll(filepath.Join(workDir, "data"), 0777), qt.IsNil)
c.Assert(os.MkdirAll(filepath.Join(workDir, "i18n"), 0777), qt.IsNil)
c.Assert(os.MkdirAll(filepath.Join(workDir, "layouts", "shortcodes"), 0777), qt.IsNil)
c.Assert(os.MkdirAll(filepath.Join(workDir, "layouts", "_default"), 0777), qt.IsNil)
c.Assert(os.MkdirAll(filepath.Join(scssDir), 0777), qt.IsNil)
b.WithSourceFile(filepath.Join(fooDir, "_moo.scss"), `
$moolor: #fff;
moo {
color: $moolor;
}
`)
b.WithSourceFile(filepath.Join(scssDir, "main.scss"), `
@import "moo";
`)
b.WithTemplatesAdded("index.html", fmt.Sprintf(`
{{ $cssOpts := (dict "includePaths" (slice "node_modules/foo") "transpiler" %q ) }}
{{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts | minify }}
T1: {{ $r.Content }}
`, test.name))
b.Build(BuildCfg{})
b.AssertFileContent(filepath.Join(workDir, "public/index.html"), `T1: moo{color:#fff}`)
})
}
}
func TestSCSSWithRegularCSSImport(t *testing.T) {
c := qt.New(t)
for _, test := range []struct {
name string
supports func() bool
}{
{"libsass", func() bool { return scss.Supports() }},
{"dartsass", func() bool { return dartsass.Supports() }},
} {
c.Run(test.name, func(c *qt.C) {
if !test.supports() {
c.Skip(fmt.Sprintf("Skip %s", test.name))
}
workDir, clean, err := htesting.CreateTempDir(hugofs.Os, fmt.Sprintf("hugo-scss-include-regular-%s", test.name))
c.Assert(err, qt.IsNil)
defer clean()
v := config.New()
v.Set("workingDir", workDir)
b := newTestSitesBuilder(c).WithLogger(loggers.NewErrorLogger())
// Need to use OS fs for this.
b.Fs = hugofs.NewDefault(v)
b.WithWorkingDir(workDir)
b.WithViper(v)
scssDir := filepath.Join(workDir, "assets", "scss")
c.Assert(os.MkdirAll(filepath.Join(workDir, "content", "sect"), 0777), qt.IsNil)
c.Assert(os.MkdirAll(filepath.Join(workDir, "data"), 0777), qt.IsNil)
c.Assert(os.MkdirAll(filepath.Join(workDir, "i18n"), 0777), qt.IsNil)
c.Assert(os.MkdirAll(filepath.Join(workDir, "layouts", "shortcodes"), 0777), qt.IsNil)
c.Assert(os.MkdirAll(filepath.Join(workDir, "layouts", "_default"), 0777), qt.IsNil)
c.Assert(os.MkdirAll(filepath.Join(scssDir), 0777), qt.IsNil)
b.WithSourceFile(filepath.Join(scssDir, "regular.css"), ``)
b.WithSourceFile(filepath.Join(scssDir, "another.css"), ``)
b.WithSourceFile(filepath.Join(scssDir, "_moo.scss"), `
$moolor: #fff;
moo {
color: $moolor;
}
`)
b.WithSourceFile(filepath.Join(scssDir, "main.scss"), `
@import "moo";
@import "regular.css";
@import "moo";
@import "another.css";
/* foo */
`)
b.WithTemplatesAdded("index.html", fmt.Sprintf(`
{{ $r := resources.Get "scss/main.scss" | toCSS (dict "transpiler" %q) }}
T1: {{ $r.Content | safeHTML }}
`, test.name))
b.Build(BuildCfg{})
if test.name == "libsass" {
// LibSass does not support regular CSS imports. There
// is an open bug about it that probably will never be resolved.
// Hugo works around this by preserving them in place:
b.AssertFileContent(filepath.Join(workDir, "public/index.html"), `
T1: moo {
color: #fff; }
@import "regular.css";
moo {
color: #fff; }
@import "another.css";
/* foo */
`)
} else {
// Dart Sass does not follow regular CSS import, but they
// get pulled to the top.
b.AssertFileContent(filepath.Join(workDir, "public/index.html"), `T1: @import "regular.css";
@import "another.css";
moo {
color: #fff;
}
moo {
color: #fff;
}
/* foo */`)
}
})
}
}
func TestSCSSWithThemeOverrides(t *testing.T) {
c := qt.New(t)
for _, test := range []struct {
name string
supports func() bool
}{
{"libsass", func() bool { return scss.Supports() }},
{"dartsass", func() bool { return dartsass.Supports() }},
} {
c.Run(test.name, func(c *qt.C) {
if !test.supports() {
c.Skip(fmt.Sprintf("Skip %s", test.name))
}
workDir, clean1, err := htesting.CreateTempDir(hugofs.Os, fmt.Sprintf("hugo-scss-include-theme-overrides-%s", test.name))
c.Assert(err, qt.IsNil)
defer clean1()
theme := "mytheme"
themesDir := filepath.Join(workDir, "themes")
themeDirs := filepath.Join(themesDir, theme)
v := config.New()
v.Set("workingDir", workDir)
v.Set("theme", theme)
b := newTestSitesBuilder(c).WithLogger(loggers.NewErrorLogger())
// Need to use OS fs for this.
b.Fs = hugofs.NewDefault(v)
b.WithWorkingDir(workDir)
b.WithViper(v)
fooDir := filepath.Join(workDir, "node_modules", "foo")
scssDir := filepath.Join(workDir, "assets", "scss")
scssThemeDir := filepath.Join(themeDirs, "assets", "scss")
c.Assert(os.MkdirAll(fooDir, 0777), qt.IsNil)
c.Assert(os.MkdirAll(filepath.Join(workDir, "content", "sect"), 0777), qt.IsNil)
c.Assert(os.MkdirAll(filepath.Join(workDir, "data"), 0777), qt.IsNil)
c.Assert(os.MkdirAll(filepath.Join(workDir, "i18n"), 0777), qt.IsNil)
c.Assert(os.MkdirAll(filepath.Join(workDir, "layouts", "shortcodes"), 0777), qt.IsNil)
c.Assert(os.MkdirAll(filepath.Join(workDir, "layouts", "_default"), 0777), qt.IsNil)
c.Assert(os.MkdirAll(filepath.Join(scssDir, "components"), 0777), qt.IsNil)
c.Assert(os.MkdirAll(filepath.Join(scssThemeDir, "components"), 0777), qt.IsNil)
b.WithSourceFile(filepath.Join(scssThemeDir, "components", "_imports.scss"), `
@import "moo";
@import "_boo";
@import "_zoo";
`)
b.WithSourceFile(filepath.Join(scssThemeDir, "components", "_moo.scss"), `
$moolor: #fff;
moo {
color: $moolor;
}
`)
// Only in theme.
b.WithSourceFile(filepath.Join(scssThemeDir, "components", "_zoo.scss"), `
$zoolor: pink;
zoo {
color: $zoolor;
}
`)
b.WithSourceFile(filepath.Join(scssThemeDir, "components", "_boo.scss"), `
$boolor: orange;
boo {
color: $boolor;
}
`)
b.WithSourceFile(filepath.Join(scssThemeDir, "main.scss"), `
@import "components/imports";
`)
b.WithSourceFile(filepath.Join(scssDir, "components", "_moo.scss"), `
$moolor: #ccc;
moo {
color: $moolor;
}
`)
b.WithSourceFile(filepath.Join(scssDir, "components", "_boo.scss"), `
$boolor: green;
boo {
color: $boolor;
}
`)
b.WithTemplatesAdded("index.html", fmt.Sprintf(`
{{ $cssOpts := (dict "includePaths" (slice "node_modules/foo" ) "transpiler" %q ) }}
{{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts | minify }}
T1: {{ $r.Content }}
`, test.name))
b.Build(BuildCfg{})
b.AssertFileContent(
filepath.Join(workDir, "public/index.html"),
`T1: moo{color:#ccc}boo{color:green}zoo{color:pink}`,
)
})
}
}
// https://github.com/gohugoio/hugo/issues/6274
func TestSCSSWithIncludePathsSass(t *testing.T) {
c := qt.New(t)
for _, test := range []struct {
name string
supports func() bool
}{
{"libsass", func() bool { return scss.Supports() }},
{"dartsass", func() bool { return dartsass.Supports() }},
} {
c.Run(test.name, func(c *qt.C) {
if !test.supports() {
c.Skip(fmt.Sprintf("Skip %s", test.name))
}
})
}
if !scss.Supports() {
t.Skip("Skip SCSS")
}
workDir, clean1, err := htesting.CreateTempDir(hugofs.Os, "hugo-scss-includepaths")
c.Assert(err, qt.IsNil)
defer clean1()
v := config.New()
v.Set("workingDir", workDir)
v.Set("theme", "mytheme")
b := newTestSitesBuilder(t).WithLogger(loggers.NewErrorLogger())
// Need to use OS fs for this.
b.Fs = hugofs.NewDefault(v)
b.WithWorkingDir(workDir)
b.WithViper(v)
hulmaDir := filepath.Join(workDir, "node_modules", "hulma")
scssDir := filepath.Join(workDir, "themes/mytheme/assets", "scss")
c.Assert(os.MkdirAll(hulmaDir, 0777), qt.IsNil)
c.Assert(os.MkdirAll(scssDir, 0777), qt.IsNil)
b.WithSourceFile(filepath.Join(scssDir, "main.scss"), `
@import "hulma/hulma";
`)
b.WithSourceFile(filepath.Join(hulmaDir, "hulma.sass"), `
$hulma: #ccc;
foo
color: $hulma;
`)
b.WithTemplatesAdded("index.html", `
{{ $scssOptions := (dict "targetPath" "css/styles.css" "enableSourceMap" false "includePaths" (slice "node_modules")) }}
{{ $r := resources.Get "scss/main.scss" | toCSS $scssOptions | minify }}
T1: {{ $r.Content }}
`)
b.Build(BuildCfg{})
b.AssertFileContent(filepath.Join(workDir, "public/index.html"), `T1: foo{color:#ccc}`)
}
func TestResourceChainBasic(t *testing.T) {
ts := httptest.NewServer(http.FileServer(http.Dir("testdata/")))
t.Cleanup(func() {

View file

@ -0,0 +1,173 @@
// 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 dartsass_test
import (
"testing"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/hugolib"
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/dartsass"
)
func TestTransformIncludePaths(t *testing.T) {
if !dartsass.Supports() {
t.Skip()
}
c := qt.New(t)
files := `
-- assets/scss/main.scss --
@import "moo";
-- node_modules/foo/_moo.scss --
$moolor: #fff;
moo {
color: $moolor;
}
-- config.toml --
-- layouts/index.html --
{{ $cssOpts := (dict "includePaths" (slice "node_modules/foo") "transpiler" "dartsass" ) }}
{{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts | minify }}
T1: {{ $r.Content }}
`
b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: c,
TxtarString: files,
NeedsOsFS: true,
}).Build()
b.AssertFileContent("public/index.html", `T1: moo{color:#fff}`)
}
func TestTransformImportRegularCSS(t *testing.T) {
if !dartsass.Supports() {
t.Skip()
}
c := qt.New(t)
files := `
-- assets/scss/_moo.scss --
$moolor: #fff;
moo {
color: $moolor;
}
-- assets/scss/another.css --
-- assets/scss/main.scss --
@import "moo";
@import "regular.css";
@import "moo";
@import "another.css";
/* foo */
-- assets/scss/regular.css --
-- config.toml --
-- layouts/index.html --
{{ $r := resources.Get "scss/main.scss" | toCSS (dict "transpiler" "dartsass") }}
T1: {{ $r.Content | safeHTML }}
`
b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: c,
TxtarString: files,
NeedsOsFS: true,
},
).Build()
// Dart Sass does not follow regular CSS import, but they
// get pulled to the top.
b.AssertFileContent("public/index.html", `T1: @import "regular.css";
@import "another.css";
moo {
color: #fff;
}
moo {
color: #fff;
}
/* foo */`)
}
func TestTransformThemeOverrides(t *testing.T) {
if !dartsass.Supports() {
t.Skip()
}
c := qt.New(t)
files := `
-- assets/scss/components/_boo.scss --
$boolor: green;
boo {
color: $boolor;
}
-- assets/scss/components/_moo.scss --
$moolor: #ccc;
moo {
color: $moolor;
}
-- config.toml --
theme = 'mytheme'
-- layouts/index.html --
{{ $cssOpts := (dict "includePaths" (slice "node_modules/foo" ) "transpiler" "dartsass" ) }}
{{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts | minify }}
T1: {{ $r.Content }}
-- themes/mytheme/assets/scss/components/_boo.scss --
$boolor: orange;
boo {
color: $boolor;
}
-- themes/mytheme/assets/scss/components/_imports.scss --
@import "moo";
@import "_boo";
@import "_zoo";
-- themes/mytheme/assets/scss/components/_moo.scss --
$moolor: #fff;
moo {
color: $moolor;
}
-- themes/mytheme/assets/scss/components/_zoo.scss --
$zoolor: pink;
zoo {
color: $zoolor;
}
-- themes/mytheme/assets/scss/main.scss --
@import "components/imports";
`
b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: c,
TxtarString: files,
NeedsOsFS: true,
},
).Build()
b.AssertFileContent("public/index.html", `T1: moo{color:#ccc}boo{color:green}zoo{color:pink}`)
}

View file

@ -0,0 +1,173 @@
// 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 scss_test
import (
"testing"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/hugolib"
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/scss"
)
func TestTransformIncludePaths(t *testing.T) {
if !scss.Supports() {
t.Skip()
}
c := qt.New(t)
files := `
-- assets/scss/main.scss --
@import "moo";
-- node_modules/foo/_moo.scss --
$moolor: #fff;
moo {
color: $moolor;
}
-- config.toml --
-- layouts/index.html --
{{ $cssOpts := (dict "includePaths" (slice "node_modules/foo") ) }}
{{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts | minify }}
T1: {{ $r.Content }}
`
b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: c,
TxtarString: files,
NeedsOsFS: true,
}).Build()
b.AssertFileContent("public/index.html", `T1: moo{color:#fff}`)
}
func TestTransformImportRegularCSS(t *testing.T) {
if !scss.Supports() {
t.Skip()
}
c := qt.New(t)
files := `
-- assets/scss/_moo.scss --
$moolor: #fff;
moo {
color: $moolor;
}
-- assets/scss/another.css --
-- assets/scss/main.scss --
@import "moo";
@import "regular.css";
@import "moo";
@import "another.css";
/* foo */
-- assets/scss/regular.css --
-- config.toml --
-- layouts/index.html --
{{ $r := resources.Get "scss/main.scss" | toCSS }}
T1: {{ $r.Content | safeHTML }}
`
b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: c,
TxtarString: files,
NeedsOsFS: true,
}).Build()
// LibSass does not support regular CSS imports. There
// is an open bug about it that probably will never be resolved.
// Hugo works around this by preserving them in place:
b.AssertFileContent("public/index.html", `
T1: moo {
color: #fff; }
@import "regular.css";
moo {
color: #fff; }
@import "another.css";
/* foo */
`)
}
func TestTransformThemeOverrides(t *testing.T) {
if !scss.Supports() {
t.Skip()
}
c := qt.New(t)
files := `
-- assets/scss/components/_boo.scss --
$boolor: green;
boo {
color: $boolor;
}
-- assets/scss/components/_moo.scss --
$moolor: #ccc;
moo {
color: $moolor;
}
-- config.toml --
theme = 'mytheme'
-- layouts/index.html --
{{ $cssOpts := (dict "includePaths" (slice "node_modules/foo" ) "transpiler" "dartsass" ) }}
{{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts | minify }}
T1: {{ $r.Content }}
-- themes/mytheme/assets/scss/components/_boo.scss --
$boolor: orange;
boo {
color: $boolor;
}
-- themes/mytheme/assets/scss/components/_imports.scss --
@import "moo";
@import "_boo";
@import "_zoo";
-- themes/mytheme/assets/scss/components/_moo.scss --
$moolor: #fff;
moo {
color: $moolor;
}
-- themes/mytheme/assets/scss/components/_zoo.scss --
$zoolor: pink;
zoo {
color: $zoolor;
}
-- themes/mytheme/assets/scss/main.scss --
@import "components/imports";
`
b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: c,
TxtarString: files,
NeedsOsFS: true,
}).Build()
b.AssertFileContent("public/index.html", `T1: moo{color:#ccc}boo{color:green}zoo{color:pink}`)
}