Skip to content

Commit

Permalink
test: Improve codecov for checksum.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicconike committed Jul 16, 2024
1 parent 5884a15 commit f657865
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .github/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ coverage:
status:
project:
default:
target: auto
target: 80%
threshold: 5%
base: auto
patch:
Expand Down
10 changes: 4 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ on:
branches: master

jobs:
semantic-release:
name: Semantic Release
release:
name: Release
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
permissions:
Expand All @@ -32,7 +32,6 @@ jobs:

- name: Semantic Release
uses: go-semantic-release/action@v1
id: semrel
with:
github-token: ${{ steps.app-token.outputs.token }}
changelog-file: CHANGELOG.md
Expand All @@ -41,7 +40,6 @@ jobs:

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v6
if: steps.semrel.outputs.new_release_published == 'true'
with:
distribution: goreleaser
version: '~> v2'
Expand All @@ -50,6 +48,6 @@ jobs:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}

- name: Update pkg.go.dev
if: steps.semrel.outputs.new_release_published == 'true'
run: |
go list -m github.com/Nicconike/goautomate@${{ steps.semrel.outputs.new_release_version }}
VERSION=$(git describe --tags --abbrev=0)
go list -m github.com/Nicconike/goautomate@VERSION
4 changes: 0 additions & 4 deletions .semrelrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
{
"path": "CHANGELOG.md",
"type": "changelog"
},
{
"path": "go.mod",
"type": "go-mod"
}
]
}
Expand Down
201 changes: 141 additions & 60 deletions tests/unit/checksum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,94 @@ import (
)

func TestGetOfficialChecksum(t *testing.T) {
// Mock server to simulate the Go downloads JSON API
server := httptest.NewServer(http.HandlerFunc(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"`
}{
tests := []struct {
name string
serverFunc func(http.ResponseWriter, *http.Request)
filename string
want string
wantErr bool
}{
{
name: "Valid filename",
serverFunc: func(w http.ResponseWriter, r *http.Request) {
releases := []pkg.GoRelease{
{
Filename: "go1.22.5.linux-amd64.tar.gz",
SHA256: "904b924d435eaea086515bc63235b192ea441bd8c9b198c507e85009e6e4c7f0",
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)
},
}
json.NewEncoder(w).Encode(releases)
}))
defer server.Close()

// Replace the actual URL with the test server URL
originalURL := pkg.URL
pkg.URL = server.URL
defer func() { pkg.URL = originalURL }()

tests := []struct {
name string
filename string
want string
wantErr bool
}{
{"Valid filename", "go1.22.5.linux-amd64.tar.gz", "904b924d435eaea086515bc63235b192ea441bd8c9b198c507e85009e6e4c7f0", false},
{"Invalid filename", "invalid.tar.gz", "", true},
filename: "go1.22.5.linux-amd64.tar.gz",
want: "904b924d435eaea086515bc63235b192ea441bd8c9b198c507e85009e6e4c7f0",
wantErr: false,
},
{
name: "Invalid 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: "invalid.tar.gz",
want: "",
wantErr: true,
},
{
name: "HTTP error",
serverFunc: func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
},
filename: "go1.22.5.linux-amd64.tar.gz",
want: "",
wantErr: true,
},
{
name: "Invalid JSON",
serverFunc: func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("invalid json"))
},
filename: "go1.22.5.linux-amd64.tar.gz",
want: "",
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(tt.serverFunc))
defer server.Close()

originalURL := pkg.URL
pkg.URL = server.URL
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)
Expand All @@ -64,35 +112,68 @@ func TestGetOfficialChecksum(t *testing.T) {
}

func TestCalculateFileChecksum(t *testing.T) {
// Create a temporary file
content := []byte("test content")
tmpfile, err := os.CreateTemp("", "example")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
// Test with a valid file
t.Run("Valid file", func(t *testing.T) {
content := []byte("test content")
tmpfile, err := os.CreateTemp("", "example")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())

if _, err := tmpfile.Write(content); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
if _, err := tmpfile.Write(content); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}

// Calculate expected SHA256
expectedSHA256 := "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72"
expectedSHA256 := "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72"

got, err := pkg.CalculateFileChecksum(tmpfile.Name())
if err != nil {
t.Fatalf("CalculateFileChecksum() error = %v", err)
}
if got != expectedSHA256 {
t.Errorf("CalculateFileChecksum() = %v, want %v", got, expectedSHA256)
}
got, err := pkg.CalculateFileChecksum(tmpfile.Name())
if err != nil {
t.Fatalf("CalculateFileChecksum() error = %v", err)
}
if got != expectedSHA256 {
t.Errorf("CalculateFileChecksum() = %v, want %v", got, expectedSHA256)
}
})

// Test with non-existent file
_, err = pkg.CalculateFileChecksum("non_existent_file")
if err == nil {
t.Error("CalculateFileChecksum() expected error for non-existent file, got nil")
}
// Test with a non-existent file
t.Run("Non-existent file", func(t *testing.T) {
_, err := pkg.CalculateFileChecksum("non_existent_file")
if err == nil {
t.Error("CalculateFileChecksum() expected error for non-existent file, got nil")
}
})

// Test with a directory instead of a file
t.Run("Directory instead of file", func(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "testdir")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)

_, err = pkg.CalculateFileChecksum(tmpDir)
if err == nil {
t.Error("CalculateFileChecksum() expected error for directory, got nil")
}
})

// Test with a file that becomes inaccessible 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())
if err == nil {
t.Error("CalculateFileChecksum() expected error for non-existent file, got nil")
}
})
}

0 comments on commit f657865

Please sign in to comment.