Moves gen docs to /tools with tests

Signed-off-by: JoshVanL <vleeuwenjoshua@gmail.com>
This commit is contained in:
JoshVanL 2020-04-28 12:29:20 +01:00
parent 0657e34396
commit 152f8e9542
No known key found for this signature in database
GPG Key ID: E7A7196576A219DA
5 changed files with 112 additions and 22 deletions

View File

@ -95,6 +95,7 @@ filegroup(
"//test/integration:all-srcs",
"//test/unit/gen:all-srcs",
"//test/unit/listers:all-srcs",
"//tools/cobra:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],

View File

@ -276,7 +276,6 @@ filegroup(
"//hack/bin:all-srcs",
"//hack/boilerplate:all-srcs",
"//hack/build:all-srcs",
"//hack/cobra:all-srcs",
"//hack/filter-crd:all-srcs",
],
tags = ["automanaged"],

View File

@ -1,13 +1,13 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = ["main.go"],
importpath = "github.com/jetstack/cert-manager/hack/cobra",
importpath = "github.com/jetstack/cert-manager/tools/cobra",
visibility = ["//visibility:private"],
deps = [
"//cmd/cainjector/cmd:go_default_library",
"//cmd/controller/cmd:go_default_library",
"//cmd/cainjector/app:go_default_library",
"//cmd/controller/app:go_default_library",
"//cmd/ctl/cmd:go_default_library",
"@com_github_mitchellh_go_homedir//:go_default_library",
"@com_github_spf13_cobra//:go_default_library",
@ -35,3 +35,9 @@ filegroup(
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = ["main_test.go"],
embed = [":go_default_library"],
)

View File

@ -27,34 +27,54 @@ import (
"github.com/spf13/cobra/doc"
"github.com/spf13/pflag"
cainjcmd "github.com/jetstack/cert-manager/cmd/cainjector/cmd"
controllercmd "github.com/jetstack/cert-manager/cmd/controller/cmd"
cainjectorapp "github.com/jetstack/cert-manager/cmd/cainjector/app"
controllerapp "github.com/jetstack/cert-manager/cmd/controller/app"
ctlcmd "github.com/jetstack/cert-manager/cmd/ctl/cmd"
)
func main() {
args := os.Args
if err := run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
os.Exit(0)
}
func run(args []string) error {
if len(args) != 2 {
must(errors.New("expecting single output directory argument"))
return errors.New("expecting single output directory argument")
}
// remove all global flags that are imported in
pflag.CommandLine = nil
root, err := homedir.Expand(args[1])
must(err)
if err != nil {
return err
}
must(ensureDirectory(root))
if err := ensureDirectory(root); err != nil {
return err
}
for _, c := range []*cobra.Command{
cainjcmd.NewCommandStartInjectorController(nil, nil, nil),
controllercmd.NewCommandStartCertManagerController(nil),
cainjectorapp.NewCommandStartInjectorController(nil, nil, nil),
controllerapp.NewCommandStartCertManagerController(nil),
ctlcmd.NewCertManagerCtlCommand(nil, nil, nil, nil),
} {
dir := filepath.Join(root, c.Use)
must(ensureDirectory(dir))
must(doc.GenMarkdownTree(c, dir))
if err := ensureDirectory(dir); err != nil {
return err
}
if err := doc.GenMarkdownTree(c, dir); err != nil {
return err
}
}
return nil
}
func ensureDirectory(dir string) error {
@ -72,10 +92,3 @@ func ensureDirectory(dir string) error {
return nil
}
func must(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
}

71
tools/cobra/main_test.go Normal file
View File

@ -0,0 +1,71 @@
/*
Copyright 2020 The Jetstack cert-manager contributors.
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 main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestRun(t *testing.T) {
rootDir, err := ioutil.TempDir(os.TempDir(), "cert-manager-cobra")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := os.RemoveAll(rootDir); err != nil {
t.Fatal(err)
}
}()
tests := map[string]struct {
input []string
expDirs []string
expErr bool
}{
"if no arguments given should error": {
input: []string{"cobra"},
expErr: true,
},
"if two arguments given should error": {
input: []string{"cobra", "foo", "bar"},
expErr: true,
},
"if directory given, should write docs": {
input: []string{"cobra", filepath.Join(rootDir, "foo")},
expDirs: []string{"foo/ca-injector", "foo/cert-manager-controller", "foo/cert-manager-ctl"},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
err := run(test.input)
if test.expErr != (err != nil) {
t.Errorf("got unexpected error, exp=%t got=%v",
test.expErr, err)
}
for _, dir := range test.expDirs {
if _, err := os.Stat(filepath.Join(rootDir, dir)); err != nil {
t.Errorf("stat error on expected directory: %s", err)
}
}
})
}
}