"encoding/csv"
"encoding/json"
"errors"
+ "fmt"
"net/http"
"strings"
"time"
"github.com/gohugoio/hugo/deps"
- jww "github.com/spf13/jwalterweatherman"
)
// New returns a new instance of the data-namespaced template functions.
url := strings.Join(urlParts, "")
var clearCacheSleep = func(i int, u string) {
- jww.ERROR.Printf("Retry #%d for %s and sleeping for %s", i, url, resSleep)
+ ns.deps.Log.WARN.Printf("Retry #%d for %s and sleeping for %s", i, url, resSleep)
time.Sleep(resSleep)
deleteCache(url, ns.deps.Fs.Source, ns.deps.Cfg)
}
var req *http.Request
req, err = http.NewRequest("GET", url, nil)
if err != nil {
- jww.ERROR.Printf("Failed to create request for getCSV: %s", err)
- return nil, err
+ return nil, fmt.Errorf("Failed to create request for getCSV for resource %s: %s", url, err)
}
req.Header.Add("Accept", "text/csv")
var c []byte
c, err = ns.getResource(req)
if err != nil {
- jww.ERROR.Printf("Failed to read csv resource %q with error message %s", url, err)
- return nil, err
+ ns.deps.Log.ERROR.Printf("Failed to read CSV resource %q: %s", url, err)
+ return nil, nil
}
if !bytes.Contains(c, []byte(sep)) {
- err = errors.New("Cannot find separator " + sep + " in CSV.")
- return
+ ns.deps.Log.ERROR.Printf("Cannot find separator %s in CSV for %s", sep, url)
+ return nil, nil
}
if d, err = parseCSV(c, sep); err != nil {
- jww.ERROR.Printf("Failed to parse csv file %s with error message %s", url, err)
+ ns.deps.Log.WARN.Printf("Failed to parse CSV file %s: %s", url, err)
clearCacheSleep(i, url)
continue
}
break
}
+
+ if err != nil {
+ ns.deps.Log.ERROR.Printf("Failed to read CSV resource %q: %s", url, err)
+ return nil, nil
+ }
+
return
}
var req *http.Request
req, err = http.NewRequest("GET", url, nil)
if err != nil {
- jww.ERROR.Printf("Failed to create request for getJSON: %s", err)
- return nil, err
+ return nil, fmt.Errorf("Failed to create request for getJSON resource %s: %s", url, err)
}
req.Header.Add("Accept", "application/json")
var c []byte
c, err = ns.getResource(req)
if err != nil {
- jww.ERROR.Printf("Failed to get json resource %s with error message %s", url, err)
- return nil, err
+ ns.deps.Log.ERROR.Printf("Failed to get JSON resource %s: %s", url, err)
+ return nil, nil
}
err = json.Unmarshal(c, &v)
if err != nil {
- jww.ERROR.Printf("Cannot read json from resource %s with error message %s", url, err)
- jww.ERROR.Printf("Retry #%d for %s and sleeping for %s", i, url, resSleep)
+ ns.deps.Log.WARN.Printf("Cannot read JSON from resource %s: %s", url, err)
+ ns.deps.Log.WARN.Printf("Retry #%d for %s and sleeping for %s", i, url, resSleep)
time.Sleep(resSleep)
deleteCache(url, ns.deps.Fs.Source, ns.deps.Cfg)
continue
}
break
}
+
+ if err != nil {
+ ns.deps.Log.ERROR.Printf("Failed to get JSON resource %s: %s", url, err)
+ return nil, nil
+ }
return
}
"strings"
"testing"
+ jww "github.com/spf13/jwalterweatherman"
+
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetCSV(t *testing.T) {
t.Parallel()
- ns := newTestNs()
-
for i, test := range []struct {
sep string
url string
} {
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) {
// 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)
func TestGetJSON(t *testing.T) {
t.Parallel()
- ns := newTestNs()
-
for i, test := range []struct {
url string
content string
{
`http://malformed/`,
`{gomeetup:["Sydney","San Francisco","Stockholm"]}`,
- false,
+ jww.LevelError,
},
{
`http://nofound/404`,
``,
- false,
+ jww.LevelError,
},
// Locals
{
{
"fail/no-file",
"",
- false,
+ jww.LevelError,
},
} {
+
msg := fmt.Sprintf("Test %d", i)
+ ns := newTestNs()
// Setup HTTP test server
var srv *httptest.Server
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)