mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-09-05 11:06:23 -07:00
Move some test utility functions to testutil
This commit is contained in:
parent
3d19f9a088
commit
dc454d1bc1
4 changed files with 78 additions and 26 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/gochan-org/gochan/pkg/config"
|
||||
"github.com/gochan-org/gochan/pkg/gcutil/testutil"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
@ -12,7 +13,7 @@ var (
|
|||
)
|
||||
|
||||
func TestProvision(t *testing.T) {
|
||||
_, err := goToGochanRoot(t)
|
||||
_, err := testutil.GoToGochanRoot(t)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -5,9 +5,6 @@ package gcsql
|
|||
import (
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
|
@ -92,28 +89,6 @@ var (
|
|||
}
|
||||
)
|
||||
|
||||
// TODO: move this to a dedicated gochan-specific testing utility package
|
||||
func goToGochanRoot(t *testing.T) (string, error) {
|
||||
t.Helper()
|
||||
dir, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for d := 0; d < 6; d++ {
|
||||
if path.Base(dir) == "gochan" {
|
||||
return dir, nil
|
||||
}
|
||||
if err = os.Chdir(".."); err != nil {
|
||||
return dir, err
|
||||
}
|
||||
if dir, err = os.Getwd(); err != nil {
|
||||
return dir, err
|
||||
}
|
||||
}
|
||||
return dir, errors.New("test running from unexpected dir, should be in gochan root or the current testing dir")
|
||||
}
|
||||
|
||||
func setupMockDB(t *testing.T, dbType string, dbName string) (mock sqlmock.Sqlmock, err error) {
|
||||
gcdb, err = setupDBConn("localhost", dbType, dbName, "gochan", "gochan", "")
|
||||
if !assert.NoError(t, err) {
|
||||
|
|
35
pkg/gcutil/testutil/mockresponsewriter.go
Normal file
35
pkg/gcutil/testutil/mockresponsewriter.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package testutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// MockResponseWriter can be used in place of a http.ResponseWriter interface for tests
|
||||
type MockResponseWriter struct {
|
||||
StatusCode int
|
||||
Buffer *bytes.Buffer
|
||||
header http.Header
|
||||
}
|
||||
|
||||
func (m MockResponseWriter) Header() http.Header {
|
||||
PanicIfNotTest()
|
||||
if m.header == nil {
|
||||
m.header = make(http.Header)
|
||||
}
|
||||
return m.header
|
||||
|
||||
}
|
||||
|
||||
func (m MockResponseWriter) Write(ba []byte) (int, error) {
|
||||
PanicIfNotTest()
|
||||
if m.Buffer == nil {
|
||||
m.Buffer = new(bytes.Buffer)
|
||||
}
|
||||
return m.Buffer.Write(ba)
|
||||
}
|
||||
|
||||
func (m MockResponseWriter) WriteHeader(statusCode int) {
|
||||
PanicIfNotTest()
|
||||
m.StatusCode = statusCode
|
||||
}
|
41
pkg/gcutil/testutil/testutil.go
Normal file
41
pkg/gcutil/testutil/testutil.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
package testutil
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const (
|
||||
numParentDirsBeforeFail = 6
|
||||
)
|
||||
|
||||
// PanicIfNotTest panics if the function was called directly or indirectly by a test function via go test
|
||||
func PanicIfNotTest() {
|
||||
if !strings.HasSuffix(os.Args[0], ".test") {
|
||||
panic("the testutil package should only be used in tests")
|
||||
}
|
||||
}
|
||||
|
||||
// GoToGochanRoot gets the
|
||||
func GoToGochanRoot(t *testing.T) (string, error) {
|
||||
t.Helper()
|
||||
|
||||
var dir string
|
||||
var err error
|
||||
for d := 0; d < numParentDirsBeforeFail; d++ {
|
||||
dir, err = os.Getwd()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if path.Base(dir) == "gochan" {
|
||||
return dir, nil
|
||||
}
|
||||
if err = os.Chdir(".."); err != nil {
|
||||
return dir, err
|
||||
}
|
||||
}
|
||||
return dir, errors.New("test running from unexpected dir, should be in gochan root or the current testing dir")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue