/
command_test.go
68 lines (63 loc) · 1.82 KB
/
command_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2019 The Jaeger Authors.
//
// 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 docs
import (
"os"
"strings"
"testing"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestOutputFormats(t *testing.T) {
tests := []struct {
file string
flag string
err string
}{
{file: "docs.md"},
{file: "docs.1", flag: "--format=man"},
{file: "docs.rst", flag: "--format=rst"},
{file: "docs.yaml", flag: "--format=yaml"},
{flag: "--format=foo", err: "undefined value of format, possible values are: [md man rst yaml]"},
}
for _, test := range tests {
v := viper.New()
cmd := Command(v)
cmd.ParseFlags([]string{test.flag})
err := cmd.Execute()
if err == nil {
f, err := os.ReadFile(test.file)
require.NoError(t, err)
assert.True(t, strings.Contains(string(f), "documentation"))
} else {
assert.Equal(t, test.err, err.Error())
}
}
}
func TestDocsForParent(t *testing.T) {
parent := &cobra.Command{
Use: "root_command",
Short: "some description",
}
v := viper.New()
docs := Command(v)
parent.AddCommand(docs)
err := docs.RunE(docs, []string{})
require.NoError(t, err)
f, err := os.ReadFile("root_command.md")
require.NoError(t, err)
assert.True(t, strings.Contains(string(f), "some description"))
}