Welcome to mirror list, hosted at ThFree Co, Russian Federation.

template_resources_test.go « tpl - github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3385c5ef7a7454105a427224749d8a9d3ab7dd11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright 2016 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tpl

import (
	"bytes"
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/url"
	"os"
	"strings"
	"testing"
	"time"

	"github.com/spf13/afero"
	"github.com/spf13/hugo/helpers"
	"github.com/spf13/hugo/hugofs"
	"github.com/spf13/viper"
	"github.com/stretchr/testify/assert"
)

func TestScpCache(t *testing.T) {

	tests := []struct {
		path    string
		content []byte
		ignore  bool
	}{
		{"http://Foo.Bar/foo_Bar-Foo", []byte(`T€st Content 123`), false},
		{"fOO,bar:foo%bAR", []byte(`T€st Content 123 fOO,bar:foo%bAR`), false},
		{"FOo/BaR.html", []byte(`FOo/BaR.html T€st Content 123`), false},
		{"трям/трям", []byte(`T€st трям/трям Content 123`), false},
		{"은행", []byte(`T€st C은행ontent 123`), false},
		{"Банковский кассир", []byte(`Банковский кассир T€st Content 123`), false},
		{"Банковский кассир", []byte(`Банковский кассир T€st Content 456`), true},
	}

	fs := new(afero.MemMapFs)

	for _, test := range tests {
		c, err := resGetCache(test.path, fs, test.ignore)
		if err != nil {
			t.Errorf("Error getting cache: %s", err)
		}
		if c != nil {
			t.Errorf("There is content where there should not be anything: %s", string(c))
		}

		err = resWriteCache(test.path, test.content, fs, test.ignore)
		if err != nil {
			t.Errorf("Error writing cache: %s", err)
		}

		c, err = resGetCache(test.path, fs, test.ignore)
		if err != nil {
			t.Errorf("Error getting cache after writing: %s", err)
		}
		if test.ignore {
			if c != nil {
				t.Errorf("Cache ignored but content is not nil: %s", string(c))
			}
		} else {
			if !bytes.Equal(c, test.content) {
				t.Errorf("\nExpected: %s\nActual: %s\n", string(test.content), string(c))
			}
		}
	}
}

func TestScpGetLocal(t *testing.T) {
	fs := new(afero.MemMapFs)
	ps := helpers.FilePathSeparator
	tests := []struct {
		path    string
		content []byte
	}{
		{"testpath" + ps + "test.txt", []byte(`T€st Content 123 fOO,bar:foo%bAR`)},
		{"FOo" + ps + "BaR.html", []byte(`FOo/BaR.html T€st Content 123`)},
		{"трям" + ps + "трям", []byte(`T€st трям/трям Content 123`)},
		{"은행", []byte(`T€st C은행ontent 123`)},
		{"Банковский кассир", []byte(`Банковский кассир T€st Content 123`)},
	}

	for _, test := range tests {
		r := bytes.NewReader(test.content)
		err := helpers.WriteToDisk(test.path, r, fs)
		if err != nil {
			t.Error(err)
		}

		c, err := resGetLocal(test.path, fs)
		if err != nil {
			t.Errorf("Error getting resource content: %s", err)
		}
		if !bytes.Equal(c, test.content) {
			t.Errorf("\nExpected: %s\nActual: %s\n", string(test.content), string(c))
		}
	}

}

func getTestServer(handler func(w http.ResponseWriter, r *http.Request)) (*httptest.Server, *http.Client) {
	testServer := httptest.NewServer(http.HandlerFunc(handler))
	client := &http.Client{
		Transport: &http.Transport{Proxy: func(r *http.Request) (*url.URL, error) {
			// Remove when https://github.com/golang/go/issues/13686 is fixed
			r.Host = "gohugo.io"
			return url.Parse(testServer.URL)
		}},
	}
	return testServer, client
}

func TestScpGetRemote(t *testing.T) {
	fs := new(afero.MemMapFs)

	tests := []struct {
		path    string
		content []byte
		ignore  bool
	}{
		{"http://Foo.Bar/foo_Bar-Foo", []byte(`T€st Content 123`), false},
		{"http://Doppel.Gänger/foo_Bar-Foo", []byte(`T€st Cont€nt 123`), false},
		{"http://Doppel.Gänger/Fizz_Bazz-Foo", []byte(`T€st Банковский кассир Cont€nt 123`), false},
		{"http://Doppel.Gänger/Fizz_Bazz-Bar", []byte(`T€st Банковский кассир Cont€nt 456`), true},
	}

	for _, test := range tests {

		srv, cl := getTestServer(func(w http.ResponseWriter, r *http.Request) {
			w.Write(test.content)
		})
		defer func() { srv.Close() }()

		c, err := resGetRemote(test.path, fs, cl)
		if err != nil {
			t.Errorf("Error getting resource content: %s", err)
		}
		if !bytes.Equal(c, test.content) {
			t.Errorf("\nNet Expected: %s\nNet Actual: %s\n", string(test.content), string(c))
		}
		cc, cErr := resGetCache(test.path, fs, test.ignore)
		if cErr != nil {
			t.Error(cErr)
		}
		if test.ignore {
			if cc != nil {
				t.Errorf("Cache ignored but content is not nil: %s", string(cc))
			}
		} else {
			if !bytes.Equal(cc, test.content) {
				t.Errorf("\nCache Expected: %s\nCache Actual: %s\n", string(test.content), string(cc))
			}
		}
	}
}

func TestParseCSV(t *testing.T) {

	tests := []struct {
		csv []byte
		sep string
		exp string
		err bool
	}{
		{[]byte("a,b,c\nd,e,f\n"), "", "", true},
		{[]byte("a,b,c\nd,e,f\n"), "~/", "", true},
		{[]byte("a,b,c\nd,e,f"), "|", "a,b,cd,e,f", false},
		{[]byte("q,w,e\nd,e,f"), ",", "qwedef", false},
		{[]byte("a|b|c\nd|e|f|g"), "|", "abcdefg", true},
		{[]byte("z|y|c\nd|e|f"), "|", "zycdef", false},
	}
	for _, test := range tests {
		csv, err := parseCSV(test.csv, test.sep)
		if test.err && err == nil {
			t.Error("Expecting an error")
		}
		if test.err {
			continue
		}
		if !test.err && err != nil {
			t.Error(err)
		}

		act := ""
		for _, v := range csv {
			act = act + strings.Join(v, "")
		}

		if act != test.exp {
			t.Errorf("\nExpected: %s\nActual: %s\n%#v\n", test.exp, act, csv)
		}

	}
}

// https://twitter.com/francesc/status/603066617124126720
// for the construct: defer testRetryWhenDone().Reset()
type wd struct {
	Reset func()
}

func testRetryWhenDone() wd {
	cd := viper.GetString("cacheDir")
	viper.Set("cacheDir", helpers.GetTempDir("", hugofs.Source()))
	var tmpSleep time.Duration
	tmpSleep, resSleep = resSleep, time.Millisecond
	return wd{func() {
		viper.Set("cacheDir", cd)
		resSleep = tmpSleep
	}}
}

func TestGetJSONFailParse(t *testing.T) {
	defer testRetryWhenDone().Reset()

	reqCount := 0
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if reqCount > 0 {
			w.Header().Add("Content-type", "application/json")
			fmt.Fprintln(w, `{"gomeetup":["Sydney", "San Francisco", "Stockholm"]}`)
		} else {
			w.WriteHeader(http.StatusInternalServerError)
			fmt.Fprintln(w, `ERROR 500`)
		}
		reqCount++
	}))
	defer ts.Close()
	url := ts.URL + "/test.json"
	defer os.Remove(getCacheFileID(url))

	want := map[string]interface{}{"gomeetup": []interface{}{"Sydney", "San Francisco", "Stockholm"}}
	have := getJSON(url)
	assert.NotNil(t, have)
	if have != nil {
		assert.EqualValues(t, want, have)
	}
}

func TestGetCSVFailParseSep(t *testing.T) {
	defer testRetryWhenDone().Reset()

	reqCount := 0
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if reqCount > 0 {
			w.Header().Add("Content-type", "application/json")
			fmt.Fprintln(w, `gomeetup,city`)
			fmt.Fprintln(w, `yes,Sydney`)
			fmt.Fprintln(w, `yes,San Francisco`)
			fmt.Fprintln(w, `yes,Stockholm`)
		} else {
			w.WriteHeader(http.StatusInternalServerError)
			fmt.Fprintln(w, `ERROR 500`)
		}
		reqCount++
	}))
	defer ts.Close()
	url := ts.URL + "/test.csv"
	defer os.Remove(getCacheFileID(url))

	want := [][]string{{"gomeetup", "city"}, {"yes", "Sydney"}, {"yes", "San Francisco"}, {"yes", "Stockholm"}}
	have := getCSV(",", url)
	assert.NotNil(t, have)
	if have != nil {
		assert.EqualValues(t, want, have)
	}
}

func TestGetCSVFailParse(t *testing.T) {
	defer testRetryWhenDone().Reset()

	reqCount := 0
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Add("Content-type", "application/json")
		if reqCount > 0 {
			fmt.Fprintln(w, `gomeetup,city`)
			fmt.Fprintln(w, `yes,Sydney`)
			fmt.Fprintln(w, `yes,San Francisco`)
			fmt.Fprintln(w, `yes,Stockholm`)
		} else {
			fmt.Fprintln(w, `gomeetup,city`)
			fmt.Fprintln(w, `yes,Sydney,Bondi,`) // wrong number of fields in line
			fmt.Fprintln(w, `yes,San Francisco`)
			fmt.Fprintln(w, `yes,Stockholm`)
		}
		reqCount++
	}))
	defer ts.Close()
	url := ts.URL + "/test.csv"
	defer os.Remove(getCacheFileID(url))

	want := [][]string{{"gomeetup", "city"}, {"yes", "Sydney"}, {"yes", "San Francisco"}, {"yes", "Stockholm"}}
	have := getCSV(",", url)
	assert.NotNil(t, have)
	if have != nil {
		assert.EqualValues(t, want, have)
	}
}