2023-03-05 19:28:31 -07:00
|
|
|
// Copyright 2013-2023 The Cobra Authors
|
2022-09-16 04:55:56 -07:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2015-11-23 16:19:16 -07:00
|
|
|
package doc
|
2015-08-18 15:33:41 -07:00
|
|
|
|
|
|
|
import (
|
2016-01-20 18:06:00 -07:00
|
|
|
"bufio"
|
2015-08-18 15:33:41 -07:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-06-21 09:25:26 -07:00
|
|
|
"path/filepath"
|
2015-08-18 15:33:41 -07:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2016-01-20 18:06:00 -07:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2015-08-18 15:33:41 -07:00
|
|
|
)
|
|
|
|
|
2021-02-07 17:08:50 -07:00
|
|
|
func assertNoErr(t *testing.T, e error) {
|
|
|
|
if e != nil {
|
|
|
|
t.Error(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 15:33:41 -07:00
|
|
|
func translate(in string) string {
|
2022-05-14 13:10:36 -07:00
|
|
|
return strings.ReplaceAll(in, "-", "\\-")
|
2015-08-18 15:33:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenManDoc(t *testing.T) {
|
2015-09-08 13:02:02 -07:00
|
|
|
header := &GenManHeader{
|
|
|
|
Title: "Project",
|
|
|
|
Section: "2",
|
|
|
|
}
|
2017-11-02 08:22:38 -07:00
|
|
|
|
2015-08-18 15:33:41 -07:00
|
|
|
// We generate on a subcommand so we have both subcommands and parents
|
2017-11-02 08:22:38 -07:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := GenMan(echoCmd, header, buf); err != nil {
|
2016-01-06 04:21:23 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-11-02 08:22:38 -07:00
|
|
|
output := buf.String()
|
2015-08-18 15:33:41 -07:00
|
|
|
|
2015-09-08 09:32:15 -07:00
|
|
|
// Make sure parent has - in CommandPath() in SEE ALSO:
|
2017-11-02 08:22:38 -07:00
|
|
|
parentPath := echoCmd.Parent().CommandPath()
|
2022-05-14 13:10:36 -07:00
|
|
|
dashParentPath := strings.ReplaceAll(parentPath, " ", "-")
|
2015-09-08 09:32:15 -07:00
|
|
|
expected := translate(dashParentPath)
|
2015-09-08 13:02:02 -07:00
|
|
|
expected = expected + "(" + header.Section + ")"
|
2017-11-02 08:22:38 -07:00
|
|
|
checkStringContains(t, output, expected)
|
|
|
|
|
|
|
|
checkStringContains(t, output, translate(echoCmd.Name()))
|
|
|
|
checkStringContains(t, output, translate(echoCmd.Name()))
|
|
|
|
checkStringContains(t, output, "boolone")
|
|
|
|
checkStringContains(t, output, "rootflag")
|
|
|
|
checkStringContains(t, output, translate(rootCmd.Name()))
|
|
|
|
checkStringContains(t, output, translate(echoSubCmd.Name()))
|
|
|
|
checkStringOmits(t, output, translate(deprecatedCmd.Name()))
|
|
|
|
checkStringContains(t, output, translate("Auto generated"))
|
2015-11-07 18:21:25 -07:00
|
|
|
}
|
|
|
|
|
2018-04-24 09:15:12 -07:00
|
|
|
func TestGenManNoHiddenParents(t *testing.T) {
|
|
|
|
header := &GenManHeader{
|
|
|
|
Title: "Project",
|
|
|
|
Section: "2",
|
|
|
|
}
|
|
|
|
|
|
|
|
// We generate on a subcommand so we have both subcommands and parents
|
|
|
|
for _, name := range []string{"rootflag", "strtwo"} {
|
|
|
|
f := rootCmd.PersistentFlags().Lookup(name)
|
|
|
|
f.Hidden = true
|
|
|
|
defer func() { f.Hidden = false }()
|
|
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := GenMan(echoCmd, header, buf); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
output := buf.String()
|
|
|
|
|
|
|
|
// Make sure parent has - in CommandPath() in SEE ALSO:
|
|
|
|
parentPath := echoCmd.Parent().CommandPath()
|
2022-05-14 13:10:36 -07:00
|
|
|
dashParentPath := strings.ReplaceAll(parentPath, " ", "-")
|
2018-04-24 09:15:12 -07:00
|
|
|
expected := translate(dashParentPath)
|
|
|
|
expected = expected + "(" + header.Section + ")"
|
|
|
|
checkStringContains(t, output, expected)
|
|
|
|
|
|
|
|
checkStringContains(t, output, translate(echoCmd.Name()))
|
|
|
|
checkStringContains(t, output, translate(echoCmd.Name()))
|
|
|
|
checkStringContains(t, output, "boolone")
|
|
|
|
checkStringOmits(t, output, "rootflag")
|
|
|
|
checkStringContains(t, output, translate(rootCmd.Name()))
|
|
|
|
checkStringContains(t, output, translate(echoSubCmd.Name()))
|
|
|
|
checkStringOmits(t, output, translate(deprecatedCmd.Name()))
|
|
|
|
checkStringContains(t, output, translate("Auto generated"))
|
|
|
|
checkStringOmits(t, output, "OPTIONS INHERITED FROM PARENT COMMANDS")
|
|
|
|
}
|
|
|
|
|
2015-11-07 18:21:25 -07:00
|
|
|
func TestGenManNoGenTag(t *testing.T) {
|
2017-11-02 08:22:38 -07:00
|
|
|
echoCmd.DisableAutoGenTag = true
|
|
|
|
defer func() { echoCmd.DisableAutoGenTag = false }()
|
2015-11-07 18:21:25 -07:00
|
|
|
|
|
|
|
header := &GenManHeader{
|
|
|
|
Title: "Project",
|
|
|
|
Section: "2",
|
|
|
|
}
|
2017-11-02 08:22:38 -07:00
|
|
|
|
2015-11-07 18:21:25 -07:00
|
|
|
// We generate on a subcommand so we have both subcommands and parents
|
2017-11-02 08:22:38 -07:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := GenMan(echoCmd, header, buf); err != nil {
|
2016-01-06 04:21:23 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-11-02 08:22:38 -07:00
|
|
|
output := buf.String()
|
2015-11-07 18:21:25 -07:00
|
|
|
|
|
|
|
unexpected := translate("#HISTORY")
|
2017-11-02 08:22:38 -07:00
|
|
|
checkStringOmits(t, output, unexpected)
|
2020-04-29 10:15:55 -07:00
|
|
|
unexpected = translate("Auto generated by spf13/cobra")
|
|
|
|
checkStringOmits(t, output, unexpected)
|
2015-08-18 15:33:41 -07:00
|
|
|
}
|
2016-01-20 18:06:00 -07:00
|
|
|
|
|
|
|
func TestGenManSeeAlso(t *testing.T) {
|
2017-11-02 08:22:38 -07:00
|
|
|
rootCmd := &cobra.Command{Use: "root", Run: emptyRun}
|
|
|
|
aCmd := &cobra.Command{Use: "aaa", Run: emptyRun, Hidden: true} // #229
|
|
|
|
bCmd := &cobra.Command{Use: "bbb", Run: emptyRun}
|
|
|
|
cCmd := &cobra.Command{Use: "ccc", Run: emptyRun}
|
|
|
|
rootCmd.AddCommand(aCmd, bCmd, cCmd)
|
2016-01-20 18:06:00 -07:00
|
|
|
|
2017-11-02 08:22:38 -07:00
|
|
|
buf := new(bytes.Buffer)
|
2016-01-20 18:06:00 -07:00
|
|
|
header := &GenManHeader{}
|
2017-11-02 08:22:38 -07:00
|
|
|
if err := GenMan(rootCmd, header, buf); err != nil {
|
2016-01-20 18:06:00 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-11-02 08:22:38 -07:00
|
|
|
scanner := bufio.NewScanner(buf)
|
2016-01-20 18:06:00 -07:00
|
|
|
|
2017-11-02 08:22:38 -07:00
|
|
|
if err := assertLineFound(scanner, ".SH SEE ALSO"); err != nil {
|
|
|
|
t.Fatalf("Couldn't find SEE ALSO section header: %v", err)
|
2016-01-20 18:06:00 -07:00
|
|
|
}
|
2017-11-02 08:22:38 -07:00
|
|
|
if err := assertNextLineEquals(scanner, ".PP"); err != nil {
|
|
|
|
t.Fatalf("First line after SEE ALSO wasn't break-indent: %v", err)
|
2016-01-20 18:06:00 -07:00
|
|
|
}
|
2021-08-05 10:08:25 -07:00
|
|
|
if err := assertNextLineEquals(scanner, `\fBroot-bbb(1)\fP, \fBroot-ccc(1)\fP`); err != nil {
|
2017-11-02 08:22:38 -07:00
|
|
|
t.Fatalf("Second line after SEE ALSO wasn't correct: %v", err)
|
2016-01-20 18:06:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-20 08:27:44 -07:00
|
|
|
func TestManPrintFlagsHidesShortDeprecated(t *testing.T) {
|
2017-11-02 08:22:38 -07:00
|
|
|
c := &cobra.Command{}
|
|
|
|
c.Flags().StringP("foo", "f", "default", "Foo flag")
|
2021-02-07 17:08:50 -07:00
|
|
|
assertNoErr(t, c.Flags().MarkShorthandDeprecated("foo", "don't use it no more"))
|
2016-06-20 14:29:54 -07:00
|
|
|
|
2017-11-02 08:22:38 -07:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
manPrintFlags(buf, c.Flags())
|
2016-06-20 14:29:54 -07:00
|
|
|
|
2017-11-02 08:22:38 -07:00
|
|
|
got := buf.String()
|
2016-06-20 14:29:54 -07:00
|
|
|
expected := "**--foo**=\"default\"\n\tFoo flag\n\n"
|
2017-11-02 08:22:38 -07:00
|
|
|
if got != expected {
|
|
|
|
t.Errorf("Expected %v, got %v", expected, got)
|
2016-06-20 14:29:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 09:25:26 -07:00
|
|
|
func TestGenManTree(t *testing.T) {
|
2017-11-02 08:22:38 -07:00
|
|
|
c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}
|
2016-06-21 09:25:26 -07:00
|
|
|
header := &GenManHeader{Section: "2"}
|
2024-08-24 04:05:26 -07:00
|
|
|
tmpdir, err := os.MkdirTemp("", "test-gen-man-tree")
|
2016-06-21 09:25:26 -07:00
|
|
|
if err != nil {
|
2017-04-19 05:39:58 -07:00
|
|
|
t.Fatalf("Failed to create tmpdir: %s", err.Error())
|
2016-06-21 09:25:26 -07:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
2017-11-02 08:22:38 -07:00
|
|
|
if err := GenManTree(c, header, tmpdir); err != nil {
|
2016-06-21 09:25:26 -07:00
|
|
|
t.Fatalf("GenManTree failed: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(tmpdir, "do.2")); err != nil {
|
|
|
|
t.Fatalf("Expected file 'do.2' to exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
if header.Title != "" {
|
|
|
|
t.Fatalf("Expected header.Title to be unmodified")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 08:22:38 -07:00
|
|
|
func assertLineFound(scanner *bufio.Scanner, expectedLine string) error {
|
2016-01-20 18:06:00 -07:00
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if line == expectedLine {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
2017-11-02 08:22:38 -07:00
|
|
|
return fmt.Errorf("scan failed: %s", err)
|
2016-01-20 18:06:00 -07:00
|
|
|
}
|
|
|
|
|
2017-11-02 08:22:38 -07:00
|
|
|
return fmt.Errorf("hit EOF before finding %v", expectedLine)
|
2016-01-20 18:06:00 -07:00
|
|
|
}
|
|
|
|
|
2017-11-02 08:22:38 -07:00
|
|
|
func assertNextLineEquals(scanner *bufio.Scanner, expectedLine string) error {
|
2016-01-20 18:06:00 -07:00
|
|
|
if scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if line == expectedLine {
|
|
|
|
return nil
|
|
|
|
}
|
2017-11-02 08:22:38 -07:00
|
|
|
return fmt.Errorf("got %v, not %v", line, expectedLine)
|
2016-01-20 18:06:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
2017-11-02 08:22:38 -07:00
|
|
|
return fmt.Errorf("scan failed: %v", err)
|
2016-01-20 18:06:00 -07:00
|
|
|
}
|
|
|
|
|
2017-11-02 08:22:38 -07:00
|
|
|
return fmt.Errorf("hit EOF before finding %v", expectedLine)
|
2016-01-20 18:06:00 -07:00
|
|
|
}
|
2017-04-24 11:40:15 -07:00
|
|
|
|
|
|
|
func BenchmarkGenManToFile(b *testing.B) {
|
2024-08-24 04:05:26 -07:00
|
|
|
file, err := os.CreateTemp("", "")
|
2017-04-24 11:40:15 -07:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-11-02 08:22:38 -07:00
|
|
|
if err := GenMan(rootCmd, nil, file); err != nil {
|
2017-04-24 11:40:15 -07:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|