feat: Add support for creating autolink references
This commit is contained in:
parent
d6dba93586
commit
6cf2e9ee3e
11 changed files with 616 additions and 29 deletions
197
pkg/cmd/repo/autolink/create/create_test.go
Normal file
197
pkg/cmd/repo/autolink/create/create_test.go
Normal file
|
|
@ -0,0 +1,197 @@
|
|||
package create
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/MakeNowJust/heredoc"
|
||||
"github.com/cli/cli/v2/internal/browser"
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
"github.com/cli/cli/v2/pkg/cmd/repo/autolink/domain"
|
||||
"github.com/cli/cli/v2/pkg/cmdutil"
|
||||
"github.com/cli/cli/v2/pkg/iostreams"
|
||||
"github.com/google/shlex"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestNewCmdCreate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
output createOptions
|
||||
wantErr bool
|
||||
errMsg string
|
||||
}{
|
||||
{
|
||||
name: "no argument",
|
||||
input: "",
|
||||
wantErr: true,
|
||||
errMsg: "Cannot create autolink: keyPrefix and urlTemplate arguments are both required",
|
||||
},
|
||||
{
|
||||
name: "one argument",
|
||||
input: "TEST-",
|
||||
wantErr: true,
|
||||
errMsg: "Cannot create autolink: keyPrefix and urlTemplate arguments are both required",
|
||||
},
|
||||
{
|
||||
name: "two argument",
|
||||
input: "TICKET- https://example.com/TICKET?query=<num>",
|
||||
output: createOptions{
|
||||
KeyPrefix: "TICKET-",
|
||||
URLTemplate: "https://example.com/TICKET?query=<num>",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "numeric flag",
|
||||
input: "TICKET- https://example.com/TICKET?query=<num> --numeric",
|
||||
output: createOptions{
|
||||
KeyPrefix: "TICKET-",
|
||||
URLTemplate: "https://example.com/TICKET?query=<num>",
|
||||
Numeric: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ios, _, _, _ := iostreams.Test()
|
||||
f := &cmdutil.Factory{
|
||||
IOStreams: ios,
|
||||
}
|
||||
f.HttpClient = func() (*http.Client, error) {
|
||||
return &http.Client{}, nil
|
||||
}
|
||||
|
||||
argv, err := shlex.Split(tt.input)
|
||||
require.NoError(t, err)
|
||||
|
||||
var gotOpts *createOptions
|
||||
cmd := NewCmdCreate(f, func(opts *createOptions) error {
|
||||
gotOpts = opts
|
||||
return nil
|
||||
})
|
||||
|
||||
cmd.SetArgs(argv)
|
||||
cmd.SetIn(&bytes.Buffer{})
|
||||
cmd.SetOut(&bytes.Buffer{})
|
||||
cmd.SetErr(&bytes.Buffer{})
|
||||
|
||||
_, err = cmd.ExecuteC()
|
||||
if tt.wantErr {
|
||||
require.EqualError(t, err, tt.errMsg)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.output.KeyPrefix, gotOpts.KeyPrefix)
|
||||
assert.Equal(t, tt.output.URLTemplate, gotOpts.URLTemplate)
|
||||
assert.Equal(t, tt.output.Numeric, gotOpts.Numeric)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type stubAutoLinkCreator struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (g stubAutoLinkCreator) Create(repo ghrepo.Interface, request AutolinkCreateRequest) (*domain.Autolink, error) {
|
||||
if g.err != nil {
|
||||
return nil, g.err
|
||||
}
|
||||
|
||||
return &domain.Autolink{
|
||||
ID: 1,
|
||||
KeyPrefix: request.KeyPrefix,
|
||||
URLTemplate: request.URLTemplate,
|
||||
IsAlphanumeric: request.IsAlphanumeric,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type testAutolinkClientCreateError struct{}
|
||||
|
||||
func (e testAutolinkClientCreateError) Error() string {
|
||||
return "autolink client create error"
|
||||
}
|
||||
|
||||
func TestCreateRun(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
opts *createOptions
|
||||
stubCreator stubAutoLinkCreator
|
||||
expectedErr error
|
||||
errMsg string
|
||||
wantStdout string
|
||||
wantStderr string
|
||||
}{
|
||||
{
|
||||
name: "success, alphanumeric",
|
||||
opts: &createOptions{
|
||||
KeyPrefix: "TICKET-",
|
||||
URLTemplate: "https://example.com/TICKET?query=<num>",
|
||||
},
|
||||
stubCreator: stubAutoLinkCreator{},
|
||||
wantStdout: heredoc.Doc(`
|
||||
✓ Alphanumeric autolink created in OWNER/REPO (id: 1)
|
||||
TICKET-<num> → https://example.com/TICKET?query=<num>
|
||||
`),
|
||||
},
|
||||
{
|
||||
name: "success, numeric",
|
||||
opts: &createOptions{
|
||||
KeyPrefix: "TICKET-",
|
||||
URLTemplate: "https://example.com/TICKET?query=<num>",
|
||||
Numeric: true,
|
||||
},
|
||||
stubCreator: stubAutoLinkCreator{},
|
||||
wantStdout: heredoc.Doc(`
|
||||
✓ Numeric autolink created in OWNER/REPO (id: 1)
|
||||
TICKET-<num> → https://example.com/TICKET?query=<num>
|
||||
`),
|
||||
},
|
||||
{
|
||||
name: "client error",
|
||||
opts: &createOptions{
|
||||
KeyPrefix: "TICKET-",
|
||||
URLTemplate: "https://example.com/TICKET?query=<num>",
|
||||
},
|
||||
stubCreator: stubAutoLinkCreator{err: testAutolinkClientCreateError{}},
|
||||
expectedErr: testAutolinkClientCreateError{},
|
||||
errMsg: fmt.Sprint("error creating autolink: ", testAutolinkClientCreateError{}.Error()),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ios, _, stdout, stderr := iostreams.Test()
|
||||
|
||||
opts := tt.opts
|
||||
opts.IO = ios
|
||||
opts.Browser = &browser.Stub{}
|
||||
|
||||
opts.IO = ios
|
||||
opts.BaseRepo = func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil }
|
||||
|
||||
opts.AutolinkClient = &tt.stubCreator
|
||||
err := createRun(opts)
|
||||
|
||||
if tt.expectedErr != nil {
|
||||
require.Error(t, err)
|
||||
assert.ErrorIs(t, err, tt.expectedErr)
|
||||
assert.Equal(t, err.Error(), tt.errMsg)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
if tt.wantStdout != "" {
|
||||
assert.Equal(t, tt.wantStdout, stdout.String())
|
||||
}
|
||||
|
||||
if tt.wantStderr != "" {
|
||||
assert.Equal(t, tt.wantStderr, stderr.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue