mirror of
https://github.com/Jguer/yay
synced 2024-09-06 16:43:03 +00:00
remove gock from completion package (#1764)
* remove gock from completion package * fix ci builder * undo changes to default flow
This commit is contained in:
parent
70a9765854
commit
44a0a243ed
3
.github/workflows/testing-git.yml
vendored
3
.github/workflows/testing-git.yml
vendored
|
@ -24,7 +24,8 @@ jobs:
|
|||
${{ runner.os }}-go-
|
||||
- name: checkout pacman-git
|
||||
run: |
|
||||
pacman -Syu --overwrite=* --noconfirm sudo base-devel
|
||||
pacman -Sy --overwrite=* --noconfirm archlinux-keyring
|
||||
pacman -Su --overwrite=* --noconfirm sudo base-devel
|
||||
git clone https://aur.archlinux.org/pacman-git
|
||||
useradd github
|
||||
chmod -R 777 pacman-git
|
||||
|
|
|
@ -20,9 +20,14 @@ type PkgSynchronizer interface {
|
|||
SyncPackages(...string) []db.IPackage
|
||||
}
|
||||
|
||||
type httpRequestDoer interface {
|
||||
Do(req *http.Request) (*http.Response, error)
|
||||
}
|
||||
|
||||
// Show provides completion info for shells.
|
||||
func Show(ctx context.Context, httpClient *http.Client,
|
||||
dbExecutor PkgSynchronizer, aurURL, completionPath string, interval int, force bool) error {
|
||||
func Show(ctx context.Context, httpClient httpRequestDoer,
|
||||
dbExecutor PkgSynchronizer, aurURL, completionPath string, interval int, force bool,
|
||||
) error {
|
||||
err := Update(ctx, httpClient, dbExecutor, aurURL, completionPath, interval, force)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -40,8 +45,9 @@ func Show(ctx context.Context, httpClient *http.Client,
|
|||
}
|
||||
|
||||
// Update updates completion cache to be used by Complete.
|
||||
func Update(ctx context.Context, httpClient *http.Client,
|
||||
dbExecutor PkgSynchronizer, aurURL, completionPath string, interval int, force bool) error {
|
||||
func Update(ctx context.Context, httpClient httpRequestDoer,
|
||||
dbExecutor PkgSynchronizer, aurURL, completionPath string, interval int, force bool,
|
||||
) error {
|
||||
info, err := os.Stat(completionPath)
|
||||
|
||||
if os.IsNotExist(err) || (interval != -1 && time.Since(info.ModTime()).Hours() >= float64(interval*24)) || force {
|
||||
|
@ -70,7 +76,7 @@ func Update(ctx context.Context, httpClient *http.Client,
|
|||
}
|
||||
|
||||
// CreateAURList creates a new completion file.
|
||||
func createAURList(ctx context.Context, client *http.Client, aurURL string, out io.Writer) error {
|
||||
func createAURList(ctx context.Context, client httpRequestDoer, aurURL string, out io.Writer) error {
|
||||
u, err := url.Parse(aurURL)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -78,7 +84,7 @@ func createAURList(ctx context.Context, client *http.Client, aurURL string, out
|
|||
|
||||
u.Path = path.Join(u.Path, "packages.gz")
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", u.String(), http.NoBody)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), http.NoBody)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -4,11 +4,11 @@ import (
|
|||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/h2non/gock.v1"
|
||||
)
|
||||
|
||||
const samplePackageResp = `
|
||||
|
@ -33,41 +33,64 @@ 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) {
|
||||
defer gock.Off()
|
||||
|
||||
gock.New("https://aur.archlinux.org").
|
||||
Get("/packages.gz").
|
||||
Reply(200).
|
||||
BodyString(samplePackageResp)
|
||||
|
||||
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(), &http.Client{}, "https://aur.archlinux.org", out)
|
||||
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) {
|
||||
defer gock.Off()
|
||||
|
||||
gock.New("https://aur.archlinux.org").
|
||||
Get("/packages.gz").
|
||||
ReplyError(errors.New("Not available"))
|
||||
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(), &http.Client{}, "https://aur.archlinux.org", out)
|
||||
assert.EqualError(t, err, "Get \"https://aur.archlinux.org/packages.gz\": Not available")
|
||||
err := createAURList(context.TODO(), doer, "https://aur.archlinux.org", out)
|
||||
assert.EqualError(t, err, "Not available")
|
||||
}
|
||||
|
||||
func Test_createAURListStatusError(t *testing.T) {
|
||||
defer gock.Off()
|
||||
t.Parallel()
|
||||
doer := &mockDoer{
|
||||
t: t,
|
||||
wantUrl: "https://aur.archlinux.org/packages.gz",
|
||||
returnStatusCode: 503,
|
||||
returnBody: samplePackageResp,
|
||||
returnErr: nil,
|
||||
}
|
||||
|
||||
gock.New("https://aur.archlinux.org").
|
||||
Get("/packages.gz").
|
||||
Reply(503).
|
||||
BodyString(samplePackageResp)
|
||||
out := &bytes.Buffer{}
|
||||
err := createAURList(context.TODO(), &http.Client{}, "https://aur.archlinux.org", out)
|
||||
err := createAURList(context.TODO(), doer, "https://aur.archlinux.org", out)
|
||||
assert.EqualError(t, err, "invalid status code: 503")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue