policy+tui: raise coverage above target (M1)
policy 66% -> 94.7%: TestLoad exercises the previously-untested Load path — empty/missing path fall back to Default, valid YAML parses onto the Default base (absent fields keep their default; overrides apply), malformed YAML and a non-regular file (directory) both error. Documents that a map override entry is replaced wholesale by yaml.v3 (Evaluate re-merges defaults, so it's harmless). tui 57% -> 87.8%: cover Init, the "o" open branches (URL vs none), "s" skip and prev/next boundary guards, non-key messages, enter-at-last-item, esc/ctrl+c quit, openURL's command, and Run's headless empty-list path. The residual is the TTY-only tea.Program.Run() launch, not unit-testable headless. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package policy
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -9,6 +11,50 @@ import (
|
||||
"incredigo/internal/discover"
|
||||
)
|
||||
|
||||
func TestLoad(t *testing.T) {
|
||||
// Empty path → built-in default.
|
||||
if p, err := Load(""); err != nil || p.Defaults.WarnAfter != 60 {
|
||||
t.Fatalf("Load(\"\") = %+v, %v; want default", p, err)
|
||||
}
|
||||
|
||||
// Missing file → default (not an error).
|
||||
miss := filepath.Join(t.TempDir(), "nope.yaml")
|
||||
if p, err := Load(miss); err != nil || p.Defaults.StaleAfter != 90 {
|
||||
t.Fatalf("Load(missing) = %+v, %v; want default", p, err)
|
||||
}
|
||||
|
||||
// Valid file → parsed onto the Default() base: a struct field absent from the
|
||||
// file keeps its default (stale_after stays 90), while an override is applied.
|
||||
f := filepath.Join(t.TempDir(), "policy.yaml")
|
||||
if err := os.WriteFile(f, []byte("defaults:\n warn_after: 10d\noverrides:\n aws_key:\n warn_after: 5d\n stale_after: 45d\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p, err := Load(f)
|
||||
if err != nil {
|
||||
t.Fatalf("Load(valid): %v", err)
|
||||
}
|
||||
if p.Defaults.WarnAfter != 10 || p.Defaults.StaleAfter != 90 {
|
||||
t.Errorf("defaults = %+v, want {10 90} (stale kept from Default)", p.Defaults)
|
||||
}
|
||||
if got := p.Overrides[discover.KindAWSKey]; got.WarnAfter != 5 || got.StaleAfter != 45 {
|
||||
t.Errorf("aws_key override = %+v, want {5 45}", got)
|
||||
}
|
||||
|
||||
// Malformed YAML → error.
|
||||
bad := filepath.Join(t.TempDir(), "bad.yaml")
|
||||
if err := os.WriteFile(bad, []byte("defaults: [this is not a mapping"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := Load(bad); err == nil {
|
||||
t.Error("Load(malformed) returned no error")
|
||||
}
|
||||
|
||||
// Unreadable (a directory, not a regular file) → a non-IsNotExist error.
|
||||
if _, err := Load(t.TempDir()); err == nil {
|
||||
t.Error("Load(directory) returned no error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDaysUnmarshal(t *testing.T) {
|
||||
ok := map[string]Days{
|
||||
"60": 60,
|
||||
|
||||
Reference in New Issue
Block a user