Skip to content
Myr

man_docs_test.go

This file

Path

doc/man_docs_test.go

231 lines · 6.4 KiB · Go

Last changed by

Update to latest go-md2man (#2201)Mikel Olasagasti Urangabefore adoption8519630

History of this file

Take it

The walk stopped after 200 commits: lines older than 89d7b83 point at it.

update copyright year (#1927)Unai Martinez-Corralbefore adoption9e6b58a

line 1why, one step back
1// Copyright 2013-2023 The Cobra Authors

add missing license headers (#1809)Unai Martinez-Corralbefore adoption6d978a9

lines 2–14why, one step back
2//
3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 15–35why, one step back
15package doc
1617import (18 "bufio"19 "bytes"20 "fmt"21 "os"22 "path/filepath"23 "strings"24 "testing"2526 "github.com/spf13/cobra"27)2829func assertNoErr(t *testing.T, e error) {30 if e != nil {31 t.Error(e)32 }33}3435func translate(in string) string {

Prefer ReplaceAll instead of Replace(..., -1) (#1530)Hugobefore adoption25f5bb5

line 36why, one step back
36 return strings.ReplaceAll(in, "-", "\\-")

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 37–53why, one step back
37}
3839func TestGenManDoc(t *testing.T) {40 header := &GenManHeader{41 Title: "Project",42 Section: "2",43 }4445 // We generate on a subcommand so we have both subcommands and parents46 buf := new(bytes.Buffer)47 if err := GenMan(echoCmd, header, buf); err != nil {48 t.Fatal(err)49 }50 output := buf.String()5152 // Make sure parent has - in CommandPath() in SEE ALSO:53 parentPath := echoCmd.Parent().CommandPath()

Prefer ReplaceAll instead of Replace(..., -1) (#1530)Hugobefore adoption25f5bb5

line 54why, one step back
54 dashParentPath := strings.ReplaceAll(parentPath, " ", "-")

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 55–88why, one step back
55 expected := translate(dashParentPath)
56 expected = expected + "(" + header.Section + ")"57 checkStringContains(t, output, expected)5859 checkStringContains(t, output, translate(echoCmd.Name()))60 checkStringContains(t, output, translate(echoCmd.Name()))61 checkStringContains(t, output, "boolone")62 checkStringContains(t, output, "rootflag")63 checkStringContains(t, output, translate(rootCmd.Name()))64 checkStringContains(t, output, translate(echoSubCmd.Name()))65 checkStringOmits(t, output, translate(deprecatedCmd.Name()))66 checkStringContains(t, output, translate("Auto generated"))67}6869func TestGenManNoHiddenParents(t *testing.T) {70 header := &GenManHeader{71 Title: "Project",72 Section: "2",73 }7475 // We generate on a subcommand so we have both subcommands and parents76 for _, name := range []string{"rootflag", "strtwo"} {77 f := rootCmd.PersistentFlags().Lookup(name)78 f.Hidden = true79 defer func() { f.Hidden = false }()80 }81 buf := new(bytes.Buffer)82 if err := GenMan(echoCmd, header, buf); err != nil {83 t.Fatal(err)84 }85 output := buf.String()8687 // Make sure parent has - in CommandPath() in SEE ALSO:88 parentPath := echoCmd.Parent().CommandPath()

Prefer ReplaceAll instead of Replace(..., -1) (#1530)Hugobefore adoption25f5bb5

line 89why, one step back
89 dashParentPath := strings.ReplaceAll(parentPath, " ", "-")

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 90–148why, one step back
90 expected := translate(dashParentPath)
91 expected = expected + "(" + header.Section + ")"92 checkStringContains(t, output, expected)9394 checkStringContains(t, output, translate(echoCmd.Name()))95 checkStringContains(t, output, translate(echoCmd.Name()))96 checkStringContains(t, output, "boolone")97 checkStringOmits(t, output, "rootflag")98 checkStringContains(t, output, translate(rootCmd.Name()))99 checkStringContains(t, output, translate(echoSubCmd.Name()))100 checkStringOmits(t, output, translate(deprecatedCmd.Name()))101 checkStringContains(t, output, translate("Auto generated"))102 checkStringOmits(t, output, "OPTIONS INHERITED FROM PARENT COMMANDS")103}104105func TestGenManNoGenTag(t *testing.T) {106 echoCmd.DisableAutoGenTag = true107 defer func() { echoCmd.DisableAutoGenTag = false }()108109 header := &GenManHeader{110 Title: "Project",111 Section: "2",112 }113114 // We generate on a subcommand so we have both subcommands and parents115 buf := new(bytes.Buffer)116 if err := GenMan(echoCmd, header, buf); err != nil {117 t.Fatal(err)118 }119 output := buf.String()120121 unexpected := translate("#HISTORY")122 checkStringOmits(t, output, unexpected)123 unexpected = translate("Auto generated by spf13/cobra")124 checkStringOmits(t, output, unexpected)125}126127func TestGenManSeeAlso(t *testing.T) {128 rootCmd := &cobra.Command{Use: "root", Run: emptyRun}129 aCmd := &cobra.Command{Use: "aaa", Run: emptyRun, Hidden: true} // #229130 bCmd := &cobra.Command{Use: "bbb", Run: emptyRun}131 cCmd := &cobra.Command{Use: "ccc", Run: emptyRun}132 rootCmd.AddCommand(aCmd, bCmd, cCmd)133134 buf := new(bytes.Buffer)135 header := &GenManHeader{}136 if err := GenMan(rootCmd, header, buf); err != nil {137 t.Fatal(err)138 }139 scanner := bufio.NewScanner(buf)140141 if err := assertLineFound(scanner, ".SH SEE ALSO"); err != nil {142 t.Fatalf("Couldn't find SEE ALSO section header: %v", err)143 }144 if err := assertNextLineEquals(scanner, `\fBroot-bbb(1)\fP, \fBroot-ccc(1)\fP`); err != nil {145 t.Fatalf("Second line after SEE ALSO wasn't correct: %v", err)146 }147}148

doc: fix typo, Deperecated -> Deprecated (#2000)gocurrbefore adoption60d056d

line 149why, one step back
149func TestManPrintFlagsHidesShortDeprecated(t *testing.T) {

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 150–166why, one step back
150 c := &cobra.Command{}
151 c.Flags().StringP("foo", "f", "default", "Foo flag")152 assertNoErr(t, c.Flags().MarkShorthandDeprecated("foo", "don't use it no more"))153154 buf := new(bytes.Buffer)155 manPrintFlags(buf, c.Flags())156157 got := buf.String()158 expected := "**--foo**=\"default\"\n\tFoo flag\n\n"159 if got != expected {160 t.Errorf("Expected %v, got %v", expected, got)161 }162}163164func TestGenManTree(t *testing.T) {165 c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"}166 header := &GenManHeader{Section: "2"}

Replace deprecated ioutil usage (#2181)Nir Sofferbefore adoption511af59

line 167why, one step back
167 tmpdir, err := os.MkdirTemp("", "test-gen-man-tree")

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 168–217why, one step back
168 if err != nil {
169 t.Fatalf("Failed to create tmpdir: %s", err.Error())170 }171 defer os.RemoveAll(tmpdir)172173 if err := GenManTree(c, header, tmpdir); err != nil {174 t.Fatalf("GenManTree failed: %s", err.Error())175 }176177 if _, err := os.Stat(filepath.Join(tmpdir, "do.2")); err != nil {178 t.Fatalf("Expected file 'do.2' to exist")179 }180181 if header.Title != "" {182 t.Fatalf("Expected header.Title to be unmodified")183 }184}185186func assertLineFound(scanner *bufio.Scanner, expectedLine string) error {187 for scanner.Scan() {188 line := scanner.Text()189 if line == expectedLine {190 return nil191 }192 }193194 if err := scanner.Err(); err != nil {195 return fmt.Errorf("scan failed: %s", err)196 }197198 return fmt.Errorf("hit EOF before finding %v", expectedLine)199}200201func assertNextLineEquals(scanner *bufio.Scanner, expectedLine string) error {202 if scanner.Scan() {203 line := scanner.Text()204 if line == expectedLine {205 return nil206 }207 return fmt.Errorf("got %v, not %v", line, expectedLine)208 }209210 if err := scanner.Err(); err != nil {211 return fmt.Errorf("scan failed: %v", err)212 }213214 return fmt.Errorf("hit EOF before finding %v", expectedLine)215}216217func BenchmarkGenManToFile(b *testing.B) {

Replace deprecated ioutil usage (#2181)Nir Sofferbefore adoption511af59

line 218why, one step back
218 file, err := os.CreateTemp("", "")

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 219–231why, one step back
219 if err != nil {
220 b.Fatal(err)221 }222 defer os.Remove(file.Name())223 defer file.Close()224225 b.ResetTimer()226 for i := 0; i < b.N; i++ {227 if err := GenMan(rootCmd, nil, file); err != nil {228 b.Fatal(err)229 }230 }231}