Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional linters for readability and maintainability #134

Merged
merged 4 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ linters:
# Added.
- gofumpt
- bodyclose
- nakedret
- predeclared
- godox
- unconvert

linters-settings:
nakedret:
max-func-lines: 0
4 changes: 2 additions & 2 deletions cmd/fuzzcrypto/std/rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func FuzzRSAOAEP(f *testing.F) {
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(dec, []byte(msg)) {
if !bytes.Equal(dec, msg) {
t.Errorf("got:%x want:%x", dec, msg)
}
})
Expand All @@ -105,7 +105,7 @@ func FuzzRSAPKCS1(f *testing.F) {
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(dec, []byte(msg)) {
if !bytes.Equal(dec, msg) {
t.Errorf("got:%x want:%x", dec, msg)
}
})
Expand Down
4 changes: 1 addition & 3 deletions cmd/releasego/get-upstream-commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ go1.17.9b7 ed86dfc4e441 src https://go-boringcrypto.storage.googleapis.com/go1.1
}

func newFixture(t *testing.T) (local, upstream string) {
local = newEmptyGitRepo(t)
upstream = newUpstreamGitRepo(t)
return
return newEmptyGitRepo(t), newUpstreamGitRepo(t)
}

func newEmptyGitRepo(t *testing.T) string {
Expand Down
5 changes: 2 additions & 3 deletions githubutil/githubutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ func ParseRepoFlag(repo *string) (owner, name string, err error) {
if *repo == "" {
return "", "", errors.New("repo not specified")
}
var found bool
owner, name, found = strings.Cut(*repo, "/")
owner, name, found := strings.Cut(*repo, "/")
if !found {
return "", "", fmt.Errorf("unable to split repo into owner and name: %v", repo)
}
return
return owner, name, nil
}

const (
Expand Down
14 changes: 7 additions & 7 deletions gitpr/gitpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func sendJSONRequest(request *http.Request, response interface{}) (status int, e

httpResponse, err := client.Do(request)
if err != nil {
return
return 0, err
}
defer httpResponse.Body.Close()
status = httpResponse.StatusCode
Expand All @@ -212,14 +212,14 @@ func sendJSONRequest(request *http.Request, response interface{}) (status int, e

jsonBytes, err := ioutil.ReadAll(httpResponse.Body)
if err != nil {
return
return status, err
}

fmt.Printf("---- Full response:\n%v\n", string(jsonBytes))
fmt.Printf("----\n")

err = json.Unmarshal(jsonBytes, response)
return
return status, err
}

// sendJSONRequestSuccessful sends a request for JSON information via sendJSONRequest and verifies
Expand Down Expand Up @@ -268,20 +268,20 @@ type GitHubRequestError struct {
func PostGitHub(ownerRepo string, request *GitHubRequest, pat string) (response *GitHubResponse, err error) {
prSubmitContent, err := json.MarshalIndent(request, "", "")
if err != nil {
return
return nil, err
}
fmt.Printf("Submitting payload: %s\n", prSubmitContent)

httpRequest, err := http.NewRequest("POST", "https://api.github.com/repos/"+ownerRepo+"/pulls", bytes.NewReader(prSubmitContent))
if err != nil {
return
return nil, err
}
httpRequest.SetBasicAuth("", pat)

response = &GitHubResponse{}
statusCode, err := sendJSONRequest(httpRequest, response)
if err != nil {
return
return nil, err
}

switch statusCode {
Expand Down Expand Up @@ -320,7 +320,7 @@ func PostGitHub(ownerRepo string, request *GitHubRequest, pat string) (response
default:
err = fmt.Errorf("unexpected http status code: %v", statusCode)
}
return
return response, err
}

func QueryGraphQL(pat string, query string, variables map[string]interface{}, result interface{}) error {
Expand Down
52 changes: 26 additions & 26 deletions internal/akams/akams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func TestCreateBulk(t *testing.T) {
{ShortURL: "short2", TargetURL: "target2", LastModifiedBy: "lm", Owners: "o"},
}
wantBody := mustMarshal(t, links)
close, client := setup(t, srvHandler{respStatus: http.StatusOK, wantMethod: http.MethodPost, wantPath: "bulk", wantBody: wantBody})
defer close()
closer, client := setup(t, srvHandler{respStatus: http.StatusOK, wantMethod: http.MethodPost, wantPath: "bulk", wantBody: wantBody})
defer closer()
if err := client.CreateBulk(context.Background(), links); err != nil {
t.Fatal(err)
}
Expand All @@ -62,11 +62,11 @@ func TestCreateBulk_Chunked_BulkSize(t *testing.T) {
}
wantBody1 := mustMarshal(t, links[:2])
wantBody2 := mustMarshal(t, links[2:])
close, client := setup(t,
closer, client := setup(t,
srvHandler{respStatus: http.StatusOK, wantMethod: http.MethodPost, wantPath: "bulk", wantBody: wantBody1},
srvHandler{respStatus: http.StatusOK, wantMethod: http.MethodPost, wantPath: "bulk", wantBody: wantBody2},
)
defer close()
defer closer()
client.SetBulkLimit(2, 0)
if err := client.CreateBulk(context.Background(), links); err != nil {
t.Fatal(err)
Expand All @@ -82,12 +82,12 @@ func TestCreateBulk_Chunked_BodySize(t *testing.T) {
wantBody1 := mustMarshal(t, links[:1])
wantBody2 := mustMarshal(t, links[1:2])
wantBody3 := mustMarshal(t, links[2:])
close, client := setup(t,
closer, client := setup(t,
srvHandler{respStatus: http.StatusOK, wantMethod: http.MethodPost, wantPath: "bulk", wantBody: wantBody1},
srvHandler{respStatus: http.StatusOK, wantMethod: http.MethodPost, wantPath: "bulk", wantBody: wantBody2},
srvHandler{respStatus: http.StatusOK, wantMethod: http.MethodPost, wantPath: "bulk", wantBody: wantBody3},
)
defer close()
defer closer()
client.SetBulkLimit(0, len(wantBody1))
if err := client.CreateBulk(context.Background(), links); err != nil {
t.Fatal(err)
Expand All @@ -99,8 +99,8 @@ func TestCreateBulk_Fail_Request(t *testing.T) {
status := []int{http.StatusNotFound, http.StatusInternalServerError}
for _, s := range status {
t.Run(fmt.Sprint(s), func(t *testing.T) {
close, client := setup(t, srvHandler{respStatus: s, respBody: errStr, wantMethod: http.MethodPost, wantPath: "bulk", wantBody: "[]"})
defer close()
closer, client := setup(t, srvHandler{respStatus: s, respBody: errStr, wantMethod: http.MethodPost, wantPath: "bulk", wantBody: "[]"})
defer closer()
err := client.CreateBulk(context.Background(), []akams.CreateLinkRequest{})
testRequestFail(t, err, s, errStr)
})
Expand All @@ -111,8 +111,8 @@ func TestCreateBulk_Fail_MaxSize(t *testing.T) {
links := []akams.CreateLinkRequest{
{ShortURL: "short", TargetURL: "target", CreatedBy: "cb"},
}
close, client := setup(t)
defer close()
closer, client := setup(t)
defer closer()
client.SetBulkLimit(0, 1)
err := client.CreateBulk(context.Background(), links)
wantErr := "item 0 is too large: 91 bytes > 1 byte maximum"
Expand All @@ -130,8 +130,8 @@ func TestUpdateBulk(t *testing.T) {
status := []int{http.StatusAccepted, http.StatusNoContent, http.StatusNotFound}
for _, s := range status {
t.Run(fmt.Sprint(s), func(t *testing.T) {
close, client := setup(t, srvHandler{respStatus: s, wantMethod: http.MethodPut, wantPath: "bulk", wantBody: wantBody})
defer close()
closer, client := setup(t, srvHandler{respStatus: s, wantMethod: http.MethodPut, wantPath: "bulk", wantBody: wantBody})
defer closer()
if err := client.UpdateBulk(context.Background(), links); err != nil {
t.Fatal(err)
}
Expand All @@ -144,8 +144,8 @@ func TestUpdateBulk_Fail(t *testing.T) {
status := []int{http.StatusOK, http.StatusInternalServerError}
for _, s := range status {
t.Run(fmt.Sprint(s), func(t *testing.T) {
close, client := setup(t, srvHandler{respStatus: s, respBody: errStr, wantMethod: http.MethodPut, wantPath: "bulk", wantBody: "[]"})
defer close()
closer, client := setup(t, srvHandler{respStatus: s, respBody: errStr, wantMethod: http.MethodPut, wantPath: "bulk", wantBody: "[]"})
defer closer()
err := client.UpdateBulk(context.Background(), []akams.UpdateLinkRequest{})
testRequestFail(t, err, s, errStr)
})
Expand All @@ -156,8 +156,8 @@ func TestUpdateBulk_Fail_MaxSize(t *testing.T) {
links := []akams.UpdateLinkRequest{
{ShortURL: "short", TargetURL: "target"},
}
close, client := setup(t)
defer close()
closer, client := setup(t)
defer closer()
client.SetBulkLimit(0, 1)
err := client.UpdateBulk(context.Background(), links)
wantErr := "item 0 is too large: 74 bytes > 1 byte maximum"
Expand All @@ -173,8 +173,8 @@ func TestCreateOrUpdateBulk_NeedCreateOnly(t *testing.T) {
{ShortURL: "short2", TargetURL: "target2", LastModifiedBy: "lm", Owners: "o"},
}
wantBody := mustMarshal(t, links)
close, client := setup(t, srvHandler{respStatus: http.StatusOK, wantMethod: http.MethodPost, wantPath: "bulk", wantBody: wantBody})
defer close()
closer, client := setup(t, srvHandler{respStatus: http.StatusOK, wantMethod: http.MethodPost, wantPath: "bulk", wantBody: wantBody})
defer closer()
if err := client.CreateOrUpdateBulk(context.Background(), links); err != nil {
t.Fatal(err)
}
Expand All @@ -186,13 +186,13 @@ func TestCreateOrUpdateBulk_NeedUpdateOnly(t *testing.T) {
{ShortURL: "short", TargetURL: "target", CreatedBy: "cb"},
{ShortURL: "short2", TargetURL: "target2", CreatedBy: "cb2"},
}
close, client := setup(t,
closer, client := setup(t,
srvHandler{respStatus: http.StatusBadRequest, wantMethod: http.MethodPost, wantPath: "bulk"},
srvHandler{respStatus: http.StatusOK, wantMethod: http.MethodGet, wantPath: links[0].ShortURL},
srvHandler{respStatus: http.StatusOK, wantMethod: http.MethodGet, wantPath: links[1].ShortURL},
srvHandler{respStatus: http.StatusAccepted, wantMethod: http.MethodPut, wantPath: "bulk"},
)
defer close()
defer closer()
if err := client.CreateOrUpdateBulk(context.Background(), links); err != nil {
t.Fatal(err)
}
Expand All @@ -204,14 +204,14 @@ func TestCreateOrUpdateBulk_NeedUpdateAndCreate(t *testing.T) {
{ShortURL: "short", TargetURL: "target", CreatedBy: "cb"},
{ShortURL: "short2", TargetURL: "target2", CreatedBy: "cb2"},
}
close, client := setup(t,
closer, client := setup(t,
srvHandler{respStatus: http.StatusBadRequest, wantMethod: http.MethodPost, wantPath: "bulk"},
srvHandler{respStatus: http.StatusNotFound, wantMethod: http.MethodGet, wantPath: links[0].ShortURL},
srvHandler{respStatus: http.StatusOK, wantMethod: http.MethodGet, wantPath: links[1].ShortURL},
srvHandler{respStatus: http.StatusOK, wantMethod: http.MethodPost, wantPath: "bulk"},
srvHandler{respStatus: http.StatusAccepted, wantMethod: http.MethodPut, wantPath: "bulk"},
)
defer close()
defer closer()
if err := client.CreateOrUpdateBulk(context.Background(), links); err != nil {
t.Fatal(err)
}
Expand All @@ -224,10 +224,10 @@ func TestCreateOrUpdateBulk_CreateFail(t *testing.T) {
}
status := http.StatusExpectationFailed
errStr := "fake error"
close, client := setup(t,
closer, client := setup(t,
srvHandler{respStatus: status, respBody: errStr, wantMethod: http.MethodPost, wantPath: "bulk"},
)
defer close()
defer closer()
err := client.CreateOrUpdateBulk(context.Background(), links)
testRequestFail(t, err, status, errStr)
}
Expand All @@ -239,11 +239,11 @@ func TestCreateOrUpdateBulk_GetFail(t *testing.T) {
}
status := http.StatusExpectationFailed
errStr := "fake error"
close, client := setup(t,
closer, client := setup(t,
srvHandler{respStatus: http.StatusBadRequest, wantMethod: http.MethodPost, wantPath: "bulk"},
srvHandler{respStatus: status, respBody: errStr, wantMethod: http.MethodGet, wantPath: links[0].ShortURL},
)
defer close()
defer closer()
err := client.CreateOrUpdateBulk(context.Background(), links)
testRequestFail(t, err, status, errStr)
}
Expand Down