github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/alias/list/list_test.go (about) 1 package list 2 3 import ( 4 "bytes" 5 "io" 6 "testing" 7 8 "github.com/MakeNowJust/heredoc" 9 "github.com/ungtb10d/cli/v2/internal/config" 10 "github.com/ungtb10d/cli/v2/pkg/cmdutil" 11 "github.com/ungtb10d/cli/v2/pkg/iostreams" 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestAliasList(t *testing.T) { 17 tests := []struct { 18 name string 19 config string 20 isTTY bool 21 wantErr bool 22 wantStdout string 23 wantStderr string 24 }{ 25 { 26 name: "empty", 27 config: "", 28 isTTY: true, 29 wantErr: true, 30 wantStdout: "", 31 wantStderr: "", 32 }, 33 { 34 name: "some", 35 config: heredoc.Doc(` 36 aliases: 37 co: pr checkout 38 gc: "!gh gist create \"$@\" | pbcopy" 39 `), 40 isTTY: true, 41 wantStdout: "co: pr checkout\ngc: !gh gist create \"$@\" | pbcopy\n", 42 wantStderr: "", 43 }, 44 } 45 for _, tt := range tests { 46 t.Run(tt.name, func(t *testing.T) { 47 cfg := config.NewFromString(tt.config) 48 49 ios, _, stdout, stderr := iostreams.Test() 50 ios.SetStdoutTTY(tt.isTTY) 51 ios.SetStdinTTY(tt.isTTY) 52 ios.SetStderrTTY(tt.isTTY) 53 54 factory := &cmdutil.Factory{ 55 IOStreams: ios, 56 Config: func() (config.Config, error) { 57 return cfg, nil 58 }, 59 } 60 61 cmd := NewCmdList(factory, nil) 62 cmd.SetArgs([]string{}) 63 64 cmd.SetIn(&bytes.Buffer{}) 65 cmd.SetOut(io.Discard) 66 cmd.SetErr(io.Discard) 67 68 _, err := cmd.ExecuteC() 69 if tt.wantErr { 70 require.Error(t, err) 71 } else { 72 require.NoError(t, err) 73 } 74 75 assert.Equal(t, tt.wantStdout, stdout.String()) 76 assert.Equal(t, tt.wantStderr, stderr.String()) 77 }) 78 } 79 }