goutils/dbg/dbg_test.go

121 lines
2.7 KiB
Go
Raw Normal View History

2018-12-18 02:12:51 +00:00
package dbg
import (
2023-05-12 00:14:19 +00:00
"fmt"
2018-12-18 02:12:51 +00:00
"io/ioutil"
"os"
"testing"
2023-05-12 00:14:19 +00:00
"git.wntrmute.dev/kyle/goutils/assert"
"git.wntrmute.dev/kyle/goutils/testio"
2018-12-18 02:12:51 +00:00
)
func TestNew(t *testing.T) {
buf := testio.NewBufCloser(nil)
dbg := New()
dbg.out = buf
dbg.Print("hello")
dbg.Println("hello")
dbg.Printf("hello %s", "world")
2023-05-12 00:14:19 +00:00
assert.BoolT(t, buf.Len() == 0)
2018-12-18 02:12:51 +00:00
dbg.Enabled = true
dbg.Print("hello") // +5
dbg.Println("hello") // +6
dbg.Printf("hello %s", "world") // +11
2023-05-12 00:14:19 +00:00
assert.BoolT(t, buf.Len() == 22, fmt.Sprintf("buffer should be length 22 but is length %d", buf.Len()))
2018-12-18 02:12:51 +00:00
err := dbg.Close()
2023-05-12 00:14:19 +00:00
assert.NoErrorT(t, err)
2018-12-18 02:12:51 +00:00
}
func TestTo(t *testing.T) {
buf := testio.NewBufCloser(nil)
dbg := To(buf)
dbg.Print("hello")
dbg.Println("hello")
dbg.Printf("hello %s", "world")
2023-05-12 00:14:19 +00:00
assert.BoolT(t, buf.Len() == 0, "debug output should be suppressed")
2018-12-18 02:12:51 +00:00
dbg.Enabled = true
dbg.Print("hello") // +5
dbg.Println("hello") // +6
dbg.Printf("hello %s", "world") // +11
2023-05-12 00:14:19 +00:00
assert.BoolT(t, buf.Len() == 22, "didn't get the expected debug output")
2018-12-18 02:12:51 +00:00
err := dbg.Close()
2023-05-12 00:14:19 +00:00
assert.NoErrorT(t, err)
2018-12-18 02:12:51 +00:00
}
func TestToFile(t *testing.T) {
testFile, err := ioutil.TempFile("", "dbg")
2023-05-12 00:14:19 +00:00
assert.NoErrorT(t, err)
2018-12-18 02:12:51 +00:00
err = testFile.Close()
2023-05-12 00:14:19 +00:00
assert.NoErrorT(t, err)
2018-12-18 02:12:51 +00:00
testFileName := testFile.Name()
defer os.Remove(testFileName)
dbg, err := ToFile(testFileName)
2023-05-12 00:14:19 +00:00
assert.NoErrorT(t, err)
2018-12-18 02:12:51 +00:00
dbg.Print("hello")
dbg.Println("hello")
dbg.Printf("hello %s", "world")
stat, err := os.Stat(testFileName)
2023-05-12 00:14:19 +00:00
assert.NoErrorT(t, err)
2018-12-18 02:12:51 +00:00
2023-05-12 00:14:19 +00:00
assert.BoolT(t, stat.Size() == 0, "no debug output should have been sent to the log file")
2018-12-18 02:12:51 +00:00
dbg.Enabled = true
dbg.Print("hello") // +5
dbg.Println("hello") // +6
dbg.Printf("hello %s", "world") // +11
stat, err = os.Stat(testFileName)
2023-05-12 00:14:19 +00:00
assert.NoErrorT(t, err)
2018-12-18 02:12:51 +00:00
2023-05-12 00:14:19 +00:00
assert.BoolT(t, stat.Size() == 22, fmt.Sprintf("have %d bytes in the log file, expected 22", stat.Size()))
2018-12-18 02:12:51 +00:00
err = dbg.Close()
2023-05-12 00:14:19 +00:00
assert.NoErrorT(t, err)
2018-12-18 02:12:51 +00:00
}
func TestWriting(t *testing.T) {
data := []byte("hello, world")
buf := testio.NewBufCloser(nil)
dbg := To(buf)
n, err := dbg.Write(data)
2023-05-12 00:14:19 +00:00
assert.NoErrorT(t, err)
assert.BoolT(t, n == 0, "expected nothing to be written to the buffer")
2018-12-18 02:12:51 +00:00
dbg.Enabled = true
n, err = dbg.Write(data)
2023-05-12 00:14:19 +00:00
assert.NoErrorT(t, err)
assert.BoolT(t, n == 12, fmt.Sprintf("wrote %d bytes in the buffer, expected to write 12", n))
2018-12-18 02:12:51 +00:00
err = dbg.Close()
2023-05-12 00:14:19 +00:00
assert.NoErrorT(t, err)
2018-12-18 02:12:51 +00:00
}
func TestToFileError(t *testing.T) {
testFile, err := ioutil.TempFile("", "dbg")
2023-05-12 00:14:19 +00:00
assert.NoErrorT(t, err)
2018-12-18 02:12:51 +00:00
err = testFile.Chmod(0400)
2023-05-12 00:14:19 +00:00
assert.NoErrorT(t, err)
2018-12-18 02:12:51 +00:00
err = testFile.Close()
2023-05-12 00:14:19 +00:00
assert.NoErrorT(t, err)
2018-12-18 02:12:51 +00:00
testFileName := testFile.Name()
_, err = ToFile(testFileName)
2023-05-12 00:14:19 +00:00
assert.ErrorT(t, err)
2018-12-18 02:12:51 +00:00
err = os.Remove(testFileName)
2023-05-12 00:14:19 +00:00
assert.NoErrorT(t, err)
2018-12-18 02:12:51 +00:00
}