Files
narratio/internal/app/test_environment_test.go

79 lines
2.1 KiB
Go

package app
import (
"context"
"os"
"path/filepath"
"testing"
"gitea.maximumdirect.net/eric/narratio/internal/config"
configkit "go.maximumdirect.net/configkit"
)
func TestMain(m *testing.M) {
dir, err := os.MkdirTemp("", "narratio-app-test-*")
if err != nil {
panic(err)
}
conventionalSecretsDirectory = filepath.Join(dir, "secrets")
code := m.Run()
_ = os.RemoveAll(dir)
os.Exit(code)
}
// restoreEnvAfterTest clears names for a test and restores both their prior
// values and their set/unset state during cleanup.
func restoreEnvAfterTest(t *testing.T, names ...string) {
t.Helper()
originals := make(map[string]string, len(names))
present := make(map[string]bool, len(names))
for _, name := range names {
value, ok := os.LookupEnv(name)
originals[name] = value
present[name] = ok
if err := os.Unsetenv(name); err != nil {
t.Fatalf("Unsetenv(%q): %v", name, err)
}
}
t.Cleanup(func() {
for _, name := range names {
if present[name] {
_ = os.Setenv(name, originals[name])
} else {
_ = os.Unsetenv(name)
}
}
})
}
func testSecretReference(name string) *configkit.Secret {
reference := configkit.SecretEnv(name)
return &reference
}
func mustWriteSecretFile(t *testing.T, path, value string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(value), 0o600); err != nil {
t.Fatal(err)
}
}
func commandDependenciesForPipelineCandidates(candidates ...string) commandDependencies {
search := configkit.SearchPaths(candidates...)
return commandDependencies{pipelineSearch: &search}
}
func guardedMissingPipelineDiscovery(t *testing.T) (commandDependencies, string, pipelineExecutor) {
t.Helper()
candidate := filepath.Join(t.TempDir(), "missing-pipeline.yml")
deps := commandDependenciesForPipelineCandidates(candidate)
executor := func(context.Context, *config.Config, BoundedPlan, RunOptions) (*RunSummary, error) {
t.Fatal("pipeline execution continued after pipeline discovery failed")
return nil, nil
}
return deps, candidate, executor
}