db: clean out dependency on stretchr

This commit is contained in:
Kyle Isom 2023-05-11 17:14:19 -07:00
parent 63957ff22a
commit 72fdc255e7
3 changed files with 27 additions and 30 deletions

View File

@ -13,7 +13,7 @@ go_test(
srcs = ["dbg_test.go"], srcs = ["dbg_test.go"],
embed = [":dbg"], embed = [":dbg"],
deps = [ deps = [
"//assert",
"//testio", "//testio",
"@com_github_stretchr_testify//require",
], ],
) )

View File

@ -1,12 +1,13 @@
package dbg package dbg
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"testing" "testing"
"git.wntrmute.dev/kyle/goutils/assert"
"git.wntrmute.dev/kyle/goutils/testio" "git.wntrmute.dev/kyle/goutils/testio"
"github.com/stretchr/testify/require"
) )
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
@ -17,16 +18,16 @@ func TestNew(t *testing.T) {
dbg.Print("hello") dbg.Print("hello")
dbg.Println("hello") dbg.Println("hello")
dbg.Printf("hello %s", "world") dbg.Printf("hello %s", "world")
require.Equal(t, 0, buf.Len()) assert.BoolT(t, buf.Len() == 0)
dbg.Enabled = true dbg.Enabled = true
dbg.Print("hello") // +5 dbg.Print("hello") // +5
dbg.Println("hello") // +6 dbg.Println("hello") // +6
dbg.Printf("hello %s", "world") // +11 dbg.Printf("hello %s", "world") // +11
require.Equal(t, 22, buf.Len()) assert.BoolT(t, buf.Len() == 22, fmt.Sprintf("buffer should be length 22 but is length %d", buf.Len()))
err := dbg.Close() err := dbg.Close()
require.NoError(t, err) assert.NoErrorT(t, err)
} }
func TestTo(t *testing.T) { func TestTo(t *testing.T) {
@ -36,39 +37,38 @@ func TestTo(t *testing.T) {
dbg.Print("hello") dbg.Print("hello")
dbg.Println("hello") dbg.Println("hello")
dbg.Printf("hello %s", "world") dbg.Printf("hello %s", "world")
require.Equal(t, 0, buf.Len()) assert.BoolT(t, buf.Len() == 0, "debug output should be suppressed")
dbg.Enabled = true dbg.Enabled = true
dbg.Print("hello") // +5 dbg.Print("hello") // +5
dbg.Println("hello") // +6 dbg.Println("hello") // +6
dbg.Printf("hello %s", "world") // +11 dbg.Printf("hello %s", "world") // +11
assert.BoolT(t, buf.Len() == 22, "didn't get the expected debug output")
require.Equal(t, 22, buf.Len())
err := dbg.Close() err := dbg.Close()
require.NoError(t, err) assert.NoErrorT(t, err)
} }
func TestToFile(t *testing.T) { func TestToFile(t *testing.T) {
testFile, err := ioutil.TempFile("", "dbg") testFile, err := ioutil.TempFile("", "dbg")
require.NoError(t, err) assert.NoErrorT(t, err)
err = testFile.Close() err = testFile.Close()
require.NoError(t, err) assert.NoErrorT(t, err)
testFileName := testFile.Name() testFileName := testFile.Name()
defer os.Remove(testFileName) defer os.Remove(testFileName)
dbg, err := ToFile(testFileName) dbg, err := ToFile(testFileName)
require.NoError(t, err) assert.NoErrorT(t, err)
dbg.Print("hello") dbg.Print("hello")
dbg.Println("hello") dbg.Println("hello")
dbg.Printf("hello %s", "world") dbg.Printf("hello %s", "world")
stat, err := os.Stat(testFileName) stat, err := os.Stat(testFileName)
require.NoError(t, err) assert.NoErrorT(t, err)
require.EqualValues(t, 0, stat.Size()) assert.BoolT(t, stat.Size() == 0, "no debug output should have been sent to the log file")
dbg.Enabled = true dbg.Enabled = true
dbg.Print("hello") // +5 dbg.Print("hello") // +5
@ -76,12 +76,12 @@ func TestToFile(t *testing.T) {
dbg.Printf("hello %s", "world") // +11 dbg.Printf("hello %s", "world") // +11
stat, err = os.Stat(testFileName) stat, err = os.Stat(testFileName)
require.NoError(t, err) assert.NoErrorT(t, err)
require.EqualValues(t, 22, stat.Size()) assert.BoolT(t, stat.Size() == 22, fmt.Sprintf("have %d bytes in the log file, expected 22", stat.Size()))
err = dbg.Close() err = dbg.Close()
require.NoError(t, err) assert.NoErrorT(t, err)
} }
func TestWriting(t *testing.T) { func TestWriting(t *testing.T) {
@ -90,31 +90,31 @@ func TestWriting(t *testing.T) {
dbg := To(buf) dbg := To(buf)
n, err := dbg.Write(data) n, err := dbg.Write(data)
require.NoError(t, err) assert.NoErrorT(t, err)
require.EqualValues(t, 0, n) assert.BoolT(t, n == 0, "expected nothing to be written to the buffer")
dbg.Enabled = true dbg.Enabled = true
n, err = dbg.Write(data) n, err = dbg.Write(data)
require.NoError(t, err) assert.NoErrorT(t, err)
require.EqualValues(t, 12, n) assert.BoolT(t, n == 12, fmt.Sprintf("wrote %d bytes in the buffer, expected to write 12", n))
err = dbg.Close() err = dbg.Close()
require.NoError(t, err) assert.NoErrorT(t, err)
} }
func TestToFileError(t *testing.T) { func TestToFileError(t *testing.T) {
testFile, err := ioutil.TempFile("", "dbg") testFile, err := ioutil.TempFile("", "dbg")
require.NoError(t, err) assert.NoErrorT(t, err)
err = testFile.Chmod(0400) err = testFile.Chmod(0400)
require.NoError(t, err) assert.NoErrorT(t, err)
err = testFile.Close() err = testFile.Close()
require.NoError(t, err) assert.NoErrorT(t, err)
testFileName := testFile.Name() testFileName := testFile.Name()
_, err = ToFile(testFileName) _, err = ToFile(testFileName)
require.Error(t, err) assert.ErrorT(t, err)
err = os.Remove(testFileName) err = os.Remove(testFileName)
require.NoError(t, err) assert.NoErrorT(t, err)
} }

3
go.mod
View File

@ -7,7 +7,6 @@ require (
github.com/kr/text v0.2.0 github.com/kr/text v0.2.0
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/pkg/sftp v1.12.0 github.com/pkg/sftp v1.12.0
github.com/stretchr/testify v1.6.1
golang.org/x/crypto v0.0.0-20220314234659-1baeb1ce4c0b golang.org/x/crypto v0.0.0-20220314234659-1baeb1ce4c0b
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad golang.org/x/sys v0.0.0-20220412211240-33da011f77ad
gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v2 v2.4.0
@ -21,7 +20,5 @@ require (
require ( require (
github.com/kr/fs v0.1.0 // indirect github.com/kr/fs v0.1.0 // indirect
github.com/kr/pretty v0.1.0 // indirect github.com/kr/pretty v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
) )