Skip to content

Commit

Permalink
feat: Added "make lint" to target and added it to make test. Resolved…
Browse files Browse the repository at this point in the history
… all lint errors as well

Signed-off-by: Marc-philippe Fuller <marc-philippe.fuller@intel.com>
  • Loading branch information
marcpfuller committed Jan 26, 2022
1 parent 09605dd commit 6168474
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 12 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
linters:
disable:
enable:
- gosec
13 changes: 10 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.PHONY: build test clean prepare update docker
.PHONY: build test unittest lint clean prepare update docker

GO=CGO_ENABLED=1 GO111MODULE=on go
ARCH=$(shell uname -m)

MICROSERVICES=cmd/device-rfid-llrp

Expand All @@ -19,8 +20,14 @@ tidy:
cmd/device-rfid-llrp:
$(GO) build $(GOFLAGS) -o $@ ./cmd

test:
$(GO) test -coverprofile=coverage.out ./...
unittest:
$(GO) test ./... -coverprofile=coverage.out ./...

lint:
@which golangci-lint >/dev/null || echo "WARNING: go linter not installed. To install, run\n curl -sSfL https://github.com/raw/golangci/golangci-lint/master/install.sh | sh -s -- -b \$$(go env GOPATH)/bin v1.42.1"
@if [ "z${ARCH}" = "zx86_64" ] && which golangci-lint >/dev/null ; then golangci-lint run --config .golangci.yml ; else echo "WARNING: Linting skipped (not on x86_64 or linter not installed)"; fi

test: unittest lint
$(GO) vet ./...
gofmt -l $$(find . -type f -name '*.go'| grep -v "/vendor/")
[ "`gofmt -l $$(find . -type f -name '*.go'| grep -v "/vendor/")`" = "" ]
Expand Down
5 changes: 2 additions & 3 deletions cmd/llrp/llrp.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,9 @@ func sendCustom(ctx context.Context, c *llrp.Client, custom string) error {
if err := json.Unmarshal(data, msgs); err != nil {
return err
}

for i, cst := range *msgs {
for i := range *msgs {
log.Infof("sending custom %d", i)
logErr(send(ctx, c, &cst, &llrp.CustomMessage{}))
logErr(send(ctx, c, &(*msgs)[i], &llrp.CustomMessage{}))
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ func (d *Driver) discover(ctx context.Context) {
d.lc.Warn("Discover process has been cancelled!", "ctxErr", ctx.Err())
}

d.lc.Info(fmt.Sprintf("Discovered %d new devices in %v.", len(result), time.Now().Sub(t1)))
d.lc.Info(fmt.Sprintf("Discovered %d new devices in %v.", len(result), time.Since(t1)))
// pass the discovered devices to the EdgeX SDK to be passed through to the provision watchers
d.deviceCh <- result
}
2 changes: 2 additions & 0 deletions internal/llrp/reader_functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func writeCapture(dir string, idx uint32, result []byte, typ MessageType, decode
bfn := filepath.Join(dir, baseName+".bytes")
jfn := filepath.Join(dir, baseName+".json")

//nolint: gosec //G306: Expect WriteFile permissions to be 0600 or less
if err := ioutil.WriteFile(bfn, result, 0644); err != nil {
return err
}
Expand All @@ -183,6 +184,7 @@ func writeCapture(dir string, idx uint32, result []byte, typ MessageType, decode
return err
}

//nolint: gosec //G306: Expect WriteFile permissions to be 0600 or less
if err := ioutil.WriteFile(jfn, j, 0644); err != nil {
return err
}
Expand Down
12 changes: 7 additions & 5 deletions internal/llrp/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"
"encoding/binary"
"encoding/hex"
"github.com/pkg/errors"
"io"
"io/ioutil"
"math/rand"
Expand All @@ -19,6 +18,8 @@ import (
"sync"
"testing"
"time"

"github.com/pkg/errors"
)

func TestClient_readHeader(t *testing.T) {
Expand Down Expand Up @@ -194,6 +195,7 @@ func dummyReply(h *Header, rfid net.Conn) error {
// randomReply returns a function that sends replies with a random payload
// such that minSz <= len(payload) <= maxSz.
func randomReply(minSz, maxSz uint32) func(h *Header, rfid net.Conn) error {
//nolint:gosec // G404: Use of weak random number generator
rnd := rand.New(rand.NewSource(1))
if minSz > maxSz {
panic(errors.Errorf("bad sizes: %d > %d", minSz, maxSz))
Expand Down Expand Up @@ -372,11 +374,9 @@ func TestClient_ManySenders(t *testing.T) {
var h Header
err := connectSuccess(&h, rfid)
op, nextOp := dummyRead, randomReply(0, 1024)
s1, s2 := "read", "reply"
for err == nil {
err = op(&h, rfid)
op, nextOp = nextOp, op
s1, s2 = s2, s1
}
rfidErrs <- errors.Wrap(err, "mock failed")
}()
Expand All @@ -392,8 +392,10 @@ func TestClient_ManySenders(t *testing.T) {
go func() {
defer msgGrp.Done()

//nolint:gosec // G404: Use of weak random number generator
sz := rand.Int31n(1024)
data := make([]byte, sz)
//nolint:gosec // G404: Use of weak random number generator
rand.Read(data)

_, _, err := c.SendMessage(ctx, MsgCustomMessage, data)
Expand Down Expand Up @@ -454,11 +456,9 @@ func BenchmarkReader_ManySenders(b *testing.B) {
var h Header
err := connectSuccess(&h, rfid)
op, nextOp := dummyRead, randomReply(0, 1024)
s1, s2 := "read", "reply"
for err == nil {
err = op(&h, rfid)
op, nextOp = nextOp, op
s1, s2 = s2, s1
}
rfidErrs <- errors.Wrap(err, "mock failed")
}()
Expand All @@ -477,8 +477,10 @@ func BenchmarkReader_ManySenders(b *testing.B) {
go func() {
defer msgGrp.Done()

//nolint:gosec // G404: Use of weak random number generator
sz := rand.Int31n(1024)
data := make([]byte, sz)
//nolint:gosec // G404: Use of weak random number generator
rand.Read(data)

_, _, err := r.SendMessage(ctx, MsgCustomMessage, data)
Expand Down
1 change: 1 addition & 0 deletions internal/retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ func (ebo ExpBackOff) nextWait(attempts int) time.Duration {
if ebo.Jitter {
// Choose a random value in [0, 2^attempt) with uniform probability.
// The expected value is (1/2)*2^attempt = 2^(attempt-1).
//nolint:gosec // G404: Use of weak random number generator
s := time.Duration(rand.Int63n(1 << attempts))
if s > 0 && ebo.BackOff > ebo.Max/s {
wait = ebo.Max
Expand Down

0 comments on commit 6168474

Please sign in to comment.