From c6946011130acea5e36f9d7050d4356d77b467e2 Mon Sep 17 00:00:00 2001 From: Isaac Hall Date: Mon, 31 Jan 2022 09:36:23 -0800 Subject: [PATCH] Add Makefile for test, lint, vet. Fix lint issues. --- .github/workflows/ci.yml | 22 ++++++++++++++++++++++ Makefile | 13 +++++++++++++ client.go | 7 +++---- client_operations_test.go | 25 ++++++++++++++++--------- client_test.go | 3 +++ logger.go | 1 + 6 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 Makefile diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..c4fbc7f --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,22 @@ +name: CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + lint: + name: Lint + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@master + + - name: lint + run: | + go get -u honnef.co/go/tools/cmd/staticcheck@latest && + $HOME/go/bin/staticcheck && + make vet diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5712ae0 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +all: build lint test vet + +build: + go build ./... + +lint: + staticcheck + +test: + go test -race ./... + +vet: + go vet ./... diff --git a/client.go b/client.go index 43ea3c6..926f541 100644 --- a/client.go +++ b/client.go @@ -54,10 +54,9 @@ var ( Transport: defaultTransport, } - acceptVersion = fmt.Sprintf("application/vnd.recurly.%s", APIVersion) - recurlyVersion = fmt.Sprintf("recurly.%s", APIVersion) - userAgent = fmt.Sprintf("Recurly/%s; go %s", clientVersion, runtime.Version()) - pathPattern = regexp.MustCompile(`{[^}]+}`) + acceptVersion = fmt.Sprintf("application/vnd.recurly.%s", APIVersion) + userAgent = fmt.Sprintf("Recurly/%s; go %s", clientVersion, runtime.Version()) + pathPattern = regexp.MustCompile(`{[^}]+}`) ) // Client submits API requests to Recurly diff --git a/client_operations_test.go b/client_operations_test.go index 2ade5de..03881fc 100644 --- a/client_operations_test.go +++ b/client_operations_test.go @@ -26,13 +26,16 @@ func TestCreateAccount(test *testing.T) { Code: String("new_account"), } - account, _ := client.CreateAccount(body) + account, err := client.CreateAccount(body) + t.Assert(err, nil, "Error not expected") t.Assert(account.Id, "abcd1234", "account.Id") } func TestCreateAccountWithContext(test *testing.T) { t := &T{test} - ctx, _ := context.WithTimeout(context.Background(), 20*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + defer cancel() + scenario := &Scenario{ T: t, AssertRequest: func(req *http.Request) { @@ -51,7 +54,8 @@ func TestCreateAccountWithContext(test *testing.T) { Code: String("new_account"), } - account, _ := client.CreateAccountWithContext(ctx, body) + account, err := client.CreateAccountWithContext(ctx, body) + t.Assert(err, nil, "Error not expected") t.Assert(account.Id, "abcd1234", "account.Id") } @@ -71,12 +75,12 @@ func TestListAccounts(test *testing.T) { "next": "/accounts?cursor=efgh5678%3A1588803986.0&limit=1&order=desc&sort=created_at", "data": [ { - "id": "abcd1234", + "id": "abcd1234", "first_name": "marigold", "last_name": "sunflower" }, { - "id": "efgh5678", + "id": "efgh5678", "first_name": "juniper", "last_name": "pinecone" } @@ -103,7 +107,9 @@ func TestListAccounts(test *testing.T) { func TestListAccountsWithContext(test *testing.T) { t := &T{test} - ctx, _ := context.WithTimeout(context.Background(), 20*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + defer cancel() + scenario := &Scenario{ T: t, AssertRequest: func(req *http.Request) { @@ -118,12 +124,12 @@ func TestListAccountsWithContext(test *testing.T) { "next": "/accounts?cursor=efgh5678%3A1588803986.0&limit=1&order=desc&sort=created_at", "data": [ { - "id": "abcd1234", + "id": "abcd1234", "first_name": "marigold", "last_name": "sunflower" }, { - "id": "efgh5678", + "id": "efgh5678", "first_name": "juniper", "last_name": "pinecone" } @@ -168,6 +174,7 @@ func TestCreateAccountWithNilContext(test *testing.T) { Code: String("new_account"), } - account, _ := client.CreateAccountWithContext(nil, body) + account, err := client.CreateAccountWithContext(context.Background(), body) + t.Assert(err, nil, "Error not expected") t.Assert(account.Id, "abcd1234", "account.Id") } diff --git a/client_test.go b/client_test.go index 6de2a7c..19a1054 100644 --- a/client_test.go +++ b/client_test.go @@ -141,6 +141,9 @@ func TestClientInjectsResponseMetadataIntoResource(test *testing.T) { client = scenario.MockHTTPClient() empty, err := client.DeleteResource("abcd1234") + if err != nil { + t.Errorf("DeleteResource returned err: %v", err) + } resp = empty.GetResponse() t.Assert(resp.Request.ID, "msy-1234", "resp.Request.ID") diff --git a/logger.go b/logger.go index 5c59638..5e28e79 100644 --- a/logger.go +++ b/logger.go @@ -90,6 +90,7 @@ func (log *Logger) stdOut(v ...interface{}) { } } +//lint:ignore U1000 TODO: should `Error` call stdErr instead of stdOut? func (log *Logger) stdErr(v ...interface{}) { if log.StdErr != nil { log.StdErr.Print(v...)