Skip to content

Commit

Permalink
Add project structure, tooling, and clkm package.
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrt committed Dec 13, 2024
1 parent 8766829 commit 5ba5324
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: 'ci'

on:
push:
branches:
- 'main'
pull_request:

jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: 'actions/checkout@v4'
- uses: 'actions/setup-go@v5'
with:
go-version: '1.23.3'
- run: './validate.sh'
- uses: 'codecov/codecov-action@v5'
with:
token: '${{ secrets.CODECOV_TOKEN }}'
disable_search: true
fail_ci_if_error: true
files: '.build/coverage/coverage.out'
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
.build/
.idea/
Thumbs.db
36 changes: 36 additions & 0 deletions clkm/clk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package clkm

import (
"context"

"github.com/benbjohnson/clock"
"github.com/ibrt/golang-utils/injectz"
)

type contextKey int

const (
clkContextKey contextKey = iota
)

var (
_ injectz.Initializer = Initializer
)

// Clock describes a clock.
type Clock clock.Clock

// Initializer initializes.
func Initializer(_ context.Context) (injectz.Injector, injectz.Releaser) {
return NewSingletonInjector(clock.New()), injectz.NewNoopReleaser()
}

// NewSingletonInjector injects.
func NewSingletonInjector(clk Clock) injectz.Injector {
return injectz.NewSingletonInjector(clkContextKey, clk)
}

// MustGet extracts, panics if not found.
func MustGet(ctx context.Context) Clock {
return ctx.Value(clkContextKey).(Clock)
}
40 changes: 40 additions & 0 deletions clkm/clk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package clkm_test

import (
"context"
"testing"
"time"

"github.com/ibrt/golang-utils/fixturez"
. "github.com/onsi/gomega"

"github.com/ibrt/golang-modules/clkm"
"github.com/ibrt/golang-modules/clkm/tclkm"
)

type RealSuite struct {
CLK *tclkm.RealHelper
}

func TestRealSuite(t *testing.T) {
fixturez.RunSuite(t, &RealSuite{})
}

func (s *RealSuite) TestRealHelper(ctx context.Context, g *WithT) {
g.Expect(clkm.MustGet(ctx)).NotTo(BeNil())
g.Expect(clkm.MustGet(ctx)).NotTo(BeZero())
}

type MockSuite struct {
Clock *tclkm.MockHelper
}

func TestMockSuite(t *testing.T) {
fixturez.RunSuite(t, &MockSuite{})
}

func (s *MockSuite) TestMockHelper(ctx context.Context, g *WithT) {
now := time.Now().Add(-time.Minute)
s.Clock.Mock.Set(now)
g.Expect(clkm.MustGet(ctx).Now()).To(Equal(now))
}
63 changes: 63 additions & 0 deletions clkm/tclkm/clk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package tclkm

import (
"context"
"time"

"github.com/benbjohnson/clock"
"github.com/ibrt/golang-utils/fixturez"
"github.com/ibrt/golang-utils/injectz"
"github.com/onsi/gomega"
"go.uber.org/mock/gomock"

"github.com/ibrt/golang-modules/clkm"
)

var (
_ fixturez.BeforeSuite = (*RealHelper)(nil)
_ fixturez.AfterSuite = (*RealHelper)(nil)
_ fixturez.BeforeSuite = (*MockHelper)(nil)
_ fixturez.AfterSuite = (*MockHelper)(nil)
_ fixturez.BeforeTest = (*MockHelper)(nil)
)

// RealHelper is a test helper.
type RealHelper struct {
releaser injectz.Releaser
}

// BeforeSuite implements fixturez.BeforeSuite.
func (h *RealHelper) BeforeSuite(ctx context.Context, _ *gomega.WithT) context.Context {
injector, releaser := clkm.Initializer(ctx)
h.releaser = releaser
return injector(ctx)
}

// AfterSuite implements fixturez.AfterSuite.
func (h *RealHelper) AfterSuite(_ context.Context, _ *gomega.WithT) {
h.releaser()
h.releaser = nil
}

// MockHelper is a test helper.
type MockHelper struct {
Mock *clock.Mock
}

// BeforeSuite implements fixturez.BeforeSuite.
func (h *MockHelper) BeforeSuite(ctx context.Context, _ *gomega.WithT) context.Context {
h.Mock = clock.NewMock()
h.Mock.Set(time.Now())
return clkm.NewSingletonInjector(h.Mock)(ctx)
}

// AfterSuite implements fixturez.AfterSuite.
func (h *MockHelper) AfterSuite(_ context.Context, _ *gomega.WithT) {
h.Mock = nil
}

// BeforeTest implements fixturez.BeforeTest.
func (h *MockHelper) BeforeTest(ctx context.Context, _ *gomega.WithT, _ *gomock.Controller) context.Context {
h.Mock.Set(time.Now().UTC())
return ctx
}
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ignore:
- '**/*.gen.go'
23 changes: 23 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module github.com/ibrt/golang-modules

go 1.23

require (
github.com/benbjohnson/clock v1.3.5
github.com/ibrt/golang-utils v0.3.0
github.com/onsi/gomega v1.36.1
go.uber.org/mock v0.5.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.18.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/rodaine/table v1.3.0 // indirect
golang.org/x/net v0.32.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
60 changes: 60 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o=
github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 h1:5iH8iuqE5apketRbSFBy+X1V0o+l+8NF1avt4HWl7cA=
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
github.com/ibrt/golang-utils v0.3.0 h1:HgszqPM/OciM/1MXOCfFGXw8aacZg9zvvAjStVSjb0Q=
github.com/ibrt/golang-utils v0.3.0/go.mod h1:pm/WsgeRSqriXaFRSQ37pfXoSkygQs251Q03ZcxzsVg=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/onsi/ginkgo/v2 v2.20.1 h1:YlVIbqct+ZmnEph770q9Q7NVAz4wwIiVNahee6JyUzo=
github.com/onsi/ginkgo/v2 v2.20.1/go.mod h1:lG9ey2Z29hR41WMVthyJBGUBcBhGOtoPF2VFMvBXFCI=
github.com/onsi/gomega v1.36.1 h1:bJDPBO7ibjxcbHMgSCoo4Yj18UWbKDlLwX1x9sybDcw=
github.com/onsi/gomega v1.36.1/go.mod h1:PvZbdDc8J6XJEpDK4HCuRBm8a6Fzp9/DmhC9C7yFlog=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rodaine/table v1.3.0 h1:4/3S3SVkHnVZX91EHFvAMV7K42AnJ0XuymRR2C5HlGE=
github.com/rodaine/table v1.3.0/go.mod h1:47zRsHar4zw0jgxGxL9YtFfs7EGN6B/TaS+/Dmk4WxU=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM=
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
26 changes: 26 additions & 0 deletions validate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

set -ex
cd "$(dirname "${BASH_SOURCE[0]}")"

CMD_GOLINT="go run golang.org/x/lint/golint@latest"
CMD_STATICCHECK="go run honnef.co/go/tools/cmd/staticcheck@latest"
CMD_GOCOV="go run github.com/axw/gocov/gocov@latest"
CMD_GOCOV_HTML="go run github.com/matm/gocov-html/cmd/gocov-html@latest "

DIR_BUILD=".build"
DIR_COVERAGE="$DIR_BUILD/coverage"

rm -rf "$DIR_BUILD"
mkdir -p "$DIR_COVERAGE"

go mod tidy
go generate ./...
go build -v ./...
diff -u <(echo -n) <(gofmt -d ./)
$CMD_GOLINT -set_exit_status ./...
go vet ./...
$CMD_STATICCHECK ./...
go test -trimpath -race -failfast -shuffle=on -covermode=atomic -coverprofile="$DIR_COVERAGE/coverage.out" -count=1 ./...
$CMD_GOCOV convert "$DIR_COVERAGE/coverage.out" > "$DIR_COVERAGE/coverage.json"
$CMD_GOCOV_HTML -t golang < "$DIR_COVERAGE/coverage.json"> "$DIR_COVERAGE/coverage.html"

0 comments on commit 5ba5324

Please sign in to comment.