Commit b50e0b70 authored by Alessio Caiazza's avatar Alessio Caiazza

Merge branch 'jv-less-testify-assert' into 'master'

Replace some cases of testify/assert with require NO CHANGELOG

See merge request gitlab-org/gitlab-workhorse!600
parents ad3b4ab0 f492173c
......@@ -10,7 +10,7 @@ import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/redis"
)
......@@ -33,7 +33,7 @@ func expectHandlerWithWatcher(t *testing.T, watchHandler WatchKeyHandler, data s
h.ServeHTTP(rw, req)
assert.Equal(t, expectedHttpStatus, rw.Code, msgAndArgs...)
require.Equal(t, expectedHttpStatus, rw.Code, msgAndArgs...)
}
func expectHandler(t *testing.T, data string, contentType string, expectedHttpStatus int, msgAndArgs ...interface{}) {
......@@ -79,7 +79,7 @@ func expectWatcherToBeExecuted(t *testing.T, watchKeyStatus redis.WatchKeyStatus
parsableData := `{"token":"token","last_update":"last_update"}`
expectHandlerWithWatcher(t, watchKeyHandler, parsableData, "application/json", httpStatus, msgAndArgs...)
assert.True(t, executed, msgAndArgs...)
require.True(t, executed, msgAndArgs...)
}
func TestRegisterHandlerWatcherError(t *testing.T) {
......
......@@ -6,7 +6,6 @@ import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gocloud.dev/blob/azureblob"
)
......@@ -110,10 +109,10 @@ func TestTestURLOpenersForParams(t *testing.T) {
err := setOptionsFromURLParams(test.query, &o.Options)
if test.wantErr {
assert.NotNil(t, err)
require.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.Equal(t, test.wantOpts, o.Options)
require.Nil(t, err)
require.Equal(t, test.wantOpts, o.Options)
}
})
}
......
......@@ -10,7 +10,6 @@ import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
......@@ -81,6 +80,6 @@ func startSmartHTTPServer(t testing.TB, s gitalypb.SmartHTTPServiceServer) (stri
return fmt.Sprintf("%s://%s", ln.Addr().Network(), ln.Addr().String()), func() {
srv.GracefulStop()
assert.NoError(t, os.RemoveAll(tmp), "error removing temp dir %q", tmp)
require.NoError(t, os.RemoveAll(tmp), "error removing temp dir %q", tmp)
}
}
......@@ -7,7 +7,6 @@ import (
"testing"
"testing/iotest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
......@@ -27,7 +26,7 @@ func TestCountingResponseWriterStatus(t *testing.T) {
crw := NewCountingResponseWriter(&testResponseWriter{})
crw.WriteHeader(123)
crw.WriteHeader(456)
assert.Equal(t, 123, crw.Status())
require.Equal(t, 123, crw.Status())
}
func TestCountingResponseWriterCount(t *testing.T) {
......@@ -36,7 +35,7 @@ func TestCountingResponseWriterCount(t *testing.T) {
_, err := crw.Write(bytes.Repeat([]byte{'.'}, n))
require.NoError(t, err)
}
assert.Equal(t, int64(63), crw.Count())
require.Equal(t, int64(63), crw.Count())
}
func TestCountingResponseWriterWrite(t *testing.T) {
......@@ -47,5 +46,5 @@ func TestCountingResponseWriterWrite(t *testing.T) {
_, err := io.Copy(crw, iotest.OneByteReader(bytes.NewReader(testData)))
require.NoError(t, err)
assert.Equal(t, string(testData), string(trw.data))
require.Equal(t, string(testData), string(trw.data))
}
......@@ -9,7 +9,6 @@ import (
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
......@@ -39,7 +38,7 @@ func TestFixRemoteAddr(t *testing.T) {
FixRemoteAddr(req)
assert.Equal(t, tc.expected, req.RemoteAddr)
require.Equal(t, tc.expected, req.RemoteAddr)
}
}
......@@ -92,8 +91,8 @@ func TestReadRequestBody(t *testing.T) {
req, _ := http.NewRequest("POST", "/test", bytes.NewBuffer(data))
result, err := ReadRequestBody(rw, req, 1000)
assert.NoError(t, err)
assert.Equal(t, data, result)
require.NoError(t, err)
require.Equal(t, data, result)
}
func TestReadRequestBodyLimit(t *testing.T) {
......@@ -102,7 +101,7 @@ func TestReadRequestBodyLimit(t *testing.T) {
req, _ := http.NewRequest("POST", "/test", bytes.NewBuffer(data))
_, err := ReadRequestBody(rw, req, 2)
assert.Error(t, err)
require.Error(t, err)
}
func TestCloneRequestWithBody(t *testing.T) {
......@@ -111,26 +110,26 @@ func TestCloneRequestWithBody(t *testing.T) {
req, _ := http.NewRequest("POST", "/test", bytes.NewBuffer(input))
newReq := CloneRequestWithNewBody(req, newInput)
assert.NotEqual(t, req, newReq)
assert.NotEqual(t, req.Body, newReq.Body)
assert.NotEqual(t, len(newInput), newReq.ContentLength)
require.NotEqual(t, req, newReq)
require.NotEqual(t, req.Body, newReq.Body)
require.NotEqual(t, len(newInput), newReq.ContentLength)
var buffer bytes.Buffer
io.Copy(&buffer, newReq.Body)
assert.Equal(t, newInput, buffer.Bytes())
require.Equal(t, newInput, buffer.Bytes())
}
func TestApplicationJson(t *testing.T) {
req, _ := http.NewRequest("POST", "/test", nil)
req.Header.Set("Content-Type", "application/json")
assert.True(t, IsApplicationJson(req), "expected to match 'application/json' as 'application/json'")
require.True(t, IsApplicationJson(req), "expected to match 'application/json' as 'application/json'")
req.Header.Set("Content-Type", "application/json; charset=utf-8")
assert.True(t, IsApplicationJson(req), "expected to match 'application/json; charset=utf-8' as 'application/json'")
require.True(t, IsApplicationJson(req), "expected to match 'application/json; charset=utf-8' as 'application/json'")
req.Header.Set("Content-Type", "text/plain")
assert.False(t, IsApplicationJson(req), "expected not to match 'text/plain' as 'application/json'")
require.False(t, IsApplicationJson(req), "expected not to match 'text/plain' as 'application/json'")
}
func TestFail500WorksWithNils(t *testing.T) {
......@@ -140,8 +139,8 @@ func TestFail500WorksWithNils(t *testing.T) {
Fail500(w, nil, nil)
assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.Equal(t, "Internal server error\n", body.String())
require.Equal(t, http.StatusInternalServerError, w.Code)
require.Equal(t, "Internal server error\n", body.String())
}
func TestLogError(t *testing.T) {
......
......@@ -6,7 +6,7 @@ import (
"time"
"github.com/rafaeljusto/redigomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
......@@ -76,8 +76,8 @@ func TestWatchKeySeenChange(t *testing.T) {
go func() {
val, err := WatchKey(runnerKey, "something", time.Second)
assert.NoError(t, err, "Expected no error")
assert.Equal(t, WatchKeyStatusSeenChange, val, "Expected value to change")
require.NoError(t, err, "Expected no error")
require.Equal(t, WatchKeyStatusSeenChange, val, "Expected value to change")
wg.Done()
}()
......@@ -96,8 +96,8 @@ func TestWatchKeyNoChange(t *testing.T) {
go func() {
val, err := WatchKey(runnerKey, "something", time.Second)
assert.NoError(t, err, "Expected no error")
assert.Equal(t, WatchKeyStatusNoChange, val, "Expected notification without change to value")
require.NoError(t, err, "Expected no error")
require.Equal(t, WatchKeyStatusNoChange, val, "Expected notification without change to value")
wg.Done()
}()
......@@ -112,8 +112,8 @@ func TestWatchKeyTimeout(t *testing.T) {
conn.Command("GET", runnerKey).Expect("something")
val, err := WatchKey(runnerKey, "something", time.Millisecond)
assert.NoError(t, err, "Expected no error")
assert.Equal(t, WatchKeyStatusTimeout, val, "Expected value to not change")
require.NoError(t, err, "Expected no error")
require.Equal(t, WatchKeyStatusTimeout, val, "Expected value to not change")
// Clean up watchers since Process isn't doing that for us (not running)
deleteWatchers(runnerKey)
......@@ -126,8 +126,8 @@ func TestWatchKeyAlreadyChanged(t *testing.T) {
conn.Command("GET", runnerKey).Expect("somethingelse")
val, err := WatchKey(runnerKey, "something", time.Second)
assert.NoError(t, err, "Expected no error")
assert.Equal(t, WatchKeyStatusAlreadyChanged, val, "Expected value to have already changed")
require.NoError(t, err, "Expected no error")
require.Equal(t, WatchKeyStatusAlreadyChanged, val, "Expected value to have already changed")
// Clean up watchers since Process isn't doing that for us (not running)
deleteWatchers(runnerKey)
......@@ -151,8 +151,8 @@ func TestWatchKeyMassivelyParallel(t *testing.T) {
for i := 0; i < runTimes; i++ {
go func() {
val, err := WatchKey(runnerKey, "something", time.Second)
assert.NoError(t, err, "Expected no error")
assert.Equal(t, WatchKeyStatusSeenChange, val, "Expected value to change")
require.NoError(t, err, "Expected no error")
require.Equal(t, WatchKeyStatusSeenChange, val, "Expected value to change")
wg.Done()
}()
}
......
......@@ -7,7 +7,7 @@ import (
"github.com/gomodule/redigo/redis"
"github.com/rafaeljusto/redigomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/config"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
......@@ -16,12 +16,12 @@ import (
func mockRedisServer(t *testing.T, connectReceived *bool) string {
ln, err := net.Listen("tcp", "127.0.0.1:0")
assert.Nil(t, err)
require.Nil(t, err)
go func() {
defer ln.Close()
conn, err := ln.Accept()
assert.Nil(t, err)
require.Nil(t, err)
*connectReceived = true
conn.Write([]byte("OK\n"))
}()
......@@ -68,10 +68,10 @@ func TestDefaultDialFunc(t *testing.T) {
dialer := DefaultDialFunc(cfg, true)
conn, err := dialer()
assert.Nil(t, err)
require.Nil(t, err)
conn.Receive()
assert.True(t, connectReceived)
require.True(t, connectReceived)
})
}
}
......@@ -79,17 +79,18 @@ func TestDefaultDialFunc(t *testing.T) {
func TestConfigureNoConfig(t *testing.T) {
pool = nil
Configure(nil, nil)
assert.Nil(t, pool, "Pool should be nil")
require.Nil(t, pool, "Pool should be nil")
}
func TestConfigureMinimalConfig(t *testing.T) {
cfg := &config.RedisConfig{URL: config.TomlURL{}, Password: ""}
Configure(cfg, DefaultDialFunc)
if assert.NotNil(t, pool, "Pool should not be nil") {
assert.Equal(t, 1, pool.MaxIdle)
assert.Equal(t, 1, pool.MaxActive)
assert.Equal(t, 3*time.Minute, pool.IdleTimeout)
}
require.NotNil(t, pool, "Pool should not be nil")
require.Equal(t, 1, pool.MaxIdle)
require.Equal(t, 1, pool.MaxActive)
require.Equal(t, 3*time.Minute, pool.IdleTimeout)
pool = nil
}
......@@ -104,24 +105,25 @@ func TestConfigureFullConfig(t *testing.T) {
ReadTimeout: &r,
}
Configure(cfg, DefaultDialFunc)
if assert.NotNil(t, pool, "Pool should not be nil") {
assert.Equal(t, i, pool.MaxIdle)
assert.Equal(t, a, pool.MaxActive)
assert.Equal(t, 3*time.Minute, pool.IdleTimeout)
}
require.NotNil(t, pool, "Pool should not be nil")
require.Equal(t, i, pool.MaxIdle)
require.Equal(t, a, pool.MaxActive)
require.Equal(t, 3*time.Minute, pool.IdleTimeout)
pool = nil
}
func TestGetConnFail(t *testing.T) {
conn := Get()
assert.Nil(t, conn, "Expected `conn` to be nil")
require.Nil(t, conn, "Expected `conn` to be nil")
}
func TestGetConnPass(t *testing.T) {
_, teardown := setupMockPool()
defer teardown()
conn := Get()
assert.NotNil(t, conn, "Expected `conn` to be non-nil")
require.NotNil(t, conn, "Expected `conn` to be non-nil")
}
func TestGetStringPass(t *testing.T) {
......@@ -129,22 +131,22 @@ func TestGetStringPass(t *testing.T) {
defer teardown()
conn.Command("GET", "foobar").Expect("baz")
str, err := GetString("foobar")
if assert.NoError(t, err, "Expected `err` to be nil") {
var value string
assert.IsType(t, value, str, "Expected value to be a string")
assert.Equal(t, "baz", str, "Expected it to be equal")
}
require.NoError(t, err, "Expected `err` to be nil")
var value string
require.IsType(t, value, str, "Expected value to be a string")
require.Equal(t, "baz", str, "Expected it to be equal")
}
func TestGetStringFail(t *testing.T) {
_, err := GetString("foobar")
assert.Error(t, err, "Expected error when not connected to redis")
require.Error(t, err, "Expected error when not connected to redis")
}
func TestSentinelConnNoSentinel(t *testing.T) {
s := sentinelConn("", []config.TomlURL{})
assert.Nil(t, s, "Sentinel without urls should return nil")
require.Nil(t, s, "Sentinel without urls should return nil")
}
func TestSentinelConnDialURL(t *testing.T) {
......@@ -173,18 +175,18 @@ func TestSentinelConnDialURL(t *testing.T) {
}
s := sentinelConn("foobar", sentinelUrls)
assert.Equal(t, len(addrs), len(s.Addrs))
require.Equal(t, len(addrs), len(s.Addrs))
for i := range addrs {
assert.Equal(t, addrs[i], s.Addrs[i])
require.Equal(t, addrs[i], s.Addrs[i])
}
conn, err := s.Dial(s.Addrs[0])
assert.Nil(t, err)
require.Nil(t, err)
conn.Receive()
assert.True(t, connectReceived)
require.True(t, connectReceived)
})
}
}
......@@ -199,21 +201,21 @@ func TestSentinelConnTwoURLs(t *testing.T) {
}
s := sentinelConn("foobar", sentinelUrls)
assert.Equal(t, len(addrs), len(s.Addrs))
require.Equal(t, len(addrs), len(s.Addrs))
for i := range addrs {
assert.Equal(t, addrs[i], s.Addrs[i])
require.Equal(t, addrs[i], s.Addrs[i])
}
}
func TestDialOptionsBuildersPassword(t *testing.T) {
dopts := dialOptionsBuilder(&config.RedisConfig{Password: "foo"}, false)
assert.Equal(t, 1, len(dopts))
require.Equal(t, 1, len(dopts))
}
func TestDialOptionsBuildersSetTimeouts(t *testing.T) {
dopts := dialOptionsBuilder(nil, true)
assert.Equal(t, 2, len(dopts))
require.Equal(t, 2, len(dopts))
}
func TestDialOptionsBuildersSetTimeoutsConfig(t *testing.T) {
......@@ -222,11 +224,11 @@ func TestDialOptionsBuildersSetTimeoutsConfig(t *testing.T) {
WriteTimeout: &config.TomlDuration{Duration: time.Second * time.Duration(15)},
}
dopts := dialOptionsBuilder(cfg, true)
assert.Equal(t, 2, len(dopts))
require.Equal(t, 2, len(dopts))
}
func TestDialOptionsBuildersSelectDB(t *testing.T) {
db := 3
dopts := dialOptionsBuilder(&config.RedisConfig{DB: &db}, false)
assert.Equal(t, 1, len(dopts))
require.Equal(t, 1, len(dopts))
}
......@@ -11,7 +11,6 @@ import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/zipartifacts"
......@@ -99,5 +98,5 @@ func TestErrNotAZip(t *testing.T) {
defer cancel()
_, err = zipartifacts.OpenArchive(ctx, f.Name())
assert.Equal(t, zipartifacts.ErrorCode[zipartifacts.CodeNotZip], err, "OpenArchive requires a zip file")
require.Equal(t, zipartifacts.ErrorCode[zipartifacts.CodeNotZip], err, "OpenArchive requires a zip file")
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment