Skip to content

Commit ba8cc1b

Browse files
committed
fix test
1 parent b294923 commit ba8cc1b

File tree

1 file changed

+9
-35
lines changed

1 file changed

+9
-35
lines changed

modules/charset/charset_test.go

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,6 @@ import (
1515
"github.com/stretchr/testify/assert"
1616
)
1717

18-
func resetDefaultCharsetsOrder() {
19-
defaultDetectedCharsetsOrder := make([]string, 0, len(setting.Repository.DetectedCharsetsOrder))
20-
for _, charset := range setting.Repository.DetectedCharsetsOrder {
21-
defaultDetectedCharsetsOrder = append(defaultDetectedCharsetsOrder, strings.ToLower(strings.TrimSpace(charset)))
22-
}
23-
setting.Repository.DetectedCharsetScore = map[string]int{}
24-
i := 0
25-
for _, charset := range defaultDetectedCharsetsOrder {
26-
canonicalCharset := strings.ToLower(strings.TrimSpace(charset))
27-
if _, has := setting.Repository.DetectedCharsetScore[canonicalCharset]; !has {
28-
setting.Repository.DetectedCharsetScore[canonicalCharset] = i
29-
i++
30-
}
31-
}
32-
}
33-
3418
func TestMaybeRemoveBOM(t *testing.T) {
3519
res := maybeRemoveBOM([]byte{0xc3, 0xa1, 0xc3, 0xa9, 0xc3, 0xad, 0xc3, 0xb3, 0xc3, 0xba}, ConvertOpts{})
3620
assert.Equal(t, []byte{0xc3, 0xa1, 0xc3, 0xa9, 0xc3, 0xad, 0xc3, 0xb3, 0xc3, 0xba}, res)
@@ -40,8 +24,6 @@ func TestMaybeRemoveBOM(t *testing.T) {
4024
}
4125

4226
func TestToUTF8(t *testing.T) {
43-
resetDefaultCharsetsOrder()
44-
4527
// Note: golang compiler seems so behave differently depending on the current
4628
// locale, so some conversions might behave differently. For that reason, we don't
4729
// depend on particular conversions but in expected behaviors.
@@ -97,7 +79,6 @@ func TestToUTF8(t *testing.T) {
9779
}
9880

9981
func TestToUTF8WithFallback(t *testing.T) {
100-
resetDefaultCharsetsOrder()
10182
// "ABC"
10283
res := ToUTF8WithFallback([]byte{0x41, 0x42, 0x43}, ConvertOpts{})
10384
assert.Equal(t, []byte{0x41, 0x42, 0x43}, res)
@@ -144,8 +125,6 @@ func TestToUTF8WithFallback(t *testing.T) {
144125
}
145126

146127
func TestToUTF8DropErrors(t *testing.T) {
147-
resetDefaultCharsetsOrder()
148-
149128
// "ABC"
150129
res := ToUTF8DropErrors([]byte{0x41, 0x42, 0x43})
151130
assert.Equal(t, []byte{0x41, 0x42, 0x43}, res)
@@ -187,12 +166,17 @@ func TestToUTF8DropErrors(t *testing.T) {
187166
}
188167

189168
func TestDetectEncoding(t *testing.T) {
190-
resetDefaultCharsetsOrder()
191169
testSuccess := func(b []byte, expected string) {
192170
encoding, err := DetectEncoding(b)
193171
assert.NoError(t, err)
194172
assert.Equal(t, expected, encoding)
195173
}
174+
175+
// invalid bytes
176+
encoding, err := DetectEncoding([]byte{0xfa})
177+
assert.Error(t, err)
178+
assert.Equal(t, "UTF-8", encoding)
179+
196180
// utf-8
197181
b := []byte("just some ascii")
198182
testSuccess(b, "UTF-8")
@@ -207,21 +191,12 @@ func TestDetectEncoding(t *testing.T) {
207191

208192
// iso-8859-1: d<accented e>cor<newline>
209193
b = []byte{0x44, 0xe9, 0x63, 0x6f, 0x72, 0x0a}
210-
encoding, err := DetectEncoding(b)
194+
encoding, err = DetectEncoding(b)
211195
assert.NoError(t, err)
212196
assert.Contains(t, encoding, "ISO-8859-1")
213197

214-
old := setting.Repository.AnsiCharset
215-
setting.Repository.AnsiCharset = "placeholder"
216-
defer func() {
217-
setting.Repository.AnsiCharset = old
218-
}()
219-
testSuccess(b, "placeholder")
220-
221-
// invalid bytes
222-
b = []byte{0xfa}
223-
_, err = DetectEncoding(b)
224-
assert.Error(t, err)
198+
defer test.MockVariableValue(&setting.Repository.AnsiCharset, "MyEncoding")()
199+
testSuccess(b, "MyEncoding")
225200
}
226201

227202
func stringMustStartWith(t *testing.T, expected string, value []byte) {
@@ -233,7 +208,6 @@ func stringMustEndWith(t *testing.T, expected string, value []byte) {
233208
}
234209

235210
func TestToUTF8WithFallbackReader(t *testing.T) {
236-
resetDefaultCharsetsOrder()
237211
test.MockVariableValue(&ToUTF8WithFallbackReaderPrefetchSize)
238212

239213
block := "aá啊🤔"

0 commit comments

Comments
 (0)