tpl/data: Revise error handling in getJSON and getCSV

The most important part being: Log ERROR, but do not stop the build on remote errors.

Fixes #5076
This commit is contained in:
Bjørn Erik Pedersen
2018-09-10 21:02:18 +02:00
parent 4f72e79120
commit 43d446522a
3 changed files with 56 additions and 28 deletions

View File

@@ -21,6 +21,8 @@ import (
"strings"
"testing"
jww "github.com/spf13/jwalterweatherman"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -28,8 +30,6 @@ import (
func TestGetCSV(t *testing.T) {
t.Parallel()
ns := newTestNs()
for i, test := range []struct {
sep string
url string
@@ -78,6 +78,8 @@ func TestGetCSV(t *testing.T) {
} {
msg := fmt.Sprintf("Test %d", i)
ns := newTestNs()
// Setup HTTP test server
var srv *httptest.Server
srv, ns.client = getTestServer(func(w http.ResponseWriter, r *http.Request) {
@@ -108,11 +110,14 @@ func TestGetCSV(t *testing.T) {
// Get on with it
got, err := ns.GetCSV(test.sep, test.url)
require.NoError(t, err, msg)
if _, ok := test.expect.(bool); ok {
assert.Error(t, err, msg)
require.Equal(t, 1, int(ns.deps.Log.LogCountForLevelsGreaterThanorEqualTo(jww.LevelError)))
require.Nil(t, got)
continue
}
require.NoError(t, err, msg)
require.Equal(t, 0, int(ns.deps.Log.LogCountForLevelsGreaterThanorEqualTo(jww.LevelError)))
require.NotNil(t, got, msg)
assert.EqualValues(t, test.expect, got, msg)
@@ -122,8 +127,6 @@ func TestGetCSV(t *testing.T) {
func TestGetJSON(t *testing.T) {
t.Parallel()
ns := newTestNs()
for i, test := range []struct {
url string
content string
@@ -137,12 +140,12 @@ func TestGetJSON(t *testing.T) {
{
`http://malformed/`,
`{gomeetup:["Sydney","San Francisco","Stockholm"]}`,
false,
jww.LevelError,
},
{
`http://nofound/404`,
``,
false,
jww.LevelError,
},
// Locals
{
@@ -153,10 +156,12 @@ func TestGetJSON(t *testing.T) {
{
"fail/no-file",
"",
false,
jww.LevelError,
},
} {
msg := fmt.Sprintf("Test %d", i)
ns := newTestNs()
// Setup HTTP test server
var srv *httptest.Server
@@ -189,10 +194,18 @@ func TestGetJSON(t *testing.T) {
got, err := ns.GetJSON(test.url)
if _, ok := test.expect.(bool); ok {
assert.Error(t, err, msg)
require.Error(t, err, msg)
continue
}
if errLevel, ok := test.expect.(jww.Threshold); ok {
logCount := ns.deps.Log.LogCountForLevelsGreaterThanorEqualTo(errLevel)
require.True(t, logCount >= 1, fmt.Sprintf("got log count %d", logCount))
continue
}
require.NoError(t, err, msg)
require.Equal(t, 0, int(ns.deps.Log.LogCountForLevelsGreaterThanorEqualTo(jww.LevelError)), msg)
require.NotNil(t, got, msg)
assert.EqualValues(t, test.expect, got, msg)