-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdebug_file_test.go
More file actions
262 lines (206 loc) · 6.45 KB
/
debug_file_test.go
File metadata and controls
262 lines (206 loc) · 6.45 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//nolint:paralleltest // Tests modify package-level session log state, cannot run in parallel
package pn532
import (
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// cleanupSessionLog ensures session log state is clean after tests.
// Must be called in test cleanup to avoid state leakage between tests.
func cleanupSessionLog(t *testing.T) {
t.Helper()
if sessionLogFile != nil {
_ = sessionLogFile.Close()
}
sessionLogFile = nil
sessionLogPath = ""
sessionLogWriter = nil
}
func TestInitSessionLog_CreatesFile(t *testing.T) {
// Save current working directory
origDir, err := os.Getwd()
require.NoError(t, err)
// Create temp directory and change to it
tempDir := t.TempDir()
require.NoError(t, os.Chdir(tempDir))
t.Cleanup(func() {
cleanupSessionLog(t)
_ = os.Chdir(origDir)
})
path, err := InitSessionLog()
require.NoError(t, err)
assert.NotEmpty(t, path)
// Verify file exists
_, err = os.Stat(path)
require.NoError(t, err, "Log file should exist")
// Verify filename format: pn532_YYYYMMDD_HHMMSS.log
matched, err := regexp.MatchString(`^pn532_\d{8}_\d{6}\.log$`, path)
require.NoError(t, err)
assert.True(t, matched, "Filename should match pn532_YYYYMMDD_HHMMSS.log pattern, got: %s", path)
}
func TestInitSessionLog_WritesHeader(t *testing.T) {
origDir, err := os.Getwd()
require.NoError(t, err)
tempDir := t.TempDir()
require.NoError(t, os.Chdir(tempDir))
t.Cleanup(func() {
cleanupSessionLog(t)
_ = os.Chdir(origDir)
})
path, err := InitSessionLog()
require.NoError(t, err)
// Close to flush and read the file
require.NoError(t, CloseSessionLog())
content, err := os.ReadFile(path) //nolint:gosec // path is from InitSessionLog
require.NoError(t, err)
contentStr := string(content)
// Verify header content
assert.Contains(t, contentStr, "=== PN532 Debug Session Log ===")
assert.Contains(t, contentStr, "Started:")
assert.Contains(t, contentStr, "PID:")
assert.Contains(t, contentStr, "OS:")
assert.Contains(t, contentStr, "Go Version:")
assert.Contains(t, contentStr, "Executable:")
assert.Contains(t, contentStr, "Command Line:")
}
func TestCloseSessionLog_WritesFooter(t *testing.T) {
origDir, err := os.Getwd()
require.NoError(t, err)
tempDir := t.TempDir()
require.NoError(t, os.Chdir(tempDir))
t.Cleanup(func() {
cleanupSessionLog(t)
_ = os.Chdir(origDir)
})
path, err := InitSessionLog()
require.NoError(t, err)
err = CloseSessionLog()
require.NoError(t, err)
content, err := os.ReadFile(path) //nolint:gosec // path is from InitSessionLog
require.NoError(t, err)
contentStr := string(content)
// Verify footer content
assert.Contains(t, contentStr, "=== Session ended ===")
}
func TestCloseSessionLog_NilFile(t *testing.T) {
t.Cleanup(func() {
cleanupSessionLog(t)
})
// Ensure clean state
sessionLogFile = nil
sessionLogPath = ""
sessionLogWriter = nil
// Should not error or panic when no file is open
err := CloseSessionLog()
assert.NoError(t, err)
}
func TestGetSessionLogPath_ReturnsCorrectPath(t *testing.T) {
origDir, err := os.Getwd()
require.NoError(t, err)
tempDir := t.TempDir()
require.NoError(t, os.Chdir(tempDir))
t.Cleanup(func() {
cleanupSessionLog(t)
_ = os.Chdir(origDir)
})
// Before init, should be empty
assert.Empty(t, GetSessionLogPath())
path, err := InitSessionLog()
require.NoError(t, err)
// After init, should return the path
assert.Equal(t, path, GetSessionLogPath())
require.NoError(t, CloseSessionLog())
// After close, should be empty again
assert.Empty(t, GetSessionLogPath())
}
func TestInitSessionLog_ErrorOnInvalidDirectory(t *testing.T) {
origDir, err := os.Getwd()
require.NoError(t, err)
// Change to a non-existent directory to trigger error
// We can't actually do this easily, so test the state cleanup instead
tempDir := t.TempDir()
require.NoError(t, os.Chdir(tempDir))
t.Cleanup(func() {
cleanupSessionLog(t)
_ = os.Chdir(origDir)
})
// First init should succeed
path1, err := InitSessionLog()
require.NoError(t, err)
require.NoError(t, CloseSessionLog())
// Second init should also succeed (clean state)
path2, err := InitSessionLog()
require.NoError(t, err)
require.NoError(t, CloseSessionLog())
// Paths should be different (different timestamps)
// But they should both be in the same directory
assert.Equal(t, filepath.Dir(path1), filepath.Dir(path2))
}
func TestMultipleInitCloseCycles(t *testing.T) {
origDir, err := os.Getwd()
require.NoError(t, err)
tempDir := t.TempDir()
require.NoError(t, os.Chdir(tempDir))
t.Cleanup(func() {
cleanupSessionLog(t)
_ = os.Chdir(origDir)
})
// Run multiple init/close cycles
paths := make([]string, 0, 3)
for i := range 3 {
path, err := InitSessionLog()
require.NoError(t, err, "Init cycle %d failed", i)
paths = append(paths, path)
// Verify file is accessible
_, err = os.Stat(path)
require.NoError(t, err, "File should exist after init")
// Write something to verify the log is working
Debugf("Test message %d", i)
err = CloseSessionLog()
require.NoError(t, err, "Close cycle %d failed", i)
// Verify state is clean
assert.Empty(t, GetSessionLogPath())
assert.Nil(t, sessionLogFile)
assert.Nil(t, sessionLogWriter)
}
// Verify all files exist and have content
for i, path := range paths {
content, err := os.ReadFile(path) //nolint:gosec // path is from InitSessionLog
require.NoError(t, err, "Failed to read log file %d", i)
assert.Contains(t, string(content), "Test message")
}
}
func TestInitSessionLog_StateInitialization(t *testing.T) {
origDir, err := os.Getwd()
require.NoError(t, err)
tempDir := t.TempDir()
require.NoError(t, os.Chdir(tempDir))
t.Cleanup(func() {
cleanupSessionLog(t)
_ = os.Chdir(origDir)
})
_, err = InitSessionLog()
require.NoError(t, err)
// Verify all state variables are set
assert.NotNil(t, sessionLogFile)
assert.NotEmpty(t, sessionLogPath)
assert.NotNil(t, sessionLogWriter)
require.NoError(t, CloseSessionLog())
}
func TestWriteSessionHeader_ContentFormat(t *testing.T) {
var buf strings.Builder
writeSessionHeader(&buf)
content := buf.String()
// Verify header markers
assert.True(t, strings.HasPrefix(content, "=== PN532 Debug Session Log ==="))
assert.Contains(t, content, "================================")
// Verify all required fields are present
assert.Contains(t, content, "Started:")
assert.Contains(t, content, "PID:")
assert.Contains(t, content, "OS:")
assert.Contains(t, content, "Go Version:")
}