package hugolib
import (
"io/ioutil"
"path/filepath"
"runtime"
"testing"
"bytes"
"fmt"
"regexp"
"strings"
"text/template"
"github.com/gohugoio/hugo/langs"
"github.com/sanity-io/litter"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/deps"
"github.com/spf13/afero"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/tpl"
"github.com/spf13/viper"
"os"
"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/hugofs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type sitesBuilder struct {
Cfg config.Provider
Fs *hugofs.Fs
T testing.TB
logger *loggers.Logger
dumper litter.Options
// Aka the Hugo server mode.
running bool
H *HugoSites
theme string
// Default toml
configFormat string
// Default is empty.
// TODO(bep) revisit this and consider always setting it to something.
// Consider this in relation to using the BaseFs.PublishFs to all publishing.
workingDir string
// Base data/content
contentFilePairs []string
templateFilePairs []string
i18nFilePairs []string
dataFilePairs []string
// Additional data/content.
// As in "use the base, but add these on top".
contentFilePairsAdded []string
templateFilePairsAdded []string
i18nFilePairsAdded []string
dataFilePairsAdded []string
}
func newTestSitesBuilder(t testing.TB) *sitesBuilder {
v := viper.New()
fs := hugofs.NewMem(v)
litterOptions := litter.Options{
HidePrivateFields: true,
StripPackageNames: true,
Separator: " ",
}
return &sitesBuilder{T: t, Fs: fs, configFormat: "toml", dumper: litterOptions}
}
func createTempDir(prefix string) (string, func(), error) {
workDir, err := ioutil.TempDir("", prefix)
if err != nil {
return "", nil, err
}
if runtime.GOOS == "darwin" && !strings.HasPrefix(workDir, "/private") {
// To get the entry folder in line with the rest. This its a little bit
// mysterious, but so be it.
workDir = "/private" + workDir
}
return workDir, func() { os.RemoveAll(workDir) }, nil
}
func (s *sitesBuilder) Running() *sitesBuilder {
s.running = true
return s
}
func (s *sitesBuilder) WithLogger(logger *loggers.Logger) *sitesBuilder {
s.logger = logger
return s
}
func (s *sitesBuilder) WithWorkingDir(dir string) *sitesBuilder {
s.workingDir = dir
return s
}
func (s *sitesBuilder) WithConfigTemplate(data interface{}, format, configTemplate string) *sitesBuilder {
if format == "" {
format = "toml"
}
templ, err := template.New("test").Parse(configTemplate)
if err != nil {
s.Fatalf("Template parse failed: %s", err)
}
var b bytes.Buffer
templ.Execute(&b, data)
return s.WithConfigFile(format, b.String())
}
func (s *sitesBuilder) WithViper(v *viper.Viper) *sitesBuilder {
loadDefaultSettingsFor(v)
s.Cfg = v
return s
}
func (s *sitesBuilder) WithConfigFile(format, conf string) *sitesBuilder {
writeSource(s.T, s.Fs, "config."+format, conf)
s.configFormat = format
return s
}
func (s *sitesBuilder) WithThemeConfigFile(format, conf string) *sitesBuilder {
if s.theme == "" {
s.theme = "test-theme"
}
filename := filepath.Join("themes", s.theme, "config."+format)
writeSource(s.T, s.Fs, filename, conf)
return s
}
func (s *sitesBuilder) WithSourceFile(filename, content str