Skip to content

Commit 9e4c3f7

Browse files
committed
Merge branch 'main' into zapdiff
2 parents ab802c0 + d1c2404 commit 9e4c3f7

5 files changed

Lines changed: 20 additions & 9 deletions

File tree

cmd/cli/zapdiff.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,3 @@ func listXMLFiles(p string) (paths []string, err error) {
103103

104104
return
105105
}
106-

zapdiff/check.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
)
88

99
func checkMismatches(ep elementPair, baseName string, n1, n2 string) (mm []XmlMismatch) {
10-
e1Children := make(map[string]*etree.Element)
11-
e2Children := make(map[string]*etree.Element)
10+
e1Children := make(map[string]*etree.Element, len(ep.e1.ChildElements()))
11+
e2Children := make(map[string]*etree.Element, len(ep.e2.ChildElements()))
1212
mm = make([]XmlMismatch, 0)
1313

1414
for _, c1 := range ep.e1.ChildElements() {
@@ -63,8 +63,8 @@ func checkMismatches(ep elementPair, baseName string, n1, n2 string) (mm []XmlMi
6363

6464
func checkAttributes(ep elementPair, id string, baseName string, n1, n2 string) (mm []XmlMismatch) {
6565
mm = make([]XmlMismatch, 0)
66-
e1Attrs := make(map[string]string)
67-
e2Attrs := make(map[string]string)
66+
e1Attrs := make(map[string]string, len(ep.e1.Attr))
67+
e2Attrs := make(map[string]string, len(ep.e2.Attr))
6868

6969
for _, a := range ep.e1.Attr {
7070
e1Attrs[a.Key] = a.Value

zapdiff/csv.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import (
1010
// WriteMismatchesToCSV writes the given mismatches to the writer in CSV format.
1111
func WriteMismatchesToCSV(w io.Writer, mm []XmlMismatch, l XmlMismatchLevel) (err error) {
1212
csvWriter := csv.NewWriter(w)
13-
defer csvWriter.Flush()
13+
defer func() {
14+
csvWriter.Flush()
15+
if err == nil {
16+
err = csvWriter.Error()
17+
}
18+
}()
1419

1520
// Write header
1621
header := []string{"Level", "Type", "File", "Element Xpath", "Details"}

zapdiff/element_id.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,20 @@ import (
88

99
func parentAndSelfAttr(e *etree.Element, attr string) string {
1010
parentID := getEntityUniqueIdentifier(e.Parent())
11-
return fmt.Sprintf("%s/%s[@%s='%s']", parentID, e.Tag, attr, e.SelectAttrValue(attr, ""))
11+
a := e.SelectAttr(attr)
12+
if a == nil {
13+
return fmt.Sprintf("%s/%s", parentID, getElementXPathSegment(e))
14+
}
15+
return fmt.Sprintf("%s/%s[@%s='%s']", parentID, e.Tag, attr, a.Value)
1216
}
1317

1418
func parentAndSelfText(e *etree.Element) string {
1519
parentID := getEntityUniqueIdentifier(e.Parent())
16-
return fmt.Sprintf("%s[%s='%s']/%s", parentID, e.Tag, e.Text(), e.Tag)
20+
text := e.Text()
21+
if text == "" {
22+
return fmt.Sprintf("%s/%s", parentID, getElementXPathSegment(e))
23+
}
24+
return fmt.Sprintf("%s/%s[text()='%s']", parentID, e.Tag, text)
1725
}
1826

1927
func getEntityUniqueIdentifier(e *etree.Element) string {

zapdiff/xpath.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ func getElementXPathSegment(e *etree.Element) (s string) {
1919
cnt++
2020
if sib == e {
2121
idx = cnt
22-
break
2322
}
2423
}
2524
}

0 commit comments

Comments
 (0)