-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathconfig_export_data_test.go
More file actions
172 lines (128 loc) · 5.41 KB
/
Copy pathconfig_export_data_test.go
File metadata and controls
172 lines (128 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package cli
import (
"encoding/json"
"os"
"testing"
"github.com/jandedobbeleer/oh-my-posh/src/cache"
"github.com/jandedobbeleer/oh-my-posh/src/config"
"github.com/jandedobbeleer/oh-my-posh/src/maps"
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
"github.com/jandedobbeleer/oh-my-posh/src/segments"
"github.com/jandedobbeleer/oh-my-posh/src/template"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Does not run Execute/Enabled(), so the caller controls Enabled and the writer's
// fields directly, keeping the test hermetic (no real environment probing).
func newRecordedSessionSegment(t *testing.T, alias string) *config.Segment {
t.Helper()
env := new(mock.Environment)
env.On("Getenv", "SSH_CONNECTION").Return("")
env.On("Getenv", "SSH_CLIENT").Return("")
segment := &config.Segment{Type: config.SESSION, Alias: alias}
require.NoError(t, segment.MapSegmentWithWriter(env))
segment.Enabled = true
return segment
}
func TestBuildDataDocument_EnvSectionDropsInternalKeysKeepsRest(t *testing.T) {
template.Cache = &cache.Template{
Segments: maps.NewConcurrent[any](),
PWD: "/home/jan",
UserName: "jan",
Var: maps.Simple[any]{"foo": "bar"},
}
cfg := &config.Config{}
doc, err := buildDataDocument(cfg)
require.NoError(t, err)
var parsed map[string]json.RawMessage
require.NoError(t, json.Unmarshal(doc, &parsed))
require.Contains(t, parsed, "env")
var env map[string]json.RawMessage
require.NoError(t, json.Unmarshal(parsed["env"], &env))
assert.NotContains(t, env, "SegmentsCache", "internal cache plumbing must not be recorded")
assert.NotContains(t, env, "Var", "config vars are already covered by the config's own var section")
assert.Contains(t, env, "PWD")
assert.Contains(t, env, "UserName")
}
// unwrapRecorded decodes a segment's raw message as the RecordedSegment
// envelope {"enabled":...,"data":...} that buildDataDocument now always writes,
// and returns its enabled flag plus the inner writer data unmarshaled into out.
func unwrapRecorded(t *testing.T, raw json.RawMessage, out any) bool {
t.Helper()
var envelope config.RecordedSegment
require.NoError(t, json.Unmarshal(raw, &envelope))
require.NoError(t, json.Unmarshal(envelope.Data, out))
return envelope.Enabled
}
func TestBuildDataDocument_RecordsEveryConfiguredSegmentWithItsEnabledState(t *testing.T) {
template.Cache = &cache.Template{Segments: maps.NewConcurrent[any]()}
enabled := newRecordedSessionSegment(t, "")
enabled.Writer().(*segments.Session).SSHSession = true
disabled := newRecordedSessionSegment(t, "disabled-alias")
disabled.Enabled = false
noWriter := &config.Segment{Type: config.TEXT, Alias: "no-writer", Enabled: true}
cfg := &config.Config{
Blocks: []*config.Block{
{Segments: []*config.Segment{enabled, disabled, noWriter}},
},
}
doc, err := buildDataDocument(cfg)
require.NoError(t, err)
var parsed map[string]json.RawMessage
require.NoError(t, json.Unmarshal(doc, &parsed))
require.Contains(t, parsed, "version", "the recorder must stamp a version marker so replay is hermetic")
var version int
require.NoError(t, json.Unmarshal(parsed["version"], &version))
assert.Equal(t, config.DataVersion, version)
var segs map[string]json.RawMessage
require.NoError(t, json.Unmarshal(parsed["segments"], &segs))
assert.Contains(t, segs, "session")
assert.Contains(t, segs, "disabled-alias", "a segment that wasn't enabled at record time must still be recorded, "+
"so replay can suppress it without a live probe instead of falling through to one")
assert.NotContains(t, segs, "no-writer", "a segment with a nil writer must be skipped")
var session map[string]json.RawMessage
enabled1 := unwrapRecorded(t, segs["session"], &session)
assert.True(t, enabled1)
assert.JSONEq(t, `true`, string(session["SSHSession"]))
var disabledSession map[string]json.RawMessage
enabled2 := unwrapRecorded(t, segs["disabled-alias"], &disabledSession)
assert.False(t, enabled2, "the recorded enabled flag must reflect the segment's own state, not force true")
}
func TestBuildDataDocument_CollisionWarnsAndLastWriterWins(t *testing.T) {
template.Cache = &cache.Template{Segments: maps.NewConcurrent[any]()}
first := newRecordedSessionSegment(t, "")
first.Writer().(*segments.Session).SSHSession = false
second := newRecordedSessionSegment(t, "")
second.Writer().(*segments.Session).SSHSession = true
cfg := &config.Config{
Blocks: []*config.Block{
{Segments: []*config.Segment{first, second}},
},
}
stderrR, stderrW, err := os.Pipe()
require.NoError(t, err)
originalStderr := os.Stderr
os.Stderr = stderrW
doc, docErr := buildDataDocument(cfg)
os.Stderr = originalStderr
require.NoError(t, stderrW.Close())
var warning []byte
buf := make([]byte, 4096)
n, _ := stderrR.Read(buf)
warning = buf[:n]
_ = stderrR.Close()
require.NoError(t, docErr)
assert.Contains(t, string(warning), "session", "collision warning should name the colliding key")
var parsed map[string]json.RawMessage
require.NoError(t, json.Unmarshal(doc, &parsed))
var segs map[string]json.RawMessage
require.NoError(t, json.Unmarshal(parsed["segments"], &segs))
var session map[string]json.RawMessage
unwrapRecorded(t, segs["session"], &session)
assert.JSONEq(t, `true`, string(session["SSHSession"]), "the last segment sharing the key should win")
}
func TestDataCmd_Flags(t *testing.T) {
flag := dataCmd.Flags().Lookup("output")
require.NotNil(t, flag)
assert.Equal(t, "o", flag.Shorthand)
}