From a76c2a8c22a44bbaada4434ff5ecfaaf54de7d44 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Mon, 20 Nov 2017 14:35:11 -0800 Subject: Improve OPML import/export --- reader/opml/parser.go | 5 +++-- reader/opml/parser_test.go | 2 +- reader/opml/serializer.go | 7 ++----- reader/opml/serializer_test.go | 21 ++++++++++++++++----- 4 files changed, 22 insertions(+), 13 deletions(-) (limited to 'reader/opml') diff --git a/reader/opml/parser.go b/reader/opml/parser.go index 5d8babd..2270641 100644 --- a/reader/opml/parser.go +++ b/reader/opml/parser.go @@ -6,12 +6,13 @@ package opml import ( "encoding/xml" - "fmt" "io" + "github.com/miniflux/miniflux2/errors" "golang.org/x/net/html/charset" ) +// Parse reads an OPML file and returns a SubcriptionList. func Parse(data io.Reader) (SubcriptionList, error) { opml := new(Opml) decoder := xml.NewDecoder(data) @@ -19,7 +20,7 @@ func Parse(data io.Reader) (SubcriptionList, error) { err := decoder.Decode(opml) if err != nil { - return nil, fmt.Errorf("Unable to parse OPML file: %v\n", err) + return nil, errors.NewLocalizedError("Unable to parse OPML file: %v", err) } return opml.Transform(), nil diff --git a/reader/opml/parser_test.go b/reader/opml/parser_test.go index 02543df..95a832b 100644 --- a/reader/opml/parser_test.go +++ b/reader/opml/parser_test.go @@ -133,6 +133,6 @@ func TestParseInvalidXML(t *testing.T) { _, err := Parse(bytes.NewBufferString(data)) if err == nil { - t.Error(err) + t.Error("Parse should generate an error") } } diff --git a/reader/opml/serializer.go b/reader/opml/serializer.go index 20c7046..3ca859a 100644 --- a/reader/opml/serializer.go +++ b/reader/opml/serializer.go @@ -11,6 +11,7 @@ import ( "log" ) +// Serialize returns a SubcriptionList in OPML format. func Serialize(subscriptions SubcriptionList) string { var b bytes.Buffer writer := bufio.NewWriter(&b) @@ -34,7 +35,7 @@ func Serialize(subscriptions SubcriptionList) string { } encoder := xml.NewEncoder(writer) - encoder.Indent(" ", " ") + encoder.Indent(" ", " ") if err := encoder.Encode(opml); err != nil { log.Println(err) return "" @@ -47,10 +48,6 @@ func groupSubscriptionsByFeed(subscriptions SubcriptionList) map[string]Subcript groups := make(map[string]SubcriptionList) for _, subscription := range subscriptions { - // if subs, ok := groups[subscription.CategoryName]; !ok { - // groups[subscription.CategoryName] = SubcriptionList{} - // } - groups[subscription.CategoryName] = append(groups[subscription.CategoryName], subscription) } diff --git a/reader/opml/serializer_test.go b/reader/opml/serializer_test.go index b1ef2a6..dd431d9 100644 --- a/reader/opml/serializer_test.go +++ b/reader/opml/serializer_test.go @@ -4,8 +4,11 @@ package opml -import "testing" -import "bytes" +import ( + "bytes" + "fmt" + "testing" +) func TestSerialize(t *testing.T) { var subscriptions SubcriptionList @@ -14,6 +17,7 @@ func TestSerialize(t *testing.T) { subscriptions = append(subscriptions, &Subcription{Title: "Feed 3", FeedURL: "http://example.org/feed/3", SiteURL: "http://example.org/3", CategoryName: "Category 2"}) output := Serialize(subscriptions) + fmt.Println(output) feeds, err := Parse(bytes.NewBufferString(output)) if err != nil { t.Error(err) @@ -23,9 +27,16 @@ func TestSerialize(t *testing.T) { t.Errorf("Wrong number of subscriptions: %d instead of %d", len(feeds), 3) } - for i := 0; i < len(feeds); i++ { - if !feeds[i].Equals(subscriptions[i]) { - t.Errorf(`Subscription are different: "%v" vs "%v"`, subscriptions[i], feeds[i]) + found := false + for _, feed := range feeds { + if feed.Title == "Feed 1" && feed.CategoryName == "Category 1" && + feed.FeedURL == "http://example.org/feed/1" && feed.SiteURL == "http://example.org/1" { + found = true + break } } + + if !found { + t.Error("Serialized feed is incorrect") + } } -- cgit v1.2.3