Skip to content

Commit

Permalink
build: replace with tools directory
Browse files Browse the repository at this point in the history
More decoupled, easier to maintain.
  • Loading branch information
odsod committed Jul 22, 2020
1 parent 0523fa3 commit 2afd2ac
Show file tree
Hide file tree
Showing 32 changed files with 827 additions and 114 deletions.
8 changes: 0 additions & 8 deletions .circleci/config.yml

This file was deleted.

9 changes: 9 additions & 0 deletions .cloudbuild/review.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
steps:
- name: gcr.io/einride/cloud-builder
args: ["/cloud-builder/git-init.sh"]
env:
- REPO_NAME=$REPO_NAME
- COMMIT_SHA=$COMMIT_SHA

- name: gcr.io/einride/cloud-builder
args: ["make"]
15 changes: 15 additions & 0 deletions .cloudbuild/rules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
gcp_project := einride
github_org := einride
repo_name := $(shell basename -s .git $(shell git config --get remote.origin.url))
git_root := $(shell git rev-parse --show-toplevel)
cloudbuild_root := $(shell realpath --relative-to $(git_root) $(dir $(lastword $(MAKEFILE_LIST))))

.PHONY: gcloud-builds-triggers-create
gcloud-builds-triggers-create:
gcloud beta builds triggers create github \
--project=$(gcp_project) \
--repo-owner=$(github_org) \
--repo-name=$(repo_name) \
--pull-request-pattern='.*' \
--description='$(repo_name)-review' \
--build-config='$(cloudbuild_root)/review.yaml'
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
vendor
.idea
.gobincache
test/mocks
tools/*/*/
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

20 changes: 20 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
run:
timeout: 5m
skip-dirs:
- gen

linters:
enable-all: true
disable:
- dupl # allow duplication
- funlen # allow long functions
- gomnd # allow some magic numbers
- wsl # unwanted amount of whitespace
- godox # allow TODOs
- interfacer # deprecated by the author for having too many false positives
- gocognit # allow higher cognitive complexity
- testpackage # unwanted convention
- nestif # allow deep nesting
- unparam # allow constant parameters
- goerr113 # allow "dynamic" errors
- gofumpt # TODO: enable when whitespace diff issue is fixed
42 changes: 7 additions & 35 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,62 +1,34 @@
# all: run a complete build
.PHONY: all
all: \
circleci-config-validate \
go-generate \
go-review \
go-test \
go-lint \
go-mod-tidy \
git-verify-submodules \
git-verify-nodiff

# clean: remove generated build files
.PHONY: clean
clean:
rm -rf build

.PHONY: build
build:
@git submodule update --init --recursive $@

include build/rules.mk
build/rules.mk: build
@# included in submodule: build
include tools/git-verify-nodiff/rules.mk
include tools/golangci-lint/rules.mk
include tools/goreview/rules.mk
include tools/xtools/rules.mk

# go-mod-tidy: update go modules
.PHONY: go-mod-tidy
go-mod-tidy:
go mod tidy -v

# go-lint: lint Go files
.PHONY: go-lint
go-lint: $(GOLANGCI_LINT)
# dupl: disabled due to the testdata in spherical_point_cloud_test
$(GOLANGCI_LINT) run --enable-all --disable=dupl

# go-test: run Go test suite
.PHONY: go-test
go-test:
go test -race -cover ./...

# go-review: run goreview linter
.PHONY: go-review
go-review: $(GOREVIEW)
$(GOREVIEW) -c 1 ./...

# circleci-config-validate: validate CircleCI config
.PHONY: circleci-config-validate
circleci-config-validate: $(CIRCLECI)
$(CIRCLECI) config validate

# go-generate: generate Go code
.PHONY: go-generate
go-generate: returnmode_string.go productid_string.go

returnmode_string.go: returnmode.go $(GOBIN)
$(GOBIN) -m -run golang.org/x/tools/cmd/stringer \
-type ReturnMode -trimprefix ReturnMode -output $@ $<
returnmode_string.go: returnmode.go $(stringer)
$(stringer) -type ReturnMode -trimprefix ReturnMode -output $@ $<

productid_string.go: productid.go $(GOBIN)
$(GOBIN) -m -run golang.org/x/tools/cmd/stringer \
-type ProductID -trimprefix ProductID -output $@ $<
$(stringer) -type ProductID -trimprefix ProductID -output $@ $<
1 change: 0 additions & 1 deletion build
Submodule build deleted from c6a009
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
module github.com/einride/vlp16-go

go 1.12
go 1.14

require (
github.com/einride/unit v1.6.0
github.com/stretchr/testify v1.3.0
github.com/einride/unit v1.7.2
golang.org/x/net v0.0.0-20190909003024-a7b16738d86b
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/tools v0.0.0-20190729092621-ff9f1409240a
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522
gotest.tools/v3 v3.0.2
)
24 changes: 11 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
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/einride/unit v1.6.0 h1:V5vGFpNfGkFDPPhOduQhI4JSzePdDeHNI6NXyE7EFNo=
github.com/einride/unit v1.6.0/go.mod h1:VkeEoz/WVqQktnRS0c+Fn82+cgzbXaCxwrVAuku9RmI=
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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/einride/unit v1.7.2 h1:eZCZ4aktp31wDvVtmVSi2w3htKS92dBCntjJgH75/Uc=
github.com/einride/unit v1.7.2/go.mod h1:vaWT5fip7jxzNdDZsk62lYuoUNjXzP8hl08NNKAHBqk=
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190909003024-a7b16738d86b h1:XfVGCX+0T4WOStkaOsJRllbsiImhB2jgVBGc9L0lPGc=
golang.org/x/net v0.0.0-20190909003024-a7b16738d86b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand All @@ -18,7 +15,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190729092621-ff9f1409240a h1:mEQZbbaBjWyLNy0tmZmgEuQAR8XOQ3hL8GYi3J/NG64=
golang.org/x/tools v0.0.0-20190729092621-ff9f1409240a/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522 h1:bhOzK9QyoD0ogCnFro1m2mz41+Ib0oOhfJnBp5MR4K4=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gotest.tools/v3 v3.0.2 h1:kG1BFyqVHuQoVQiR1bWGnfz/fmHvvuiSPIV7rvl360E=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
7 changes: 3 additions & 4 deletions packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package vlp16

import (
"encoding/binary"

"golang.org/x/xerrors"
"fmt"
)

const (
Expand Down Expand Up @@ -61,7 +60,7 @@ const (
indexOfProductIDInPacket = indexOfReturnModeInPacket + lengthOfReturnMode
)

// compile-time assertion on structure of packet
// compile-time assertion on structure of packet.
var _ [1206]struct{} = [lengthOfPacket]struct{}{}

// flag is the magic value of the flag field.
Expand All @@ -71,7 +70,7 @@ const flag = 0xeeff
func (p *Packet) Validate() error {
for i := range p.Blocks {
if p.Blocks[i].Flag != flag {
return xerrors.Errorf("validate packet: invalid flag %#x in block %v", p.Blocks[i].Flag, i)
return fmt.Errorf("validate packet: invalid flag %#x in block %v", p.Blocks[i].Flag, i)
}
}
return nil
Expand Down
12 changes: 6 additions & 6 deletions packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"
"gotest.tools/v3/assert"
)

func TestPacket_Unmarshal_TestData(t *testing.T) {
Expand All @@ -17,14 +17,14 @@ func TestPacket_Unmarshal_TestData(t *testing.T) {
var packet Packet
packet.unmarshal(&data)
numPackets++
require.Equal(t, ReturnModeStrongest, packet.ReturnMode)
require.Equal(t, ProductIDVLP16, packet.ProductID)
require.True(t,
assert.Equal(t, ReturnModeStrongest, packet.ReturnMode)
assert.Equal(t, ProductIDVLP16, packet.ProductID)
assert.Assert(t,
time.Duration(packet.Timestamp)*time.Microsecond <= time.Hour,
"timestamp should be <= number of microseconds in an hour",
)
}
require.Equal(t, 1000, numPackets)
assert.Equal(t, 1000, numPackets)
}

func BenchmarkPacket_Unmarshal_Example(b *testing.B) {
Expand All @@ -38,5 +38,5 @@ func BenchmarkPacket_Unmarshal_Example(b *testing.B) {
func TestPacket_Unmarshal_Example(t *testing.T) {
actual := &Packet{}
actual.unmarshal(exampleData())
require.Equal(t, examplePacket(), actual)
assert.DeepEqual(t, examplePacket(), actual)
}
19 changes: 10 additions & 9 deletions pointcloud_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package vlp16

import (
"math"
"testing"

"github.com/stretchr/testify/require"
"gotest.tools/v3/assert"
)

func TestSphericalPointCloud_UnmarshalExamplePacket(t *testing.T) {
Expand All @@ -14,16 +15,16 @@ func TestSphericalPointCloud_UnmarshalExamplePacket(t *testing.T) {

func requirePointCloudEqual(t *testing.T, p *PointCloud, pc *PointCloud) {
const delta = 1e-5
require.Equal(t, p.TimeSinceTopOfHour, pc.TimeSinceTopOfHour)
assert.Equal(t, p.TimeSinceTopOfHour, pc.TimeSinceTopOfHour)
for i := range p.Azimuths {
require.InDelta(t, p.Azimuths[i].Radians(), pc.Azimuths[i].Radians(), delta)
assert.Assert(t, math.Abs(p.Azimuths[i].Radians()-pc.Azimuths[i].Radians()) < delta)
}
for i := range p.Points {
require.InDelta(t, p.Points[i].Distance.Metres(), pc.Points[i].Distance.Metres(), delta)
require.Equal(t, p.Points[i].Column, pc.Points[i].Column)
require.Equal(t, p.Points[i].Row, pc.Points[i].Row)
require.Equal(t, p.Points[i].Reflectivity, pc.Points[i].Reflectivity)
require.Equal(t, p.Points[i].IsLastReflection, pc.Points[i].IsLastReflection)
assert.Assert(t, math.Abs(p.Points[i].Distance.Metres()-pc.Points[i].Distance.Metres()) < delta)
assert.Equal(t, p.Points[i].Column, pc.Points[i].Column)
assert.Equal(t, p.Points[i].Row, pc.Points[i].Row)
assert.Equal(t, p.Points[i].Reflectivity, pc.Points[i].Reflectivity)
assert.Equal(t, p.Points[i].IsLastReflection, pc.Points[i].IsLastReflection)
}
}

Expand All @@ -47,7 +48,7 @@ func TestInterpolateAzimuth(t *testing.T) {
} else {
testBool = newAzimuth > azimuth
}
require.True(t, testBool)
assert.Assert(t, testBool)
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package vlp16

import (
"context"
"fmt"
"net"

"golang.org/x/net/ipv4"
"golang.org/x/xerrors"
)

// ListenUDP listens for VLP-16 UDP packets on the specified address.
Expand All @@ -17,11 +17,11 @@ func ListenUDP(ctx context.Context, addr string, receiverOpts ...ReceiverOption)
var listenConfig net.ListenConfig
packetConn, err := listenConfig.ListenPacket(ctx, "udp4", addr)
if err != nil {
return nil, xerrors.Errorf("VLP-16: listen UDP: %w", err)
return nil, fmt.Errorf("VLP-16: listen UDP: %w", err)
}
udpConn := packetConn.(*net.UDPConn)
if err := udpConn.SetReadBuffer(opts.bufferSizeBytes); err != nil {
return nil, xerrors.Errorf("VLP-16: listen UDP: %w", err)
return nil, fmt.Errorf("VLP-16: listen UDP: %w", err)
}
conn := ipv4.NewPacketConn(udpConn)
c := &Receiver{conn: conn}
Expand Down Expand Up @@ -54,11 +54,11 @@ func (c *Receiver) Receive(ctx context.Context) error {
c.currMessageIndex = 0
deadline, _ := ctx.Deadline()
if err := c.conn.SetReadDeadline(deadline); err != nil {
return xerrors.Errorf("VLP-16 receiver: %w", err)
return fmt.Errorf("VLP-16 receiver: %w", err)
}
n, err := c.conn.ReadBatch(c.messages, 0)
if err != nil {
return xerrors.Errorf("VLP-16 receiver: %w", err)
return fmt.Errorf("VLP-16 receiver: %w", err)
}
c.messageBufSize = n
}
Expand Down Expand Up @@ -90,7 +90,7 @@ func (c *Receiver) PointCloud() *PointCloud {
// Close the client's underlying UDP connection.
func (c *Receiver) Close() error {
if err := c.conn.Close(); err != nil {
return xerrors.Errorf("VLP-16 receiver: close: %w", err)
return fmt.Errorf("VLP-16 receiver: close: %w", err)
}
return nil
}
20 changes: 10 additions & 10 deletions receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"
"golang.org/x/net/nettest"
"golang.org/x/sync/errgroup"
"gotest.tools/v3/assert"
)

func TestClient_Receive(t *testing.T) {
Expand All @@ -17,28 +17,28 @@ func TestClient_Receive(t *testing.T) {
defer cancel()
addr := getFreeAddress(t)
rx, err := ListenUDP(ctx, addr)
require.NoError(t, err)
assert.NilError(t, err)
var g errgroup.Group
g.Go(func() error {
return rx.Receive(ctx)
})
conn, err := net.Dial("udp4", addr)
require.NoError(t, err)
assert.NilError(t, err)
_, err = conn.Write(exampleData()[:])
require.NoError(t, err)
require.NoError(t, conn.Close())
require.NoError(t, g.Wait())
require.Equal(t, exampleData()[:], rx.RawPacket())
require.Equal(t, examplePacket(), rx.Packet())
assert.NilError(t, err)
assert.NilError(t, conn.Close())
assert.NilError(t, g.Wait())
assert.DeepEqual(t, exampleData()[:], rx.RawPacket())
assert.DeepEqual(t, examplePacket(), rx.Packet())
requirePointCloudEqual(t, examplePointCloud(), rx.PointCloud())
}

func getFreeAddress(t *testing.T) string {
t.Helper()
l, err := nettest.NewLocalPacketListener("udp4")
require.NoError(t, err)
assert.NilError(t, err)
defer func() {
require.NoError(t, l.Close())
assert.NilError(t, l.Close())
}()
return l.LocalAddr().String()
}
Loading

0 comments on commit 2afd2ac

Please sign in to comment.