github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/pr/diff/diff_test.go (about) 1 package diff 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "net/http" 7 "testing" 8 9 "github.com/cli/cli/api" 10 "github.com/cli/cli/context" 11 "github.com/cli/cli/internal/ghrepo" 12 "github.com/cli/cli/pkg/cmd/pr/shared" 13 "github.com/cli/cli/pkg/cmdutil" 14 "github.com/cli/cli/pkg/httpmock" 15 "github.com/cli/cli/pkg/iostreams" 16 "github.com/cli/cli/test" 17 "github.com/google/go-cmp/cmp" 18 "github.com/google/shlex" 19 "github.com/stretchr/testify/assert" 20 "github.com/stretchr/testify/require" 21 ) 22 23 func Test_NewCmdDiff(t *testing.T) { 24 tests := []struct { 25 name string 26 args string 27 isTTY bool 28 want DiffOptions 29 wantErr string 30 }{ 31 { 32 name: "number argument", 33 args: "123", 34 isTTY: true, 35 want: DiffOptions{ 36 SelectorArg: "123", 37 UseColor: "auto", 38 }, 39 }, 40 { 41 name: "no argument", 42 args: "", 43 isTTY: true, 44 want: DiffOptions{ 45 SelectorArg: "", 46 UseColor: "auto", 47 }, 48 }, 49 { 50 name: "no color when redirected", 51 args: "", 52 isTTY: false, 53 want: DiffOptions{ 54 SelectorArg: "", 55 UseColor: "never", 56 }, 57 }, 58 { 59 name: "no argument with --repo override", 60 args: "-R owner/repo", 61 isTTY: true, 62 wantErr: "argument required when using the --repo flag", 63 }, 64 { 65 name: "invalid --color argument", 66 args: "--color doublerainbow", 67 isTTY: true, 68 wantErr: `did not understand color: "doublerainbow". Expected one of always, never, or auto`, 69 }, 70 } 71 for _, tt := range tests { 72 t.Run(tt.name, func(t *testing.T) { 73 io, _, _, _ := iostreams.Test() 74 io.SetStdoutTTY(tt.isTTY) 75 io.SetStdinTTY(tt.isTTY) 76 io.SetStderrTTY(tt.isTTY) 77 78 f := &cmdutil.Factory{ 79 IOStreams: io, 80 } 81 82 var opts *DiffOptions 83 cmd := NewCmdDiff(f, func(o *DiffOptions) error { 84 opts = o 85 return nil 86 }) 87 cmd.PersistentFlags().StringP("repo", "R", "", "") 88 89 argv, err := shlex.Split(tt.args) 90 require.NoError(t, err) 91 cmd.SetArgs(argv) 92 93 cmd.SetIn(&bytes.Buffer{}) 94 cmd.SetOut(ioutil.Discard) 95 cmd.SetErr(ioutil.Discard) 96 97 _, err = cmd.ExecuteC() 98 if tt.wantErr != "" { 99 require.EqualError(t, err, tt.wantErr) 100 return 101 } else { 102 require.NoError(t, err) 103 } 104 105 assert.Equal(t, tt.want.SelectorArg, opts.SelectorArg) 106 assert.Equal(t, tt.want.UseColor, opts.UseColor) 107 }) 108 } 109 } 110 111 func runCommand(rt http.RoundTripper, remotes context.Remotes, isTTY bool, cli string) (*test.CmdOut, error) { 112 io, _, stdout, stderr := iostreams.Test() 113 io.SetStdoutTTY(isTTY) 114 io.SetStdinTTY(isTTY) 115 io.SetStderrTTY(isTTY) 116 117 factory := &cmdutil.Factory{ 118 IOStreams: io, 119 HttpClient: func() (*http.Client, error) { 120 return &http.Client{Transport: rt}, nil 121 }, 122 } 123 124 cmd := NewCmdDiff(factory, nil) 125 126 argv, err := shlex.Split(cli) 127 if err != nil { 128 return nil, err 129 } 130 cmd.SetArgs(argv) 131 132 cmd.SetIn(&bytes.Buffer{}) 133 cmd.SetOut(ioutil.Discard) 134 cmd.SetErr(ioutil.Discard) 135 136 _, err = cmd.ExecuteC() 137 return &test.CmdOut{ 138 OutBuf: stdout, 139 ErrBuf: stderr, 140 }, err 141 } 142 143 func TestPRDiff_notty(t *testing.T) { 144 http := &httpmock.Registry{} 145 defer http.Verify(t) 146 147 shared.RunCommandFinder("", &api.PullRequest{Number: 123}, ghrepo.New("OWNER", "REPO")) 148 149 http.Register( 150 httpmock.REST("GET", "repos/OWNER/REPO/pulls/123"), 151 httpmock.StringResponse(testDiff)) 152 153 output, err := runCommand(http, nil, false, "") 154 if err != nil { 155 t.Fatalf("unexpected error: %s", err) 156 } 157 if diff := cmp.Diff(testDiff, output.String()); diff != "" { 158 t.Errorf("command output did not match:\n%s", diff) 159 } 160 } 161 162 func TestPRDiff_tty(t *testing.T) { 163 http := &httpmock.Registry{} 164 defer http.Verify(t) 165 166 shared.RunCommandFinder("123", &api.PullRequest{Number: 123}, ghrepo.New("OWNER", "REPO")) 167 168 http.Register( 169 httpmock.REST("GET", "repos/OWNER/REPO/pulls/123"), 170 httpmock.StringResponse(testDiff), 171 ) 172 173 output, err := runCommand(http, nil, true, "123") 174 if err != nil { 175 t.Fatalf("unexpected error: %s", err) 176 } 177 assert.Contains(t, output.String(), "\x1b[32m+site: bin/gh\x1b[m") 178 } 179 180 const testDiff = `diff --git a/.github/workflows/releases.yml b/.github/workflows/releases.yml 181 index 73974448..b7fc0154 100644 182 --- a/.github/workflows/releases.yml 183 +++ b/.github/workflows/releases.yml 184 @@ -44,6 +44,11 @@ jobs: 185 token: ${{secrets.SITE_GITHUB_TOKEN}} 186 - name: Publish documentation site 187 if: "!contains(github.ref, '-')" # skip prereleases 188 + env: 189 + GIT_COMMITTER_NAME: cli automation 190 + GIT_AUTHOR_NAME: cli automation 191 + GIT_COMMITTER_EMAIL: noreply@github.com 192 + GIT_AUTHOR_EMAIL: noreply@github.com 193 run: make site-publish 194 - name: Move project cards 195 if: "!contains(github.ref, '-')" # skip prereleases 196 diff --git a/Makefile b/Makefile 197 index f2b4805c..3d7bd0f9 100644 198 --- a/Makefile 199 +++ b/Makefile 200 @@ -22,8 +22,8 @@ test: 201 go test ./... 202 .PHONY: test 203 204 -site: 205 - git clone https://github.com/github/cli.github.com.git "$@" 206 +site: bin/gh 207 + bin/gh repo clone github/cli.github.com "$@" 208 209 site-docs: site 210 git -C site pull 211 `