Add unit tests to make sure docs are up to date

pull/7385/head
Priya Wadhwa 2020-04-01 16:13:54 -07:00
parent a9dcfe82fe
commit c409c4c91c
2 changed files with 49 additions and 2 deletions

View File

@ -0,0 +1,47 @@
/*
Copyright 2020 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 cmd
import (
"fmt"
"io/ioutil"
"path/filepath"
"testing"
"k8s.io/minikube/pkg/generate"
)
func TestCommandsDocs(t *testing.T) {
dir := "../../../site/content/en/docs/Reference/Commands/"
for _, sc := range RootCmd.Commands() {
t.Run(sc.Name(), func(t *testing.T) {
fp := filepath.Join(dir, fmt.Sprintf("%s.md", sc.Name()))
expectedContents, err := ioutil.ReadFile(fp)
if err != nil {
t.Fatalf("Docs are not updated. Please run `make generate-docs` to update commands documentation: %v", err)
}
actualContents, err := generate.DocForCommand(sc)
if err != nil {
t.Fatalf("error getting contents: %v", err)
}
if string(actualContents) != string(expectedContents) {
t.Fatalf("Docs are not updated. Please run `make generate-docs` to update commands documentation.")
}
})
}
}

View File

@ -38,7 +38,7 @@ import (
func Docs(root *cobra.Command, path string) error {
cmds := root.Commands()
for _, c := range cmds {
contents, err := docForCommand(c)
contents, err := DocForCommand(c)
if err != nil {
return errors.Wrapf(err, "generating doc for %s", c.Name)
}
@ -49,7 +49,7 @@ func Docs(root *cobra.Command, path string) error {
return nil
}
func docForCommand(command *cobra.Command) ([]byte, error) {
func DocForCommand(command *cobra.Command) ([]byte, error) {
buf := bytes.NewBuffer([]byte{})
if err := generateTitle(command, buf); err != nil {
return nil, errors.Wrap(err, "generating title")