2021-04-16 22:33:50 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Kubernetes Authors All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package generate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"k8s.io/minikube/pkg/minikube/out"
|
|
|
|
)
|
|
|
|
|
2021-05-11 22:17:43 +00:00
|
|
|
func TestDocs(docPath string, pathToCheck string) error {
|
2021-04-16 22:33:50 +00:00
|
|
|
counter := 0
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
date := time.Now().Format("2006-01-02")
|
|
|
|
title := out.Fmt(title, out.V{"Command": "Integration Tests", "Description": "All minikube integration tests", "Date": date})
|
|
|
|
_, err := buf.Write([]byte(title))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-11 22:17:43 +00:00
|
|
|
err = filepath.Walk(pathToCheck, func(path string, info os.FileInfo, err error) error {
|
2021-04-16 22:33:50 +00:00
|
|
|
if info.IsDir() || !strings.HasSuffix(path, ".go") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fset := token.NewFileSet()
|
|
|
|
r, e := ioutil.ReadFile(path)
|
|
|
|
if e != nil {
|
|
|
|
return errors.Wrap(e, fmt.Sprintf("error reading file %s", path))
|
|
|
|
}
|
|
|
|
file, e := parser.ParseFile(fset, "", r, parser.ParseComments)
|
|
|
|
if e != nil {
|
|
|
|
return errors.Wrap(e, fmt.Sprintf("error parsing file %s", path))
|
|
|
|
}
|
|
|
|
|
|
|
|
ast.Inspect(file, func(x ast.Node) bool {
|
|
|
|
if fd, ok := x.(*ast.FuncDecl); ok {
|
|
|
|
fnName := fd.Name.Name
|
|
|
|
if !shouldParse(fnName) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(fnName, "valid") {
|
2021-04-16 22:45:44 +00:00
|
|
|
e := writeSubTest(fnName, buf)
|
|
|
|
if e != nil {
|
|
|
|
return false
|
|
|
|
}
|
2021-04-16 22:33:50 +00:00
|
|
|
} else {
|
2021-04-16 22:45:44 +00:00
|
|
|
e := writeTest(fnName, buf)
|
|
|
|
if e != nil {
|
|
|
|
return false
|
|
|
|
}
|
2021-04-16 22:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
counter++
|
|
|
|
comments := fd.Doc
|
|
|
|
if comments == nil {
|
2021-05-11 22:17:43 +00:00
|
|
|
e := writeComment(fnName, "// NEEDS DOC\n", buf)
|
2021-04-16 22:45:44 +00:00
|
|
|
return e == nil
|
2021-04-16 22:33:50 +00:00
|
|
|
}
|
|
|
|
for _, comment := range comments.List {
|
|
|
|
if strings.Contains(comment.Text, "TODO") {
|
|
|
|
continue
|
|
|
|
}
|
2021-05-11 22:17:43 +00:00
|
|
|
e := writeComment(fnName, comment.Text, buf)
|
2021-04-16 22:45:44 +00:00
|
|
|
if e != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, e := buf.WriteString("\n")
|
|
|
|
if e != nil {
|
|
|
|
return false
|
2021-04-16 22:33:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(docPath, buf.Bytes(), 0o644)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func shouldParse(name string) bool {
|
|
|
|
if strings.HasPrefix(name, "Test") && !strings.HasPrefix(name, "TestMain") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(name, "valid") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeTest(testName string, w *bytes.Buffer) error {
|
|
|
|
_, err := w.WriteString("## " + testName + "\n")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeSubTest(testName string, w *bytes.Buffer) error {
|
|
|
|
_, err := w.WriteString("#### " + testName + "\n")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-11 22:17:43 +00:00
|
|
|
func writeComment(testName string, comment string, w *bytes.Buffer) error {
|
2021-04-16 22:33:50 +00:00
|
|
|
// Remove the leading // from the testdoc comments
|
|
|
|
comment = comment[3:]
|
2021-05-11 22:17:43 +00:00
|
|
|
comment = strings.TrimPrefix(comment, testName+" ")
|
2021-04-16 22:33:50 +00:00
|
|
|
_, err := w.WriteString(comment + "\n")
|
|
|
|
return err
|
|
|
|
}
|