-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consistent random identifier names #185
Changes from 6 commits
bee3a9d
1b0ff04
13e3699
fc125de
8def2bb
11668c8
391c009
36e733d
f7f2e86
f37c1ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,9 @@ $(MGEN): ./msgp/defs_test.go | |
go generate ./msgp | ||
|
||
test: all | ||
# keep in sync with 'make travis' | ||
go test -v ./msgp | ||
go test -v ./gen | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should add this line to the |
||
go test -v ./_generated | ||
|
||
bench: all | ||
|
@@ -51,5 +53,7 @@ travis: | |
go build -o "$${GOPATH%%:*}/bin/msgp" . | ||
go generate ./msgp | ||
go generate ./_generated | ||
# keep in sync with 'make test' | ||
go test ./msgp | ||
go test ./gen | ||
go test ./_generated |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,308 @@ | ||
package gen | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this file just a duplicate of the one in the root of the file tree? Remove it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops! Done. |
||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"go/parser" | ||
"go/token" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"reflect" | ||
"sort" | ||
"testing" | ||
"text/template" | ||
) | ||
|
||
// When stuff's going wrong, you'll be glad this is here! | ||
const debugTemp = false | ||
|
||
// Ensure that consistent identifiers are generated on a per-method basis by msgp. | ||
// | ||
// Also ensure that no duplicate identifiers appear in a method. | ||
// | ||
// structs are currently processed alphabetically by msgp. this test relies on | ||
// that property. | ||
// | ||
func TestIssue185Idents(t *testing.T) { | ||
var identCases = []struct { | ||
tpl *template.Template | ||
expectedChanged []string | ||
}{ | ||
{tpl: issue185IdentsTpl, expectedChanged: []string{"Test1"}}, | ||
{tpl: issue185ComplexIdentsTpl, expectedChanged: []string{"Test2"}}, | ||
} | ||
|
||
methods := []string{"DecodeMsg", "EncodeMsg", "Msgsize", "MarshalMsg", "UnmarshalMsg"} | ||
|
||
for idx, identCase := range identCases { | ||
// generate the code, extract the generated variable names, mapped to function name | ||
var tplData issue185TplData | ||
varsBefore, err := loadVars(identCase.tpl, tplData) | ||
if err != nil { | ||
t.Fatalf("%d: could not extract before vars: %v", idx, err) | ||
} | ||
|
||
// regenerate the code with extra field(s), extract the generated variable | ||
// names, mapped to function name | ||
tplData.Extra = true | ||
varsAfter, err := loadVars(identCase.tpl, tplData) | ||
if err != nil { | ||
t.Fatalf("%d: could not extract after vars: %v", idx, err) | ||
} | ||
|
||
// ensure that all declared variable names inside each of the methods we | ||
// expect to change have actually changed | ||
for _, stct := range identCase.expectedChanged { | ||
for _, method := range methods { | ||
fn := fmt.Sprintf("%s.%s", stct, method) | ||
|
||
bv, av := varsBefore.Value(fn), varsAfter.Value(fn) | ||
if len(bv) > 0 && len(av) > 0 && reflect.DeepEqual(bv, av) { | ||
t.Fatalf("%d vars identical! expected vars to change for %s", idx, fn) | ||
} | ||
delete(varsBefore, fn) | ||
delete(varsAfter, fn) | ||
} | ||
} | ||
|
||
// all of the remaining keys should not have changed | ||
for bmethod, bvars := range varsBefore { | ||
avars := varsAfter.Value(bmethod) | ||
|
||
if !reflect.DeepEqual(bvars, avars) { | ||
t.Fatalf("%d: vars changed! expected vars identical for %s", idx, bmethod) | ||
} | ||
delete(varsBefore, bmethod) | ||
delete(varsAfter, bmethod) | ||
} | ||
|
||
if len(varsBefore) > 0 || len(varsAfter) > 0 { | ||
t.Fatalf("%d: unexpected methods remaining", idx) | ||
} | ||
} | ||
} | ||
|
||
type issue185TplData struct { | ||
Extra bool | ||
} | ||
|
||
func TestIssue185Overlap(t *testing.T) { | ||
var overlapCases = []struct { | ||
tpl *template.Template | ||
data issue185TplData | ||
}{ | ||
{tpl: issue185IdentsTpl, data: issue185TplData{Extra: false}}, | ||
{tpl: issue185IdentsTpl, data: issue185TplData{Extra: true}}, | ||
{tpl: issue185ComplexIdentsTpl, data: issue185TplData{Extra: false}}, | ||
{tpl: issue185ComplexIdentsTpl, data: issue185TplData{Extra: true}}, | ||
} | ||
|
||
for idx, o := range overlapCases { | ||
// regenerate the code with extra field(s), extract the generated variable | ||
// names, mapped to function name | ||
mvars, err := loadVars(o.tpl, o.data) | ||
if err != nil { | ||
t.Fatalf("%d: could not extract after vars: %v", idx, err) | ||
} | ||
|
||
identCnt := 0 | ||
for fn, vars := range mvars { | ||
sort.Strings(vars) | ||
|
||
// Loose sanity check to make sure the tests expectations aren't broken. | ||
// If the prefix ever changes, this needs to change. | ||
for i := 0; i < len(vars); i++ { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use a range loop here |
||
if vars[i][0] == 'z' { | ||
identCnt++ | ||
} | ||
} | ||
|
||
for i := 0; i < len(vars)-1; i++ { | ||
if vars[i] == vars[i+1] { | ||
t.Fatalf("%d: duplicate var %s in function %s", idx, vars[i], fn) | ||
} | ||
} | ||
} | ||
|
||
// one last sanity check: if there aren't any vars that start with 'z', | ||
// this test's expectations are unsatisfiable. | ||
if identCnt == 0 { | ||
t.Fatalf("%d: no generated identifiers found", idx) | ||
} | ||
} | ||
} | ||
|
||
func loadVars(tpl *template.Template, tplData interface{}) (vars extractedVars, err error) { | ||
tempDir, err := ioutil.TempDir("", "msgp-") | ||
if err != nil { | ||
err = fmt.Errorf("could not create temp dir: %v", err) | ||
return | ||
} | ||
|
||
if !debugTemp { | ||
defer os.RemoveAll(tempDir) | ||
} else { | ||
fmt.Println(tempDir) | ||
} | ||
|
||
tfile := filepath.Join(tempDir, "msg.go") | ||
genFile := filepath.Join(tempDir, "msg_gen.go") | ||
|
||
if err = goGenerateTpl(tempDir, tfile, tpl, tplData); err != nil { | ||
err = fmt.Errorf("could not generate code: %v", err) | ||
return | ||
} | ||
|
||
vars, err = extractVars(genFile) | ||
if err != nil { | ||
err = fmt.Errorf("could not extract after vars: %v", err) | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
type varVisitor struct { | ||
vars []string | ||
fset *token.FileSet | ||
} | ||
|
||
func (v *varVisitor) Visit(node ast.Node) (w ast.Visitor) { | ||
gen, ok := node.(*ast.GenDecl) | ||
if !ok { | ||
return v | ||
} | ||
for _, spec := range gen.Specs { | ||
if vspec, ok := spec.(*ast.ValueSpec); ok { | ||
for _, n := range vspec.Names { | ||
v.vars = append(v.vars, n.Name) | ||
} | ||
} | ||
} | ||
return v | ||
} | ||
|
||
type extractedVars map[string][]string | ||
|
||
func (e extractedVars) Value(key string) []string { | ||
if v, ok := e[key]; ok { | ||
return v | ||
} | ||
panic(fmt.Errorf("unknown key %s", key)) | ||
} | ||
|
||
func extractVars(file string) (extractedVars, error) { | ||
fset := token.NewFileSet() | ||
|
||
f, err := parser.ParseFile(fset, file, nil, 0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
vars := make(map[string][]string) | ||
for _, d := range f.Decls { | ||
switch d := d.(type) { | ||
case *ast.FuncDecl: | ||
sn := "" | ||
switch rt := d.Recv.List[0].Type.(type) { | ||
case *ast.Ident: | ||
sn = rt.Name | ||
case *ast.StarExpr: | ||
sn = rt.X.(*ast.Ident).Name | ||
default: | ||
panic("unknown receiver type") | ||
} | ||
|
||
key := fmt.Sprintf("%s.%s", sn, d.Name.Name) | ||
vis := &varVisitor{fset: fset} | ||
ast.Walk(vis, d.Body) | ||
vars[key] = vis.vars | ||
} | ||
} | ||
return vars, nil | ||
} | ||
|
||
func goGenerateTpl(cwd, tfile string, tpl *template.Template, tplData interface{}) error { | ||
outf, err := os.OpenFile(tfile, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0600) | ||
if err != nil { | ||
return err | ||
} | ||
defer outf.Close() | ||
|
||
if err := tpl.Execute(outf, tplData); err != nil { | ||
return err | ||
} | ||
|
||
cmd := exec.Command("go", "generate") | ||
cmd.Dir = cwd | ||
return cmd.Run() | ||
} | ||
|
||
var issue185IdentsTpl = template.Must(template.New("").Parse(` | ||
package issue185 | ||
|
||
//go:generate msgp | ||
|
||
type Test1 struct { | ||
Foo string | ||
Bar string | ||
{{ if .Extra }}Baz []string{{ end }} | ||
Qux string | ||
} | ||
|
||
type Test2 struct { | ||
Foo string | ||
Bar string | ||
Baz string | ||
} | ||
`)) | ||
|
||
var issue185ComplexIdentsTpl = template.Must(template.New("").Parse(` | ||
package issue185 | ||
|
||
//go:generate msgp | ||
|
||
type Test1 struct { | ||
Foo string | ||
Bar string | ||
Baz string | ||
} | ||
|
||
type Test2 struct { | ||
Foo string | ||
Bar string | ||
Baz []string | ||
Qux map[string]string | ||
Yep map[string]map[string]string | ||
Quack struct { | ||
Quack struct { | ||
Quack struct { | ||
{{ if .Extra }}Extra []string{{ end }} | ||
Quack string | ||
} | ||
} | ||
} | ||
Nup struct { | ||
Foo string | ||
Bar string | ||
Baz []string | ||
Qux map[string]string | ||
Yep map[string]map[string]string | ||
} | ||
Ding struct { | ||
Dong struct { | ||
Dung struct { | ||
Thing string | ||
} | ||
} | ||
} | ||
} | ||
|
||
type Test3 struct { | ||
Foo string | ||
Bar string | ||
Baz string | ||
} | ||
`)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This and the
travis
rule can probably just bego test ./...
now. At one point long ago I think there was an ordering dependency that needed to be satisfied, but I can't see why that wouldn't be a valid way to run the tests now.