@@ -12,6 +12,9 @@ import (
12
12
"os"
13
13
"testing"
14
14
15
+ "github.com/agiledragon/gomonkey/v2"
16
+ "github.com/sirupsen/logrus"
17
+ "github.com/stretchr/testify/assert"
15
18
"github.com/stretchr/testify/require"
16
19
"github.com/urfave/cli/v2"
17
20
)
@@ -341,3 +344,50 @@ func TestGetPrefetchPatterns(t *testing.T) {
341
344
require .NoError (t , err )
342
345
require .Equal (t , "/" , patterns )
343
346
}
347
+
348
+ func TestGetGlobalFlags (t * testing.T ) {
349
+ flags := getGlobalFlags ()
350
+ require .Equal (t , 3 , len (flags ))
351
+ }
352
+
353
+ func TestSetupLogLevelWithLogFile (t * testing.T ) {
354
+ logFilePath := "test_log_file.log"
355
+ defer os .Remove (logFilePath )
356
+
357
+ c := & cli.Context {}
358
+
359
+ patches := gomonkey .ApplyMethodSeq (c , "String" , []gomonkey.OutputCell {
360
+ {Values : []interface {}{"info" }, Times : 1 },
361
+ {Values : []interface {}{"test_log_file.log" }, Times : 2 },
362
+ })
363
+ defer patches .Reset ()
364
+ setupLogLevel (c )
365
+
366
+ file , err := os .Open (logFilePath )
367
+ assert .NoError (t , err )
368
+ assert .NotNil (t , file )
369
+ file .Close ()
370
+
371
+ logrusOutput := logrus .StandardLogger ().Out
372
+ assert .NotNil (t , logrusOutput )
373
+
374
+ logrus .Info ("This is a test log message" )
375
+ content , err := os .ReadFile (logFilePath )
376
+ assert .NoError (t , err )
377
+ assert .Contains (t , string (content ), "This is a test log message" )
378
+ }
379
+
380
+ func TestSetupLogLevelWithInvalidLogFile (t * testing.T ) {
381
+
382
+ c := & cli.Context {}
383
+
384
+ patches := gomonkey .ApplyMethodSeq (c , "String" , []gomonkey.OutputCell {
385
+ {Values : []interface {}{"info" }, Times : 1 },
386
+ {Values : []interface {}{"test/test_log_file.log" }, Times : 2 },
387
+ })
388
+ defer patches .Reset ()
389
+ setupLogLevel (c )
390
+
391
+ logrusOutput := logrus .StandardLogger ().Out
392
+ assert .NotNil (t , logrusOutput )
393
+ }
0 commit comments