Skip to content

Commit cd6294e

Browse files
authored
fix: handle nil logger in SetLogger method and add corresponding test (#27)
1 parent cd4926c commit cd6294e

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

pkg/app/app.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ func NewApp() *App {
2626
}
2727

2828
// SetLogger sets the logger
29-
func (a *App) SetLogger(logger logger.Logger) {
30-
a.logger = logger
29+
func (a *App) SetLogger(log logger.Logger) {
30+
if log == nil {
31+
a.logger = logger.NoLogger()
32+
return
33+
}
34+
a.logger = log
3135
}
3236

3337
// SetBackupCmd sets the backup command

pkg/app/app_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,16 @@ func TestRunCommandReportsError(t *testing.T) {
2929
t.Fatalf("Expected error, got nil")
3030
}
3131
}
32+
33+
func TestSetLoggerNil(t *testing.T) {
34+
a := app.NewApp()
35+
if a == nil {
36+
t.Fatalf("Expected non-nil app, got nil")
37+
}
38+
a.SetLogger(nil)
39+
a.SetBackupCmd([]string{"echo", "hello"})
40+
err := a.Run()
41+
if err != nil {
42+
t.Fatalf("Expected nil error, got %v", err)
43+
}
44+
}

0 commit comments

Comments
 (0)