@@ -6,6 +6,7 @@ package charset
66import (
77 "bytes"
88 "io"
9+ "os"
910 "strings"
1011 "testing"
1112
@@ -15,20 +16,12 @@ import (
1516 "github.com/stretchr/testify/assert"
1617)
1718
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- }
19+ func TestMain (m * testing.M ) {
2320 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- }
21+ for i , charset := range setting .Repository .DetectedCharsetsOrder {
22+ setting .Repository .DetectedCharsetScore [strings .ToLower (charset )] = i
3123 }
24+ os .Exit (m .Run ())
3225}
3326
3427func TestMaybeRemoveBOM (t * testing.T ) {
@@ -40,8 +33,6 @@ func TestMaybeRemoveBOM(t *testing.T) {
4033}
4134
4235func TestToUTF8 (t * testing.T ) {
43- resetDefaultCharsetsOrder ()
44-
4536 // Note: golang compiler seems so behave differently depending on the current
4637 // locale, so some conversions might behave differently. For that reason, we don't
4738 // depend on particular conversions but in expected behaviors.
@@ -97,7 +88,6 @@ func TestToUTF8(t *testing.T) {
9788}
9889
9990func TestToUTF8WithFallback (t * testing.T ) {
100- resetDefaultCharsetsOrder ()
10191 // "ABC"
10292 res := ToUTF8WithFallback ([]byte {0x41 , 0x42 , 0x43 }, ConvertOpts {})
10393 assert .Equal (t , []byte {0x41 , 0x42 , 0x43 }, res )
@@ -144,8 +134,6 @@ func TestToUTF8WithFallback(t *testing.T) {
144134}
145135
146136func TestToUTF8DropErrors (t * testing.T ) {
147- resetDefaultCharsetsOrder ()
148-
149137 // "ABC"
150138 res := ToUTF8DropErrors ([]byte {0x41 , 0x42 , 0x43 })
151139 assert .Equal (t , []byte {0x41 , 0x42 , 0x43 }, res )
@@ -187,12 +175,17 @@ func TestToUTF8DropErrors(t *testing.T) {
187175}
188176
189177func TestDetectEncoding (t * testing.T ) {
190- resetDefaultCharsetsOrder ()
191178 testSuccess := func (b []byte , expected string ) {
192179 encoding , err := DetectEncoding (b )
193180 assert .NoError (t , err )
194181 assert .Equal (t , expected , encoding )
195182 }
183+
184+ // invalid bytes
185+ encoding , err := DetectEncoding ([]byte {0xfa })
186+ assert .Error (t , err )
187+ assert .Equal (t , "UTF-8" , encoding )
188+
196189 // utf-8
197190 b := []byte ("just some ascii" )
198191 testSuccess (b , "UTF-8" )
@@ -207,21 +200,12 @@ func TestDetectEncoding(t *testing.T) {
207200
208201 // iso-8859-1: d<accented e>cor<newline>
209202 b = []byte {0x44 , 0xe9 , 0x63 , 0x6f , 0x72 , 0x0a }
210- encoding , err : = DetectEncoding (b )
203+ encoding , err = DetectEncoding (b )
211204 assert .NoError (t , err )
212205 assert .Contains (t , encoding , "ISO-8859-1" )
213206
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 )
207+ defer test .MockVariableValue (& setting .Repository .AnsiCharset , "MyEncoding" )()
208+ testSuccess (b , "MyEncoding" )
225209}
226210
227211func stringMustStartWith (t * testing.T , expected string , value []byte ) {
@@ -233,7 +217,6 @@ func stringMustEndWith(t *testing.T, expected string, value []byte) {
233217}
234218
235219func TestToUTF8WithFallbackReader (t * testing.T ) {
236- resetDefaultCharsetsOrder ()
237220 test .MockVariableValue (& ToUTF8WithFallbackReaderPrefetchSize )
238221
239222 block := "aá啊🤔"
0 commit comments