2022-02-09 07:41:04 -05:00
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-04-16 04:40:21 -04:00
|
|
|
"encoding/base64"
|
2023-01-04 12:24:36 -05:00
|
|
|
"errors"
|
2022-02-09 07:41:04 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-03-17 12:22:34 -04:00
|
|
|
"regexp"
|
2022-02-09 07:41:04 -05:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
|
|
|
|
|
|
|
qt "github.com/frankban/quicktest"
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
"github.com/gohugoio/hugo/common/herrors"
|
|
|
|
"github.com/gohugoio/hugo/common/hexec"
|
|
|
|
"github.com/gohugoio/hugo/common/loggers"
|
2023-01-04 12:24:36 -05:00
|
|
|
"github.com/gohugoio/hugo/common/maps"
|
2022-02-09 07:41:04 -05:00
|
|
|
"github.com/gohugoio/hugo/config"
|
2023-01-04 12:24:36 -05:00
|
|
|
"github.com/gohugoio/hugo/config/allconfig"
|
2022-02-09 07:41:04 -05:00
|
|
|
"github.com/gohugoio/hugo/config/security"
|
|
|
|
"github.com/gohugoio/hugo/deps"
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
|
|
|
"github.com/gohugoio/hugo/htesting"
|
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"golang.org/x/tools/txtar"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewIntegrationTestBuilder(conf IntegrationTestConfig) *IntegrationTestBuilder {
|
2022-02-26 04:42:21 -05:00
|
|
|
// Code fences.
|
|
|
|
conf.TxtarString = strings.ReplaceAll(conf.TxtarString, "§§§", "```")
|
2023-02-24 01:23:10 -05:00
|
|
|
// Multiline strings.
|
|
|
|
conf.TxtarString = strings.ReplaceAll(conf.TxtarString, "§§", "`")
|
2022-02-26 04:42:21 -05:00
|
|
|
|
2022-02-09 07:41:04 -05:00
|
|
|
data := txtar.Parse([]byte(conf.TxtarString))
|
|
|
|
|
|
|
|
c, ok := conf.T.(*qt.C)
|
|
|
|
if !ok {
|
|
|
|
c = qt.New(conf.T)
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.NeedsOsFS {
|
2022-05-27 07:23:37 -04:00
|
|
|
if !filepath.IsAbs(conf.WorkingDir) {
|
|
|
|
tempDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-integration-test")
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
conf.WorkingDir = filepath.Join(tempDir, conf.WorkingDir)
|
|
|
|
if !conf.PrintAndKeepTempDir {
|
|
|
|
c.Cleanup(clean)
|
|
|
|
} else {
|
|
|
|
fmt.Println("\nUsing WorkingDir dir:", conf.WorkingDir)
|
|
|
|
}
|
2022-02-09 07:41:04 -05:00
|
|
|
}
|
2022-03-21 04:35:15 -04:00
|
|
|
} else if conf.WorkingDir == "" {
|
|
|
|
conf.WorkingDir = helpers.FilePathSeparator
|
2022-02-09 07:41:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return &IntegrationTestBuilder{
|
|
|
|
Cfg: conf,
|
|
|
|
C: c,
|
|
|
|
data: data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntegrationTestBuilder is a (partial) rewrite of sitesBuilder.
|
|
|
|
// The main problem with the "old" one was that it was that the test data was often a little hidden,
|
|
|
|
// so it became hard to look at a test and determine what it should do, especially coming back to the
|
|
|
|
// test after a year or so.
|
|
|
|
type IntegrationTestBuilder struct {
|
|
|
|
*qt.C
|
|
|
|
|
|
|
|
data *txtar.Archive
|
|
|
|
|
|
|
|
fs *hugofs.Fs
|
|
|
|
H *HugoSites
|
|
|
|
|
|
|
|
Cfg IntegrationTestConfig
|
|
|
|
|
|
|
|
changedFiles []string
|
|
|
|
createdFiles []string
|
|
|
|
removedFiles []string
|
|
|
|
renamedFiles []string
|
|
|
|
|
|
|
|
buildCount int
|
2023-03-04 05:09:03 -05:00
|
|
|
GCCount int
|
2022-02-09 07:41:04 -05:00
|
|
|
counters *testCounters
|
|
|
|
logBuff lockingBuffer
|
|
|
|
|
|
|
|
builderInit sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
type lockingBuffer struct {
|
|
|
|
sync.Mutex
|
|
|
|
bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *lockingBuffer) Write(p []byte) (n int, err error) {
|
|
|
|
b.Lock()
|
|
|
|
n, err = b.Buffer.Write(p)
|
|
|
|
b.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) AssertLogContains(text string) {
|
|
|
|
s.Helper()
|
|
|
|
s.Assert(s.logBuff.String(), qt.Contains, text)
|
|
|
|
}
|
|
|
|
|
2022-03-17 12:22:34 -04:00
|
|
|
func (s *IntegrationTestBuilder) AssertLogMatches(expression string) {
|
|
|
|
s.Helper()
|
|
|
|
re := regexp.MustCompile(expression)
|
|
|
|
s.Assert(re.MatchString(s.logBuff.String()), qt.IsTrue, qt.Commentf(s.logBuff.String()))
|
|
|
|
}
|
|
|
|
|
2022-02-09 07:41:04 -05:00
|
|
|
func (s *IntegrationTestBuilder) AssertBuildCountData(count int) {
|
|
|
|
s.Helper()
|
|
|
|
s.Assert(s.H.init.data.InitCount(), qt.Equals, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) AssertBuildCountGitInfo(count int) {
|
|
|
|
s.Helper()
|
|
|
|
s.Assert(s.H.init.gitInfo.InitCount(), qt.Equals, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) AssertBuildCountLayouts(count int) {
|
|
|
|
s.Helper()
|
|
|
|
s.Assert(s.H.init.layouts.InitCount(), qt.Equals, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) AssertBuildCountTranslations(count int) {
|
|
|
|
s.Helper()
|
|
|
|
s.Assert(s.H.init.translations.InitCount(), qt.Equals, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) AssertFileContent(filename string, matches ...string) {
|
|
|
|
s.Helper()
|
|
|
|
content := strings.TrimSpace(s.FileContent(filename))
|
|
|
|
for _, m := range matches {
|
|
|
|
lines := strings.Split(m, "\n")
|
|
|
|
for _, match := range lines {
|
|
|
|
match = strings.TrimSpace(match)
|
|
|
|
if match == "" || strings.HasPrefix(match, "#") {
|
|
|
|
continue
|
|
|
|
}
|
2022-02-17 07:04:00 -05:00
|
|
|
s.Assert(content, qt.Contains, match, qt.Commentf(m))
|
2022-02-09 07:41:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-11 07:13:18 -05:00
|
|
|
func (s *IntegrationTestBuilder) AssertFileContentExact(filename string, matches ...string) {
|
|
|
|
s.Helper()
|
|
|
|
content := s.FileContent(filename)
|
|
|
|
for _, m := range matches {
|
|
|
|
s.Assert(content, qt.Contains, m, qt.Commentf(m))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 07:41:04 -05:00
|
|
|
func (s *IntegrationTestBuilder) AssertDestinationExists(filename string, b bool) {
|
|
|
|
checker := qt.IsTrue
|
|
|
|
if !b {
|
|
|
|
checker = qt.IsFalse
|
|
|
|
}
|
|
|
|
s.Assert(s.destinationExists(filepath.Clean(filename)), checker)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) destinationExists(filename string) bool {
|
2022-03-21 04:35:15 -04:00
|
|
|
b, err := helpers.Exists(filename, s.fs.PublishDir)
|
2022-02-09 07:41:04 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2022-05-15 05:40:34 -04:00
|
|
|
func (s *IntegrationTestBuilder) AssertIsFileError(err error) herrors.FileError {
|
2022-05-02 10:07:52 -04:00
|
|
|
s.Assert(err, qt.ErrorAs, new(herrors.FileError))
|
2022-05-15 05:40:34 -04:00
|
|
|
return herrors.UnwrapFileError(err)
|
2022-02-09 07:41:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) AssertRenderCountContent(count int) {
|
|
|
|
s.Helper()
|
|
|
|
s.Assert(s.counters.contentRenderCounter, qt.Equals, uint64(count))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) AssertRenderCountPage(count int) {
|
|
|
|
s.Helper()
|
|
|
|
s.Assert(s.counters.pageRenderCounter, qt.Equals, uint64(count))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) Build() *IntegrationTestBuilder {
|
|
|
|
s.Helper()
|
|
|
|
_, err := s.BuildE()
|
2022-02-17 07:04:00 -05:00
|
|
|
if s.Cfg.Verbose || err != nil {
|
2022-02-09 07:41:04 -05:00
|
|
|
fmt.Println(s.logBuff.String())
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
s.Assert(err, qt.IsNil)
|
2023-03-04 05:09:03 -05:00
|
|
|
if s.Cfg.RunGC {
|
|
|
|
s.GCCount, err = s.H.GC()
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
2022-02-09 07:41:04 -05:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) BuildE() (*IntegrationTestBuilder, error) {
|
|
|
|
s.Helper()
|
2022-06-04 11:39:34 -04:00
|
|
|
if err := s.initBuilder(); err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
2023-05-15 02:39:52 -04:00
|
|
|
err := s.build(s.Cfg.BuildCfg)
|
2022-02-09 07:41:04 -05:00
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type IntegrationTestDebugConfig struct {
|
|
|
|
Out io.Writer
|
|
|
|
|
|
|
|
PrintDestinationFs bool
|
|
|
|
PrintPagemap bool
|
|
|
|
|
|
|
|
PrefixDestinationFs string
|
|
|
|
PrefixPagemap string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) EditFileReplace(filename string, replacementFunc func(s string) string) *IntegrationTestBuilder {
|
|
|
|
absFilename := s.absFilename(filename)
|
|
|
|
b, err := afero.ReadFile(s.fs.Source, absFilename)
|
|
|
|
s.Assert(err, qt.IsNil)
|
|
|
|
s.changedFiles = append(s.changedFiles, absFilename)
|
|
|
|
oldContent := string(b)
|
|
|
|
s.writeSource(absFilename, replacementFunc(oldContent))
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) EditFiles(filenameContent ...string) *IntegrationTestBuilder {
|
|
|
|
for i := 0; i < len(filenameContent); i += 2 {
|
|
|
|
filename, content := filepath.FromSlash(filenameContent[i]), filenameContent[i+1]
|
|
|
|
absFilename := s.absFilename(filename)
|
|
|
|
s.changedFiles = append(s.changedFiles, absFilename)
|
|
|
|
s.writeSource(absFilename, content)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) AddFiles(filenameContent ...string) *IntegrationTestBuilder {
|
|
|
|
for i := 0; i < len(filenameContent); i += 2 {
|
|
|
|
filename, content := filepath.FromSlash(filenameContent[i]), filenameContent[i+1]
|
|
|
|
absFilename := s.absFilename(filename)
|
|
|
|
s.createdFiles = append(s.createdFiles, absFilename)
|
|
|
|
s.writeSource(absFilename, content)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) RemoveFiles(filenames ...string) *IntegrationTestBuilder {
|
|
|
|
for _, filename := range filenames {
|
|
|
|
absFilename := s.absFilename(filename)
|
|
|
|
s.removedFiles = append(s.removedFiles, absFilename)
|
|
|
|
s.Assert(s.fs.Source.Remove(absFilename), qt.IsNil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) RenameFile(old, new string) *IntegrationTestBuilder {
|
|
|
|
absOldFilename := s.absFilename(old)
|
|
|
|
absNewFilename := s.absFilename(new)
|
|
|
|
s.renamedFiles = append(s.renamedFiles, absOldFilename)
|
|
|
|
s.createdFiles = append(s.createdFiles, absNewFilename)
|
2023-03-04 05:09:03 -05:00
|
|
|
s.Assert(s.fs.Source.MkdirAll(filepath.Dir(absNewFilename), 0777), qt.IsNil)
|
2022-02-09 07:41:04 -05:00
|
|
|
s.Assert(s.fs.Source.Rename(absOldFilename, absNewFilename), qt.IsNil)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) FileContent(filename string) string {
|
|
|
|
s.Helper()
|
2022-03-21 04:35:15 -04:00
|
|
|
return s.readWorkingDir(s, s.fs, filepath.FromSlash(filename))
|
2022-02-09 07:41:04 -05:00
|
|
|
}
|
|
|
|
|
2022-06-04 11:39:34 -04:00
|
|
|
func (s *IntegrationTestBuilder) initBuilder() error {
|
|
|
|
var initErr error
|
2022-02-09 07:41:04 -05:00
|
|
|
s.builderInit.Do(func() {
|
|
|
|
var afs afero.Fs
|
|
|
|
if s.Cfg.NeedsOsFS {
|
|
|
|
afs = afero.NewOsFs()
|
|
|
|
} else {
|
|
|
|
afs = afero.NewMemMapFs()
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Cfg.LogLevel == 0 {
|
|
|
|
s.Cfg.LogLevel = jww.LevelWarn
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := loggers.NewBasicLoggerForWriter(s.Cfg.LogLevel, &s.logBuff)
|
|
|
|
|
2022-04-16 04:40:21 -04:00
|
|
|
isBinaryRe := regexp.MustCompile(`^(.*)(\.png|\.jpg)$`)
|
|
|
|
|
2022-02-09 07:41:04 -05:00
|
|
|
for _, f := range s.data.Files {
|
|
|
|
filename := filepath.Join(s.Cfg.WorkingDir, f.Name)
|
2022-04-16 04:40:21 -04:00
|
|
|
data := bytes.TrimSuffix(f.Data, []byte("\n"))
|
|
|
|
if isBinaryRe.MatchString(filename) {
|
|
|
|
var err error
|
|
|
|
data, err = base64.StdEncoding.DecodeString(string(data))
|
|
|
|
s.Assert(err, qt.IsNil)
|
|
|
|
|
|
|
|
}
|
2022-02-09 07:41:04 -05:00
|
|
|
s.Assert(afs.MkdirAll(filepath.Dir(filename), 0777), qt.IsNil)
|
2022-04-16 04:40:21 -04:00
|
|
|
s.Assert(afero.WriteFile(afs, filename, data, 0666), qt.IsNil)
|
2022-02-09 07:41:04 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
configDir := "config"
|
|
|
|
if _, err := afs.Stat(filepath.Join(s.Cfg.WorkingDir, "config")); err != nil {
|
|
|
|
configDir = ""
|
2023-01-15 12:45:51 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
var flags config.Provider
|
|
|
|
if s.Cfg.BaseCfg != nil {
|
|
|
|
flags = s.Cfg.BaseCfg
|
|
|
|
} else {
|
|
|
|
flags = config.New()
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Cfg.Running {
|
|
|
|
flags.Set("internal", maps.Params{
|
|
|
|
"running": s.Cfg.Running,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Cfg.WorkingDir != "" {
|
|
|
|
flags.Set("workingDir", s.Cfg.WorkingDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := allconfig.LoadConfig(
|
|
|
|
allconfig.ConfigSourceDescriptor{
|
|
|
|
Flags: flags,
|
|
|
|
ConfigDir: configDir,
|
|
|
|
Fs: afs,
|
|
|
|
Logger: logger,
|
|
|
|
Environ: s.Cfg.Environ,
|
2022-02-09 07:41:04 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
if err != nil {
|
|
|
|
initErr = err
|
|
|
|
return
|
|
|
|
}
|
2022-02-09 07:41:04 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
fs := hugofs.NewFrom(afs, res.LoadingInfo.BaseConfig)
|
2022-03-21 04:35:15 -04:00
|
|
|
|
|
|
|
s.Assert(err, qt.IsNil)
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
depsCfg := deps.DepsCfg{Configs: res, Fs: fs, Logger: logger}
|
2022-02-09 07:41:04 -05:00
|
|
|
sites, err := NewHugoSites(depsCfg)
|
2022-06-04 11:39:34 -04:00
|
|
|
if err != nil {
|
|
|
|
initErr = err
|
|
|
|
return
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
if sites == nil {
|
|
|
|
initErr = errors.New("no sites")
|
|
|
|
return
|
|
|
|
}
|
2022-02-09 07:41:04 -05:00
|
|
|
|
|
|
|
s.H = sites
|
|
|
|
s.fs = fs
|
|
|
|
|
|
|
|
if s.Cfg.NeedsNpmInstall {
|
|
|
|
wd, _ := os.Getwd()
|
|
|
|
s.Assert(os.Chdir(s.Cfg.WorkingDir), qt.IsNil)
|
|
|
|
s.C.Cleanup(func() { os.Chdir(wd) })
|
|
|
|
sc := security.DefaultConfig
|
|
|
|
sc.Exec.Allow = security.NewWhitelist("npm")
|
|
|
|
ex := hexec.New(sc)
|
|
|
|
command, err := ex.New("npm", "install")
|
|
|
|
s.Assert(err, qt.IsNil)
|
|
|
|
s.Assert(command.Run(), qt.IsNil)
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
2022-06-04 11:39:34 -04:00
|
|
|
|
|
|
|
return initErr
|
2022-02-09 07:41:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) absFilename(filename string) string {
|
|
|
|
filename = filepath.FromSlash(filename)
|
|
|
|
if filepath.IsAbs(filename) {
|
|
|
|
return filename
|
|
|
|
}
|
|
|
|
if s.Cfg.WorkingDir != "" && !strings.HasPrefix(filename, s.Cfg.WorkingDir) {
|
|
|
|
filename = filepath.Join(s.Cfg.WorkingDir, filename)
|
|
|
|
}
|
|
|
|
return filename
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) build(cfg BuildCfg) error {
|
|
|
|
s.Helper()
|
|
|
|
defer func() {
|
|
|
|
s.changedFiles = nil
|
|
|
|
s.createdFiles = nil
|
|
|
|
s.removedFiles = nil
|
|
|
|
s.renamedFiles = nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
changeEvents := s.changeEvents()
|
|
|
|
s.logBuff.Reset()
|
|
|
|
s.counters = &testCounters{}
|
|
|
|
cfg.testCounters = s.counters
|
|
|
|
|
|
|
|
if s.buildCount > 0 && (len(changeEvents) == 0) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s.buildCount++
|
|
|
|
|
|
|
|
err := s.H.Build(cfg, changeEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logErrorCount := s.H.NumLogErrors()
|
|
|
|
if logErrorCount > 0 {
|
|
|
|
return fmt.Errorf("logged %d error(s): %s", logErrorCount, s.logBuff.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) changeEvents() []fsnotify.Event {
|
|
|
|
var events []fsnotify.Event
|
|
|
|
for _, v := range s.removedFiles {
|
|
|
|
events = append(events, fsnotify.Event{
|
|
|
|
Name: v,
|
|
|
|
Op: fsnotify.Remove,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, v := range s.renamedFiles {
|
|
|
|
events = append(events, fsnotify.Event{
|
|
|
|
Name: v,
|
|
|
|
Op: fsnotify.Rename,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, v := range s.changedFiles {
|
|
|
|
events = append(events, fsnotify.Event{
|
|
|
|
Name: v,
|
|
|
|
Op: fsnotify.Write,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, v := range s.createdFiles {
|
|
|
|
events = append(events, fsnotify.Event{
|
|
|
|
Name: v,
|
|
|
|
Op: fsnotify.Create,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return events
|
|
|
|
}
|
|
|
|
|
2022-03-21 04:35:15 -04:00
|
|
|
func (s *IntegrationTestBuilder) readWorkingDir(t testing.TB, fs *hugofs.Fs, filename string) string {
|
2022-02-09 07:41:04 -05:00
|
|
|
t.Helper()
|
2022-03-21 04:35:15 -04:00
|
|
|
return s.readFileFromFs(t, fs.WorkingDirReadOnly, filename)
|
2022-02-09 07:41:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) readFileFromFs(t testing.TB, fs afero.Fs, filename string) string {
|
|
|
|
t.Helper()
|
|
|
|
filename = filepath.Clean(filename)
|
|
|
|
b, err := afero.ReadFile(fs, filename)
|
|
|
|
if err != nil {
|
|
|
|
// Print some debug info
|
|
|
|
hadSlash := strings.HasPrefix(filename, helpers.FilePathSeparator)
|
|
|
|
start := 0
|
|
|
|
if hadSlash {
|
|
|
|
start = 1
|
|
|
|
}
|
|
|
|
end := start + 1
|
|
|
|
|
|
|
|
parts := strings.Split(filename, helpers.FilePathSeparator)
|
|
|
|
if parts[start] == "work" {
|
|
|
|
end++
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Assert(err, qt.IsNil)
|
|
|
|
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) writeSource(filename, content string) {
|
|
|
|
s.Helper()
|
|
|
|
s.writeToFs(s.fs.Source, filename, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestBuilder) writeToFs(fs afero.Fs, filename, content string) {
|
|
|
|
s.Helper()
|
|
|
|
if err := afero.WriteFile(fs, filepath.FromSlash(filename), []byte(content), 0755); err != nil {
|
|
|
|
s.Fatalf("Failed to write file: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type IntegrationTestConfig struct {
|
|
|
|
T testing.TB
|
|
|
|
|
|
|
|
// The files to use on txtar format, see
|
|
|
|
// https://pkg.go.dev/golang.org/x/exp/cmd/txtar
|
|
|
|
TxtarString string
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
// COnfig to use as the base. We will also read the config from the txtar.
|
|
|
|
BaseCfg config.Provider
|
|
|
|
|
|
|
|
// Environment variables passed to the config loader.
|
|
|
|
Environ []string
|
|
|
|
|
2022-02-09 07:41:04 -05:00
|
|
|
// Whether to simulate server mode.
|
|
|
|
Running bool
|
|
|
|
|
|
|
|
// Will print the log buffer after the build
|
|
|
|
Verbose bool
|
|
|
|
|
|
|
|
LogLevel jww.Threshold
|
|
|
|
|
|
|
|
// Whether it needs the real file system (e.g. for js.Build tests).
|
|
|
|
NeedsOsFS bool
|
|
|
|
|
2023-03-04 05:09:03 -05:00
|
|
|
// Whether to run GC after each build.
|
|
|
|
RunGC bool
|
|
|
|
|
2022-04-16 04:40:21 -04:00
|
|
|
// Do not remove the temp dir after the test.
|
|
|
|
PrintAndKeepTempDir bool
|
|
|
|
|
2022-02-09 07:41:04 -05:00
|
|
|
// Whether to run npm install before Build.
|
|
|
|
NeedsNpmInstall bool
|
|
|
|
|
|
|
|
WorkingDir string
|
2023-05-15 02:39:52 -04:00
|
|
|
|
|
|
|
BuildCfg BuildCfg
|
2022-02-09 07:41:04 -05:00
|
|
|
}
|