Skip to content

Commit c9bb066

Browse files
committed
Add file diff into go test
1 parent 64adb52 commit c9bb066

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/labstack/gommon v0.2.8 // indirect
1010
github.com/mattn/go-colorable v0.0.9 // indirect
1111
github.com/mattn/go-isatty v0.0.4 // indirect
12+
github.com/sergi/go-diff v1.0.0
1213
github.com/spf13/cobra v0.0.3
1314
github.com/spf13/pflag v1.0.2
1415
github.com/stretchr/testify v1.3.0 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs
1414
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
1515
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1616
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
17+
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
18+
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
1719
github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
1820
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
1921
github.com/spf13/pflag v1.0.2 h1:Fy0orTDgHdbnzHcsOgfCN4LtHf0ec3wwtiwJqwvf3Gc=

main_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ package main
22

33
import (
44
"bytes"
5+
"fmt"
6+
"io/ioutil"
57
"os"
68
"path"
9+
"path/filepath"
710
"runtime"
811
"strings"
912
"testing"
1013

14+
"github.com/sergi/go-diff/diffmatchpatch"
1115
"github.com/yoozoo/protoapi/cmd"
1216
)
1317

@@ -89,4 +93,41 @@ func TestCmd(t *testing.T) {
8993
test(t, cmd)
9094
}
9195
}
96+
97+
files, err := filePathWalkDir("test/expected")
98+
if err != nil {
99+
t.Fatal(err)
100+
}
101+
102+
for _, expectedFileName := range files {
103+
resultFileName := strings.Replace(expectedFileName, "expected", "result", 1)
104+
105+
expectedFile, err := ioutil.ReadFile(expectedFileName)
106+
if err != nil {
107+
t.Error("expectedFile read err: ", expectedFileName, err)
108+
}
109+
110+
resultFile, err := ioutil.ReadFile(resultFileName)
111+
if err != nil {
112+
t.Error("resultFile read err: ", resultFileName, err)
113+
}
114+
115+
if string(expectedFile) != string(resultFile) {
116+
dmp := diffmatchpatch.New()
117+
diffs := dmp.DiffMain(string(expectedFile), string(resultFile), false)
118+
t.Error("file different: ", expectedFileName, resultFileName)
119+
fmt.Println(dmp.DiffPrettyText(diffs))
120+
}
121+
}
122+
}
123+
124+
func filePathWalkDir(root string) ([]string, error) {
125+
var files []string
126+
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
127+
if !info.IsDir() {
128+
files = append(files, path)
129+
}
130+
return nil
131+
})
132+
return files, err
92133
}

0 commit comments

Comments
 (0)