buildr/modules/test_ctx.go

68 lines
1.4 KiB
Go
Raw Normal View History

package modules
import (
"context"
"io"
"log/slog"
"testing"
"code.icb4dc0.de/buildr/buildr/modules/state"
)
var _ ExecutionContext = (*TestExecutionContext)(nil)
type TestExecutionContext struct {
context.Context
2023-06-22 16:06:56 +00:00
TB testing.TB
StateStore state.Store
TestWorkingDir string
OutputDirectory string
}
func (t TestExecutionContext) Name() string {
return t.TB.Name()
}
func (t TestExecutionContext) WorkingDir() string {
return t.TestWorkingDir
}
func (t TestExecutionContext) OutDir() string {
return t.OutputDirectory
}
func (t TestExecutionContext) BinariesDir() string {
return ""
}
func (t TestExecutionContext) StdOut() io.Writer {
return testWriter{TB: t.TB}
}
func (t TestExecutionContext) StdErr() io.Writer {
return testWriter{TB: t.TB}
}
func (t TestExecutionContext) Logger() *slog.Logger {
return slog.New(slog.NewTextHandler(testWriter{TB: t.TB}, nil))
}
func (t TestExecutionContext) GetState(ctx context.Context, key string) ([]byte, state.Metadata, error) {
return t.StateStore.Get(ctx, state.KeyOfStrings(key))
}
func (t TestExecutionContext) SetState(ctx context.Context, key string, value []byte) error {
return t.StateStore.Set(ctx, state.KeyOfStrings(key), value)
}
var _ io.Writer = (*testWriter)(nil)
type testWriter struct {
testing.TB
}
func (t testWriter) Write(p []byte) (n int, err error) {
t.Log(string(p))
return len(p), nil
}