package app import ( "context" "os" "path/filepath" "testing" configkit "gitea.maximumdirect.net/eric/ConfigKit" "gitea.maximumdirect.net/eric/narratio/internal/config" ) // 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 { ownedCandidates := append([]string(nil), candidates...) return commandDependencies{resolvePipelineConfigPath: func(flagValue string) (string, error) { return resolvePipelineConfigPathWithCandidates(flagValue, ownedCandidates) }} } func guardedMissingPipelineDiscovery(t *testing.T) (commandDependencies, string, pipelineExecutor) { t.Helper() candidate := filepath.Join(t.TempDir(), "missing-pipeline.yml") deps := commandDependenciesForPipelineCandidates(candidate) deps.loadSecretsDir = func(string) (*configkit.SecretsReport, error) { t.Fatal("secret bootstrap continued after pipeline discovery failed") return nil, nil } deps.loadPipelineConfig = func(string, config.PipelineLoadOptions) (*config.PipelineConfig, error) { t.Fatal("typed pipeline loading continued after pipeline discovery failed") return nil, nil } 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 }