Skip to content

Commit

Permalink
refactor: fix lint issues for unittests
Browse files Browse the repository at this point in the history
ci: fix workflows
lint: add .golangci.yml
  • Loading branch information
Nicconike committed Sep 14, 2024
1 parent efeb78b commit fd6183f
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 16 deletions.
13 changes: 10 additions & 3 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@ jobs:
category: "/language:go"

lint:
name: Lint
runs-on: ubuntu-latest
needs: codeql
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
- name: Checkout Repo
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 1.23
- uses: golangci/golangci-lint-action@v3

- name: GolangCI Lint
uses: golangci/golangci-lint-action@v3
11 changes: 6 additions & 5 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
github.actor != 'dependabot[bot]' &&
github.actor != 'github-actions[bot]' &&
github.actor != 'protected-auto-commits[bot]'
name: Packages
name: Docker Images
runs-on: ubuntu-latest
permissions:
contents: read
Expand All @@ -34,6 +34,7 @@ jobs:
- name: Checkout Repo
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ steps.app-token.outputs.token }}

- name: Set up Docker Buildx
Expand All @@ -57,8 +58,8 @@ jobs:
uses: docker/metadata-action@v5
with:
images: |
${{ vars.DOCKER_USERNAME }}/${{ github.repository }}
ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}
${{ vars.DOCKER_USERNAME }}/automatedgo
ghcr.io/${{ github.repository_owner }}/AutomatedGo
tags: |
type=semver,pattern={{version}}
type=ref,event=branch,pattern={{branch}}
Expand All @@ -76,9 +77,9 @@ jobs:
secrets: |
GITHUB_TOKEN=${{ steps.app-token.outputs.token }}
- name: Generate artifact attestation
- name: Generate Artifact Attestation
uses: actions/attest-build-provenance@v1
with:
subject-name: ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}
subject-name: ghcr.io/${{ github.repository_owner }}/AutomatedGo
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jobs:
- name: Fetch latest tags
run: git fetch --force --tags

- name: Set up Go
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: "1.22.x"
Expand Down
9 changes: 9 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
run:
timeout: 1m
output:
formats:
- format: colored-line-number
path: stdout
linters:
enable:
- errcheck
8 changes: 6 additions & 2 deletions tests/unit/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ func resetTestVersionURL() {
func TestGetLatestVersion(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("go1.17.1\n"))
if _, err := w.Write([]byte("go1.17.1\n")); err != nil {
t.Fatalf("Failed to write response: %v", err)
}
}))
defer server.Close()

Expand Down Expand Up @@ -66,7 +68,9 @@ func TestGetLatestVersionReadError(t *testing.T) {
func TestGetLatestVersionMalformedResponse(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("malformed\nresponse\n"))
if _, err := w.Write([]byte("malformed\nresponse\n")); err != nil {
t.Fatalf("Failed to write response: %v", err)
}
}))
defer server.Close()

Expand Down
16 changes: 12 additions & 4 deletions tests/unit/checksum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func TestGetOfficialChecksum(t *testing.T) {
},
},
}
json.NewEncoder(w).Encode(releases)
if err := json.NewEncoder(w).Encode(releases); err != nil {
t.Fatalf("Failed to encode JSON: %v", err)
}
},
filename: "go1.22.5.linux-amd64.tar.gz",
want: "904b924d435eaea086515bc63235b192ea441bd8c9b198c507e85009e6e4c7f0",
Expand All @@ -65,7 +67,9 @@ func TestGetOfficialChecksum(t *testing.T) {
},
},
}
json.NewEncoder(w).Encode(releases)
if err := json.NewEncoder(w).Encode(releases); err != nil {
t.Fatalf("Failed to encode JSON: %v", err)
}
},
filename: "go1.22.5.linux-amd64.tar.gz",
want: "904b924d435eaea086515bc63235b192ea441bd8c9b198c507e85009e6e4c7f0",
Expand All @@ -91,7 +95,9 @@ func TestGetOfficialChecksum(t *testing.T) {
},
},
}
json.NewEncoder(w).Encode(releases)
if err := json.NewEncoder(w).Encode(releases); err != nil {
t.Fatalf("Failed to encode JSON: %v", err)
}
},
filename: "invalid.tar.gz",
want: "",
Expand Down Expand Up @@ -127,7 +133,9 @@ func TestGetOfficialChecksum(t *testing.T) {
{
name: "Invalid JSON",
serverFunc: func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("invalid json"))
if _, err := w.Write([]byte("invalid json")); err != nil {
t.Fatalf("Failed to write invalid JSON: %v", err)
}
},
filename: "go1.22.5.linux-amd64.tar.gz",
want: "",
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ func TestDownloadFile(t *testing.T) {
// Create a test server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("test content"))
if _, err := w.Write([]byte("test content")); err != nil {
t.Fatalf("Failed to write response: %v", err)
}
}))
defer server.Close()

Expand Down

0 comments on commit fd6183f

Please sign in to comment.