Skip to content

Commit

Permalink
Merge pull request #12 from anchore/feat/logging-tests
Browse files Browse the repository at this point in the history
chore: Add some simple logger tests
  • Loading branch information
robcresswell committed Jan 5, 2023
2 parents 7981243 + 2104b60 commit 18cf5f6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
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")
}

0 comments on commit 18cf5f6

Please sign in to comment.