|
| 1 | +package download |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io/ioutil" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/cli/cli/internal/ghrepo" |
| 10 | + "github.com/cli/cli/pkg/cmdutil" |
| 11 | + "github.com/cli/cli/pkg/iostreams" |
| 12 | + "github.com/google/shlex" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func Test_NewCmdDownload(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + args string |
| 21 | + isTTY bool |
| 22 | + want DownloadOptions |
| 23 | + wantErr string |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "empty", |
| 27 | + args: "", |
| 28 | + isTTY: true, |
| 29 | + wantErr: "either run ID or `--pattern` is required", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "with run ID", |
| 33 | + args: "2345", |
| 34 | + isTTY: true, |
| 35 | + want: DownloadOptions{ |
| 36 | + RunID: "2345", |
| 37 | + FilePatterns: []string(nil), |
| 38 | + DestinationDir: ".", |
| 39 | + }, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "to destination", |
| 43 | + args: "2345 -D tmp/dest", |
| 44 | + isTTY: true, |
| 45 | + want: DownloadOptions{ |
| 46 | + RunID: "2345", |
| 47 | + FilePatterns: []string(nil), |
| 48 | + DestinationDir: "tmp/dest", |
| 49 | + }, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "repo level with patterns", |
| 53 | + args: "-p one -p two", |
| 54 | + isTTY: true, |
| 55 | + want: DownloadOptions{ |
| 56 | + RunID: "", |
| 57 | + FilePatterns: []string{"one", "two"}, |
| 58 | + DestinationDir: ".", |
| 59 | + }, |
| 60 | + }, |
| 61 | + } |
| 62 | + for _, tt := range tests { |
| 63 | + t.Run(tt.name, func(t *testing.T) { |
| 64 | + io, _, _, _ := iostreams.Test() |
| 65 | + io.SetStdoutTTY(tt.isTTY) |
| 66 | + io.SetStdinTTY(tt.isTTY) |
| 67 | + io.SetStderrTTY(tt.isTTY) |
| 68 | + |
| 69 | + f := &cmdutil.Factory{ |
| 70 | + IOStreams: io, |
| 71 | + HttpClient: func() (*http.Client, error) { |
| 72 | + return nil, nil |
| 73 | + }, |
| 74 | + BaseRepo: func() (ghrepo.Interface, error) { |
| 75 | + return nil, nil |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + var opts *DownloadOptions |
| 80 | + cmd := NewCmdDownload(f, func(o *DownloadOptions) error { |
| 81 | + opts = o |
| 82 | + return nil |
| 83 | + }) |
| 84 | + cmd.PersistentFlags().StringP("repo", "R", "", "") |
| 85 | + |
| 86 | + argv, err := shlex.Split(tt.args) |
| 87 | + require.NoError(t, err) |
| 88 | + cmd.SetArgs(argv) |
| 89 | + |
| 90 | + cmd.SetIn(&bytes.Buffer{}) |
| 91 | + cmd.SetOut(ioutil.Discard) |
| 92 | + cmd.SetErr(ioutil.Discard) |
| 93 | + |
| 94 | + _, err = cmd.ExecuteC() |
| 95 | + if tt.wantErr != "" { |
| 96 | + require.EqualError(t, err, tt.wantErr) |
| 97 | + return |
| 98 | + } else { |
| 99 | + require.NoError(t, err) |
| 100 | + } |
| 101 | + |
| 102 | + assert.Equal(t, tt.want.RunID, opts.RunID) |
| 103 | + assert.Equal(t, tt.want.FilePatterns, opts.FilePatterns) |
| 104 | + assert.Equal(t, tt.want.DestinationDir, opts.DestinationDir) |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func Test_runDownload(t *testing.T) { |
| 110 | + tests := []struct { |
| 111 | + name string |
| 112 | + opts DownloadOptions |
| 113 | + artifacts []Artifact |
| 114 | + wantDownloaded []string |
| 115 | + wantErr string |
| 116 | + }{ |
| 117 | + { |
| 118 | + name: "download non-expired", |
| 119 | + opts: DownloadOptions{ |
| 120 | + RunID: "2345", |
| 121 | + DestinationDir: ".", |
| 122 | + FilePatterns: []string(nil), |
| 123 | + }, |
| 124 | + artifacts: []Artifact{ |
| 125 | + { |
| 126 | + Name: "artifact-1", |
| 127 | + DownloadURL: "http://download.com/artifact-1.zip", |
| 128 | + Expired: false, |
| 129 | + }, |
| 130 | + { |
| 131 | + Name: "expired-artifact", |
| 132 | + DownloadURL: "http://download.com/expired.zip", |
| 133 | + Expired: true, |
| 134 | + }, |
| 135 | + { |
| 136 | + Name: "artifact-2", |
| 137 | + DownloadURL: "http://download.com/artifact-2.zip", |
| 138 | + Expired: false, |
| 139 | + }, |
| 140 | + }, |
| 141 | + wantDownloaded: []string{ |
| 142 | + "http://download.com/artifact-1.zip", |
| 143 | + "http://download.com/artifact-2.zip", |
| 144 | + }, |
| 145 | + }, |
| 146 | + } |
| 147 | + for _, tt := range tests { |
| 148 | + t.Run(tt.name, func(t *testing.T) { |
| 149 | + opts := &tt.opts |
| 150 | + io, _, stdout, stderr := iostreams.Test() |
| 151 | + opts.IO = io |
| 152 | + platform := &stubPlatform{listResults: tt.artifacts} |
| 153 | + opts.Platform = platform |
| 154 | + |
| 155 | + err := runDownload(opts) |
| 156 | + if tt.wantErr != "" { |
| 157 | + require.EqualError(t, err, tt.wantErr) |
| 158 | + } else { |
| 159 | + require.NoError(t, err) |
| 160 | + } |
| 161 | + |
| 162 | + assert.Equal(t, tt.wantDownloaded, platform.downloaded) |
| 163 | + assert.Equal(t, "", stdout.String()) |
| 164 | + assert.Equal(t, "", stderr.String()) |
| 165 | + }) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +type stubPlatform struct { |
| 170 | + listResults []Artifact |
| 171 | + downloaded []string |
| 172 | +} |
| 173 | + |
| 174 | +func (p *stubPlatform) List(runID string) ([]Artifact, error) { |
| 175 | + return p.listResults, nil |
| 176 | +} |
| 177 | + |
| 178 | +func (p *stubPlatform) Download(url string, dir string) error { |
| 179 | + p.downloaded = append(p.downloaded, url) |
| 180 | + return nil |
| 181 | +} |
0 commit comments