2023-05-22 23:38:02 +02:00
|
|
|
//go:build !integration
|
|
|
|
// +build !integration
|
|
|
|
|
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"
|
2022-08-05 22:55:54 +02:00
|
|
|
"io"
|
2021-05-16 22:45:36 +02:00
|
|
|
"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
|
|
|
|
`
|
|
|
|
|
2022-06-17 18:49:41 +02:00
|
|
|
type mockDoer struct {
|
|
|
|
t *testing.T
|
|
|
|
returnBody string
|
|
|
|
returnStatusCode int
|
|
|
|
returnErr error
|
|
|
|
wantUrl string
|
|
|
|
}
|
2020-07-25 00:55:46 +02:00
|
|
|
|
2022-06-17 18:49:41 +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,
|
2022-08-05 22:55:54 +02:00
|
|
|
Body: io.NopCloser(bytes.NewBufferString(m.returnBody)),
|
2022-06-17 18:49:41 +02:00
|
|
|
}, m.returnErr
|
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2022-06-17 18:49:41 +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{}
|
2023-01-03 22:43:56 +01:00
|
|
|
err := createAURList(context.Background(), 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) {
|
2022-06-17 18:49:41 +02:00
|
|
|
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{}
|
2023-01-03 22:43:56 +01:00
|
|
|
err := createAURList(context.Background(), doer, "https://aur.archlinux.org", out)
|
2022-06-17 18:49:41 +02:00
|
|
|
assert.EqualError(t, err, "Not available")
|
2020-07-25 00:55:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_createAURListStatusError(t *testing.T) {
|
2022-06-17 18:49:41 +02:00
|
|
|
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{}
|
2023-01-03 22:43:56 +01:00
|
|
|
err := createAURList(context.Background(), doer, "https://aur.archlinux.org", out)
|
2020-07-25 00:55:46 +02:00
|
|
|
assert.EqualError(t, err, "invalid status code: 503")
|
|
|
|
}
|