Skip to content

Commit

Permalink
fix more lint
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnewhall committed Jan 3, 2022
1 parent 0c0d80e commit 10d53f1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 48 deletions.
2 changes: 1 addition & 1 deletion cnfgfile/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,5 @@ func ExampleUnmarshal() {
}

fmt.Printf("interval: %v, location: %v, provided: %v", c.Interval, c.Location, c.Provided)
// Output: interval: 5m0s, location: Earth, provided: true
// Output: interval: 5m, location: Earth, provided: true
}
93 changes: 48 additions & 45 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,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) { //nolint:paralleltest
func TestBrokenENV(t *testing.T) { //nolint:paralleltest // cannot parallel env vars.
type testBroken struct {
Broke []interface{} `xml:"broke"`
}
Expand All @@ -90,8 +90,8 @@ func TestBrokenENV(t *testing.T) { //nolint:paralleltest
Broke map[string]interface{} `xml:"broke"`
}

os.Setenv("TEST_BROKE_0", "f00")
os.Setenv("TEST_BROKE_broke", "foo")
t.Setenv("TEST_BROKE_0", "f00")
t.Setenv("TEST_BROKE_broke", "foo")

a := assert.New(t)
c := &testBroken{}
Expand All @@ -113,7 +113,7 @@ func TestBrokenENV(t *testing.T) { //nolint:paralleltest
a.False(ok)
}

func TestUnmarshalENVerrors(t *testing.T) { //nolint:paralleltest
func TestUnmarshalENVerrors(t *testing.T) { //nolint:paralleltest // cannot parallel env vars.
a := assert.New(t)

type tester struct {
Expand All @@ -123,13 +123,13 @@ func TestUnmarshalENVerrors(t *testing.T) { //nolint:paralleltest
Error error `xml:"error"`
}

os.Setenv("YO_WORKS_foostring", "fooval")
os.Setenv("YO_WORKS_foo2string", "foo2val")
os.Setenv("YO_YUP_server99_0", "128")
os.Setenv("YO_YUP_server99_1", "129")
os.Setenv("YO_YUP_server99_2", "130")
os.Setenv("YO_YUP_server100_0", "256")
os.Setenv("YO_ERROR", "this is an error")
t.Setenv("YO_WORKS_foostring", "fooval")
t.Setenv("YO_WORKS_foo2string", "foo2val")
t.Setenv("YO_YUP_server99_0", "128")
t.Setenv("YO_YUP_server99_1", "129")
t.Setenv("YO_YUP_server99_2", "130")
t.Setenv("YO_YUP_server100_0", "256")
t.Setenv("YO_ERROR", "this is an error")

c := tester{}
ok, err := cnfg.UnmarshalENV(&c, "YO")
Expand All @@ -152,12 +152,12 @@ func TestUnmarshalENVerrors(t *testing.T) { //nolint:paralleltest
HasStuff []map[string]string `xml:"stuff"`
}

os.Setenv("MORE_BROKEN", "value")
os.Setenv("MORE_BROKEN_0_freesauce", "at-charlies")
os.Setenv("MORE_BROKEN2_0_freesoup", "at-daves")
os.Setenv("MORE_STUFF_0_freesoda", "not-at-pops")
os.Setenv("MORE_STUFF_0_freetime", "at-pops")
os.Setenv("MORE_STUFF_0_a", "")
t.Setenv("MORE_BROKEN", "value")
t.Setenv("MORE_BROKEN_0_freesauce", "at-charlies")
t.Setenv("MORE_BROKEN2_0_freesoup", "at-daves")
t.Setenv("MORE_STUFF_0_freesoda", "not-at-pops")
t.Setenv("MORE_STUFF_0_freetime", "at-pops")
t.Setenv("MORE_STUFF_0_a", "")

c2 := tester2{HasStuff: []map[string]string{{"freesoda": "at-pops"}, {"a": "v"}}}
ok, err = cnfg.UnmarshalENV(&c2, "MORE")
Expand All @@ -174,36 +174,35 @@ func TestUnmarshalENVerrors(t *testing.T) { //nolint:paralleltest
a.Nil(c2.NotBroken3, "a nil map without overrides must remain nil")
}

func TestUnmarshalENV(t *testing.T) {
// do not run this in parallel with other tests that change environment variables
t.Parallel()

// do not run this in parallel with other tests that change environment variables.
func TestUnmarshalENV(t *testing.T) { //nolint:paralleltest // cannot parallel env vars.
a := assert.New(t)
c := &testStruct{}
ok, err := cnfg.UnmarshalENV(c, "PRE")

a.Nil(err, "there must not be an error when parsing no variables")
a.False(ok, "there are no environment variables set, so ok should be false")
testThingENV(a)
testOscureENV(a)
testSpecialENV(a)
testThingENV(t, a)
testOscureENV(t, a)
testSpecialENV(t, a)

f := true
g := &f
_, err = cnfg.UnmarshalENV(g, "OOO")
a.NotNil(err, "unmarshaling a non-struct pointer must produce an error")
}

func testThingENV(a *assert.Assertions) {
func testThingENV(t *testing.T, a *assert.Assertions) {
t.Helper()
os.Clearenv()
os.Setenv("PRE_PSLICE_0_BOOL", "true")
os.Setenv("PRE_PSLICE_0_FLOAT", "123.4567")
t.Setenv("PRE_PSLICE_0_BOOL", "true")
t.Setenv("PRE_PSLICE_0_FLOAT", "123.4567")

os.Setenv("PRE_SSLICE_0_STRING", "foo")
os.Setenv("PRE_SSLICE_0_INT", "123")
t.Setenv("PRE_SSLICE_0_STRING", "foo")
t.Setenv("PRE_SSLICE_0_INT", "123")

os.Setenv("PRE_STRUCT_BOOL", "false")
os.Setenv("PRE_PSTRUCT_STRING", "foo2")
t.Setenv("PRE_STRUCT_BOOL", "false")
t.Setenv("PRE_PSTRUCT_STRING", "foo2")

c := &testStruct{}

Expand All @@ -216,7 +215,9 @@ func testThingENV(a *assert.Assertions) {
testUnmarshalFileValues(a, c, err, "testThingENV")
}

func testOscureENV(a *assert.Assertions) {
func testOscureENV(t *testing.T, a *assert.Assertions) {
t.Helper()

type testObscure struct {
FloatSlice []float32 `xml:"is"`
UintSliceP []*uint16 `xml:"uis"`
Expand All @@ -225,14 +226,14 @@ func testOscureENV(a *assert.Assertions) {
}

os.Clearenv()
os.Setenv("OB_IS_0", "-5")
os.Setenv("OB_IS_1", "8")
t.Setenv("OB_IS_0", "-5")
t.Setenv("OB_IS_1", "8")

os.Setenv("OB_UIS_0", "12")
os.Setenv("OB_UIS_1", "22")
t.Setenv("OB_UIS_0", "12")
t.Setenv("OB_UIS_1", "22")

os.Setenv("OB_PSI_0", "-1")
os.Setenv("OB_WUT_0_BOOL", "true")
t.Setenv("OB_PSI_0", "-1")
t.Setenv("OB_WUT_0_BOOL", "true")

c := &testObscure{}
testit := func() {
Expand Down Expand Up @@ -264,7 +265,9 @@ func testOscureENV(a *assert.Assertions) {
testit() // twice to make sure it's idempotent
}

func testSpecialENV(a *assert.Assertions) {
func testSpecialENV(t *testing.T, a *assert.Assertions) {
t.Helper()

type testSpecial struct {
Dur time.Duration `xml:"dur"`
CDur cnfg.Duration `xml:"cdur"`
Expand All @@ -277,11 +280,11 @@ func testSpecialENV(a *assert.Assertions) {
}

os.Clearenv()
os.Setenv("TEST_DUR", "1m")
os.Setenv("TEST_CDUR", "1s")
os.Setenv("TEST_TIME", "2019-12-18T00:35:49+08:00")
os.Setenv("TEST_SUB_URL", "https://golift.io/cnfg?rad=true")
os.Setenv("TEST_SUB_IP", "123.45.67.89")
t.Setenv("TEST_DUR", "1m")
t.Setenv("TEST_CDUR", "1s")
t.Setenv("TEST_TIME", "2019-12-18T00:35:49+08:00")
t.Setenv("TEST_SUB_URL", "https://golift.io/cnfg?rad=true")
t.Setenv("TEST_SUB_IP", "123.45.67.89")

c := &testSpecial{}
ok, err := (&cnfg.ENV{Pfx: "TEST"}).Unmarshal(c)
Expand All @@ -294,7 +297,7 @@ func testSpecialENV(a *assert.Assertions) {
a.Equal("123.45.67.89", c.Sub.IP.String(), "the IP wasn't parsed properly")
a.Nil(c.Durs)

os.Setenv("TEST_TIME", "not a real time")
t.Setenv("TEST_TIME", "not a real time")

c = &testSpecial{}
ok, err = (&cnfg.ENV{Pfx: "TEST"}).Unmarshal(c)
Expand Down
3 changes: 1 addition & 2 deletions parse_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cnfg //nolint:testpackage

import (
"os"
"reflect"
"testing"

Expand All @@ -28,7 +27,7 @@ func TestParseByteSlice(t *testing.T) { //nolint:paralleltest
F []byte `xml:"bytes,delenv"` //nolint:staticcheck
}

os.Setenv("D_BYTES", "byte slice incoming")
t.Setenv("D_BYTES", "byte slice incoming")

f := &test{}
ok, err := UnmarshalENV(f, "D")
Expand Down

0 comments on commit 10d53f1

Please sign in to comment.