// Copyright 2019 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 hugolib
import (
"fmt"
"math/rand"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/bep/logg"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/modules/npm"
"github.com/spf13/afero"
"github.com/gohugoio/hugo/common/hugo"
"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/htesting"
"github.com/gohugoio/hugo/hugofs"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/testmodBuilder/mods"
)
func TestHugoModulesVariants(t *testing.T) {
if !htesting.IsCI() {
t.Skip("skip (relative) long running modules test when running locally")
}
tomlConfig := `
baseURL="https://example.org"
workingDir = %q
[module]
[[module.imports]]
path="github.com/gohugoio/hugoTestModule2"
%s
`
createConfig := func(workingDir, moduleOpts string) string {
return fmt.Sprintf(tomlConfig, workingDir, moduleOpts)
}
newTestBuilder := func(t testing.TB, moduleOpts string) *sitesBuilder {
b := newTestSitesBuilder(t)
tempDir := t.TempDir()
workingDir := filepath.Join(tempDir, "myhugosite")
b.Assert(os.MkdirAll(workingDir, 0o777), qt.IsNil)
cfg := config.New()
cfg.Set("workingDir", workingDir)
cfg.Set("publishDir", "public")
b.Fs = hugofs.NewDefault(cfg)
b.WithWorkingDir(workingDir).WithConfigFile("toml", createConfig(workingDir, moduleOpts))
b.WithTemplates(
"index.html", `
Param from module: {{ site.Params.Hugo }}|
{{ $js := resources.Get "jslibs/alpinejs/alpine.js" }}
JS imported in module: {{ with $js }}{{ .RelPermalink }}{{ end }}|
`,
"_default/single.html", `{{ .Content }}`)
b.WithContent("p1.md", `---
title: "Page"
---
[A link](https://bep.is)
`)
b.WithSourceFile("go.mod", `
module github.com/gohugoio/tests/testHugoModules
`)
b.WithSourceFile("go.sum", `
github.com/gohugoio/hugoTestModule2 v0.0.0-20200131160637-9657d7697877 h1:WLM2bQCKIWo04T6NsIWsX/Vtirhf0TnpY66xyqGlgVY=
github.com/gohugoio/hugoTestModule2 v0.0.0-20200131160637-9657d7697877/go.mod h1:CBFZS3khIAXKxReMwq0le8sEl/D8hcXmixlOHVv+Gd0=
`)
return b
}
t.Run("Target in subfolder", func(t *testing.T) {
b := newTestBuilder(t, "ignoreImports=true")
b.Build(BuildCfg{})
b.AssertFileContent("public/p1/index.html", `<p>Page|https://bep.is|Title: |Text: A link|END</p>`)
})
t.Run("Ignore config", func(t *testing.T) {
b := newTestBuilder(t, "ignoreConfig=true")
b.Build(BuildCfg{})
b.AssertFileContent("public/index.html", `
Param from module: |
JS imported in module: |
`)
})
t.Run("Ignore imports", func(t *testing.T) {
b := newTestBuilder(t, "ignoreImports=true")
b.Build(BuildCfg{})
b.AssertFileContent("public/index.html", `
Param from module: Rocks|
JS imported in module: |
`)
})
t.Run("Create package.json", func(t *testing.T) {
b := newTestBuilder(t, "")
b.WithSourceFile("package.json", `{
"name": "mypack",
"version": "1.2.3",
"scripts": {
"client": "wait-on http://localhost:1313 && open http://localhost:1313",
"start": "run-p client server",
"test": "echo 'hoge' > hoge"
},
"dependencies": {
"nonon": "error"
}
}`)
b