Skip to content

Commit

Permalink
feat: gist rename test
Browse files Browse the repository at this point in the history
  • Loading branch information
wingkwong committed Mar 21, 2023
1 parent b705ea9 commit 3b27e06
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions pkg/cmd/gist/rename/rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package rename

import (
"bytes"
"net/http"
"testing"

"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/pkg/cmd/gist/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/google/shlex"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -81,3 +85,101 @@ func TestNewCmdRename(t *testing.T) {
})
}
}

func TestRenameRun(t *testing.T) {
tests := []struct {
name string
opts *RenameOptions
gist *shared.Gist
httpStubs func(*httpmock.Registry)
nontty bool
stdin string
wantOut string
wantParams map[string]interface{}
}{
{
name: "no such gist",
wantOut: "gist not found: 1234",
},
{
name: "rename from old.txt to new.txt",
opts: &RenameOptions{
Selector: "1234",
OldFileName: "old.txt",
NewFileName: "new.txt",
},
gist: &shared.Gist{
ID: "1234",
Files: map[string]*shared.GistFile{
"old.txt": {
Filename: "old.txt",
Type: "text/plain",
},
},
Owner: &shared.GistOwner{Login: "octocat"},
},
httpStubs: func(reg *httpmock.Registry) {
reg.Register(httpmock.REST("POST", "gists/1234"),
httpmock.StatusStringResponse(201, "{}"))
},
wantParams: map[string]interface{}{
"files": map[string]interface{}{
"new.txt": map[string]interface{}{
"filename": "new.txt",
"type": "text/plain",
},
},
},
},
}

for _, tt := range tests {
reg := &httpmock.Registry{}
if tt.gist == nil {
reg.Register(httpmock.REST("GET", "gists/1234"),
httpmock.StatusStringResponse(404, "Not Found"))
} else {
reg.Register(httpmock.REST("GET", "gists/1234"),
httpmock.JSONResponse(tt.gist))
reg.Register(httpmock.GraphQL(`query UserCurrent\b`),
httpmock.StringResponse(`{"data":{"viewer":{"login":"octocat"}}}`))
}

if tt.httpStubs != nil {
tt.httpStubs(reg)
}

if tt.opts == nil {
tt.opts = &RenameOptions{}
}

tt.opts.HttpClient = func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil
}
ios, stdin, stdout, stderr := iostreams.Test()
stdin.WriteString(tt.stdin)
ios.SetStdoutTTY(!tt.nontty)
ios.SetStdinTTY(!tt.nontty)

tt.opts.Selector = "1234"
tt.opts.OldFileName = "old.txt"
tt.opts.NewFileName = "new.txt"
tt.opts.IO = ios

tt.opts.Config = func() (config.Config, error) {
return config.NewBlankConfig(), nil
}

t.Run(tt.name, func(t *testing.T) {
err := renameRun(tt.opts)
reg.Verify(t)
if tt.wantOut != "" {
assert.EqualError(t, err, tt.wantOut)
return
}
assert.NoError(t, err)
assert.Equal(t, "", stdout.String())
assert.Equal(t, "", stderr.String())
})
}
}

0 comments on commit 3b27e06

Please sign in to comment.