Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions cmd/kitgen/main_test.go
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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())
}
}

Expand Down Expand Up @@ -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
}
23 changes: 17 additions & 6 deletions cmd/kitgen/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"runtime"
"strings"
"testing"
)
Expand All @@ -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) {
Expand All @@ -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")
}
2 changes: 1 addition & 1 deletion cmd/kitgen/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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?
}
Expand Down