From 934cbe14f737cf54e98538d91d11de02d6b34e8e Mon Sep 17 00:00:00 2001 From: Richard Carson Derr Date: Thu, 11 Apr 2024 00:06:48 -0400 Subject: [PATCH] story(codeql): fully specify go version (#177) * build(issue-176): bump and fully qualify go version * chore(docs): updated coverage badge. * test(issue-176): increase test coverage * chore(docs): updated coverage badge. * build(issue-176): disable 1.22 coverage for now * chore(docs): updated coverage badge. --------- Co-authored-by: GitHub Action --- .github/workflows/codeql.yaml | 2 +- .github/workflows/coverage.yaml | 3 +- README.md | 2 +- go.mod | 2 +- queue/sqs/sqs_test.go | 4 +- queue/sqs/sqsslog/sqslog_test.go | 111 +++++++++++++++++++++++++++++++ queue/sqs/sqsslog/sqsslog.go | 2 +- 7 files changed, 120 insertions(+), 6 deletions(-) create mode 100644 queue/sqs/sqsslog/sqslog_test.go diff --git a/.github/workflows/codeql.yaml b/.github/workflows/codeql.yaml index a49beae..13a06f6 100644 --- a/.github/workflows/codeql.yaml +++ b/.github/workflows/codeql.yaml @@ -30,7 +30,7 @@ jobs: # Custom Go version because of: https://github.com/github/codeql/issues/13992#issuecomment-1711721716 - uses: actions/setup-go@v5 with: - go-version: '1.21' + go-version: '1.22.0' - name: Initialize CodeQL uses: github/codeql-action/init@v3 diff --git a/.github/workflows/coverage.yaml b/.github/workflows/coverage.yaml index fbda55d..496abf9 100644 --- a/.github/workflows/coverage.yaml +++ b/.github/workflows/coverage.yaml @@ -24,8 +24,9 @@ jobs: go-version-file: 'go.mod' - name: Run Test + # re-enable new coverage flow once this issue is resolved, https://github.com/golang/go/issues/65570 run: | - go test -v ./... -covermode=count -coverprofile=coverage.out + GOEXPERIMENT=nocoverageredesign go test -v ./... -covermode=count -coverprofile=coverage.out go tool cover -func=coverage.out -o=coverage.out - name: Go Coverage Badge # Pass the `coverage.out` output to this action diff --git a/README.md b/README.md index 7461f88..649fbb5 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go) [![Go Reference](https://pkg.go.dev/badge/github.com/z5labs/bedrock.svg)](https://pkg.go.dev/github.com/z5labs/bedrock) [![Go Report Card](https://goreportcard.com/badge/github.com/z5labs/bedrock)](https://goreportcard.com/report/github.com/z5labs/bedrock) -![Coverage](https://img.shields.io/badge/Coverage-95.5%25-brightgreen) +![Coverage](https://img.shields.io/badge/Coverage-96.2%25-brightgreen) [![build](https://github.com/z5labs/bedrock/actions/workflows/build.yaml/badge.svg)](https://github.com/z5labs/bedrock/actions/workflows/build.yaml) **bedrock provides a minimal, modular and composable foundation for diff --git a/go.mod b/go.mod index 8a7736c..cb5527a 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/z5labs/bedrock -go 1.21 +go 1.22.0 require ( cloud.google.com/go/pubsub v1.37.0 diff --git a/queue/sqs/sqs_test.go b/queue/sqs/sqs_test.go index f53286f..e1840df 100644 --- a/queue/sqs/sqs_test.go +++ b/queue/sqs/sqs_test.go @@ -243,7 +243,9 @@ func TestBatchDeleteProcessor_Process(t *testing.T) { t.Run("if sqs successfully deleted all messages", func(t *testing.T) { client := sqsBatchDeleteClientFunc(func(ctx context.Context, dmbi *sqs.DeleteMessageBatchInput, f ...func(*sqs.Options)) (*sqs.DeleteMessageBatchOutput, error) { - resp := &sqs.DeleteMessageBatchOutput{} + resp := &sqs.DeleteMessageBatchOutput{ + Successful: make([]types.DeleteMessageBatchResultEntry, 10), + } return resp, nil }) diff --git a/queue/sqs/sqsslog/sqslog_test.go b/queue/sqs/sqsslog/sqslog_test.go new file mode 100644 index 0000000..1c14429 --- /dev/null +++ b/queue/sqs/sqsslog/sqslog_test.go @@ -0,0 +1,111 @@ +// Copyright (c) 2024 Z5Labs and Contributors +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +package sqsslog + +import ( + "bytes" + "encoding/json" + "log/slog" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestMessageId(t *testing.T) { + t.Run("will marshal as a string", func(t *testing.T) { + t.Run("if marshalling to json", func(t *testing.T) { + var buf bytes.Buffer + log := slog.New(slog.NewJSONHandler(&buf, &slog.HandlerOptions{})) + + log.Info("hello", MessageId("1234")) + + var record struct { + MessageId string `json:"sqs_message_id"` + } + err := json.Unmarshal(buf.Bytes(), &record) + if !assert.Nil(t, err) { + return + } + if !assert.Equal(t, "1234", record.MessageId) { + return + } + }) + }) +} + +func TestMessageIds(t *testing.T) { + t.Run("will marshal as a list of strings", func(t *testing.T) { + t.Run("if marshalling to json", func(t *testing.T) { + var buf bytes.Buffer + log := slog.New(slog.NewJSONHandler(&buf, &slog.HandlerOptions{})) + + ids := []string{"1234", "5678"} + log.Info("hello", MessageIds(ids)) + + var record struct { + MessageIds []string `json:"sqs_message_ids"` + } + err := json.Unmarshal(buf.Bytes(), &record) + if !assert.Nil(t, err) { + return + } + if !assert.Len(t, record.MessageIds, len(ids)) { + return + } + if !assert.Equal(t, ids, record.MessageIds) { + return + } + }) + }) +} + +func TestReceiptHandle(t *testing.T) { + t.Run("will marshal as a string", func(t *testing.T) { + t.Run("if marshalling to json", func(t *testing.T) { + var buf bytes.Buffer + log := slog.New(slog.NewJSONHandler(&buf, &slog.HandlerOptions{})) + + log.Info("hello", ReceiptHandle("1234")) + + var record struct { + ReceiptHandle string `json:"sqs_receipt_handle"` + } + err := json.Unmarshal(buf.Bytes(), &record) + if !assert.Nil(t, err) { + return + } + if !assert.Equal(t, "1234", record.ReceiptHandle) { + return + } + }) + }) +} + +func TestMessageAttributes(t *testing.T) { + t.Run("will marshal as object", func(t *testing.T) { + t.Run("if marshalling to json", func(t *testing.T) { + var buf bytes.Buffer + log := slog.New(slog.NewJSONHandler(&buf, &slog.HandlerOptions{})) + + attrs := map[string]string{"1234": "5678"} + log.Info("hello", MessageAttributes(attrs)) + + var record struct { + MessageAttributes map[string]string `json:"sqs_message_attributes"` + } + err := json.Unmarshal(buf.Bytes(), &record) + if !assert.Nil(t, err) { + return + } + if !assert.Len(t, record.MessageAttributes, len(attrs)) { + return + } + if !assert.Equal(t, attrs, record.MessageAttributes) { + return + } + }) + }) +} diff --git a/queue/sqs/sqsslog/sqsslog.go b/queue/sqs/sqsslog/sqsslog.go index fd0ad2f..cde3a6d 100644 --- a/queue/sqs/sqsslog/sqsslog.go +++ b/queue/sqs/sqsslog/sqsslog.go @@ -28,7 +28,7 @@ func ReceiptHandle(s string) slog.Attr { // MessageAttributes returns a slog.Attr for the SQS message attributes. func MessageAttributes(m map[string]string) slog.Attr { - attrs := make([]any, len(m)) + attrs := make([]any, 0, len(m)) for key, val := range m { attrs = append(attrs, slog.String(key, val)) }