github.com/secman-team/gh-api@v1.8.2/pkg/cmd/alias/list/list_test.go (about)

     1  package list
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"testing"
     7  
     8  	"github.com/MakeNowJust/heredoc"
     9  	"github.com/secman-team/gh-api/core/config"
    10  	"github.com/secman-team/gh-api/pkg/cmdutil"
    11  	"github.com/secman-team/gh-api/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  		wantStdout string
    22  		wantStderr string
    23  	}{
    24  		{
    25  			name:       "empty",
    26  			config:     "",
    27  			isTTY:      true,
    28  			wantStdout: "",
    29  			wantStderr: "no aliases configured\n",
    30  		},
    31  		{
    32  			name: "some",
    33  			config: heredoc.Doc(`
    34  				aliases:
    35  				  co: pr checkout
    36  				  gc: "!gh gist create \"$@\" | pbcopy"
    37  			`),
    38  			isTTY:      true,
    39  			wantStdout: "co:  pr checkout\ngc:  !gh gist create \"$@\" | pbcopy\n",
    40  			wantStderr: "",
    41  		},
    42  	}
    43  	for _, tt := range tests {
    44  		t.Run(tt.name, func(t *testing.T) {
    45  			// TODO: change underlying config implementation so Write is not
    46  			// automatically called when editing aliases in-memory
    47  			defer config.StubWriteConfig(ioutil.Discard, ioutil.Discard)()
    48  
    49  			cfg := config.NewFromString(tt.config)
    50  
    51  			io, _, stdout, stderr := iostreams.Test()
    52  			io.SetStdoutTTY(tt.isTTY)
    53  			io.SetStdinTTY(tt.isTTY)
    54  			io.SetStderrTTY(tt.isTTY)
    55  
    56  			factory := &cmdutil.Factory{
    57  				IOStreams: io,
    58  				Config: func() (config.Config, error) {
    59  					return cfg, nil
    60  				},
    61  			}
    62  
    63  			cmd := NewCmdList(factory, nil)
    64  			cmd.SetArgs([]string{})
    65  
    66  			cmd.SetIn(&bytes.Buffer{})
    67  			cmd.SetOut(ioutil.Discard)
    68  			cmd.SetErr(ioutil.Discard)
    69  
    70  			_, err := cmd.ExecuteC()
    71  			require.NoError(t, err)
    72  
    73  			assert.Equal(t, tt.wantStdout, stdout.String())
    74  			assert.Equal(t, tt.wantStderr, stderr.String())
    75  		})
    76  	}
    77  }