yay/pkg/completion/completion_test.go
Jguer 44a0a243ed
remove gock from completion package (#1764)
* remove gock from completion package

* fix ci builder

* undo changes to default flow
2022-06-17 16:49:41 +00:00

97 lines
2.2 KiB
Go

package completion
import (
"bytes"
"context"
"errors"
"io/ioutil"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
const samplePackageResp = `
# AUR package list, generated on Fri, 24 Jul 2020 22:05:22 GMT
cytadela
bitefusion
globs-svn
ri-li
globs-benchmarks-svn
dunelegacy
lumina
eternallands-sound
`
const expectPackageCompletion = `cytadela AUR
bitefusion AUR
globs-svn AUR
ri-li AUR
globs-benchmarks-svn AUR
dunelegacy AUR
lumina AUR
eternallands-sound AUR
`
type mockDoer struct {
t *testing.T
returnBody string
returnStatusCode int
returnErr error
wantUrl string
}
func (m *mockDoer) Do(req *http.Request) (*http.Response, error) {
assert.Equal(m.t, m.wantUrl, req.URL.String())
return &http.Response{
StatusCode: m.returnStatusCode,
Body: ioutil.NopCloser(bytes.NewBufferString(m.returnBody)),
}, m.returnErr
}
func Test_createAURList(t *testing.T) {
t.Parallel()
doer := &mockDoer{
t: t,
wantUrl: "https://aur.archlinux.org/packages.gz",
returnStatusCode: 200,
returnBody: samplePackageResp,
returnErr: nil,
}
out := &bytes.Buffer{}
err := createAURList(context.TODO(), doer, "https://aur.archlinux.org", out)
assert.NoError(t, err)
gotOut := out.String()
assert.Equal(t, expectPackageCompletion, gotOut)
}
func Test_createAURListHTTPError(t *testing.T) {
t.Parallel()
doer := &mockDoer{
t: t,
wantUrl: "https://aur.archlinux.org/packages.gz",
returnStatusCode: 200,
returnBody: samplePackageResp,
returnErr: errors.New("Not available"),
}
out := &bytes.Buffer{}
err := createAURList(context.TODO(), doer, "https://aur.archlinux.org", out)
assert.EqualError(t, err, "Not available")
}
func Test_createAURListStatusError(t *testing.T) {
t.Parallel()
doer := &mockDoer{
t: t,
wantUrl: "https://aur.archlinux.org/packages.gz",
returnStatusCode: 503,
returnBody: samplePackageResp,
returnErr: nil,
}
out := &bytes.Buffer{}
err := createAURList(context.TODO(), doer, "https://aur.archlinux.org", out)
assert.EqualError(t, err, "invalid status code: 503")
}