Skip to content

Commit

Permalink
test: 100% codecov for checksum.go
Browse files Browse the repository at this point in the history
ci: update workflows
  • Loading branch information
Nicconike committed Jul 16, 2024
1 parent f657865 commit 4b96091
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 18 deletions.
25 changes: 25 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,32 @@ updates:
directory: /
schedule:
interval: weekly
commit-message:
prefix: "chore"
include: "scope"
labels:
- dependencies
- dependabot
rebase-strategy: auto
versioning-strategy: "increase-if-necessary"
open-pull-requests-limit: 20
reviewers:
- "nicconike"

- package-ecosystem: github-actions
directory: /
schedule:
interval: weekly
commit-message:
prefix: "ci"
labels:
- dependencies
- dependabot
rebase-strategy: auto
open-pull-requests-limit: 20
reviewers:
- "nicconike"
groups:
github-actions:
patterns:
- "*"
5 changes: 5 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ name: "Code Coverage"
on:
push:
branches: master
paths:
- ".github/workflows/coverage.yml"
- "tests/*.py"
pull_request:
branches: master

jobs:
code-coverage:
name: Codecov
runs-on: ubuntu-latest
if: github.actor != 'dependabot[bot]' || github.actor != 'github-actions[bot]' || github.actor != 'protected-auto-commits[bot]'
steps:
- name: Checkout Repo
uses: actions/checkout@v4
Expand Down
45 changes: 38 additions & 7 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ name: Release
on:
push:
branches: master
paths:
- '.github/workflows/release.yml'
- 'go.mod'
- 'pkg/*'
- 'cmd/*'

jobs:
release:
name: Release
semantic-release:
name: Semantic Release
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
permissions:
Expand Down Expand Up @@ -38,6 +43,37 @@ jobs:
update-file: go.mod
changelog-generator-opt: "emojis=true"

- name: Update pkg.go.dev # https://pkg.go.dev/github.com/Nicconike/goautomate
run: |
VERSION=$(git describe --tags --abbrev=0)
go list -m github.com/Nicconike/goautomate@VERSION
goreleaser:
name: GoReleaser
needs: semantic-release
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
steps:
- name: GitHub App Token
uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ secrets.APP_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}

- name: Checkout Repo
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ steps.app-token.outputs.token }}

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.22.x"

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v6
with:
Expand All @@ -46,8 +82,3 @@ jobs:
args: release --clean
env:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}

- name: Update pkg.go.dev
run: |
VERSION=$(git describe --tags --abbrev=0)
go list -m github.com/Nicconike/goautomate@VERSION
4 changes: 4 additions & 0 deletions pkg/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func getOfficialChecksum(filename string) (string, error) {
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to fetch Go releases: HTTP status %d", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %w", err)
Expand Down
66 changes: 55 additions & 11 deletions tests/unit/checksum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"

"github.com/Nicconike/goautomate/pkg"
Expand All @@ -16,7 +17,7 @@ func TestGetOfficialChecksum(t *testing.T) {
serverFunc func(http.ResponseWriter, *http.Request)
filename string
want string
wantErr bool
wantErr string
}{
{
name: "Valid filename",
Expand All @@ -42,7 +43,33 @@ func TestGetOfficialChecksum(t *testing.T) {
},
filename: "go1.22.5.linux-amd64.tar.gz",
want: "904b924d435eaea086515bc63235b192ea441bd8c9b198c507e85009e6e4c7f0",
wantErr: false,
wantErr: "",
},
{
name: "Valid filename",
serverFunc: func(w http.ResponseWriter, r *http.Request) {
releases := []pkg.GoRelease{
{
Version: "go1.22.5",
Files: []struct {
Filename string `json:"filename"`
OS string `json:"os"`
Arch string `json:"arch"`
Version string `json:"version"`
SHA256 string `json:"sha256"`
}{
{
Filename: "go1.22.5.linux-amd64.tar.gz",
SHA256: "904b924d435eaea086515bc63235b192ea441bd8c9b198c507e85009e6e4c7f0",
},
},
},
}
json.NewEncoder(w).Encode(releases)
},
filename: "go1.22.5.linux-amd64.tar.gz",
want: "904b924d435eaea086515bc63235b192ea441bd8c9b198c507e85009e6e4c7f0",
wantErr: "",
},
{
name: "Invalid filename",
Expand All @@ -68,16 +95,25 @@ func TestGetOfficialChecksum(t *testing.T) {
},
filename: "invalid.tar.gz",
want: "",
wantErr: true,
wantErr: "checksum not found for invalid.tar.gz",
},
{
name: "HTTP error",
name: "HTTP GET error",
serverFunc: func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
w.WriteHeader(http.StatusInternalServerError)
},
filename: "go1.22.5.linux-amd64.tar.gz",
want: "",
wantErr: true,
wantErr: "failed to fetch Go releases: HTTP status 500",
},
{
name: "Read body error",
serverFunc: func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", "1")
},
filename: "go1.22.5.linux-amd64.tar.gz",
want: "",
wantErr: "failed to read response body",
},
{
name: "Invalid JSON",
Expand All @@ -86,7 +122,7 @@ func TestGetOfficialChecksum(t *testing.T) {
},
filename: "go1.22.5.linux-amd64.tar.gz",
want: "",
wantErr: true,
wantErr: "failed to parse JSON",
},
}

Expand All @@ -100,8 +136,17 @@ func TestGetOfficialChecksum(t *testing.T) {
defer func() { pkg.URL = originalURL }()

got, err := pkg.GetOfficialChecksum(tt.filename)
if (err != nil) != tt.wantErr {
t.Errorf("GetOfficialChecksum() error = %v, wantErr %v", err, tt.wantErr)
if tt.wantErr != "" {
if err == nil {
t.Errorf("GetOfficialChecksum() error = nil, wantErr %v", tt.wantErr)
return
}
if !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("GetOfficialChecksum() error = %v, wantErr %v", err, tt.wantErr)
return
}
} else if err != nil {
t.Errorf("GetOfficialChecksum() unexpected error = %v", err)
return
}
if got != tt.want {
Expand Down Expand Up @@ -161,14 +206,13 @@ func TestCalculateFileChecksum(t *testing.T) {
}
})

// Test with a file that becomes inaccessible after opening
// Test with a file that gets removed after opening
t.Run("File becomes inaccessible", func(t *testing.T) {
tmpfile, err := os.CreateTemp("", "example")
if err != nil {
t.Fatal(err)
}
tmpfile.Close()
// Remove the file instead of just changing permissions
os.Remove(tmpfile.Name())

_, err = pkg.CalculateFileChecksum(tmpfile.Name())
Expand Down

0 comments on commit 4b96091

Please sign in to comment.