Skip to content

Commit

Permalink
Adding report format
Browse files Browse the repository at this point in the history
  • Loading branch information
yazgazan committed Apr 23, 2017
1 parent 3d05325 commit 41246c9
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 5 deletions.
8 changes: 5 additions & 3 deletions config.go
Expand Up @@ -10,16 +10,18 @@ import (

type config struct {
diff.Output
ignore patterns
lhsFile string
rhsFile string
ignore patterns
lhsFile string
rhsFile string
outputReport bool
}

func readConfig() config {
var c config

flag.StringVar(&c.Output.Indent, "indent", " ", "indent string")
flag.BoolVar(&c.Output.ShowTypes, "show-types", false, "show types")
flag.BoolVar(&c.outputReport, "report", false, "output report format")
flag.Var(&c.ignore, "ignore", "paths to ignore (glob)")
flag.Parse()

Expand Down
40 changes: 40 additions & 0 deletions diff/diff_test.go
Expand Up @@ -404,6 +404,46 @@ func TestIgnore(t *testing.T) {
}
}

func TestReport(t *testing.T) {
want := []string{
"content",
"type",
}

d, err := Diff(
map[string]interface{}{
"match": 5,
"content": 6,
"type": 8,
},
map[string]interface{}{
"match": 5,
"content": 7,
"type": 9.0,
},
)
if err != nil {
t.Errorf("Diff(...): unexpected error: %s", err)
return
}
errs, err := Report(d, testOutput)
if err != nil {
t.Errorf("Report(Diff(...), %+v): unexpected error: %s", err, testOutput)
return
}

if len(errs) != len(want) {
t.Errorf("len(Report(Diff(...), %+v)) = %d, expected %d", testOutput, len(errs), len(want))
return
}

for i, e := range errs {
if !strings.Contains(e.Error(), want[i]) {
t.Errorf("Report(Diff(...), %+v)[%d] = %q, should contain %q", testOutput, i, e.Error(), want[i])
}
}
}

func testStrings(context string, t *testing.T, test stringTest, ss []string, indented string) {
for i, want := range test.Want {
s := ss[i]
Expand Down
13 changes: 12 additions & 1 deletion diff/map.go
Expand Up @@ -180,7 +180,18 @@ func (m Map) StringIndent(keyprefix, prefix string, conf Output) string {
}

func (m Map) Walk(path string, fn WalkFn) error {
for k, diff := range m.Diffs {
var keys []interface{}

for k := range m.Diffs {
keys = append(keys, k)
}

sort.Slice(keys, func(i, j int) bool {
return strings.Compare(fmt.Sprintf("%v", keys[i]), fmt.Sprintf("%v", keys[j])) == -1
})

for _, k := range keys {
diff := m.Diffs[k]
err := walk(m, diff, fmt.Sprintf("%s.%v", path, k), fn)
if err != nil {
return err
Expand Down
29 changes: 29 additions & 0 deletions diff/report.go
@@ -0,0 +1,29 @@
package diff

type ReportError string

func (e ReportError) Error() string {
return string(e)
}

func Report(d Differ, outConf Output) ([]error, error) {
var errs []error

err := Walk(d, func(parent, diff Differ, path string) error {
switch diff.Diff() {
case Identical:
return nil
case TypesDiffer:
errs = append(errs, ReportError(diff.StringIndent(" "+path+": ", "", outConf)))
case ContentDiffer:
if _, ok := diff.(Walker); ok {
return nil
}
errs = append(errs, ReportError(diff.StringIndent(" "+path+": ", "", outConf)))
}

return nil
})

return errs, err
}
13 changes: 12 additions & 1 deletion main.go
Expand Up @@ -30,7 +30,18 @@ func main() {

err = pruneIgnore(d, conf.ignore)

fmt.Println(d.StringIndent("", "", conf.Output))
if conf.outputReport {
errs, err := diff.Report(d, conf.Output)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Failed to generate report: %s", err)
os.Exit(StatusDiffError)
}
for _, e := range errs {
fmt.Println(e.Error())
}
} else {
fmt.Println(d.StringIndent("", "", conf.Output))
}
if d.Diff() != diff.Identical {
os.Exit(StatusDiffMismatch)
}
Expand Down

0 comments on commit 41246c9

Please sign in to comment.