Skip to content

Commit 20b56cc

Browse files
committed
cover failure paths, CLI, and builders; remove dead code
1 parent 18482ea commit 20b56cc

3 files changed

Lines changed: 110 additions & 0 deletions

File tree

engine/errorpaths_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package engine
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
)
8+
9+
func TestNewEngineFromTOMLErrors(t *testing.T) {
10+
t.Run("missing file", func(t *testing.T) {
11+
if _, err := NewEngineFromTOML(filepath.Join(t.TempDir(), "nope.toml")); err == nil {
12+
t.Fatal("expected error for missing TOML file")
13+
}
14+
})
15+
t.Run("malformed", func(t *testing.T) {
16+
path := filepath.Join(t.TempDir(), "bad.toml")
17+
if err := os.WriteFile(path, []byte("[unterminated"), 0o644); err != nil {
18+
t.Fatal(err)
19+
}
20+
if _, err := NewEngineFromTOML(path); err == nil {
21+
t.Fatal("expected error for malformed TOML")
22+
}
23+
})
24+
}
25+
26+
func TestNewEngineFromYAMLErrors(t *testing.T) {
27+
t.Run("missing file", func(t *testing.T) {
28+
if _, err := NewEngineFromYAML(filepath.Join(t.TempDir(), "nope.yaml")); err == nil {
29+
t.Fatal("expected error for missing YAML file")
30+
}
31+
})
32+
t.Run("malformed", func(t *testing.T) {
33+
path := filepath.Join(t.TempDir(), "bad.yaml")
34+
if err := os.WriteFile(path, []byte("a: b: c"), 0o644); err != nil {
35+
t.Fatal(err)
36+
}
37+
if _, err := NewEngineFromYAML(path); err == nil {
38+
t.Fatal("expected error for malformed YAML")
39+
}
40+
})
41+
}
42+
43+
func TestNewEngineFromConfigRequiresRootPath(t *testing.T) {
44+
if _, err := NewEngineFromConfig(Config{ExecStruct: []Execute{{Cmd: "x", Type: Primary}}}); err == nil {
45+
t.Fatal("expected error when RootPath is empty")
46+
}
47+
}
48+
49+
func TestStringToConfigInvalid(t *testing.T) {
50+
e := &Engine{}
51+
if err := e.StringtoConfigTOML("= 1"); err == nil {
52+
t.Error("expected error for invalid TOML string")
53+
}
54+
if err := e.StringtoConfigYAML("a: b: c"); err == nil {
55+
t.Error("expected error for invalid YAML string")
56+
}
57+
}

engine/logger_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,27 @@ func TestDisableEnableLogs(t *testing.T) {
6363
}
6464
}
6565

66+
// TestSwitchHandlerWithAttrsAndGroup ensures the level/enabled switch is
67+
// preserved through derived handlers created by With and WithGroup.
68+
func TestSwitchHandlerWithAttrsAndGroup(t *testing.T) {
69+
var buf bytes.Buffer
70+
d := newCapture("info", &buf)
71+
72+
derived := d.logger.With("component", "engine").WithGroup("scope")
73+
derived.Info("ready", "id", 1)
74+
if !strings.Contains(buf.String(), "component=engine") || !strings.Contains(buf.String(), "scope.id=1") {
75+
t.Errorf("attrs/group not propagated: %q", buf.String())
76+
}
77+
78+
// The shared enabled switch must still gate derived handlers.
79+
buf.Reset()
80+
d.Disable()
81+
derived.Error("suppressed")
82+
if buf.Len() != 0 {
83+
t.Errorf("derived handler ignored Disable: %q", buf.String())
84+
}
85+
}
86+
6687
func TestMuteLevelDisablesOutput(t *testing.T) {
6788
var buf bytes.Buffer
6889
d := newCapture("mute", &buf)
@@ -78,4 +99,12 @@ func TestMuteLevelDisablesOutput(t *testing.T) {
7899
if !strings.Contains(buf.String(), "recovered") {
79100
t.Errorf("output not restored after leaving mute: %q", buf.String())
80101
}
102+
103+
// SetLevel("mute") at runtime must also disable output.
104+
buf.Reset()
105+
d.SetLevel("mute")
106+
d.logger.Error("muted-again")
107+
if buf.Len() != 0 {
108+
t.Errorf("SetLevel(\"mute\") did not disable output: %q", buf.String())
109+
}
81110
}

process/process_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package process
55
import (
66
"context"
77
"os"
8+
"os/exec"
89
"path/filepath"
910
"syscall"
1011
"testing"
@@ -124,6 +125,29 @@ func TestShellFeaturesAreSupported(t *testing.T) {
124125
}
125126
}
126127

128+
func TestKillProcessTreeNilSafe(t *testing.T) {
129+
if err := killProcessTree(nil); err != nil {
130+
t.Errorf("killProcessTree(nil) = %v, want nil", err)
131+
}
132+
if err := killProcessTree(&exec.Cmd{}); err != nil {
133+
t.Errorf("killProcessTree on unstarted cmd = %v, want nil", err)
134+
}
135+
}
136+
137+
func TestSetRootDirectoryDefaultsToCwd(t *testing.T) {
138+
pm := NewProcessManager()
139+
if err := pm.SetRootDirectory(""); err != nil {
140+
t.Fatalf("SetRootDirectory(\"\"): %v", err)
141+
}
142+
wd, err := os.Getwd()
143+
if err != nil {
144+
t.Fatal(err)
145+
}
146+
if pm.RootDir != wd {
147+
t.Errorf("RootDir = %q, want cwd %q", pm.RootDir, wd)
148+
}
149+
}
150+
127151
func TestStartWithNoProcessesErrors(t *testing.T) {
128152
pm := NewProcessManager()
129153
if err := pm.Start(context.Background()); err == nil {

0 commit comments

Comments
 (0)