-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown_test.go
107 lines (92 loc) · 2.66 KB
/
markdown_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2024 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package cmd_test
import (
"bytes"
"errors"
"os"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/internal/cmd"
)
type markdownSuite struct{}
var _ = gc.Suite(&markdownSuite{})
// TestWriteError ensures that the cmd.PrintMarkdown function surfaces errors
// returned by the writer.
func (*markdownSuite) TestWriteError(c *gc.C) {
expectedErr := errors.New("foo")
writer := errorWriter{err: expectedErr}
command := &docTestCommand{
info: &cmd.Info{},
}
err := cmd.PrintMarkdown(writer, command, cmd.MarkdownOptions{})
c.Assert(err, gc.NotNil)
c.Check(err, gc.ErrorMatches, ".*foo")
}
// errorWriter is an io.Writer that returns an error whenever the Write method
// is called.
type errorWriter struct {
err error
}
func (e errorWriter) Write([]byte) (n int, err error) {
return 0, e.err
}
// TestOutput tests that the output of the PrintMarkdown function is
// fundamentally correct.
func (*markdownSuite) TestOutput(c *gc.C) {
seeAlso := []string{"clouds", "update-cloud", "remove-cloud", "update-credential"}
subcommands := map[string]string{
"foo": "foo the bar baz",
"bar": "bar the baz foo",
"baz": "baz the foo bar",
}
command := &docTestCommand{
info: &cmd.Info{
Name: "add-cloud",
Args: "<cloud name> [<cloud definition file>]",
Purpose: "Add a cloud definition to Juju.",
Doc: "details for add-cloud...",
Examples: "examples for add-cloud...",
SeeAlso: seeAlso,
Aliases: []string{"new-cloud", "cloud-add"},
Subcommands: subcommands,
},
flags: []testFlag{{
name: "force",
}, {
name: "file",
short: "f",
}, {
name: "credential",
short: "c",
}},
}
// These functions verify the provided argument is in the expected set.
linkForCommand := func(s string) string {
for _, cmd := range seeAlso {
if cmd == s {
return "https://docs.com/" + cmd
}
}
c.Fatalf("linkForCommand called with unexpected command %q", s)
return ""
}
linkForSubcommand := func(s string) string {
_, ok := subcommands[s]
if !ok {
c.Fatalf("linkForSubcommand called with unexpected subcommand %q", s)
}
return "https://docs.com/add-cloud/" + s
}
expected, err := os.ReadFile("testdata/add-cloud.md")
c.Assert(err, jc.ErrorIsNil)
var buf bytes.Buffer
err = cmd.PrintMarkdown(&buf, command, cmd.MarkdownOptions{
Title: `Command "juju add-cloud"`,
UsagePrefix: "juju ",
LinkForCommand: linkForCommand,
LinkForSubcommand: linkForSubcommand,
})
c.Assert(err, jc.ErrorIsNil)
c.Check(buf.String(), gc.Equals, string(expected))
}