1package daemon
2
3import (
4 "testing"
5
6 "github.com/stretchr/testify/require"
7)
8
9func TestProcessInspectorOptionsComposition(t *testing.T) {
10 cwd := func(int) (string, error) { return "/override", nil }
11 base := &processInspectorFake{
12 cwd: func(int) (string, error) { return "/base", nil },
13 comm: func(int) (string, error) { return "vim", nil },
14 }
15
16 tests := []struct {
17 name string
18 opts []Option
19 wantDetails bool
20 wantCwd string
21 wantComm string
22 wantCommErr error
23 }{
24 {name: "none", wantDetails: false},
25 {
26 name: "cwd reader only",
27 opts: []Option{WithCwdReader(cwd)},
28 wantDetails: false,
29 wantCwd: "/override",
30 wantCommErr: errProcessInspectionUnavailable,
31 },
32 {
33 name: "inspector only",
34 opts: []Option{WithProcessInspector(base)},
35 wantDetails: true,
36 wantCwd: "/base",
37 wantComm: "vim",
38 },
39 {
40 name: "inspector then cwd reader keeps base details",
41 opts: []Option{WithProcessInspector(base), WithCwdReader(cwd)},
42 wantDetails: true,
43 wantCwd: "/override",
44 wantComm: "vim",
45 },
46 {
47 name: "cwd reader then inspector replaces override",
48 opts: []Option{WithCwdReader(cwd), WithProcessInspector(base)},
49 wantDetails: true,
50 wantCwd: "/base",
51 wantComm: "vim",
52 },
53 {
54 name: "repeated cwd readers do not stack",
55 opts: []Option{WithProcessInspector(base), WithCwdReader(cwd), WithCwdReader(cwd)},
56 wantDetails: true,
57 wantCwd: "/override",
58 wantComm: "vim",
59 },
60 }
61 for _, tt := range tests {
62 t.Run(tt.name, func(t *testing.T) {
63 d := &Daemon{}
64 for _, opt := range tt.opts {
65 opt(d)
66 }
67 require.Equal(t, tt.wantDetails, d.hasProcessDetails())
68 if d.proc == nil {
69 return
70 }
71 got, err := d.proc.Cwd(1)
72 require.NoError(t, err)
73 require.Equal(t, tt.wantCwd, got)
74 comm, err := d.proc.Comm(1)
75 require.ErrorIs(t, err, tt.wantCommErr)
76 require.Equal(t, tt.wantComm, comm)
77 if o, ok := d.proc.(cwdOverrideInspector); ok {
78 _, nested := o.base.(cwdOverrideInspector)
79 require.False(t, nested)
80 }
81 })
82 }
83}