Comment association to ConformanceIt block should be validated properly.

This commit is contained in:
Srini Brahmaroutu 2018-12-17 16:21:21 -08:00
parent 19ed7fa731
commit 1cb5872cd3
2 changed files with 32 additions and 2 deletions

View File

@ -161,8 +161,26 @@ func (v *visitor) failf(expr ast.Expr, format string, a ...interface{}) {
func (v *visitor) comment(x *ast.BasicLit) string {
for _, comm := range v.cMap.Comments() {
testOffset := int(x.Pos()-comm.End()) - len("framework.ConformanceIt(\"")
if 0 < testOffset && testOffset < 3 {
return comm.Text()
//Cannot assume the offset is within three or four tabs from the test block itself.
//It is better to trim the newlines, tabs, etc and then we if the comment is followed
//by the test block itself so that we can associate the comment with it properly.
if 0 <= testOffset && testOffset <= 10 {
b1 := make([]byte, x.Pos()-comm.End())
//if we fail to open the file to compare the content we just assume the
//proximity of the comment and apply it.
myf, err := os.Open(v.FileSet.File(x.Pos()).Name())
if err == nil {
if _, err := myf.Seek(int64(comm.End()), 0); err == nil {
if _, err := myf.Read(b1); err == nil {
if strings.Compare(strings.Trim(string(b1), "\t \r\n"), "framework.ConformanceIt(\"") == 0 {
return comm.Text()
}
}
}
} else {
//comment section's end is noticed within 10 characters from framework.ConformanceIt block
return comm.Text()
}
}
}
return ""

View File

@ -80,6 +80,18 @@ var _ = framework.KubeDescribe("Feature", func() {
Description: `By default the stdout and stderr from the process
being executed in a pod MUST be sent to the pod's logs.` + "\n\n"}},
},
{"e2e/foo.go", `
var _ = framework.KubeDescribe("Feature", func() {
Context("with context and extra spaces before It block should still pick up Testname", func() {
// Testname: Test with spaces
//Description: Should pick up testname even if it is not within 3 spaces
//even when executed from memory.
framework.ConformanceIt("should work", func() {})
})
})`, []conformanceData{{URL: "https://github.com/kubernetes/kubernetes/tree/master/e2e/foo.go#L8", TestName: "Test with spaces",
Description: `Should pick up testname even if it is not within 3 spaces
even when executed from memory.` + "\n\n"}},
},
}
func TestConformance(t *testing.T) {