diff --git a/.travis.yml b/.travis.yml index 25fcfb1..c99741f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,15 +1,12 @@ language: go go: - - 1.6.3 - - 1.7.3 + - 1.11.x + - 1.12.x sudo: false install: - - go get -d -t -v ./... - - go get -v golang.org/x/tools/cover - - go get -v github.com/bradfitz/goimports - - go get -v github.com/golang/lint/golint + - go get -v github.com/golangci/golangci-lint/cmd/golangci-lint script: - - export PATH=$PATH:$HOME/gopath/bin + - export GO111MODULE=on - ./goclean.sh after_success: - go get -v github.com/mattn/goveralls diff --git a/goclean.sh b/goclean.sh index 5e996cb..5cf1890 100755 --- a/goclean.sh +++ b/goclean.sh @@ -1,21 +1,35 @@ #!/bin/bash # The script does automatic checking on a Go package and its sub-packages, including: # 1. gofmt (http://golang.org/cmd/gofmt/) -# 2. goimports (https://github.com/bradfitz/goimports) -# 3. golint (https://github.com/golang/lint) -# 4. go vet (http://golang.org/cmd/vet) -# 5. test coverage (http://blog.golang.org/cover) +# 2. golint (https://github.com/golang/lint) +# 3. go vet (http://golang.org/cmd/vet) +# 4. gosimple (https://github.com/dominikh/go-simple) +# 5. unconvert (https://github.com/mdempsky/unconvert) +# 6. goimports (https://github.com/bradfitz/goimports) +# 7. race detector (http://blog.golang.org/race-detector) +# 8. test coverage (http://blog.golang.org/cover) -set -e +set -ex # Automatic checks cd xdr2 -test -z "$(gofmt -l -w . | tee /dev/stderr)" -test -z "$(goimports -l -w . | tee /dev/stderr)" -test -z "$(golint . | tee /dev/stderr)" -go vet ./... + +# run tests env GORACE="halt_on_error=1" go test -v -race ./... +# golangci-lint (github.com/golangci/golangci-lint) is used to run each each +# static checker. + +# check linters +golangci-lint run --disable-all --deadline=10m \ + --enable=gofmt \ + --enable=golint \ + --enable=vet \ + --enable=gosimple \ + --enable=unconvert \ + --enable=ineffassign \ + --enable=goimports + # Run test coverage on each subdirectories and merge the coverage profile. echo "mode: count" > profile.cov diff --git a/xdr2/decode_test.go b/xdr2/decode_test.go index 310629d..a8ee6b5 100644 --- a/xdr2/decode_test.go +++ b/xdr2/decode_test.go @@ -270,8 +270,8 @@ func TestUnmarshal(t *testing.T) { // float64 - XDR Double-precision Floating-Point {[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, float64(0), 8, nil}, {[]byte{0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18}, float64(3.141592653589793), 8, nil}, - {[]byte{0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, float64(math.Inf(-1)), 8, nil}, - {[]byte{0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, float64(math.Inf(0)), 8, nil}, + {[]byte{0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, math.Inf(-1), 8, nil}, + {[]byte{0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, math.Inf(0), 8, nil}, // Expected Failures -- not enough bytes {[]byte{0xff, 0xff, 0xff}, float64(0), 3, &UnmarshalError{ErrorCode: ErrIO}}, {[]byte{0xff, 0x00, 0xff, 0x00}, float64(0), 4, &UnmarshalError{ErrorCode: ErrIO}}, @@ -470,8 +470,8 @@ func TestDecoder(t *testing.T) { // Double {fDecodeDouble, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, float64(0), 8, 0, nil}, {fDecodeDouble, []byte{0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18}, float64(3.141592653589793), 8, 0, nil}, - {fDecodeDouble, []byte{0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, float64(math.Inf(-1)), 8, 0, nil}, - {fDecodeDouble, []byte{0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, float64(math.Inf(0)), 8, 0, nil}, + {fDecodeDouble, []byte{0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, math.Inf(-1), 8, 0, nil}, + {fDecodeDouble, []byte{0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, math.Inf(0), 8, 0, nil}, // Enum {fDecodeEnum, []byte{0x00, 0x00, 0x00, 0x00}, int32(0), 4, 0, nil}, @@ -572,9 +572,7 @@ func TestDecoder(t *testing.T) { } for _, decoder := range decoders { for i, test := range tests { - err = nil - var dec *Decoder - dec = decoder(test) + dec := decoder(test) switch test.f { case fDecodeBool: rv, n, err = dec.DecodeBool() diff --git a/xdr2/encode.go b/xdr2/encode.go index 7bac268..cc90397 100644 --- a/xdr2/encode.go +++ b/xdr2/encode.go @@ -167,7 +167,7 @@ func (enc *Encoder) EncodeEnum(v int32, validEnums map[int32]bool) (int, error) // Represented as an XDR encoded enumeration where 0 is false and 1 is true func (enc *Encoder) EncodeBool(v bool) (int, error) { i := int32(0) - if v == true { + if v { i = 1 } return enc.EncodeInt(i) @@ -383,7 +383,7 @@ func (enc *Encoder) encodeFixedArray(v reflect.Value, ignoreOpaque bool) (int, e // copying the array into a new slice. This is rather ugly, but // the inability to create a constant slice from an // unaddressable array is a limitation of Go. - slice := make([]byte, v.Len(), v.Len()) + slice := make([]byte, v.Len()) reflect.Copy(reflect.ValueOf(slice), v) return enc.EncodeFixedOpaque(slice) } diff --git a/xdr2/encode_test.go b/xdr2/encode_test.go index 1b04cfa..52485a0 100644 --- a/xdr2/encode_test.go +++ b/xdr2/encode_test.go @@ -222,8 +222,8 @@ func TestMarshal(t *testing.T) { // float64 - XDR Double-precision Floating-Point {float64(0), []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8, nil}, {float64(3.141592653589793), []byte{0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18}, 8, nil}, - {float64(math.Inf(-1)), []byte{0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8, nil}, - {float64(math.Inf(0)), []byte{0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8, nil}, + {math.Inf(-1), []byte{0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8, nil}, + {math.Inf(0), []byte{0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8, nil}, // Expected Failure -- Short write {float64(3.141592653589793), []byte{0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d}, 7, &MarshalError{ErrorCode: ErrIO}}, @@ -404,8 +404,8 @@ func TestEncoder(t *testing.T) { // Double {fEncodeDouble, float64(0), []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8, nil}, {fEncodeDouble, float64(3.141592653589793), []byte{0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18}, 8, nil}, - {fEncodeDouble, float64(math.Inf(-1)), []byte{0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8, nil}, - {fEncodeDouble, float64(math.Inf(0)), []byte{0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8, nil}, + {fEncodeDouble, math.Inf(-1), []byte{0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8, nil}, + {fEncodeDouble, math.Inf(0), []byte{0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8, nil}, // Expected Failure -- Short write {fEncodeDouble, float64(3.141592653589793), []byte{0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d}, 7, &MarshalError{ErrorCode: ErrIO}}, @@ -493,7 +493,6 @@ func TestEncoder(t *testing.T) { var n int for i, test := range tests { - err = nil data := newFixedWriter(test.wantN) enc := NewEncoder(data) switch test.f { diff --git a/xdr2/fixedIO_test.go b/xdr2/fixedIO_test.go index d329a9a..f7fca1e 100644 --- a/xdr2/fixedIO_test.go +++ b/xdr2/fixedIO_test.go @@ -53,7 +53,7 @@ func (w *fixedWriter) Bytes() []byte { // newFixedWriter returns a new io.Writer that will error once more bytes than // the specified max have been written. func newFixedWriter(max int) *fixedWriter { - b := make([]byte, max, max) + b := make([]byte, max) fw := fixedWriter{b, 0} return &fw } diff --git a/xdr2/go.mod b/xdr2/go.mod new file mode 100644 index 0000000..3ba84ba --- /dev/null +++ b/xdr2/go.mod @@ -0,0 +1 @@ +module github.com/davecgh/go-xdr/xdr2