diff --git a/cmd/kitgen/main_test.go b/cmd/kitgen/main_test.go index 4ef5013a8..eab59bc7a 100644 --- a/cmd/kitgen/main_test.go +++ b/cmd/kitgen/main_test.go @@ -1,15 +1,16 @@ package main import ( + "bufio" "bytes" "flag" "fmt" - "io" "io/ioutil" "os/exec" "path/filepath" - "strings" "testing" + + "github.com/aryann/difflib" ) var update = flag.Bool("update", false, "update golden files") @@ -54,19 +55,13 @@ func TestProcess(t *testing.T) { } if !bytes.Equal(expected, actual) { - name := kind + filename - name = strings.Replace(name, "/", "-", -1) - - errfile, err := ioutil.TempFile("", name) - if err != nil { - t.Fatal("opening tempfile for output", err) + results := difflib.Diff(splitLines(expected), splitLines(actual)) + for _, result := range results { + if result.Delta == difflib.Common { + continue + } + t.Error(result) } - io.WriteString(errfile, string(actual)) - - diffCmd := exec.Command("diff", outpath, errfile.Name()) - diffOut, _ := diffCmd.Output() - t.Log(string(diffOut)) - t.Errorf("Processing output didn't match %q. Results recorded in %q.", outpath, errfile.Name()) } } @@ -111,3 +106,12 @@ func TestTemplatesBuild(t *testing.T) { t.Fatal(err, "\n", string(out)) } } + +func splitLines(txt []byte) []string { + s := bufio.NewScanner(bytes.NewReader(txt)) + var ss []string + for s.Scan() { + ss = append(ss, s.Text()) + } + return ss +} diff --git a/cmd/kitgen/path_test.go b/cmd/kitgen/path_test.go index 371ded1d1..818d89cd7 100644 --- a/cmd/kitgen/path_test.go +++ b/cmd/kitgen/path_test.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "runtime" "strings" "testing" ) @@ -19,10 +20,17 @@ func TestImportPath(t *testing.T) { }) } - testcase("/gopath/", "/gopath/src/somewhere", "somewhere") - testcase("/gopath", "/gopath/src/somewhere", "somewhere") - testcase("/gopath:/other", "/gopath/src/somewhere", "somewhere") - testcase("/other:/gopath/", "/gopath/src/somewhere", "somewhere") + if runtime.GOOS == "windows" { + testcase("c:\\gopath\\", "c:\\gopath\\src\\somewhere", "somewhere") + testcase("c:\\gopath", "c:\\gopath\\src\\somewhere", "somewhere") + testcase("c:\\gopath;\\other", "c:\\gopath\\src\\somewhere", "somewhere") + testcase("c:\\other;c:\\gopath\\", "c:\\gopath\\src\\somewhere", "somewhere") + } else { + testcase("/gopath/", "/gopath/src/somewhere", "somewhere") + testcase("/gopath", "/gopath/src/somewhere", "somewhere") + testcase("/gopath:/other", "/gopath/src/somewhere", "somewhere") + testcase("/other:/gopath/", "/gopath/src/somewhere", "somewhere") + } } func TestImportPathSadpath(t *testing.T) { @@ -37,7 +45,10 @@ func TestImportPathSadpath(t *testing.T) { } }) } - - testcase("", "/gopath/src/somewhere", "is not in") + if runtime.GOOS == "windows" { + testcase("", "c:\\gopath\\src\\somewhere", "is not in") + } else { + testcase("", "/gopath/src/somewhere", "is not in") + } testcase("", "./somewhere", "not an absolute") } diff --git a/cmd/kitgen/transform.go b/cmd/kitgen/transform.go index 362398c92..a66ff96da 100644 --- a/cmd/kitgen/transform.go +++ b/cmd/kitgen/transform.go @@ -178,7 +178,7 @@ func addImport(root *ast.File, path string) { for _, d := range root.Decls { if imp, is := d.(*ast.GenDecl); is && imp.Tok == token.IMPORT { for _, s := range imp.Specs { - if s.(*ast.ImportSpec).Path.Value == `"`+path+`"` { + if s.(*ast.ImportSpec).Path.Value == `"`+filepath.ToSlash(path)+`"` { return // already have one // xxx aliased imports? }