From d8f0c469b44f9ab605e5ef2fadac0aa4924c6007 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Thu, 18 Jan 2024 23:34:56 +0100 Subject: [PATCH] enable golangci-lint for examples (#2128) Signed-off-by: Matthieu MOREL --- .github/workflows/ci-test-go.yml | 2 +- examples/cockroachdb/cockroachdb_test.go | 36 +++++++++--------------- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/.github/workflows/ci-test-go.yml b/.github/workflows/ci-test-go.yml index 3f44249eb5..97cbdee663 100644 --- a/.github/workflows/ci-test-go.yml +++ b/.github/workflows/ci-test-go.yml @@ -71,7 +71,7 @@ jobs: - name: golangci-lint # TODO: Remove each example/module once it passes the golangci-lint - if: ${{ inputs.platform == 'ubuntu-latest' && inputs.go-version == '1.20.x' && !contains(fromJSON('["examples/cockroachdb", "examples/toxiproxy", "modules/compose"]'), inputs.project-directory) }} + if: ${{ inputs.platform == 'ubuntu-latest' && inputs.go-version == '1.20.x' && !contains(fromJSON('["modules/compose"]'), inputs.project-directory) }} uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc # v3 with: # Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version diff --git a/examples/cockroachdb/cockroachdb_test.go b/examples/cockroachdb/cockroachdb_test.go index 38706d9737..50e9c1e0ad 100644 --- a/examples/cockroachdb/cockroachdb_test.go +++ b/examples/cockroachdb/cockroachdb_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) // Task represents a unit of work to complete. We're going to be using this in @@ -16,12 +17,12 @@ import ( type task struct { ID string `json:"id"` Description string `json:"description"` - DateDue *time.Time `json:"date_due,string"` - DateCreated time.Time `json:"date_created,string"` + DateDue *time.Time `json:"date_due"` + DateCreated time.Time `json:"date_created"` DateUpdated time.Time `json:"date_updated"` } -func initCockroachDB(ctx context.Context, db sql.DB) error { +func initCockroachDB(ctx context.Context, db *sql.DB) error { // Actual SQL for initializing the database should probably live elsewhere const query = `CREATE DATABASE projectmanagement; CREATE TABLE projectmanagement.task( @@ -31,11 +32,10 @@ func initCockroachDB(ctx context.Context, db sql.DB) error { date_created timestamp with time zone not null, date_updated timestamp with time zone not null);` _, err := db.ExecContext(ctx, query) - return err } -func truncateCockroachDB(ctx context.Context, db sql.DB) error { +func truncateCockroachDB(ctx context.Context, db *sql.DB) error { const query = `TRUNCATE projectmanagement.task` _, err := db.ExecContext(ctx, query) return err @@ -49,9 +49,7 @@ func TestIntegrationDBInsertSelect(t *testing.T) { ctx := context.Background() cdbContainer, err := startContainer(ctx) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) t.Cleanup(func() { if err := cdbContainer.Terminate(ctx); err != nil { t.Fatalf("failed to terminate container: %s", err) @@ -59,16 +57,13 @@ func TestIntegrationDBInsertSelect(t *testing.T) { }) db, err := sql.Open("pgx", cdbContainer.URI+"/projectmanagement") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) defer db.Close() - err = initCockroachDB(ctx, *db) - if err != nil { - t.Fatal(err) - } - defer truncateCockroachDB(ctx, *db) + require.NoError(t, initCockroachDB(ctx, db)) + defer func(t *testing.T, ctx context.Context, db *sql.DB) { + require.NoError(t, truncateCockroachDB(ctx, db)) + }(t, ctx, db) now := time.Now() @@ -84,9 +79,7 @@ func TestIntegrationDBInsertSelect(t *testing.T) { tsk.DateDue, tsk.DateCreated, tsk.DateUpdated) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) // Select data savedTsk := task{ID: tsk.ID} @@ -95,10 +88,7 @@ func TestIntegrationDBInsertSelect(t *testing.T) { where id = $1` row := db.QueryRowContext(ctx, findQuery, tsk.ID) err = row.Scan(&savedTsk.Description, &savedTsk.DateDue, &savedTsk.DateCreated, &savedTsk.DateUpdated) - if err != nil { - t.Fatal(err) - } - + require.NoError(t, err) assert.Equal(t, tsk.ID, savedTsk.ID) assert.Equal(t, tsk.Description, savedTsk.Description) assert.Equal(t, tsk.DateDue, savedTsk.DateDue)