yay/pkg/completion/completion_test.go

97 lines
2.2 KiB
Go
Raw Normal View History

2020-07-25 00:55:46 +02:00
package completion
import (
"bytes"
2021-08-12 18:56:23 +02:00
"context"
2020-07-25 00:55:46 +02:00
"errors"
"io/ioutil"
"net/http"
2020-07-25 00:55:46 +02:00
"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
}
2020-07-25 00:55:46 +02:00
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
}
2021-08-11 20:13:28 +02:00
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,
}
2020-07-25 00:55:46 +02:00
out := &bytes.Buffer{}
err := createAURList(context.TODO(), doer, "https://aur.archlinux.org", out)
2020-07-25 00:55:46 +02:00
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"),
}
2021-08-11 20:13:28 +02:00
2020-07-25 00:55:46 +02:00
out := &bytes.Buffer{}
err := createAURList(context.TODO(), doer, "https://aur.archlinux.org", out)
assert.EqualError(t, err, "Not available")
2020-07-25 00:55:46 +02:00
}
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,
}
2020-07-25 00:55:46 +02:00
out := &bytes.Buffer{}
err := createAURList(context.TODO(), doer, "https://aur.archlinux.org", out)
2020-07-25 00:55:46 +02:00
assert.EqualError(t, err, "invalid status code: 503")
}