Fix
This commit is contained in:
1 parent
486502852e
commit
0e48ea427b
23 files changed
+386
-78
No files matched your search
@@ -874,6 +874,11 @@ func writeXMLProduct(w io.Writer, item string, tpl exportTemplate, p exportProdu
|
||||
}
|
||||
|
||||
func streamCSV(w io.Writer, seq exportProductSeq, tpl exportTemplate) (int, error) {
|
||||
// UTF-8 BOM makes Excel detect Unicode without its locale-dependent CSV import.
|
||||
// The BOM is emitted once per file and does not change the exported field data.
|
||||
if _, err := io.WriteString(w, "\ufeff"); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
cw := csv.NewWriter(w)
|
||||
headers := make([]string, len(tpl.Fields))
|
||||
for i, f := range tpl.Fields {
|
||||
|
||||
@@ -335,7 +335,10 @@ func TestStreamCSVUsesProductSeqWithoutCollecting(t *testing.T) {
|
||||
if n != 1 {
|
||||
t.Fatalf("count=%d", n)
|
||||
}
|
||||
lines := strings.Split(strings.TrimSpace(buf.String()), "\n")
|
||||
if !strings.HasPrefix(buf.String(), "\ufeff") {
|
||||
t.Fatalf("downloadable CSV must start with UTF-8 BOM: %q", buf.Bytes()[:min(buf.Len(), 8)])
|
||||
}
|
||||
lines := strings.Split(strings.TrimSpace(strings.TrimPrefix(buf.String(), "\ufeff")), "\n")
|
||||
if len(lines) != 2 {
|
||||
t.Fatalf("lines=%v", lines)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package feeds
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"strings"
|
||||
"testing"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func TestExportUnicodeSurvivesEveryFormat(t *testing.T) {
|
||||
name := "Črpalka, šoba Žuža – München 東京 Привет"
|
||||
tpl := exportTemplate{Fields: []exportField{{Key: "Naziv č š ž", Source: "name"}}}
|
||||
seq := func(yield func(exportProduct) error) error {
|
||||
return yield(exportProduct{Name: &name})
|
||||
}
|
||||
for _, format := range []string{"csv", "xml", "json"} {
|
||||
var buf bytes.Buffer
|
||||
var n int
|
||||
var err error
|
||||
switch format {
|
||||
case "csv":
|
||||
n, err = streamCSV(&buf, seq, tpl)
|
||||
case "xml":
|
||||
n, err = streamXML(&buf, seq, tpl, "Izvoz")
|
||||
case "json":
|
||||
n, err = streamJSON(&buf, seq, tpl)
|
||||
}
|
||||
if err != nil || n != 1 {
|
||||
t.Fatalf("%s: count=%d err=%v", format, n, err)
|
||||
}
|
||||
if !utf8.Valid(buf.Bytes()) {
|
||||
t.Fatalf("%s: invalid UTF-8", format)
|
||||
}
|
||||
switch format {
|
||||
case "csv":
|
||||
if !bytes.HasPrefix(buf.Bytes(), []byte("\ufeff")) {
|
||||
t.Fatal("CSV has no UTF-8 BOM")
|
||||
}
|
||||
r := csv.NewReader(bytes.NewReader(bytes.TrimPrefix(buf.Bytes(), []byte("\ufeff"))))
|
||||
rows, err := r.ReadAll()
|
||||
if err != nil || len(rows) != 2 || rows[0][0] != "Naziv č š ž" || rows[1][0] != name {
|
||||
t.Fatalf("CSV data lost Unicode: %v, %v", rows, err)
|
||||
}
|
||||
case "xml":
|
||||
var out struct {
|
||||
Name string `xml:"Naziv_č_š_ž"`
|
||||
}
|
||||
if !strings.Contains(buf.String(), name) {
|
||||
t.Fatalf("XML missing Unicode: %s", buf.String())
|
||||
}
|
||||
if err := xml.Unmarshal(buf.Bytes(), &out); err != nil {
|
||||
t.Fatalf("XML invalid: %v", err)
|
||||
}
|
||||
case "json":
|
||||
if !json.Valid(buf.Bytes()) || !bytes.Contains(buf.Bytes(), []byte(name)) {
|
||||
t.Fatalf("JSON invalid or data lost: %s", buf.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user