-
Notifications
You must be signed in to change notification settings - Fork 3
/
logging_unexported_test.go
756 lines (600 loc) · 22.7 KB
/
logging_unexported_test.go
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
// Copyright 2024 Adam Chalkley
//
// https://github.com/atc0005/go-nagios
//
// Licensed under the MIT License. See LICENSE file in the project root for
// full license information.
// Package nagios provides test coverage for unexported package functionality.
//
//nolint:dupl,gocognit // ignore "lines are duplicate of" and function complexity
package nagios
import (
"bytes"
"io"
"os"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestPlugin_SetDebugLoggingOutputTarget_IsValidWithValidInput(t *testing.T) {
t.Parallel()
plugin := NewPlugin()
// Assert that log output sink is still unset
if plugin.logOutputSink != nil {
t.Fatal("ERROR: plugin logOutputSink is not at the expected default unset value.")
} else {
t.Log("OK: plugin logOutputSink is at the expected default unset value.")
}
// Assert that logger is still unset
if plugin.logger != nil {
t.Fatal("ERROR: plugin logger is not at the expected default unset value.")
} else {
t.Log("OK: plugin logger is at the expected default unset value.")
}
var outputBuffer strings.Builder
plugin.SetDebugLoggingOutputTarget(&outputBuffer)
// All debug logging options were previously enabled after setting a debug
// logging output target. This behavior has changed and now debug logging
// must be explicitly enabled.
//
// assertAllDebugLoggingOptionsAreEnabled(plugin, t)
// Assert that plugin.outputSink is set as expected.
switch {
case plugin.logOutputSink == nil:
t.Fatal("ERROR: plugin logOutputSink is unset instead of the given custom value.")
case plugin.logOutputSink == defaultPluginDebugLoggingOutputTarget():
t.Fatal("ERROR: plugin logOutputSink is set to the default/fallback value instead of the expected custom value.")
case plugin.logOutputSink != &outputBuffer:
t.Error("ERROR: logOutputSink is not set to custom output target")
// t.Logf("plugin.logOutputSink address: %p", plugin.logOutputSink)
// t.Logf("&outputBuffer address: %p", &outputBuffer)
d := cmp.Diff(&outputBuffer, plugin.logOutputSink)
t.Fatalf("(-want, +got)\n:%s", d)
default:
t.Log("OK: plugin logOutputSink is at the expected custom value.")
}
assertLoggerIsConfiguredProperlyAfterSettingDebugLoggingOutputTarget(plugin, t)
}
func TestPlugin_SetDebugLoggingOutputTarget_CorrectlySetsFallbackLoggingTargetWithInvalidInput(t *testing.T) {
t.Parallel()
plugin := NewPlugin()
// By calling this method we implicitly enable debug logging.
//
// By setting an invalid output target a log message is emitted to the
// default debug log output target.
plugin.SetDebugLoggingOutputTarget(nil)
// Assert that plugin.outputSink is set as expected.
want := defaultPluginDebugLoggingOutputTarget()
got := plugin.logOutputSink
switch {
case got == nil:
t.Error("ERROR: plugin debug log output target is unset instead of the default/fallback value.")
case got != want:
t.Error("ERROR: plugin debug log output target is not set to the default/fallback value.")
d := cmp.Diff(want, got)
t.Fatalf("(-want, +got)\n:%s", d)
default:
t.Log("OK: plugin debug log output target is at the expected default/fallback value.")
}
}
func TestPlugin_setupLogger_CorrectlySetsDefaultLoggerTargetWhenDebugLogOutputSinkIsUnset(t *testing.T) {
t.Parallel()
plugin := NewPlugin()
switch {
case plugin.logger != nil:
t.Fatal("ERROR: plugin logger is not at the expected default unset value.")
default:
t.Log("OK: plugin logger is at the expected default unset value.")
}
plugin.setupLogger()
switch {
case plugin.logger == nil:
t.Fatal("ERROR: plugin logger is unset instead of being configured for use.")
default:
t.Log("OK: plugin logger is set as expected.")
}
loggerTarget := plugin.logger.Writer()
switch {
case loggerTarget == nil:
t.Fatal("ERROR: plugin logger target is unset instead of being configured for use.")
case loggerTarget != defaultPluginDebugLoggerTarget():
t.Fatal("ERROR: plugin logger target is not set to use default logger target as expected.")
default:
t.Logf("OK: plugin logger target is set to default logger target ('%#v') as expected.", defaultPluginDebugLoggerTarget())
}
}
func TestPlugin_setupLogger_CorrectlySetsLoggerTargetWhenLogOutputSinkIsSet(t *testing.T) {
t.Parallel()
plugin := NewPlugin()
// Configure log output sink to a custom target.
var outputBuffer strings.Builder
plugin.logOutputSink = &outputBuffer
switch {
case plugin.logger != nil:
t.Fatal("ERROR: plugin logger is not at the expected default unset value.")
default:
t.Log("OK: plugin logger is at the expected default unset value.")
}
plugin.setupLogger()
switch {
case plugin.logger == nil:
t.Fatal("ERROR: plugin logger is unset instead of being configured for use.")
default:
t.Log("OK: plugin logger is set as expected.")
}
loggerTarget := plugin.logger.Writer()
switch {
case loggerTarget == nil:
t.Fatal("ERROR: plugin logger target is unset instead of being configured for use.")
case loggerTarget != plugin.logOutputSink:
t.Fatal("ERROR: plugin logger target is not set to custom output sink as expected.")
default:
t.Log("OK: plugin logger target is set as expected.")
}
}
func TestPlugin_DebugLoggingEnableAll_CorrectlyConfiguresLogTargetAndLoggerWithFallbackValues(t *testing.T) {
t.Parallel()
plugin := NewPlugin()
// Assert that log output sink is still unset
if plugin.logOutputSink != nil {
t.Fatal("ERROR: plugin logOutputSink is not at the expected default unset value.")
} else {
t.Log("OK: plugin logOutputSink is at the expected default unset value.")
}
// Assert that logger is still unset
if plugin.logger != nil {
t.Fatal("ERROR: plugin logger is not at the expected default unset value.")
} else {
t.Log("OK: plugin logger is at the expected default unset value.")
}
// Expected results of calling this function:
//
// - the fallback debug log target is set
// - the logger is setup
plugin.DebugLoggingEnableAll()
switch {
case plugin.logger == nil:
t.Fatal("ERROR: plugin logger is unset instead of being configured for use.")
default:
t.Log("OK: plugin logger is set as expected.")
}
loggerTarget := plugin.logger.Writer()
switch {
case loggerTarget == nil:
t.Fatal("ERROR: plugin logger target is unset instead of being configured for use.")
case loggerTarget != plugin.logOutputSink:
t.Fatal("ERROR: plugin logger target is not set to match debug log output target as expected.")
default:
t.Log("OK: plugin logger target is set as expected.")
}
}
func TestPlugin_DebugLoggingEnableAll_CorrectlyEnablesAllDebugLoggingOptions(t *testing.T) {
t.Parallel()
plugin := NewPlugin()
plugin.DebugLoggingEnableAll()
assertAllDebugLoggingOptionsAreEnabled(plugin, t)
}
func TestPlugin_DebugLoggingDisableAll_CorrectlyLeavesLogTargetAndLoggerUnmodified(t *testing.T) {
t.Parallel()
plugin := NewPlugin()
// Assert that log output sink is still unset
if plugin.logOutputSink != nil {
t.Fatal("ERROR: plugin logOutputSink is not at the expected default unset value.")
} else {
t.Log("OK: plugin logOutputSink is at the expected default unset value.")
}
// Configure log output sink to a custom target.
var outputBuffer strings.Builder
plugin.logOutputSink = &outputBuffer
switch {
case plugin.logger != nil:
t.Fatal("ERROR: plugin logger is not at the expected default unset value.")
default:
t.Log("OK: plugin logger is at the expected default unset value.")
}
plugin.setupLogger()
switch {
case plugin.logger == nil:
t.Fatal("ERROR: plugin logger is unset instead of being configured for use.")
default:
t.Log("OK: plugin logger is set as expected.")
}
pluginLoggerBeforeDisablingLogging := plugin.logger
pluginLogTargetBeforeDisablingLogging := plugin.logOutputSink
plugin.DebugLoggingDisableAll()
// Assert that the debug log target remains untouched.
switch {
case plugin.logOutputSink != pluginLogTargetBeforeDisablingLogging:
t.Errorf("ERROR: plugin debug log target is not set to same value before logging was disabled.")
d := cmp.Diff(
plugin.logOutputSink,
pluginLogTargetBeforeDisablingLogging,
// https://stackoverflow.com/questions/73476661/cmp-equal-gives-panic-message-cannot-handle-unexported-field-at
cmp.AllowUnexported(strings.Builder{}),
)
t.Errorf("(-want, +got)\n:%s", d)
default:
t.Log("OK: plugin debug log target is set to same value before logging was disabled.")
}
// Assert that the logger remains untouched.
switch {
case plugin.logger != pluginLoggerBeforeDisablingLogging:
t.Fatal("ERROR: plugin logger is not set to same value before logging was disabled.")
default:
t.Log("OK: plugin logger is set to same value before logging was disabled.")
}
}
func TestPlugin_DebugLoggingDisableAll_CorrectlyDisablesAllDebugLoggingOptions(t *testing.T) {
t.Parallel()
plugin := NewPlugin()
plugin.DebugLoggingDisableAll()
assertAllDebugLoggingOptionsAreDisabled(plugin, t)
}
func TestPlugin_DebugLoggingEnableActions_CorrectlyConfiguresLogTargetAndLoggerWithFallbackValues(t *testing.T) {
t.Parallel()
plugin := NewPlugin()
// Assert that log output sink is still unset
if plugin.logOutputSink != nil {
t.Fatal("ERROR: plugin logOutputSink is not at the expected default unset value.")
} else {
t.Log("OK: plugin logOutputSink is at the expected default unset value.")
}
// Assert that logger is still unset
if plugin.logger != nil {
t.Fatal("ERROR: plugin logger is not at the expected default unset value.")
} else {
t.Log("OK: plugin logger is at the expected default unset value.")
}
// Expected results of calling this function:
//
// - the fallback debug log target is set
// - the logger is setup
plugin.DebugLoggingEnableActions()
switch {
case plugin.logger == nil:
t.Fatal("ERROR: plugin logger is unset instead of being configured for use.")
default:
t.Log("OK: plugin logger is set as expected.")
}
loggerTarget := plugin.logger.Writer()
switch {
case loggerTarget == nil:
t.Fatal("ERROR: plugin logger target is unset instead of being configured for use.")
case loggerTarget != plugin.logOutputSink:
t.Fatal("ERROR: plugin logger target is not set to match debug log output target as expected.")
default:
t.Log("OK: plugin logger target is set as expected.")
}
}
func TestPlugin_DebugLoggingEnableActions_CorrectlyEnablesOnlyDebugLoggingActionsOption(t *testing.T) {
t.Parallel()
plugin := NewPlugin()
// Flip everything off to start with so we can selectively enable just the
// debug logging option we're interested in.
plugin.debugLogging = allDebugLoggingOptionsDisabled()
plugin.DebugLoggingEnableActions()
selectDebugLoggingOptionsEnabled := allDebugLoggingOptionsDisabled()
selectDebugLoggingOptionsEnabled.actions = true
if !cmp.Equal(
selectDebugLoggingOptionsEnabled,
// https://stackoverflow.com/questions/73476661/cmp-equal-gives-panic-message-cannot-handle-unexported-field-at
plugin.debugLogging, cmp.AllowUnexported(debugLoggingOptions{}),
) {
d := cmp.Diff(
selectDebugLoggingOptionsEnabled,
plugin.debugLogging,
// https://stackoverflow.com/questions/73476661/cmp-equal-gives-panic-message-cannot-handle-unexported-field-at
cmp.AllowUnexported(debugLoggingOptions{}),
)
t.Errorf("(-want, +got)\n:%s", d)
}
}
func TestPlugin_DebugLoggingDisableActions_CorrectlyDisablesOnlyDebugLoggingActionsOption(t *testing.T) {
t.Parallel()
plugin := NewPlugin()
// Flip everything on to start with so we can selectively disable specific
// debug logging options.
plugin.debugLogging = allDebugLoggingOptionsEnabled()
plugin.DebugLoggingDisableActions()
selectDebugLoggingOptionsDisabled := allDebugLoggingOptionsEnabled()
selectDebugLoggingOptionsDisabled.actions = false
if !cmp.Equal(
selectDebugLoggingOptionsDisabled,
// https://stackoverflow.com/questions/73476661/cmp-equal-gives-panic-message-cannot-handle-unexported-field-at
plugin.debugLogging, cmp.AllowUnexported(debugLoggingOptions{}),
) {
d := cmp.Diff(
selectDebugLoggingOptionsDisabled,
plugin.debugLogging,
// https://stackoverflow.com/questions/73476661/cmp-equal-gives-panic-message-cannot-handle-unexported-field-at
cmp.AllowUnexported(debugLoggingOptions{}),
)
t.Errorf("(-want, +got)\n:%s", d)
}
}
func TestPlugin_DebugLoggingEnablePluginOutputSize_CorrectlyConfiguresLogTargetAndLoggerWithFallbackValues(t *testing.T) {
t.Parallel()
plugin := NewPlugin()
// Assert that log output sink is still unset
if plugin.logOutputSink != nil {
t.Fatal("ERROR: plugin logOutputSink is not at the expected default unset value.")
} else {
t.Log("OK: plugin logOutputSink is at the expected default unset value.")
}
// Assert that logger is still unset
if plugin.logger != nil {
t.Fatal("ERROR: plugin logger is not at the expected default unset value.")
} else {
t.Log("OK: plugin logger is at the expected default unset value.")
}
// Expected results of calling this function:
//
// - the fallback debug log target is set
// - the logger is setup
plugin.DebugLoggingEnablePluginOutputSize()
switch {
case plugin.logger == nil:
t.Fatal("ERROR: plugin logger is unset instead of being configured for use.")
default:
t.Log("OK: plugin logger is set as expected.")
}
loggerTarget := plugin.logger.Writer()
switch {
case loggerTarget == nil:
t.Fatal("ERROR: plugin logger target is unset instead of being configured for use.")
case loggerTarget != plugin.logOutputSink:
t.Fatal("ERROR: plugin logger target is not set to match debug log output target as expected.")
default:
t.Log("OK: plugin logger target is set as expected.")
}
}
func TestPlugin_DebugLoggingEnablePluginOutputSize_CorrectlyEnablesOnlyDebugLoggingOutputSizeOption(t *testing.T) {
t.Parallel()
plugin := NewPlugin()
// Flip everything off to start with so we can selectively enable just the
// debug logging option we're interested in.
plugin.debugLogging = allDebugLoggingOptionsDisabled()
plugin.DebugLoggingEnablePluginOutputSize()
selectDebugLoggingOptionsEnabled := allDebugLoggingOptionsDisabled()
selectDebugLoggingOptionsEnabled.pluginOutputSize = true
if !cmp.Equal(
selectDebugLoggingOptionsEnabled,
// https://stackoverflow.com/questions/73476661/cmp-equal-gives-panic-message-cannot-handle-unexported-field-at
plugin.debugLogging, cmp.AllowUnexported(debugLoggingOptions{}),
) {
d := cmp.Diff(
selectDebugLoggingOptionsEnabled,
plugin.debugLogging,
// https://stackoverflow.com/questions/73476661/cmp-equal-gives-panic-message-cannot-handle-unexported-field-at
cmp.AllowUnexported(debugLoggingOptions{}),
)
t.Errorf("(-want, +got)\n:%s", d)
}
}
func TestPlugin_DebugLoggingDisablePluginOutputSize_CorrectlyDisablesOnlyDebugLoggingOutputSizeOption(t *testing.T) {
t.Parallel()
plugin := NewPlugin()
// Flip everything on to start with so we can selectively disable specific
// debug logging options.
plugin.debugLogging = allDebugLoggingOptionsEnabled()
plugin.DebugLoggingDisablePluginOutputSize()
selectDebugLoggingOptionsDisabled := allDebugLoggingOptionsEnabled()
selectDebugLoggingOptionsDisabled.pluginOutputSize = false
if !cmp.Equal(
selectDebugLoggingOptionsDisabled,
// https://stackoverflow.com/questions/73476661/cmp-equal-gives-panic-message-cannot-handle-unexported-field-at
plugin.debugLogging, cmp.AllowUnexported(debugLoggingOptions{}),
) {
d := cmp.Diff(
selectDebugLoggingOptionsDisabled,
plugin.debugLogging,
// https://stackoverflow.com/questions/73476661/cmp-equal-gives-panic-message-cannot-handle-unexported-field-at
cmp.AllowUnexported(debugLoggingOptions{}),
)
t.Errorf("(-want, +got)\n:%s", d)
}
}
func TestPlugin_log_CorrectlyProducesNoOutputWhenLoggerIsUnset(t *testing.T) {
// credit: Modified version of Google Gemini example code (via Search)
//
// prompt: "golang using os.Pipe() to change os.Stderr and os.Stdout for tests"
// Intentionally *not* running this test in parallel since we're going to
// monkey patch io.StdOut and io.StdErr briefly.
//
// t.Parallel()
var (
origStdErr = os.Stderr
origStdOut = os.Stdout
)
// Ensure that no matter what, we restore the original values to prevent
// affecting other tests.
t.Cleanup(func() {
os.Stderr = origStdErr
os.Stdout = origStdOut
})
plugin := NewPlugin()
switch {
case plugin.logOutputSink != nil:
t.Fatal("ERROR: plugin logOutputSink is not at the expected default unset value.")
default:
t.Log("OK: plugin logOutputSink is at the expected default unset value.")
}
switch {
case plugin.logger != nil:
t.Fatal("ERROR: plugin logger is not at the expected default unset value.")
default:
t.Log("OK: plugin logger is at the expected default unset value.")
}
// Create a new pipe for capturing standard output
r, w, stdOutOsPipeErr := os.Pipe()
if stdOutOsPipeErr != nil {
t.Fatal("Error creating pipe to emulate os.Stdout as part of test setup")
}
os.Stdout = w
// Create a new pipe for capturing standard error
rErr, wErr, stdErrOsPipeErr := os.Pipe()
if stdErrOsPipeErr != nil {
t.Fatal("Error creating pipe to emulate os.Stderr as part of test setup")
}
os.Stderr = wErr
// This shouldn't go anywhere.
plugin.log("Testing")
// Close the write ends of the pipes to signal that we're done writing
if err := w.Close(); err != nil {
t.Fatalf("Error closing stdout pipe: %v", err)
}
if err := wErr.Close(); err != nil {
t.Fatalf("Error closing stdout pipe: %v", err)
}
// Restore original stdout and stderr values.
os.Stderr = origStdErr
os.Stdout = origStdOut
// Read the output from the pipes
var stdOutBuffer, stdErrBuffer bytes.Buffer
var written int64
var ioCopyErr error
written, ioCopyErr = io.Copy(&stdOutBuffer, r)
switch {
case ioCopyErr != nil:
t.Fatalf("ERROR: Failed to copy stdout pipe content to stdout buffer for evaluation: %v", ioCopyErr)
case written != 0:
t.Errorf("ERROR: Copied %d bytes of unexpected content to stdout buffer", written)
default:
t.Log("OK: io.Copy operation on stdout pipe found no content but also encountered no errors")
}
written, ioCopyErr = io.Copy(&stdErrBuffer, rErr)
switch {
case ioCopyErr != nil:
t.Fatalf("ERROR: Failed to copy stderr pipe content to stderr buffer for evaluation: %v", ioCopyErr)
case written != 0:
t.Errorf("ERROR: Copied %d bytes of unexpected content to stderr buffer", written)
default:
t.Log("OK: io.Copy operation on stderr pipe found no content but also encountered no errors")
}
capturedStdOut := stdOutBuffer.String()
switch {
case capturedStdOut != "":
want := ""
got := capturedStdOut
d := cmp.Diff(want, got)
t.Fatalf("(-want, +got)\n:%s", d)
default:
t.Log("OK: No output logged to stdout as expected.")
}
capturedStdErr := stdErrBuffer.String()
switch {
case capturedStdErr != "":
want := ""
got := capturedStdErr
d := cmp.Diff(want, got)
t.Fatalf("(-want, +got)\n:%s", d)
default:
t.Log("OK: No output logged to stderr as expected.")
}
}
func TestPlugin_logAction_CorrectlyProducesNoOutputWhenDebugLoggingActionsOptionIsDisabled(t *testing.T) {
plugin := NewPlugin()
var outputBuffer strings.Builder
plugin.SetDebugLoggingOutputTarget(&outputBuffer)
plugin.debugLogging.actions = false
// This shouldn't go anywhere.
testMsg := "Test action entry"
plugin.logAction(testMsg)
capturedDebugLogOutput := outputBuffer.String()
switch {
case strings.Contains(capturedDebugLogOutput, testMsg):
want := removeEntry(capturedDebugLogOutput, testMsg, CheckOutputEOL)
got := capturedDebugLogOutput
d := cmp.Diff(want, got)
t.Fatalf("(-want, +got)\n:%s", d)
default:
t.Log("OK: No debug logging output captured as expected.")
}
}
func TestPlugin_logPluginOutputSize_CorrectlyProducesNoOutputWhenDebugLoggingOutputSizeOptionIsDisabled(t *testing.T) {
plugin := NewPlugin()
var outputBuffer strings.Builder
plugin.SetDebugLoggingOutputTarget(&outputBuffer)
plugin.debugLogging.pluginOutputSize = false
// This shouldn't go anywhere.
testMsg := "Test output size entry"
plugin.logPluginOutputSize(testMsg)
capturedDebugLogOutput := outputBuffer.String()
switch {
case strings.Contains(capturedDebugLogOutput, testMsg):
want := removeEntry(capturedDebugLogOutput, testMsg, CheckOutputEOL)
got := capturedDebugLogOutput
d := cmp.Diff(want, got)
t.Fatalf("(-want, +got)\n:%s", d)
default:
t.Log("OK: No debug logging output captured as expected.")
}
}
func assertLoggerIsConfiguredProperlyAfterSettingDebugLoggingOutputTarget(plugin *Plugin, t *testing.T) {
t.Helper()
// Assert that plugin.logger is set as expected.
switch {
case plugin.logger == nil:
t.Fatal("ERROR: plugin logger is unset instead of being configured for use.")
default:
t.Log("OK: plugin logger is set as expected.")
}
// Assert that plugin.logger prefix is set as expected.
actualLoggerPrefix := plugin.logger.Prefix()
switch {
case actualLoggerPrefix != logMsgPrefix:
t.Error("ERROR: plugin logger prefix not set to the expected value.")
d := cmp.Diff(logMsgPrefix, actualLoggerPrefix)
t.Fatalf("(-want, +got)\n:%s", d)
default:
t.Logf("OK: plugin logger prefix is at the expected value %s", actualLoggerPrefix)
}
// Assert that plugin.logger flags is set as expected.
actualLoggerFlags := plugin.logger.Flags()
switch {
case actualLoggerFlags != logFlags:
t.Error("ERROR: plugin logger flags are not set to the expected value.")
d := cmp.Diff(logFlags, actualLoggerFlags)
t.Fatalf("(-want, +got)\n:%s", d)
default:
t.Logf("OK: plugin logger flags is set to the expected value %d", actualLoggerFlags)
}
}
func assertAllDebugLoggingOptionsAreEnabled(plugin *Plugin, t *testing.T) {
t.Helper()
// Assert that debug logging is enabled by requiring that all fields are
// set.
allDebugLoggingOptionsEnabled := allDebugLoggingOptionsEnabled()
if !cmp.Equal(
allDebugLoggingOptionsEnabled,
// https://stackoverflow.com/questions/73476661/cmp-equal-gives-panic-message-cannot-handle-unexported-field-at
plugin.debugLogging, cmp.AllowUnexported(debugLoggingOptions{}),
) {
d := cmp.Diff(
allDebugLoggingOptionsEnabled,
plugin.debugLogging,
// https://stackoverflow.com/questions/73476661/cmp-equal-gives-panic-message-cannot-handle-unexported-field-at
cmp.AllowUnexported(debugLoggingOptions{}),
)
t.Errorf("(-want, +got)\n:%s", d)
}
}
func assertAllDebugLoggingOptionsAreDisabled(plugin *Plugin, t *testing.T) {
t.Helper()
// Assert that debug logging is enabled by requiring that all fields are
// set.
allDebugLoggingOptionsDisabled := allDebugLoggingOptionsDisabled()
if !cmp.Equal(
allDebugLoggingOptionsDisabled,
// https://stackoverflow.com/questions/73476661/cmp-equal-gives-panic-message-cannot-handle-unexported-field-at
plugin.debugLogging, cmp.AllowUnexported(debugLoggingOptions{}),
) {
d := cmp.Diff(
allDebugLoggingOptionsDisabled,
plugin.debugLogging,
// https://stackoverflow.com/questions/73476661/cmp-equal-gives-panic-message-cannot-handle-unexported-field-at
cmp.AllowUnexported(debugLoggingOptions{}),
)
t.Errorf("(-want, +got)\n:%s", d)
}
}