Add some convenient integration test helpers

This commit is contained in:
Bjørn Erik Pedersen 2023-10-18 18:54:15 +02:00
parent 2eca1b3cc1
commit fd38171810
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F

View file

@ -32,6 +32,16 @@ import (
"golang.org/x/tools/txtar" "golang.org/x/tools/txtar"
) )
// Test is a convenience method to create a new IntegrationTestBuilder from some files and run a build.
func Test(t testing.TB, files string) *IntegrationTestBuilder {
return NewIntegrationTestBuilder(IntegrationTestConfig{T: t, TxtarString: files}).Build()
}
// TestRunning is a convenience method to create a new IntegrationTestBuilder from some files with Running set to true and run a build.
func TestRunning(t testing.TB, files string) *IntegrationTestBuilder {
return NewIntegrationTestBuilder(IntegrationTestConfig{T: t, TxtarString: files, Running: true}).Build()
}
func NewIntegrationTestBuilder(conf IntegrationTestConfig) *IntegrationTestBuilder { func NewIntegrationTestBuilder(conf IntegrationTestConfig) *IntegrationTestBuilder {
// Code fences. // Code fences.
conf.TxtarString = strings.ReplaceAll(conf.TxtarString, "§§§", "```") conf.TxtarString = strings.ReplaceAll(conf.TxtarString, "§§§", "```")
@ -152,7 +162,6 @@ func (s *IntegrationTestBuilder) AssertFileCount(dirname string, expected int) {
return nil return nil
}) })
s.Assert(count, qt.Equals, expected) s.Assert(count, qt.Equals, expected)
} }
func (s *IntegrationTestBuilder) AssertFileContent(filename string, matches ...string) { func (s *IntegrationTestBuilder) AssertFileContent(filename string, matches ...string) {
@ -251,7 +260,6 @@ func (s *IntegrationTestBuilder) Init() *IntegrationTestBuilder {
s.Fatalf("Failed to init builder: %s", err) s.Fatalf("Failed to init builder: %s", err)
} }
return s return s
} }
type IntegrationTestDebugConfig struct { type IntegrationTestDebugConfig struct {
@ -310,7 +318,7 @@ func (s *IntegrationTestBuilder) RenameFile(old, new string) *IntegrationTestBui
absNewFilename := s.absFilename(new) absNewFilename := s.absFilename(new)
s.renamedFiles = append(s.renamedFiles, absOldFilename) s.renamedFiles = append(s.renamedFiles, absOldFilename)
s.createdFiles = append(s.createdFiles, absNewFilename) s.createdFiles = append(s.createdFiles, absNewFilename)
s.Assert(s.fs.Source.MkdirAll(filepath.Dir(absNewFilename), 0777), qt.IsNil) s.Assert(s.fs.Source.MkdirAll(filepath.Dir(absNewFilename), 0o777), qt.IsNil)
s.Assert(s.fs.Source.Rename(absOldFilename, absNewFilename), qt.IsNil) s.Assert(s.fs.Source.Rename(absOldFilename, absNewFilename), qt.IsNil)
return s return s
} }
@ -355,8 +363,8 @@ func (s *IntegrationTestBuilder) initBuilder() error {
s.Assert(err, qt.IsNil) s.Assert(err, qt.IsNil)
} }
s.Assert(afs.MkdirAll(filepath.Dir(filename), 0777), qt.IsNil) s.Assert(afs.MkdirAll(filepath.Dir(filename), 0o777), qt.IsNil)
s.Assert(afero.WriteFile(afs, filename, data, 0666), qt.IsNil) s.Assert(afero.WriteFile(afs, filename, data, 0o666), qt.IsNil)
} }
configDir := "config" configDir := "config"
@ -402,7 +410,6 @@ func (s *IntegrationTestBuilder) initBuilder() error {
Environ: s.Cfg.Environ, Environ: s.Cfg.Environ,
}, },
) )
if err != nil { if err != nil {
initErr = err initErr = err
return return
@ -553,7 +560,7 @@ func (s *IntegrationTestBuilder) writeSource(filename, content string) {
func (s *IntegrationTestBuilder) writeToFs(fs afero.Fs, filename, content string) { func (s *IntegrationTestBuilder) writeToFs(fs afero.Fs, filename, content string) {
s.Helper() s.Helper()
if err := afero.WriteFile(fs, filepath.FromSlash(filename), []byte(content), 0755); err != nil { if err := afero.WriteFile(fs, filepath.FromSlash(filename), []byte(content), 0o755); err != nil {
s.Fatalf("Failed to write file: %s", err) s.Fatalf("Failed to write file: %s", err)
} }
} }