github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/workflow/list/list_test.go (about)

     1  package list
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"testing"
     9  
    10  	"github.com/cli/cli/internal/ghrepo"
    11  	"github.com/cli/cli/pkg/cmd/workflow/shared"
    12  	"github.com/cli/cli/pkg/cmdutil"
    13  	"github.com/cli/cli/pkg/httpmock"
    14  	"github.com/cli/cli/pkg/iostreams"
    15  	"github.com/google/shlex"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func Test_NewCmdList(t *testing.T) {
    20  	tests := []struct {
    21  		name     string
    22  		cli      string
    23  		tty      bool
    24  		wants    ListOptions
    25  		wantsErr bool
    26  	}{
    27  		{
    28  			name: "blank tty",
    29  			tty:  true,
    30  			wants: ListOptions{
    31  				Limit: defaultLimit,
    32  			},
    33  		},
    34  		{
    35  			name: "blank nontty",
    36  			wants: ListOptions{
    37  				Limit:       defaultLimit,
    38  				PlainOutput: true,
    39  			},
    40  		},
    41  		{
    42  			name: "all",
    43  			cli:  "--all",
    44  			wants: ListOptions{
    45  				Limit:       defaultLimit,
    46  				PlainOutput: true,
    47  				All:         true,
    48  			},
    49  		},
    50  		{
    51  			name: "limit",
    52  			cli:  "--limit 100",
    53  			wants: ListOptions{
    54  				Limit:       100,
    55  				PlainOutput: true,
    56  			},
    57  		},
    58  		{
    59  			name:     "bad limit",
    60  			cli:      "--limit hi",
    61  			wantsErr: true,
    62  		},
    63  	}
    64  
    65  	for _, tt := range tests {
    66  		t.Run(tt.name, func(t *testing.T) {
    67  			io, _, _, _ := iostreams.Test()
    68  			io.SetStdinTTY(tt.tty)
    69  			io.SetStdoutTTY(tt.tty)
    70  
    71  			f := &cmdutil.Factory{
    72  				IOStreams: io,
    73  			}
    74  
    75  			argv, err := shlex.Split(tt.cli)
    76  			assert.NoError(t, err)
    77  
    78  			var gotOpts *ListOptions
    79  			cmd := NewCmdList(f, func(opts *ListOptions) error {
    80  				gotOpts = opts
    81  				return nil
    82  			})
    83  			cmd.SetArgs(argv)
    84  			cmd.SetIn(&bytes.Buffer{})
    85  			cmd.SetOut(ioutil.Discard)
    86  			cmd.SetErr(ioutil.Discard)
    87  
    88  			_, err = cmd.ExecuteC()
    89  			if tt.wantsErr {
    90  				assert.Error(t, err)
    91  				return
    92  			}
    93  
    94  			assert.Equal(t, tt.wants.Limit, gotOpts.Limit)
    95  			assert.Equal(t, tt.wants.PlainOutput, gotOpts.PlainOutput)
    96  		})
    97  	}
    98  }
    99  
   100  func TestListRun(t *testing.T) {
   101  	workflows := []shared.Workflow{
   102  		{
   103  			Name:  "Go",
   104  			State: shared.Active,
   105  			ID:    707,
   106  		},
   107  		{
   108  			Name:  "Linter",
   109  			State: shared.Active,
   110  			ID:    666,
   111  		},
   112  		{
   113  			Name:  "Release",
   114  			State: shared.DisabledManually,
   115  			ID:    451,
   116  		},
   117  	}
   118  	payload := shared.WorkflowsPayload{Workflows: workflows}
   119  
   120  	tests := []struct {
   121  		name       string
   122  		opts       *ListOptions
   123  		wantOut    string
   124  		wantErrOut string
   125  		stubs      func(*httpmock.Registry)
   126  		tty        bool
   127  	}{
   128  		{
   129  			name: "blank tty",
   130  			tty:  true,
   131  			opts: &ListOptions{
   132  				Limit: defaultLimit,
   133  			},
   134  			wantOut: "Go      active  707\nLinter  active  666\n",
   135  		},
   136  		{
   137  			name: "blank nontty",
   138  			opts: &ListOptions{
   139  				Limit:       defaultLimit,
   140  				PlainOutput: true,
   141  			},
   142  			wantOut: "Go\tactive\t707\nLinter\tactive\t666\n",
   143  		},
   144  		{
   145  			name: "pagination",
   146  			opts: &ListOptions{
   147  				Limit: 101,
   148  			},
   149  			stubs: func(reg *httpmock.Registry) {
   150  				workflows := []shared.Workflow{}
   151  				var flowID int64
   152  				for flowID = 0; flowID < 103; flowID++ {
   153  					workflows = append(workflows, shared.Workflow{
   154  						ID:    flowID,
   155  						Name:  fmt.Sprintf("flow %d", flowID),
   156  						State: shared.Active,
   157  					})
   158  				}
   159  				reg.Register(
   160  					httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"),
   161  					httpmock.JSONResponse(shared.WorkflowsPayload{
   162  						Workflows: workflows[0:100],
   163  					}))
   164  				reg.Register(
   165  					httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"),
   166  					httpmock.JSONResponse(shared.WorkflowsPayload{
   167  						Workflows: workflows[100:],
   168  					}))
   169  			},
   170  			wantOut: longOutput,
   171  		},
   172  		{
   173  			name: "no results nontty",
   174  			opts: &ListOptions{
   175  				Limit:       defaultLimit,
   176  				PlainOutput: true,
   177  			},
   178  			stubs: func(reg *httpmock.Registry) {
   179  				reg.Register(
   180  					httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"),
   181  					httpmock.JSONResponse(shared.WorkflowsPayload{}),
   182  				)
   183  			},
   184  			wantOut: "",
   185  		},
   186  		{
   187  			name: "no results tty",
   188  			opts: &ListOptions{
   189  				Limit: defaultLimit,
   190  			},
   191  			tty: true,
   192  			stubs: func(reg *httpmock.Registry) {
   193  				reg.Register(
   194  					httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"),
   195  					httpmock.JSONResponse(shared.WorkflowsPayload{}),
   196  				)
   197  			},
   198  			wantOut:    "",
   199  			wantErrOut: "No workflows found\n",
   200  		},
   201  		{
   202  			name: "show all workflows",
   203  			opts: &ListOptions{
   204  				Limit: defaultLimit,
   205  				All:   true,
   206  			},
   207  			tty:     true,
   208  			wantOut: "Go       active             707\nLinter   active             666\nRelease  disabled_manually  451\n",
   209  		},
   210  		{
   211  			name: "respects limit",
   212  			opts: &ListOptions{
   213  				Limit: 1,
   214  				All:   true,
   215  			},
   216  			tty:     true,
   217  			wantOut: "Go  active  707\n",
   218  		},
   219  	}
   220  
   221  	for _, tt := range tests {
   222  		t.Run(tt.name, func(t *testing.T) {
   223  			reg := &httpmock.Registry{}
   224  			if tt.stubs == nil {
   225  				reg.Register(
   226  					httpmock.REST("GET", "repos/OWNER/REPO/actions/workflows"),
   227  					httpmock.JSONResponse(payload),
   228  				)
   229  			} else {
   230  				tt.stubs(reg)
   231  			}
   232  
   233  			tt.opts.HttpClient = func() (*http.Client, error) {
   234  				return &http.Client{Transport: reg}, nil
   235  			}
   236  
   237  			io, _, stdout, stderr := iostreams.Test()
   238  			io.SetStdoutTTY(tt.tty)
   239  			tt.opts.IO = io
   240  			tt.opts.BaseRepo = func() (ghrepo.Interface, error) {
   241  				return ghrepo.FromFullName("OWNER/REPO")
   242  			}
   243  
   244  			err := listRun(tt.opts)
   245  			assert.NoError(t, err)
   246  
   247  			assert.Equal(t, tt.wantOut, stdout.String())
   248  			assert.Equal(t, tt.wantErrOut, stderr.String())
   249  			reg.Verify(t)
   250  		})
   251  	}
   252  }
   253  
   254  const longOutput = "flow 0\tactive\t0\nflow 1\tactive\t1\nflow 2\tactive\t2\nflow 3\tactive\t3\nflow 4\tactive\t4\nflow 5\tactive\t5\nflow 6\tactive\t6\nflow 7\tactive\t7\nflow 8\tactive\t8\nflow 9\tactive\t9\nflow 10\tactive\t10\nflow 11\tactive\t11\nflow 12\tactive\t12\nflow 13\tactive\t13\nflow 14\tactive\t14\nflow 15\tactive\t15\nflow 16\tactive\t16\nflow 17\tactive\t17\nflow 18\tactive\t18\nflow 19\tactive\t19\nflow 20\tactive\t20\nflow 21\tactive\t21\nflow 22\tactive\t22\nflow 23\tactive\t23\nflow 24\tactive\t24\nflow 25\tactive\t25\nflow 26\tactive\t26\nflow 27\tactive\t27\nflow 28\tactive\t28\nflow 29\tactive\t29\nflow 30\tactive\t30\nflow 31\tactive\t31\nflow 32\tactive\t32\nflow 33\tactive\t33\nflow 34\tactive\t34\nflow 35\tactive\t35\nflow 36\tactive\t36\nflow 37\tactive\t37\nflow 38\tactive\t38\nflow 39\tactive\t39\nflow 40\tactive\t40\nflow 41\tactive\t41\nflow 42\tactive\t42\nflow 43\tactive\t43\nflow 44\tactive\t44\nflow 45\tactive\t45\nflow 46\tactive\t46\nflow 47\tactive\t47\nflow 48\tactive\t48\nflow 49\tactive\t49\nflow 50\tactive\t50\nflow 51\tactive\t51\nflow 52\tactive\t52\nflow 53\tactive\t53\nflow 54\tactive\t54\nflow 55\tactive\t55\nflow 56\tactive\t56\nflow 57\tactive\t57\nflow 58\tactive\t58\nflow 59\tactive\t59\nflow 60\tactive\t60\nflow 61\tactive\t61\nflow 62\tactive\t62\nflow 63\tactive\t63\nflow 64\tactive\t64\nflow 65\tactive\t65\nflow 66\tactive\t66\nflow 67\tactive\t67\nflow 68\tactive\t68\nflow 69\tactive\t69\nflow 70\tactive\t70\nflow 71\tactive\t71\nflow 72\tactive\t72\nflow 73\tactive\t73\nflow 74\tactive\t74\nflow 75\tactive\t75\nflow 76\tactive\t76\nflow 77\tactive\t77\nflow 78\tactive\t78\nflow 79\tactive\t79\nflow 80\tactive\t80\nflow 81\tactive\t81\nflow 82\tactive\t82\nflow 83\tactive\t83\nflow 84\tactive\t84\nflow 85\tactive\t85\nflow 86\tactive\t86\nflow 87\tactive\t87\nflow 88\tactive\t88\nflow 89\tactive\t89\nflow 90\tactive\t90\nflow 91\tactive\t91\nflow 92\tactive\t92\nflow 93\tactive\t93\nflow 94\tactive\t94\nflow 95\tactive\t95\nflow 96\tactive\t96\nflow 97\tactive\t97\nflow 98\tactive\t98\nflow 99\tactive\t99\nflow 100\tactive\t100\n"