mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
+23
-15
@@ -6,16 +6,16 @@ import (
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/coder/coder/cli/clibase"
|
||||
"github.com/coder/coder/cli/cliui"
|
||||
)
|
||||
|
||||
type format struct {
|
||||
id string
|
||||
attachFlagsFn func(cmd *cobra.Command)
|
||||
formatFn func(ctx context.Context, data any) (string, error)
|
||||
id string
|
||||
attachOptionsFn func(opts *clibase.OptionSet)
|
||||
formatFn func(ctx context.Context, data any) (string, error)
|
||||
}
|
||||
|
||||
var _ cliui.OutputFormat = &format{}
|
||||
@@ -24,9 +24,9 @@ func (f *format) ID() string {
|
||||
return f.id
|
||||
}
|
||||
|
||||
func (f *format) AttachFlags(cmd *cobra.Command) {
|
||||
if f.attachFlagsFn != nil {
|
||||
f.attachFlagsFn(cmd)
|
||||
func (f *format) AttachOptions(opts *clibase.OptionSet) {
|
||||
if f.attachOptionsFn != nil {
|
||||
f.attachOptionsFn(opts)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,8 +82,14 @@ func Test_OutputFormatter(t *testing.T) {
|
||||
cliui.JSONFormat(),
|
||||
&format{
|
||||
id: "foo",
|
||||
attachFlagsFn: func(cmd *cobra.Command) {
|
||||
cmd.Flags().StringP("foo", "f", "", "foo flag 1234")
|
||||
attachOptionsFn: func(opts *clibase.OptionSet) {
|
||||
opts.Add(clibase.Option{
|
||||
Name: "foo",
|
||||
Flag: "foo",
|
||||
FlagShorthand: "f",
|
||||
Value: clibase.DiscardValue,
|
||||
Description: "foo flag 1234",
|
||||
})
|
||||
},
|
||||
formatFn: func(_ context.Context, _ any) (string, error) {
|
||||
atomic.AddInt64(&called, 1)
|
||||
@@ -92,13 +98,15 @@ func Test_OutputFormatter(t *testing.T) {
|
||||
},
|
||||
)
|
||||
|
||||
cmd := &cobra.Command{}
|
||||
f.AttachFlags(cmd)
|
||||
cmd := &clibase.Cmd{}
|
||||
f.AttachOptions(&cmd.Options)
|
||||
|
||||
selected, err := cmd.Flags().GetString("output")
|
||||
fs := cmd.Options.FlagSet()
|
||||
|
||||
selected, err := fs.GetString("output")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "json", selected)
|
||||
usage := cmd.Flags().FlagUsages()
|
||||
usage := fs.FlagUsages()
|
||||
require.Contains(t, usage, "Available formats: json, foo")
|
||||
require.Contains(t, usage, "foo flag 1234")
|
||||
|
||||
@@ -112,13 +120,13 @@ func Test_OutputFormatter(t *testing.T) {
|
||||
require.Equal(t, data, got)
|
||||
require.EqualValues(t, 0, atomic.LoadInt64(&called))
|
||||
|
||||
require.NoError(t, cmd.Flags().Set("output", "foo"))
|
||||
require.NoError(t, fs.Set("output", "foo"))
|
||||
out, err = f.Format(ctx, data)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "foo", out)
|
||||
require.EqualValues(t, 1, atomic.LoadInt64(&called))
|
||||
|
||||
require.NoError(t, cmd.Flags().Set("output", "bar"))
|
||||
require.NoError(t, fs.Set("output", "bar"))
|
||||
out, err = f.Format(ctx, data)
|
||||
require.Error(t, err)
|
||||
require.ErrorContains(t, err, "bar")
|
||||
|
||||
Reference in New Issue
Block a user