Skip to content

Commit

Permalink
remove gock from completion package (Jguer#1764)
Browse files Browse the repository at this point in the history
* remove gock from completion package

* fix ci builder

* undo changes to default flow
  • Loading branch information
Jguer authored Jun 17, 2022
1 parent 70a9765 commit 44a0a24
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/testing-git.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 12 additions & 6 deletions pkg/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -70,15 +76,15 @@ 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
}

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
}
Expand Down
65 changes: 44 additions & 21 deletions pkg/completion/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand All @@ -33,41 +33,64 @@ lumina AUR
eternallands-sound AUR
`

func Test_createAURList(t *testing.T) {
defer gock.Off()
type mockDoer struct {
t *testing.T
returnBody string
returnStatusCode int
returnErr error
wantUrl string
}

gock.New("https://aur.archlinux.org").
Get("/packages.gz").
Reply(200).
BodyString(samplePackageResp)
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(), &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")
}

0 comments on commit 44a0a24

Please sign in to comment.