Skip to content

Commit

Permalink
test: add tests for jerm log package
Browse files Browse the repository at this point in the history
  • Loading branch information
spatocode committed Sep 30, 2023
1 parent 860d31f commit 39e1dd1
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions internal/log/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package log

import (
"io"
"os"
"testing"

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

func TestInfoLog(t *testing.T) {
assert := assert.New(t)

r, w, stdout := pipeStd(t)
Info("testing")
w.Close()
out, _ := io.ReadAll(r)
os.Stdout = stdout

assert.Contains(string(out), "INFO testing\n")
}

func TestDebugLog(t *testing.T) {
assert := assert.New(t)

r, w, stdout := pipeStd(t)
Debug("testing")
w.Close()
out, _ := io.ReadAll(r)
os.Stdout = stdout
assert.NotContains(string(out), "DEBUG testing\n")

os.Setenv("JERM_VERBOSE", "1")
r, w, stdout = pipeStd(t)
Debug("testing")
w.Close()
out, _ = io.ReadAll(r)
os.Stdout = stdout
assert.Contains(string(out), "DEBUG testing\n")
}

func TestWarnLog(t *testing.T) {
assert := assert.New(t)

r, w, stdout := pipeStd(t)
Warn("testing")
w.Close()
out, _ := io.ReadAll(r)
os.Stdout = stdout

assert.Contains(string(out), "WARN testing\n")
}

func TestErrorLog(t *testing.T) {
assert := assert.New(t)

r, w, stdout := pipeStd(t)
Error("testing")
w.Close()
out, _ := io.ReadAll(r)
os.Stdout = stdout

assert.Contains(string(out), "ERROR testing\n")
}

func pipeStd(t *testing.T) (r, w, stdout *os.File) {
stdout = os.Stdout
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
os.Stdout = w
return
}

0 comments on commit 39e1dd1

Please sign in to comment.