Skip to content

Commit

Permalink
upgrade version and some lint
Browse files Browse the repository at this point in the history
  • Loading branch information
zapisanchez committed Nov 24, 2023
1 parent 93e5e3d commit 9daa0b6
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 73 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/Arch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: ["master"]
pull_request:
branches: ["master"]

jobs:
build:
runs-on: ubuntu-latest

container:
image: archlinux:latest

steps:
- uses: actions/checkout@v3

- name: Set Up Arch Linux
run: |
pacman -Syy --noconfirm
pacman -Syu --noconfirm
pacman -S --noconfirm base-devel
pacman -S --noconfirm libxml2
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.21"

- name: Test
run: go test ./...
4 changes: 2 additions & 2 deletions clib/clib.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ static inline void MY_xmlFree(void *p) {
// Macro wrapper function. cgo cannot detect function-like macros,
// so this is how we avoid it
static inline xmlError* MY_xmlLastError() {
return xmlGetLastError();
return (xmlError*) xmlGetLastError();
}
// Macro wrapper function. cgo cannot detect function-like macros,
// so this is how we avoid it
static inline xmlError* MY_xmlCtxtLastError(void *ctx) {
return xmlCtxtGetLastError(ctx);
return (xmlError*) xmlCtxtGetLastError(ctx);
}
// Change xmlIndentTreeOutput global, return old value, so caller can
Expand Down
16 changes: 8 additions & 8 deletions dom/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,40 +265,40 @@ func TestDocumentCreateAttributeNS(t *testing.T) {
t.Errorf("Failed to create Element node: %s", err)
return
}
d.SetDocumentElement(elem)
_ = d.SetDocumentElement(elem)

attr, err := d.CreateAttribute("attr", "e & f")
if err != nil {
t.Errorf("Failed to create Attribute node: %s", err)
return
}
elem.AddChild(attr)
_ = elem.AddChild(attr)

if elem.String() != `<foo attr="e &amp; f"/>` {
t.Errorf(`Expected String '<foo attr="e &amp; f"/>', got '%s'`, elem.String())
return
}
elem.RemoveAttribute("attr")
_ = elem.RemoveAttribute("attr")

attr, err = d.CreateAttributeNS("", "attr2", "a & b")
if err != nil {
t.Errorf("Failed to create Attribute node: %s", err)
return
}
elem.AddChild(attr)
_ = elem.AddChild(attr)

if elem.String() != `<foo attr2="a &amp; b"/>` {
t.Errorf(`Expected String '<foo attr2="a &amp; b"/>', got '%s'`, elem.String())
return
}
elem.RemoveAttribute("attr2")
_ = elem.RemoveAttribute("attr2")

attr, err = d.CreateAttributeNS("http://kungfoo", "foo:attr3", "g & h")
if err != nil {
t.Errorf("Failed to create Attribute node: %s", err)
return
}
elem.AddChild(attr)
_ = elem.AddChild(attr)

if elem.String() != `<foo xmlns:foo="http://kungfoo" foo:attr3="g &amp; h"/>` {
t.Errorf(`Expected String '<foo xmlns:foo="http://kungfoo" foo:attr3="g &amp; h"/>', got '%s'`, elem.String())
Expand All @@ -318,7 +318,7 @@ func TestDocumentCreateAttributeNS(t *testing.T) {
t.Errorf("Failed to create Element node: %s", err)
return
}
d.SetDocumentElement(elem)
_ = d.SetDocumentElement(elem)

attr, err := d.CreateAttributeNS("http://kungfoo", "kung:foo", "bar")
if err != nil {
Expand Down Expand Up @@ -355,7 +355,7 @@ func TestDocumentCreateAttributeNS(t *testing.T) {
t.Errorf("Failed to create Element node: %s", err)
return
}
d.SetDocumentElement(elem)
_ = d.SetDocumentElement(elem)

badnames := []string{";", "&", "<><", "/", "1A"}
for _, name := range badnames {
Expand Down
15 changes: 8 additions & 7 deletions dom/node_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package dom
package dom_test

import (
"fmt"
"testing"

"github.com/lestrrat-go/libxml2/clib"
. "github.com/lestrrat-go/libxml2/dom"
"github.com/lestrrat-go/libxml2/types"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -123,15 +124,15 @@ func TestDOM(t *testing.T) {
return
}

doc.SetDocumentElement(root)
_ = doc.SetDocumentElement(root)
var toRemove types.Node
for i := 1; i <= 3; i++ {
child, err := doc.CreateElement(fmt.Sprintf("child%d", i))
if !assert.NoError(t, err, "dom.CreateElement(child%d) should succeed", i) {
return
}
child.AppendText(fmt.Sprintf("text%d", i))
root.AddChild(child)
_ = child.AppendText(fmt.Sprintf("text%d", i))
_ = root.AddChild(child)

if i == 2 {
toRemove = child
Expand Down Expand Up @@ -218,19 +219,19 @@ func TestCreateElementNS(t *testing.T) {
if !assert.NoError(t, err, "CreateElementNS should succeed") {
return
}
doc.SetDocumentElement(root)
_ = doc.SetDocumentElement(root)

n1, err := doc.CreateElementNS("http://foo.bar.baz", "foo:n1")
if !assert.NoError(t, err, "CreateElementNS should succeed") {
return
}
root.AddChild(n1)
_ = root.AddChild(n1)

n2, err := doc.CreateElementNS("http://foo.bar.baz", "bar:n2")
if !assert.NoError(t, err, "CreateElementNS should succeed") {
return
}
root.AddChild(n2)
_ = root.AddChild(n2)

_, err = doc.CreateElementNS("http://foo.bar.baz.quux", "foo:n3")
if !assert.Error(t, err, "CreateElementNS should fail") {
Expand Down
25 changes: 25 additions & 0 deletions libxml2.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,28 @@ correctness will be used.
Also, the return values are still shaky -- I'm still debating how to handle error cases gracefully.
*/
package libxml2

import (
"io"

"github.com/lestrrat-go/libxml2/parser"
"github.com/lestrrat-go/libxml2/types"
)

// Parse parses the given buffer and returns a Document.
func Parse(buf []byte, o ...parser.Option) (types.Document, error) {
p := parser.New(o...)
return p.Parse(buf)
}

// ParseString parses the given string and returns a Document.
func ParseString(s string, o ...parser.Option) (types.Document, error) {
p := parser.New(o...)
return p.ParseString(s)
}

// ParseReader parses XML from the given io.Reader and returns a Document.
func ParseReader(rdr io.Reader, o ...parser.Option) (types.Document, error) {
p := parser.New(o...)
return p.ParseReader(rdr)
}
14 changes: 7 additions & 7 deletions libxml2_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestBenchmarkLibxml2Xmlpath(t *testing.T) {
if !assert.NoError(t, err, "xpath.NewContext succeeds") {
return
}
xpc.RegisterNS("atom", "http://www.w3.org/2005/Atom")
_ = xpc.RegisterNS("atom", "http://www.w3.org/2005/Atom")

res, err := xpc.Find(`//atom:entry`)
if !assert.NoError(t, err, "xpc.Find succeeds") {
Expand Down Expand Up @@ -97,7 +97,7 @@ func BenchmarkLibxml2Xmlpath(b *testing.B) {
if err != nil {
b.Fatalf("%s", err)
}
xpc.RegisterNS("atom", "http://www.w3.org/2005/Atom")
_ = xpc.RegisterNS("atom", "http://www.w3.org/2005/Atom")
for i := 0; i < b.N; i++ {
iter := xpath.NodeIter(xpc.Find(`//atom:entry`))
for iter.Next() {
Expand All @@ -123,7 +123,7 @@ func BenchmarkEncodingXMLDOM(b *testing.B) {
for i := 0; i < b.N; i++ {
buf.Reset()
enc := xml.NewEncoder(&buf)
enc.Encode(f)
_ = enc.Encode(f)
}
}

Expand All @@ -142,18 +142,18 @@ func BenchmarkLibxml2DOM(b *testing.B) {
d.Free()
panic(err)
}
d.SetDocumentElement(root)
_ = d.SetDocumentElement(root)

f1xml, err := d.CreateElement("Field1")
if err != nil {
d.Free()
panic(err)
}
root.AddChild(f1xml)
_ = root.AddChild(f1xml)

f1xml.SetAttribute("Field2", f.Field2)
_ = f1xml.SetAttribute("Field2", f.Field2)

f1xml.AppendText(f.Field1)
_ = f1xml.AppendText(f.Field1)
buf.Reset()
buf.WriteString(d.Dump(false))
d.Free()
Expand Down
6 changes: 3 additions & 3 deletions libxml2_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func ExampleXML() {
}
defer doc.Free()

doc.Walk(func(n types.Node) error {
_ = doc.Walk(func(n types.Node) error {
log.Println(n.NodeName())
return nil
})
Expand All @@ -45,7 +45,7 @@ func ExampleXML() {
}
defer ctx.Free()

ctx.RegisterNS("atom", "http://www.w3.org/2005/Atom")
_ = ctx.RegisterNS("atom", "http://www.w3.org/2005/Atom")
title := xpath.String(ctx.Find("/atom:feed/atom:title/text()"))
log.Printf("feed title = %s", title)
}
Expand All @@ -65,7 +65,7 @@ func ExampleHTML() {
}
defer doc.Free()

doc.Walk(func(n types.Node) error {
_ = doc.Walk(func(n types.Node) error {
log.Println(n.NodeName())
return nil
})
Expand Down
19 changes: 10 additions & 9 deletions parser_test.go → libxml2_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package libxml2
package libxml2_test

import (
"regexp"
"testing"

"github.com/lestrrat-go/libxml2"
"github.com/lestrrat-go/libxml2/dom"
"github.com/lestrrat-go/libxml2/types"

Expand Down Expand Up @@ -74,7 +75,7 @@ var (
func parseShouldSucceed(t *testing.T, opts parser.Option, inputs []string) {
t.Logf("Test parsing with parser %v", opts)
for _, s := range inputs {
d, err := ParseString(s, opts)
d, err := libxml2.ParseString(s, opts)
if !assert.NoError(t, err, "Parse should succeed") {
return
}
Expand All @@ -84,7 +85,7 @@ func parseShouldSucceed(t *testing.T, opts parser.Option, inputs []string) {

func parseShouldFail(t *testing.T, opts parser.Option, inputs []string) {
for _, s := range inputs {
d, err := ParseString(s, opts)
d, err := libxml2.ParseString(s, opts)
if err == nil {
d.Free()
t.Errorf("Expected failure to parse '%s'", s)
Expand Down Expand Up @@ -201,7 +202,7 @@ func TestParseOptionStringer(t *testing.T) {
}

func TestParseEmpty(t *testing.T) {
doc, err := ParseString(``)
doc, err := libxml2.ParseString(``)
if err == nil {
t.Errorf("Parse of empty string should fail")
defer doc.Free()
Expand Down Expand Up @@ -245,7 +246,7 @@ func TestParseNoBlanks(t *testing.T) {
}

func TestRoundtripNoBlanks(t *testing.T) {
doc, err := ParseString(`<a> <b/> </a>`, parser.XMLParseNoBlanks)
doc, err := libxml2.ParseString(`<a> <b/> </a>`, parser.XMLParseNoBlanks)
if err != nil {
t.Errorf("failed to parse string: %s", err)
return
Expand Down Expand Up @@ -275,7 +276,7 @@ func TestGHIssue23(t *testing.T) {
<goodbye>Goodbye!</goodbye>
</rootnode>`

doc, err := ParseString(src, parser.XMLParseRecover, parser.XMLParseNoWarning, parser.XMLParseNoError)
doc, err := libxml2.ParseString(src, parser.XMLParseRecover, parser.XMLParseNoWarning, parser.XMLParseNoError)
if !assert.NoError(t, err, "should pass") {
return
}
Expand All @@ -286,7 +287,7 @@ func TestCommentWrapNodeIssue(t *testing.T) {
// should wrap comment node
const testHTML = "<p><!-- test --></p><!-- test --><p><!-- test --></p>"

doc, err := ParseHTMLString(testHTML, parser.HTMLParseRecover)
doc, err := libxml2.ParseHTMLString(testHTML, parser.HTMLParseRecover)
if err != nil {
t.Fatalf("Got error when parsing HTML: %v", err)
}
Expand All @@ -309,7 +310,7 @@ func TestCommentWrapNodeIssue(t *testing.T) {
func TestPiWrapNodeIssue(t *testing.T) {
// should wrap Pi node
const textXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<a>test <?test?></a>\n"
doc, err := ParseString(textXML)
doc, err := libxml2.ParseString(textXML)
if err != nil {
t.Fatalf("Got error when parsing xml: %v", err)
}
Expand All @@ -334,7 +335,7 @@ func TestPiWrapNodeIssue(t *testing.T) {

func TestGetNonexistentAttributeReturnsRecoverableError(t *testing.T) {
const src = `<?xml version="1.0"?><rootnode/>`
doc, err := ParseString(src)
doc, err := libxml2.ParseString(src)
if !assert.NoError(t, err, "Should parse") {
return
}
Expand Down
26 changes: 0 additions & 26 deletions parser.go

This file was deleted.

4 changes: 3 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ func (p *Parser) ParseString(s string) (types.Document, error) {
if err != nil {
return nil, errors.Wrap(err, "failed to create parse context")
}
defer func() { _ = ctx.Free() }()
defer func() {
_ = ctx.Free()
}()

docptr, err := clib.XMLCtxtReadMemory(ctx, s, "", "", int(p.Options))
if err != nil {
Expand Down
Loading

0 comments on commit 9daa0b6

Please sign in to comment.