Skip to content

Commit

Permalink
Merge pull request #3 from golift/dn2_updates
Browse files Browse the repository at this point in the history
Add tests and build.
  • Loading branch information
davidnewhall authored Oct 5, 2021
2 parents 7cef0f6 + 45aae4f commit 6cd686e
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 15 deletions.
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
21 changes: 11 additions & 10 deletions cnfgfile/file_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cnfgfile
package cnfgfile_test

import (
"fmt"
Expand All @@ -7,6 +7,7 @@ import (

"github.com/stretchr/testify/assert"
"golift.io/cnfg"
"golift.io/cnfg/cnfgfile"
)

type testStruct struct {
Expand Down Expand Up @@ -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")
}

Expand All @@ -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")
}

Expand All @@ -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")
}

Expand All @@ -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")
}

Expand All @@ -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")
}

Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
7 changes: 4 additions & 3 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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"`
}
Expand Down
1 change: 1 addition & 0 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions unparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 6cd686e

Please sign in to comment.