Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add some simple logger tests #12

Merged
merged 1 commit into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ require (
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.14.0
github.com/stretchr/testify v1.8.1
go.uber.org/zap v1.24.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
Expand All @@ -25,6 +27,7 @@ require (
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
Expand Down
41 changes: 41 additions & 0 deletions internal/logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package logger

import (
"os"
"path"
"testing"

"github.com/stretchr/testify/assert"
)

func TestLoggerInit(t *testing.T) {
assert.Nil(t, Log.zap)

InitLogger(LogConfig{Level: "info", FileLocation: "", Dev: false})

assert.NotNil(t, Log.zap)
}

func TestLogsToFileIfFileLocationProvided(t *testing.T) {
tmpDir := t.TempDir()
fileLocation := path.Join(tmpDir, "log")

InitLogger(LogConfig{Level: "info", FileLocation: fileLocation, Dev: false})

var expectedLogMsg = "test log foobar"
Log.Info(expectedLogMsg)

b, err := os.ReadFile(fileLocation)

if (b == nil) || (err != nil) {
t.Errorf("Expected log file to be created at %s", fileLocation)
}

assert.Contains(t, string(b), expectedLogMsg)
}

func TestLoggerDefaultsToInfoLevelOnInvalidLevel(t *testing.T) {
InitLogger(LogConfig{Level: "invalid", FileLocation: "", Dev: false})

assert.Equal(t, Log.zap.Level().String(), "info")
}