@@ -14,37 +14,87 @@ const (
1414 RECEIVED_INPUT = "Received input"
1515)
1616
17- // setupZapLogger is assumed to be defined in the same module.
18- // If it is in a different package, import that package accordingly.
19-
20- func TestSetupZapLoggerVerbose (t * testing.T ) {
21- // Set up the logger in verbose mode.
22- setupZapLogger (true )
23-
24- // The global logger (zap.L()) should allow debug logs.
25- // Check that debug logs are enabled.
26- if ! zap .L ().Core ().Enabled (zap .DebugLevel ) {
27- t .Error ("Expected debug level to be enabled in verbose mode but it is not" )
28- }
29-
30- // Optionally, check that Info level logs are enabled.
31- if ! zap .L ().Core ().Enabled (zap .InfoLevel ) {
32- t .Error ("Expected info level to be enabled in verbose mode but it is not" )
33- }
34- }
35-
36- func TestSetupZapLoggerNonVerbose (t * testing.T ) {
37- // Set up the logger in non-verbose (production) mode.
38- setupZapLogger (false )
39-
40- // The global logger (zap.L()) should not allow debug logs.
41- if zap .L ().Core ().Enabled (zap .DebugLevel ) {
42- t .Error ("Expected debug level to be disabled in non-verbose mode but it is enabled" )
17+ func TestSetupZapLogger (t * testing.T ) {
18+ // Define the test cases in a table-driven approach.
19+ tests := []struct {
20+ name string
21+ verbose bool
22+ filename string
23+ debugExpected bool
24+ infoExpected bool
25+ shouldCreateFile bool
26+ }{
27+ {
28+ name : "Verbose without file" ,
29+ verbose : true ,
30+ filename : "" ,
31+ debugExpected : true ,
32+ infoExpected : true ,
33+ shouldCreateFile : false ,
34+ },
35+ {
36+ name : "Verbose with file" ,
37+ verbose : true ,
38+ filename : "test_verbose.log" ,
39+ debugExpected : true ,
40+ infoExpected : true ,
41+ shouldCreateFile : true ,
42+ },
43+ {
44+ name : "Non-Verbose without file" ,
45+ verbose : false ,
46+ filename : "" ,
47+ debugExpected : false ,
48+ infoExpected : true ,
49+ shouldCreateFile : false ,
50+ },
51+ {
52+ name : "Non-Verbose with file" ,
53+ verbose : false ,
54+ filename : "test_nonverbose.log" ,
55+ debugExpected : false ,
56+ infoExpected : true ,
57+ shouldCreateFile : true ,
58+ },
4359 }
4460
45- // Info level logs must remain enabled.
46- if ! zap .L ().Core ().Enabled (zap .InfoLevel ) {
47- t .Error ("Expected info level to be enabled in non-verbose mode but it is not" )
61+ for _ , tc := range tests {
62+ t .Run (tc .name , func (t * testing.T ) {
63+ // Call the setup function with appropriate parameters.
64+ setupZapLogger (tc .verbose , tc .filename )
65+
66+ // Check that the logger's log level for debug is set as expected.
67+ if zap .L ().Core ().Enabled (zap .DebugLevel ) != tc .debugExpected {
68+ t .Errorf ("For verbose=%v, expected DebugLevel enabled to be %v, got %v" ,
69+ tc .verbose , tc .debugExpected , zap .L ().Core ().Enabled (zap .DebugLevel ))
70+ }
71+
72+ // Check that info level is always enabled.
73+ if zap .L ().Core ().Enabled (zap .InfoLevel ) != tc .infoExpected {
74+ t .Errorf ("Expected InfoLevel enabled to be %v, got %v" ,
75+ tc .infoExpected , zap .L ().Core ().Enabled (zap .InfoLevel ))
76+ }
77+
78+ // If a filename is provided, verify that the file is created.
79+ if tc .shouldCreateFile && tc .filename != "" {
80+ // Write a log entry to ensure that the logger flushes its output.
81+ zap .L ().Info ("test log entry" )
82+ // Flush any buffered log entries.
83+ if err := zap .L ().Sync (); err != nil {
84+ // On Windows, Sync may return an error, so we don't fail the test solely on that.
85+ t .Logf ("Sync error (possibly expected on Windows): %v" , err )
86+ }
87+
88+ // Check if the file exists.
89+ if _ , err := os .Stat (tc .filename ); os .IsNotExist (err ) {
90+ t .Errorf ("Expected log file %s to be created, but it does not exist" , tc .filename )
91+ } else if err != nil {
92+ t .Errorf ("Error checking log file %s: %v" , tc .filename , err )
93+ }
94+ // Cleanup: remove the test log file.
95+ os .Remove (tc .filename )
96+ }
97+ })
4898 }
4999}
50100
0 commit comments