Skip to content

Commit

Permalink
add unit tests, wip
Browse files Browse the repository at this point in the history
  • Loading branch information
patinthehat committed May 13, 2024
1 parent 94bc5a2 commit 91c663a
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 40 deletions.
11 changes: 8 additions & 3 deletions lib/github/errors.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package github

import (
"fmt"
"errors"
)

type InvalidGitHubProjectURL = error
type InvalidGitHubProjectReference = error

var (
InvalidGitHubProjectURLError InvalidGitHubProjectURL = errors.New("Invalid GitHub project URL")
InvalidGitHubProjectReferenceError InvalidGitHubProjectReference = errors.New("Invalid GitHub project reference")
)

func NewInvalidGitHubProjectURLError(URL string) InvalidGitHubProjectURL {
return fmt.Errorf("Invalid GitHub URL: %s", URL)
return InvalidGitHubProjectURLError
}

func NewInvalidGitHubProjectReferenceError(reference string) InvalidGitHubProjectReference {
return fmt.Errorf("Invalid GitHub project reference: %s", reference)
return InvalidGitHubProjectReferenceError
}
24 changes: 24 additions & 0 deletions lib/github/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package github_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/permafrost-dev/eget/lib/github"
)

var _ = Describe("GitHub Errors", func() {

Describe("NewInvalidGitHubProjectURLError", func() {
It("returns the InvalidGitHubProjectURLError", func() {
err := github.NewInvalidGitHubProjectURLError("https://github.com/not/a/real/project")
Expect(err).To(MatchError(github.InvalidGitHubProjectURLError))
})
})

Describe("NewInvalidGitHubProjectReferenceError", func() {
It("returns the InvalidGitHubProjectReferenceError", func() {
err := github.NewInvalidGitHubProjectReferenceError("master")
Expect(err).To(MatchError(github.InvalidGitHubProjectReferenceError))
})
})
})
74 changes: 37 additions & 37 deletions lib/github/github_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,44 @@ package github_test

import (
"net/http"
"testing"

. "github.com/permafrost-dev/eget/lib/github"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/permafrost-dev/eget/lib/github"
)

func TestGithubError_Error(t *testing.T) {
tests := []struct {
name string
githubError Error
want string
}{
{
name: "Forbidden error with message",
githubError: Error{
Code: http.StatusForbidden,
Status: "403 Forbidden",
Body: []byte(`{"message":"rate limit exceeded","documentation_url":"https://developer.github.com/v3/#rate-limiting"}`),
URL: "https://api.github.com/users/octocat",
},
want: "403 Forbidden: rate limit exceeded: https://developer.github.com/v3/#rate-limiting",
},
{
name: "Other error without specific message",
githubError: Error{
Code: http.StatusNotFound,
Status: "404 Not Found",
Body: []byte(`{"message":"Not Found","documentation_url":"https://developer.github.com/v3"}`),
URL: "https://api.github.com/users/octocat",
},
want: "404 Not Found (URL: https://api.github.com/users/octocat)",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.githubError.Error(); got != tt.want {
t.Errorf("GithubError.Error() = %v, want %v", got, tt.want)
}
var _ = Describe("Github Errors", func() {
var (
err *github.Error
)

BeforeEach(func() {
err = &github.Error{}
})

Describe("Error method", func() {
Context("when the status code is 403", func() {
BeforeEach(func() {
err.Code = http.StatusForbidden
err.Status = "Forbidden"
err.Body = []byte(`{"message": "API rate limit exceeded", "documentation_url": "https://developer.github.com/v3/#rate-limiting"}`)
})

It("returns the correct error message", func() {
Expect(err.Error()).To(Equal("Forbidden: API rate limit exceeded: https://developer.github.com/v3/#rate-limiting"))
})
})

Context("when the status code is not 403", func() {
BeforeEach(func() {
err.Code = http.StatusNotFound
err.Status = "Not Found"
err.URL = "https://api.github.com/repos/nonexistent"
})

It("returns a generic error message", func() {
Expect(err.Error()).To(Equal("Not Found (URL: https://api.github.com/repos/nonexistent)"))
})
})
}
}
})
})
File renamed without changes.
90 changes: 90 additions & 0 deletions lib/github/releases_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package github_test

import (
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/permafrost-dev/eget/lib/assets"
. "github.com/permafrost-dev/eget/lib/github"
)

var _ = Describe("Release", func() {
var (
release Release
)

BeforeEach(func() {
release = Release{
Assets: []ReleaseAsset{
{
Name: "asset1",
URL: "http://example.com/asset1",
DownloadURL: "http://example.com/download/asset1",
Size: 1024,
DownloadCount: 100,
ContentType: "application/octet-stream",
},
{
Name: "asset2",
URL: "http://example.com/asset2",
DownloadURL: "http://example.com/download/asset2",
Size: 2048,
DownloadCount: 200,
ContentType: "application/octet-stream",
},
},
Prerelease: false,
Tag: "v1.0.0",
CreatedAt: time.Now(),
PublishedAt: time.Now(),
}
})

Describe("ProcessReleaseAssets", func() {
It("should correctly associate each asset with its release", func() {
release.ProcessReleaseAssets()
for _, asset := range release.Assets {
Expect(asset.Release).To(Equal(&release))
}
})
})
})

var _ = Describe("ReleaseAsset", func() {
var (
releaseAsset ReleaseAsset
release Release
)

BeforeEach(func() {
release = Release{
Assets: nil,
Prerelease: false,
Tag: "v1.0.0",
CreatedAt: time.Now(),
PublishedAt: time.Now(),
}

releaseAsset = ReleaseAsset{
Release: &release,
Name: "asset",
URL: "http://example.com/asset",
DownloadURL: "http://example.com/download/asset",
Size: 1024,
DownloadCount: 100,
ContentType: "application/octet-stream",
}
})

Describe("CopyToNewAsset", func() {
It("should correctly copy ReleaseAsset to Asset", func() {
copiedAsset := releaseAsset.CopyToNewAsset()
Expect(copiedAsset).To(Equal(assets.Asset{
Name: releaseAsset.Name,
DownloadURL: releaseAsset.DownloadURL,
ReleaseDate: releaseAsset.Release.PublishedAt,
}))
})
})
})

0 comments on commit 91c663a

Please sign in to comment.