diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..b87ae7b --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +# Simple go lint and test. +os: linux +dist: bionic +language: go +go: + - 1.17.x +install: + - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.41.1 +script: + - make test diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dd34538 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +all: + @echo "try: make test" + +test: lint + go test -race -covermode=atomic ./... + # Test 32 bit OSes. + GOOS=linux GOARCH=386 go build . + GOOS=freebsd GOARCH=386 go build . + +lint: + # Test lint on four platforms. + GOOS=linux golangci-lint run --enable-all -D maligned,scopelint,interfacer,golint,tagliatelle,exhaustivestruct,cyclop + GOOS=darwin golangci-lint run --enable-all -D maligned,scopelint,interfacer,golint,tagliatelle,exhaustivestruct,cyclop + GOOS=windows golangci-lint run --enable-all -D maligned,scopelint,interfacer,golint,tagliatelle,exhaustivestruct,cyclop + GOOS=freebsd golangci-lint run --enable-all -D maligned,scopelint,interfacer,golint,tagliatelle,exhaustivestruct,cyclop diff --git a/cnfgfile/file_test.go b/cnfgfile/file_test.go index 3e70962..cea32e9 100644 --- a/cnfgfile/file_test.go +++ b/cnfgfile/file_test.go @@ -1,4 +1,4 @@ -package cnfgfile +package cnfgfile_test import ( "fmt" @@ -7,6 +7,7 @@ import ( "github.com/stretchr/testify/assert" "golift.io/cnfg" + "golift.io/cnfg/cnfgfile" ) type testStruct struct { @@ -78,13 +79,13 @@ func TestUnmarshalErrors(t *testing.T) { a := assert.New(t) c := &testStruct{} - err := Unmarshal(c, "/etc/passwd") + err := cnfgfile.Unmarshal(c, "/etc/passwd") a.NotNil(err, "there should be an error parsing a password file") - err = Unmarshal(c, "no file here") + err = cnfgfile.Unmarshal(c, "no file here") a.NotNil(err, "there should be an error parsing a missing file") - err = Unmarshal(c) + err = cnfgfile.Unmarshal(c) a.NotNil(err, "there should be an error parsing a nil file") } @@ -93,7 +94,7 @@ func TestUnmarshalJSON(t *testing.T) { a := assert.New(t) c := &testStruct{} - err := Unmarshal(c, "tests/config.json") + err := cnfgfile.Unmarshal(c, "tests/config.json") testUnmarshalValues(a, c, err, "TestUnmarshalJSON") } @@ -103,7 +104,7 @@ func TestUnmarshalXML(t *testing.T) { a := assert.New(t) c := &testStruct{} - err := Unmarshal(c, "tests/config.xml") + err := cnfgfile.Unmarshal(c, "tests/config.xml") testUnmarshalValues(a, c, err, "TestUnmarshalXML") } @@ -113,7 +114,7 @@ func TestUnmarshalYAML(t *testing.T) { a := assert.New(t) c := &testStruct{} - err := Unmarshal(c, "tests/config.yaml") + err := cnfgfile.Unmarshal(c, "tests/config.yaml") testUnmarshalValues(a, c, err, "TestUnmarshalYAML") } @@ -123,7 +124,7 @@ func TestUnmarshalTOML(t *testing.T) { a := assert.New(t) c := &testStruct{} - err := Unmarshal(c, "tests/config.toml") + err := cnfgfile.Unmarshal(c, "tests/config.toml") testUnmarshalValues(a, c, err, "TestUnmarshalTOML") } @@ -146,7 +147,7 @@ func ExampleUnmarshal() { yaml := []byte("---\ninterval: 5m\nlocation: Earth\nprovided: true") path := "/tmp/path_to_config.yaml" - err := ioutil.WriteFile(path, yaml, 0600) + err := ioutil.WriteFile(path, yaml, 0o600) if err != nil { panic(err) } @@ -157,7 +158,7 @@ func ExampleUnmarshal() { // Simply pass in your config file. If it contains ".yaml" it will be parsed as YAML. // Same for ".xml" and ".json". If the file has none of these extensions it is parsed // as TOML. Meaning if you name your config "config.conf" it needs ot be TOML formatted. - err = Unmarshal(c, path) + err = cnfgfile.Unmarshal(c, path) if err != nil { panic(err) } diff --git a/env_test.go b/env_test.go index d4f8d5c..4bf7361 100644 --- a/env_test.go +++ b/env_test.go @@ -12,6 +12,7 @@ import ( "golift.io/cnfg" ) +//nolint:staticcheck type testStruct struct { PointerSlice []*testSubConfig `json:"pslice" xml:"pslice" yaml:"pslice" toml:"pslice"` StructSlice []testSubConfig `json:"sslice" xml:"sslice" yaml:"sslice" toml:"sslice"` @@ -76,7 +77,7 @@ func testUnmarshalFileValues(a *assert.Assertions, c *testStruct, err error, fro a.Nil(c.PointerStruct2, from+"pointer struct 2 must be nil") } -func TestBrokenENV(t *testing.T) { +func TestBrokenENV(t *testing.T) { //nolint:paralleltest type testBroken struct { Broke []interface{} `xml:"broke"` } @@ -112,12 +113,12 @@ func TestBrokenENV(t *testing.T) { a.False(ok) } -func TestUnmarshalENVerrors(t *testing.T) { +func TestUnmarshalENVerrors(t *testing.T) { //nolint:paralleltest a := assert.New(t) type tester struct { unexpd map[string]string - Works map[string]string `xml:"works,delenv"` + Works map[string]string `xml:"works,delenv"` //nolint:staticcheck Rad map[string][]int `xml:"yup"` Error error `xml:"error"` } diff --git a/parse.go b/parse.go index 79151d0..3f4b6b5 100644 --- a/parse.go +++ b/parse.go @@ -55,6 +55,7 @@ func (p *parser) Struct(field reflect.Value, prefix string) (bool, error) { return exitOk, nil } +//nolint:cyclop func (p *parser) Anything(field reflect.Value, tag, envval string, force, delenv bool) (bool, error) { // log.Println("Anything", envval, tag, field.Kind(), field.Type(), field.Interface()) if exists, err := p.Interface(field, tag, envval); err != nil { diff --git a/parse_test.go b/parse_test.go index 32e220a..0e49442 100644 --- a/parse_test.go +++ b/parse_test.go @@ -21,11 +21,11 @@ func TestParseInt(t *testing.T) { } } -func TestParseByteSlice(t *testing.T) { +func TestParseByteSlice(t *testing.T) { //nolint:paralleltest a := assert.New(t) type test struct { - F []byte `xml:"bytes,delenv"` + F []byte `xml:"bytes,delenv"` //nolint:staticcheck } os.Setenv("D_BYTES", "byte slice incoming") diff --git a/unparse.go b/unparse.go index dec1665..b5461cb 100644 --- a/unparse.go +++ b/unparse.go @@ -161,6 +161,7 @@ func (p *unparser) Slice(field reflect.Value, tag string, omitempty bool) (Pairs // slice of bytes works differently than any other slice type. if field.Type().String() == "[]uint8" { output.Set(tag, string(field.Bytes())) + return output, nil }