mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-28 22:19:59 +02:00
tests: Convert from testify to quicktest
This commit is contained in:
@@ -14,19 +14,18 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
qt "github.com/frankban/quicktest"
|
||||
)
|
||||
|
||||
func TestGetCSV(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := qt.New(t)
|
||||
|
||||
for i, test := range []struct {
|
||||
sep string
|
||||
@@ -74,7 +73,7 @@ func TestGetCSV(t *testing.T) {
|
||||
false,
|
||||
},
|
||||
} {
|
||||
msg := fmt.Sprintf("Test %d", i)
|
||||
msg := qt.Commentf("Test %d", i)
|
||||
|
||||
ns := newTestNs()
|
||||
|
||||
@@ -100,7 +99,7 @@ func TestGetCSV(t *testing.T) {
|
||||
// Setup local test file for schema-less URLs
|
||||
if !strings.Contains(test.url, ":") && !strings.HasPrefix(test.url, "fail/") {
|
||||
f, err := ns.deps.Fs.Source.Create(filepath.Join(ns.deps.Cfg.GetString("workingDir"), test.url))
|
||||
require.NoError(t, err, msg)
|
||||
c.Assert(err, qt.IsNil, msg)
|
||||
f.WriteString(test.content)
|
||||
f.Close()
|
||||
}
|
||||
@@ -109,22 +108,23 @@ func TestGetCSV(t *testing.T) {
|
||||
got, err := ns.GetCSV(test.sep, test.url)
|
||||
|
||||
if _, ok := test.expect.(bool); ok {
|
||||
require.Equal(t, 1, int(ns.deps.Log.ErrorCounter.Count()))
|
||||
//require.Error(t, err, msg)
|
||||
require.Nil(t, got)
|
||||
c.Assert(int(ns.deps.Log.ErrorCounter.Count()), qt.Equals, 1)
|
||||
//c.Assert(err, msg, qt.Not(qt.IsNil))
|
||||
c.Assert(got, qt.IsNil)
|
||||
continue
|
||||
}
|
||||
|
||||
require.NoError(t, err, msg)
|
||||
require.Equal(t, 0, int(ns.deps.Log.ErrorCounter.Count()))
|
||||
require.NotNil(t, got, msg)
|
||||
c.Assert(err, qt.IsNil, msg)
|
||||
c.Assert(int(ns.deps.Log.ErrorCounter.Count()), qt.Equals, 0)
|
||||
c.Assert(got, qt.Not(qt.IsNil), msg)
|
||||
c.Assert(got, qt.DeepEquals, test.expect, msg)
|
||||
|
||||
assert.EqualValues(t, test.expect, got, msg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetJSON(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := qt.New(t)
|
||||
|
||||
for i, test := range []struct {
|
||||
url string
|
||||
@@ -159,7 +159,7 @@ func TestGetJSON(t *testing.T) {
|
||||
},
|
||||
} {
|
||||
|
||||
msg := fmt.Sprintf("Test %d", i)
|
||||
msg := qt.Commentf("Test %d", i)
|
||||
ns := newTestNs()
|
||||
|
||||
// Setup HTTP test server
|
||||
@@ -184,7 +184,7 @@ func TestGetJSON(t *testing.T) {
|
||||
// Setup local test file for schema-less URLs
|
||||
if !strings.Contains(test.url, ":") && !strings.HasPrefix(test.url, "fail/") {
|
||||
f, err := ns.deps.Fs.Source.Create(filepath.Join(ns.deps.Cfg.GetString("workingDir"), test.url))
|
||||
require.NoError(t, err, msg)
|
||||
c.Assert(err, qt.IsNil, msg)
|
||||
f.WriteString(test.content)
|
||||
f.Close()
|
||||
}
|
||||
@@ -193,20 +193,20 @@ func TestGetJSON(t *testing.T) {
|
||||
got, _ := ns.GetJSON(test.url)
|
||||
|
||||
if _, ok := test.expect.(bool); ok {
|
||||
require.Equal(t, 1, int(ns.deps.Log.ErrorCounter.Count()))
|
||||
//require.Error(t, err, msg)
|
||||
c.Assert(int(ns.deps.Log.ErrorCounter.Count()), qt.Equals, 1)
|
||||
//c.Assert(err, msg, qt.Not(qt.IsNil))
|
||||
continue
|
||||
}
|
||||
|
||||
require.Equal(t, 0, int(ns.deps.Log.ErrorCounter.Count()), msg)
|
||||
require.NotNil(t, got, msg)
|
||||
|
||||
assert.EqualValues(t, test.expect, got, msg)
|
||||
c.Assert(int(ns.deps.Log.ErrorCounter.Count()), qt.Equals, 0, msg)
|
||||
c.Assert(got, qt.Not(qt.IsNil), msg)
|
||||
c.Assert(got, qt.DeepEquals, test.expect)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCSV(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := qt.New(t)
|
||||
|
||||
for i, test := range []struct {
|
||||
csv []byte
|
||||
@@ -221,21 +221,21 @@ func TestParseCSV(t *testing.T) {
|
||||
{[]byte("a|b|c\nd|e|f|g"), "|", "abcdefg", true},
|
||||
{[]byte("z|y|c\nd|e|f"), "|", "zycdef", false},
|
||||
} {
|
||||
msg := fmt.Sprintf("Test %d: %v", i, test)
|
||||
msg := qt.Commentf("Test %d: %v", i, test)
|
||||
|
||||
csv, err := parseCSV(test.csv, test.sep)
|
||||
if test.err {
|
||||
assert.Error(t, err, msg)
|
||||
c.Assert(err, qt.Not(qt.IsNil), msg)
|
||||
continue
|
||||
}
|
||||
require.NoError(t, err, msg)
|
||||
c.Assert(err, qt.IsNil, msg)
|
||||
|
||||
act := ""
|
||||
for _, v := range csv {
|
||||
act = act + strings.Join(v, "")
|
||||
}
|
||||
|
||||
assert.Equal(t, test.exp, act, msg)
|
||||
c.Assert(act, qt.Equals, test.exp, msg)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -16,13 +16,15 @@ package data
|
||||
import (
|
||||
"testing"
|
||||
|
||||
qt "github.com/frankban/quicktest"
|
||||
"github.com/gohugoio/hugo/htesting/hqt"
|
||||
"github.com/gohugoio/hugo/langs"
|
||||
"github.com/gohugoio/hugo/tpl/internal"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestInit(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
var found bool
|
||||
var ns *internal.TemplateFuncsNamespace
|
||||
|
||||
@@ -38,6 +40,6 @@ func TestInit(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
require.True(t, found)
|
||||
require.IsType(t, &Namespace{}, ns.Context())
|
||||
c.Assert(found, qt.Equals, true)
|
||||
c.Assert(ns.Context(), hqt.IsSameType, &Namespace{})
|
||||
}
|
||||
|
@@ -15,7 +15,6 @@ package data
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
@@ -27,6 +26,7 @@ import (
|
||||
|
||||
"github.com/gohugoio/hugo/helpers"
|
||||
|
||||
qt "github.com/frankban/quicktest"
|
||||
"github.com/gohugoio/hugo/cache/filecache"
|
||||
"github.com/gohugoio/hugo/common/loggers"
|
||||
"github.com/gohugoio/hugo/config"
|
||||
@@ -35,8 +35,6 @@ import (
|
||||
"github.com/gohugoio/hugo/langs"
|
||||
"github.com/spf13/afero"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestScpGetLocal(t *testing.T) {
|
||||
@@ -88,6 +86,7 @@ func getTestServer(handler func(w http.ResponseWriter, r *http.Request)) (*httpt
|
||||
|
||||
func TestScpGetRemote(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := qt.New(t)
|
||||
fs := new(afero.MemMapFs)
|
||||
cache := filecache.NewCache(fs, 100, "")
|
||||
|
||||
@@ -102,10 +101,10 @@ func TestScpGetRemote(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
msg := fmt.Sprintf("%v", test)
|
||||
msg := qt.Commentf("%v", test)
|
||||
|
||||
req, err := http.NewRequest("GET", test.path, nil)
|
||||
require.NoError(t, err, msg)
|
||||
c.Assert(err, qt.IsNil, msg)
|
||||
|
||||
srv, cl := getTestServer(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write(test.content)
|
||||
@@ -115,23 +114,24 @@ func TestScpGetRemote(t *testing.T) {
|
||||
ns := newTestNs()
|
||||
ns.client = cl
|
||||
|
||||
var c []byte
|
||||
var cb []byte
|
||||
f := func(b []byte) (bool, error) {
|
||||
c = b
|
||||
cb = b
|
||||
return false, nil
|
||||
}
|
||||
|
||||
err = ns.getRemote(cache, f, req)
|
||||
require.NoError(t, err, msg)
|
||||
assert.Equal(t, string(test.content), string(c))
|
||||
c.Assert(err, qt.IsNil, msg)
|
||||
c.Assert(string(cb), qt.Equals, string(test.content))
|
||||
|
||||
assert.Equal(t, string(test.content), string(c))
|
||||
c.Assert(string(cb), qt.Equals, string(test.content))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestScpGetRemoteParallel(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := qt.New(t)
|
||||
|
||||
content := []byte(`T€st Content 123`)
|
||||
srv, cl := getTestServer(func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -142,7 +142,7 @@ func TestScpGetRemoteParallel(t *testing.T) {
|
||||
|
||||
url := "http://Foo.Bar/foo_Bar-Foo"
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
require.NoError(t, err)
|
||||
c.Assert(err, qt.IsNil)
|
||||
|
||||
for _, ignoreCache := range []bool{false} {
|
||||
cfg := viper.New()
|
||||
@@ -159,16 +159,16 @@ func TestScpGetRemoteParallel(t *testing.T) {
|
||||
go func(gor int) {
|
||||
defer wg.Done()
|
||||
for j := 0; j < 10; j++ {
|
||||
var c []byte
|
||||
var cb []byte
|
||||
f := func(b []byte) (bool, error) {
|
||||
c = b
|
||||
cb = b
|
||||
return false, nil
|
||||
}
|
||||
err := ns.getRemote(ns.cacheGetJSON, f, req)
|
||||
|
||||
assert.NoError(t, err)
|
||||
if string(content) != string(c) {
|
||||
t.Errorf("expected\n%q\ngot\n%q", content, c)
|
||||
c.Assert(err, qt.IsNil)
|
||||
if string(content) != string(cb) {
|
||||
t.Errorf("expected\n%q\ngot\n%q", content, cb)
|
||||
}
|
||||
|
||||
time.Sleep(23 * time.Millisecond)
|
||||
|
Reference in New Issue
Block a user