From f657865498226ef6c204e3fbece2ca1197c2e00c Mon Sep 17 00:00:00 2001 From: Nicconike <38905025+Nicconike@users.noreply.github.com> Date: Wed, 17 Jul 2024 02:53:03 +0530 Subject: [PATCH] test: Improve codecov for checksum.go --- .github/codecov.yml | 2 +- .github/workflows/release.yml | 10 +- .semrelrc | 4 - tests/unit/checksum_test.go | 201 ++++++++++++++++++++++++---------- 4 files changed, 146 insertions(+), 71 deletions(-) diff --git a/.github/codecov.yml b/.github/codecov.yml index 16fb3f5..01884fb 100644 --- a/.github/codecov.yml +++ b/.github/codecov.yml @@ -9,7 +9,7 @@ coverage: status: project: default: - target: auto + target: 80% threshold: 5% base: auto patch: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 93f25d8..29ce009 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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: @@ -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 @@ -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' @@ -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 diff --git a/.semrelrc b/.semrelrc index 02bd62a..34cd839 100644 --- a/.semrelrc +++ b/.semrelrc @@ -15,10 +15,6 @@ { "path": "CHANGELOG.md", "type": "changelog" - }, - { - "path": "go.mod", - "type": "go-mod" } ] } diff --git a/tests/unit/checksum_test.go b/tests/unit/checksum_test.go index eef433d..5cd499e 100644 --- a/tests/unit/checksum_test.go +++ b/tests/unit/checksum_test.go @@ -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) @@ -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") + } + }) }