1
0
Fork 0
mirror of https://github.com/Luzifer/rconfig.git synced 2024-11-09 16:30:04 +00:00

Port tests to pure-Go-tests

to remove dependencies to ginkgo and rconfig/v1

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-09-06 18:43:24 +02:00
parent cd793303df
commit 37f53184c3
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
15 changed files with 393 additions and 634 deletions

View file

@ -1,70 +1,37 @@
package rconfig package rconfig
import ( import (
"os" "testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
) )
var _ = Describe("Testing bool parsing", func() { func TestBoolParsing(t *testing.T) {
type t struct {
Test1 bool `default:"true"`
Test2 bool `default:"false" flag:"test2"`
Test3 bool `default:"true" flag:"test3,t"`
Test4 bool `flag:"test4"`
}
var ( var (
err error
args []string
cfg t
)
BeforeEach(func() {
cfg = t{}
args = []string{ args = []string{
"--test2", "--test2",
"-t", "-t",
} }
}) cfg struct {
Test1 bool `default:"true"`
JustBeforeEach(func() { Test2 bool `default:"false" flag:"test2"`
err = parse(&cfg, args) Test3 bool `default:"true" flag:"test3,t"`
}) Test4 bool `flag:"test4"`
TestEnvDefault bool `default:"true" env:"AN_ENV_VARIABLE_HOPEFULLY_NEVER_SET_DSFGDF"`
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
It("should have the expected values", func() {
Expect(cfg.Test1).To(Equal(true))
Expect(cfg.Test2).To(Equal(true))
Expect(cfg.Test3).To(Equal(true))
Expect(cfg.Test4).To(Equal(false))
})
})
var _ = Describe("Testing to set bool from ENV with default", func() {
type t struct {
Test1 bool `default:"true" env:"TEST1"`
} }
var (
err error
args []string
cfg t
) )
BeforeEach(func() { if err := parse(&cfg, args); err != nil {
cfg = t{} t.Fatalf("Parsing options caused error: %s", err)
args = []string{} }
})
JustBeforeEach(func() { for _, test := range [][2]interface{}{
os.Unsetenv("TEST1") {cfg.Test1, true},
err = parse(&cfg, args) {cfg.Test2, true},
}) {cfg.Test3, true},
{cfg.Test4, false},
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) {cfg.TestEnvDefault, true},
It("should have the expected values", func() { } {
Expect(cfg.Test1).To(Equal(true)) if test[0] != test[1] {
}) t.Errorf("Expected value does not match: %#v != %#v", test[0], test[1])
}) }
}
}

View file

@ -1,41 +1,34 @@
package rconfig package rconfig
import ( import (
"testing"
"time" "time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
) )
var _ = Describe("Duration", func() { func TestDurationParsing(t *testing.T) {
type t struct { var (
args = []string{
"--duration=23s", "-o", "45m",
}
cfg struct {
Test time.Duration `flag:"duration"` Test time.Duration `flag:"duration"`
TestS time.Duration `flag:"other-duration,o"` TestS time.Duration `flag:"other-duration,o"`
TestDef time.Duration `default:"30h"` TestDef time.Duration `default:"30h"`
} }
var (
err error
args []string
cfg t
) )
BeforeEach(func() { if err := parse(&cfg, args); err != nil {
cfg = t{} t.Fatalf("Parsing options caused error: %s", err)
args = []string{
"--duration=23s", "-o", "45m",
} }
})
JustBeforeEach(func() { for _, test := range [][2]interface{}{
err = parse(&cfg, args) {cfg.Test, 23 * time.Second},
}) {cfg.TestS, 45 * time.Minute},
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) {cfg.TestDef, 30 * time.Hour},
It("should have the expected values", func() { } {
Expect(cfg.Test).To(Equal(23 * time.Second)) if test[0] != test[1] {
Expect(cfg.TestS).To(Equal(45 * time.Minute)) t.Errorf("Expected value does not match: %#v != %#v", test[0], test[1])
}
Expect(cfg.TestDef).To(Equal(30 * time.Hour)) }
}) }
})

View file

@ -1,56 +1,41 @@
package rconfig package rconfig
import ( import (
. "github.com/onsi/ginkgo" "testing"
. "github.com/onsi/gomega"
) )
var _ = Describe("Testing errors", func() { func TestErrors(t *testing.T) {
for test, parsable := range map[string]interface{}{
It("should not accept string as int", func() { "use string as default to int": struct {
Expect(parse(&struct {
A int `default:"a"` A int `default:"a"`
}{}, []string{})).To(HaveOccurred()) }{},
}) "use string as default to float": struct {
It("should not accept string as float", func() {
Expect(parse(&struct {
A float32 `default:"a"` A float32 `default:"a"`
}{}, []string{})).To(HaveOccurred()) }{},
}) "use string as default to uint": struct {
It("should not accept string as uint", func() {
Expect(parse(&struct {
A uint `default:"a"` A uint `default:"a"`
}{}, []string{})).To(HaveOccurred()) }{},
}) "use string as default to uint in sub-struct": struct {
It("should not accept string as uint in sub-struct", func() {
Expect(parse(&struct {
B struct { B struct {
A uint `default:"a"` A uint `default:"a"`
} }
}{}, []string{})).To(HaveOccurred()) }{},
}) "use string list as default to int slice": struct {
A []int `default:"a,b"`
}{},
} {
if err := parse(&parsable, nil); err == nil {
t.Errorf("Expected error but got none. Test: %s", test)
}
}
It("should not accept string slice as int slice", func() { if err := parse(struct {
Expect(parse(&struct {
A []int `default:"a,bn"`
}{}, []string{})).To(HaveOccurred())
})
It("should not accept variables not being pointers", func() {
cfg := struct {
A string `default:"a"` A string `default:"a"`
}{} }{}, nil); err == nil {
t.Errorf("Expected error when feeding non-pointer struct to parse")
}
Expect(parse(cfg, []string{})).To(HaveOccurred()) if err := parse("test", nil); err == nil {
}) t.Errorf("Expected error when feeding non-pointer string to parse")
}
It("should not accept variables not being pointers to structs", func() { }
cfg := "test"
Expect(parse(cfg, []string{})).To(HaveOccurred())
})
})

View file

@ -1,44 +1,38 @@
package rconfig package rconfig
import ( import (
. "github.com/onsi/ginkgo" "testing"
. "github.com/onsi/gomega"
) )
var _ = Describe("Testing float parsing", func() { func TestFloatParsing(t *testing.T) {
type t struct { var (
args = []string{
"--float32=5.5", "-3", "6.6",
"--float64=7.7", "-6", "8.8",
}
cfg struct {
Test32 float32 `flag:"float32"` Test32 float32 `flag:"float32"`
Test32P float32 `flag:"float32p,3"` Test32P float32 `flag:"float32p,3"`
Test64 float64 `flag:"float64"` Test64 float64 `flag:"float64"`
Test64P float64 `flag:"float64p,6"` Test64P float64 `flag:"float64p,6"`
TestDef float32 `default:"66.256"` TestDef float32 `default:"66.256"`
} }
var (
err error
args []string
cfg t
) )
BeforeEach(func() { if err := parse(&cfg, args); err != nil {
cfg = t{} t.Fatalf("Parsing options caused error: %s", err)
args = []string{
"--float32=5.5", "-3", "6.6",
"--float64=7.7", "-6", "8.8",
} }
})
JustBeforeEach(func() { for _, test := range [][2]interface{}{
err = parse(&cfg, args) {cfg.Test32, float32(5.5)},
}) {cfg.Test32P, float32(6.6)},
{cfg.Test64, float64(7.7)},
{cfg.Test64P, float64(8.8)},
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) {cfg.TestDef, float32(66.256)},
It("should have the expected values", func() { } {
Expect(cfg.Test32).To(Equal(float32(5.5))) if test[0] != test[1] {
Expect(cfg.Test32P).To(Equal(float32(6.6))) t.Errorf("Expected value does not match: %#v != %#v", test[0], test[1])
Expect(cfg.Test64).To(Equal(float64(7.7))) }
Expect(cfg.Test64P).To(Equal(float64(8.8))) }
}
Expect(cfg.TestDef).To(Equal(float32(66.256)))
})
})

View file

@ -2,127 +2,90 @@ package rconfig
import ( import (
"os" "os"
"reflect"
. "github.com/onsi/ginkgo" "testing"
. "github.com/onsi/gomega"
) )
var _ = Describe("Testing general parsing", func() { func TestGeneralExecution(t *testing.T) {
type t struct { type test struct {
Test string `default:"foo" env:"shell" flag:"shell" description:"Test"` Test string `default:"foo" env:"shell" flag:"shell" description:"Test"`
Test2 string `default:"blub" env:"testvar" flag:"testvar,t" description:"Test"` Test2 string `default:"blub" env:"testvar" flag:"testvar,t" description:"Test"`
DefaultFlag string `default:"goo"` DefaultFlag string `default:"goo"`
SadFlag string SadFlag string
} }
type tValidated struct {
Test string `flag:"test" default:"" validate:"nonzero"`
}
var ( var (
err error
args []string args []string
cfg t cfg test
) )
Context("with defined arguments", func() { exec := func(desc string, tests [][2]interface{}) {
BeforeEach(func() { if err := parse(&cfg, args); err != nil {
cfg = t{} t.Fatalf("Parsing options caused error: %s", err)
}
for _, test := range tests {
if !reflect.DeepEqual(reflect.ValueOf(test[0]).Elem().Interface(), test[1]) {
t.Errorf("%q expected value does not match: %#v != %#v", desc, test[0], test[1])
}
}
}
cfg = test{}
args = []string{ args = []string{
"--shell=test23", "--shell=test23",
"-t", "bla", "-t", "bla",
} }
exec("defined arguments", [][2]interface{}{
{&cfg.Test, "test23"},
{&cfg.Test2, "bla"},
{&cfg.SadFlag, ""},
{&cfg.DefaultFlag, "goo"},
}) })
JustBeforeEach(func() { cfg = test{}
err = parse(&cfg, args)
})
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
It("should have parsed the expected values", func() {
Expect(cfg.Test).To(Equal("test23"))
Expect(cfg.Test2).To(Equal("bla"))
Expect(cfg.SadFlag).To(Equal(""))
Expect(cfg.DefaultFlag).To(Equal("goo"))
})
})
Context("with no arguments", func() {
BeforeEach(func() {
cfg = t{}
args = []string{} args = []string{}
exec("no arguments", [][2]interface{}{
{&cfg.Test, "foo"},
}) })
JustBeforeEach(func() { cfg = test{}
err = parse(&cfg, args)
})
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
It("should have used the default value", func() {
Expect(cfg.Test).To(Equal("foo"))
})
})
Context("with no arguments and set env", func() {
BeforeEach(func() {
cfg = t{}
args = []string{} args = []string{}
os.Setenv("shell", "test546") os.Setenv("shell", "test546")
exec("no arguments and set env", [][2]interface{}{
{&cfg.Test, "test546"},
}) })
AfterEach(func() {
os.Unsetenv("shell") os.Unsetenv("shell")
})
JustBeforeEach(func() { cfg = test{}
err = parse(&cfg, args)
})
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
It("should have used the value from env", func() {
Expect(cfg.Test).To(Equal("test546"))
})
})
Context("with additional arguments", func() {
BeforeEach(func() {
cfg = t{}
args = []string{ args = []string{
"--shell=test23", "--shell=test23",
"-t", "bla", "-t", "bla",
"positional1", "positional2", "positional1", "positional2",
} }
exec("additional arguments", [][2]interface{}{
{&cfg.Test, "test23"},
{&cfg.Test2, "bla"},
{&cfg.SadFlag, ""},
{&cfg.DefaultFlag, "goo"},
}) })
JustBeforeEach(func() { if !reflect.DeepEqual(Args(), []string{"positional1", "positional2"}) {
err = parse(&cfg, args) t.Errorf("expected positional arguments to match")
}) }
}
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) func TestValidationIntegration(t *testing.T) {
It("should have parsed the expected values", func() { type tValidated struct {
Expect(cfg.Test).To(Equal("test23")) Test string `flag:"test" default:"" validate:"nonzero"`
Expect(cfg.Test2).To(Equal("bla")) }
Expect(cfg.SadFlag).To(Equal(""))
Expect(cfg.DefaultFlag).To(Equal("goo"))
})
It("should have detected the positional arguments", func() {
Expect(Args()).To(Equal([]string{"positional1", "positional2"}))
})
})
Context("making use of the validator package", func() { var (
var cfgValidated tValidated
BeforeEach(func() {
cfgValidated = tValidated{} cfgValidated = tValidated{}
args = []string{} args = []string{}
}) )
JustBeforeEach(func() { if err := parseAndValidate(&cfgValidated, args); err == nil {
err = parseAndValidate(&cfgValidated, args) t.Errorf("Expected error, got none")
}) }
}
It("should have errored", func() { Expect(err).To(HaveOccurred()) })
})
})

2
go.mod
View file

@ -1,6 +1,6 @@
module github.com/Luzifer/rconfig/v2 module github.com/Luzifer/rconfig/v2
go 1.16 go 1.17
require ( require (
github.com/spf13/pflag v1.0.5 github.com/spf13/pflag v1.0.5

7
go.sum
View file

@ -1,13 +1,8 @@
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/validator.v2 v2.0.0-20180514200540-135c24b11c19 h1:WB265cn5OpO+hK3pikC9hpP1zI/KTwmyMFKloW9eOVc=
gopkg.in/validator.v2 v2.0.0-20180514200540-135c24b11c19/go.mod h1:o4V0GXN9/CAmCsvJ0oXYZvrZOe7syiDZSN1GWGZTGzc=
gopkg.in/validator.v2 v2.0.0-20210331031555-b37d688a7fb0 h1:EFLtLCwd8tGN+r/ePz3cvRtdsfYNhDEdt/vp6qsT+0A= gopkg.in/validator.v2 v2.0.0-20210331031555-b37d688a7fb0 h1:EFLtLCwd8tGN+r/ePz3cvRtdsfYNhDEdt/vp6qsT+0A=
gopkg.in/validator.v2 v2.0.0-20210331031555-b37d688a7fb0/go.mod h1:o4V0GXN9/CAmCsvJ0oXYZvrZOe7syiDZSN1GWGZTGzc= gopkg.in/validator.v2 v2.0.0-20210331031555-b37d688a7fb0/go.mod h1:o4V0GXN9/CAmCsvJ0oXYZvrZOe7syiDZSN1GWGZTGzc=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

View file

@ -1,12 +1,18 @@
package rconfig package rconfig
import ( import (
. "github.com/onsi/ginkgo" "testing"
. "github.com/onsi/gomega"
) )
var _ = Describe("Testing int parsing", func() { func TestIntParsing(t *testing.T) {
type t struct { var (
args = []string{
"--int=1", "-i", "2",
"--int8=3", "-8", "4",
"--int32=5", "-3", "6",
"--int64=7", "-6", "8",
}
cfg struct {
Test int `flag:"int"` Test int `flag:"int"`
TestP int `flag:"intp,i"` TestP int `flag:"intp,i"`
Test8 int8 `flag:"int8"` Test8 int8 `flag:"int8"`
@ -17,38 +23,25 @@ var _ = Describe("Testing int parsing", func() {
Test64P int64 `flag:"int64p,6"` Test64P int64 `flag:"int64p,6"`
TestDef int8 `default:"66"` TestDef int8 `default:"66"`
} }
var (
err error
args []string
cfg t
) )
BeforeEach(func() { if err := parse(&cfg, args); err != nil {
cfg = t{} t.Fatalf("Parsing options caused error: %s", err)
args = []string{
"--int=1", "-i", "2",
"--int8=3", "-8", "4",
"--int32=5", "-3", "6",
"--int64=7", "-6", "8",
} }
})
JustBeforeEach(func() { for _, test := range [][2]interface{}{
err = parse(&cfg, args) {cfg.Test, int(1)},
}) {cfg.TestP, 2},
{cfg.Test8, int8(3)},
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) {cfg.Test8P, int8(4)},
It("should have the expected values", func() { {cfg.Test32, int32(5)},
Expect(cfg.Test).To(Equal(1)) {cfg.Test32P, int32(6)},
Expect(cfg.TestP).To(Equal(2)) {cfg.Test64, int64(7)},
Expect(cfg.Test8).To(Equal(int8(3))) {cfg.Test64P, int64(8)},
Expect(cfg.Test8P).To(Equal(int8(4))) {cfg.TestDef, int8(66)},
Expect(cfg.Test32).To(Equal(int32(5))) } {
Expect(cfg.Test32P).To(Equal(int32(6))) if test[0] != test[1] {
Expect(cfg.Test64).To(Equal(int64(7))) t.Errorf("Expected value does not match: %#v != %#v", test[0], test[1])
Expect(cfg.Test64P).To(Equal(int64(8))) }
}
Expect(cfg.TestDef).To(Equal(int8(66))) }
})
})

View file

@ -1,40 +0,0 @@
package rconfig_test
import (
"os"
. "github.com/Luzifer/rconfig"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Testing os.Args", func() {
type t struct {
A string `default:"a" flag:"a"`
}
var (
err error
cfg t
)
JustBeforeEach(func() {
err = Parse(&cfg)
})
Context("With only valid arguments", func() {
BeforeEach(func() {
cfg = t{}
os.Args = []string{"--a=bar"}
})
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
It("should have the expected values", func() {
Expect(cfg.A).To(Equal("bar"))
})
})
})

View file

@ -2,86 +2,66 @@ package rconfig
import ( import (
"os" "os"
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
) )
var _ = Describe("Precedence", func() { func TestPrecedence(t *testing.T) {
type testcfg struct {
type t struct {
A int `default:"1" vardefault:"a" env:"a" flag:"avar,a" description:"a"` A int `default:"1" vardefault:"a" env:"a" flag:"avar,a" description:"a"`
} }
var ( var (
err error err error
cfg t cfg testcfg
args []string args []string
vardefaults map[string]string vardefaults map[string]string
) )
JustBeforeEach(func() { exec := func(desc string, fn func() interface{}, exp interface{}) {
cfg = t{} cfg = testcfg{}
SetVariableDefaults(vardefaults) SetVariableDefaults(vardefaults)
err = parse(&cfg, args) err = parse(&cfg, args)
})
Context("Provided: Flag, Env, Default, VarDefault", func() { if err != nil {
BeforeEach(func() { t.Errorf("%q parsing caused error: %s", desc, err)
}
if res := fn(); res != exp {
t.Errorf("%q expected value does not match: %#v != %#v", desc, res, exp)
}
}
// Provided: Flag, Env, Default, VarDefault
args = []string{"-a", "5"} args = []string{"-a", "5"}
os.Setenv("a", "8") os.Setenv("a", "8")
vardefaults = map[string]string{ vardefaults = map[string]string{
"a": "3", "a": "3",
} }
})
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) exec("Provided: Flag, Env, Default, VarDefault", func() interface{} { return cfg.A }, 5)
It("should have used the flag value", func() {
Expect(cfg.A).To(Equal(5))
})
})
Context("Provided: Env, Default, VarDefault", func() { // Provided: Env, Default, VarDefault
BeforeEach(func() {
args = []string{} args = []string{}
os.Setenv("a", "8") os.Setenv("a", "8")
vardefaults = map[string]string{ vardefaults = map[string]string{
"a": "3", "a": "3",
} }
})
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) exec("Provided: Env, Default, VarDefault", func() interface{} { return cfg.A }, 8)
It("should have used the env value", func() {
Expect(cfg.A).To(Equal(8))
})
})
Context("Provided: Default, VarDefault", func() { // Provided: Default, VarDefault
BeforeEach(func() {
args = []string{} args = []string{}
os.Unsetenv("a") os.Unsetenv("a")
vardefaults = map[string]string{ vardefaults = map[string]string{
"a": "3", "a": "3",
} }
})
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) exec("Provided: Default, VarDefault", func() interface{} { return cfg.A }, 3)
It("should have used the vardefault value", func() {
Expect(cfg.A).To(Equal(3))
})
})
Context("Provided: Default", func() { // Provided: Default
BeforeEach(func() {
args = []string{} args = []string{}
os.Unsetenv("a") os.Unsetenv("a")
vardefaults = map[string]string{} vardefaults = map[string]string{}
})
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) exec("Provided: Default", func() interface{} { return cfg.A }, 1)
It("should have used the default value", func() { }
Expect(cfg.A).To(Equal(1))
})
})
})

View file

@ -1,13 +0,0 @@
package rconfig_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)
func TestRconfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Rconfig Suite")
}

View file

@ -1,55 +1,45 @@
package rconfig package rconfig
import ( import (
. "github.com/onsi/ginkgo" "reflect"
. "github.com/onsi/gomega" "testing"
) )
var _ = Describe("Testing slices", func() { func TestSliceParsing(t *testing.T) {
type t struct { var (
args = []string{
"--int=4,5", "-s", "hallo,welt",
}
cfg struct {
Int []int `default:"1,2,3" flag:"int"` Int []int `default:"1,2,3" flag:"int"`
String []string `default:"a,b,c" flag:"string"` String []string `default:"a,b,c" flag:"string"`
IntP []int `default:"1,2,3" flag:"intp,i"` IntP []int `default:"1,2,3" flag:"intp,i"`
StringP []string `default:"a,b,c" flag:"stringp,s"` StringP []string `default:"a,b,c" flag:"stringp,s"`
EmptyString []string `default:""` EmptyString []string `default:""`
} }
var (
err error
args []string
cfg t
) )
BeforeEach(func() { if err := parse(&cfg, args); err != nil {
cfg = t{} t.Fatalf("Parsing options caused error: %s", err)
args = []string{
"--int=4,5", "-s", "hallo,welt",
} }
})
JustBeforeEach(func() { for _, test := range [][2]interface{}{
err = parse(&cfg, args) {len(cfg.Int), 2},
}) {cfg.Int, []int{4, 5}},
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) {len(cfg.IntP), 3},
It("should have the expected values for int-slice", func() { {cfg.IntP, []int{1, 2, 3}},
Expect(len(cfg.Int)).To(Equal(2))
Expect(cfg.Int).To(Equal([]int{4, 5})) {len(cfg.String), 3},
Expect(cfg.Int).NotTo(Equal([]int{5, 4})) {cfg.String, []string{"a", "b", "c"}},
})
It("should have the expected values for int-shorthand-slice", func() { {len(cfg.StringP), 2},
Expect(len(cfg.IntP)).To(Equal(3)) {cfg.StringP, []string{"hallo", "welt"}},
Expect(cfg.IntP).To(Equal([]int{1, 2, 3}))
}) {len(cfg.EmptyString), 0},
It("should have the expected values for string-slice", func() { } {
Expect(len(cfg.String)).To(Equal(3)) if !reflect.DeepEqual(test[0], test[1]) {
Expect(cfg.String).To(Equal([]string{"a", "b", "c"})) t.Errorf("Expected value does not match: %#v != %#v", test[0], test[1])
}) }
It("should have the expected values for string-shorthand-slice", func() { }
Expect(len(cfg.StringP)).To(Equal(2)) }
Expect(cfg.StringP).To(Equal([]string{"hallo", "welt"}))
})
It("should have no elements for an empty default string", func() {
Expect(len(cfg.EmptyString)).To(Equal(0))
})
})

View file

@ -1,36 +1,30 @@
package rconfig package rconfig
import ( import (
. "github.com/onsi/ginkgo" "testing"
. "github.com/onsi/gomega"
) )
var _ = Describe("Testing sub-structs", func() { func TestSubStructParsing(t *testing.T) {
type t struct { var (
args = []string{}
cfg struct {
Test string `default:"blubb"` Test string `default:"blubb"`
Sub struct { Sub struct {
Test string `default:"Hallo"` Test string `default:"Hallo"`
} }
} }
var (
err error
args []string
cfg t
) )
BeforeEach(func() { if err := parse(&cfg, args); err != nil {
cfg = t{} t.Fatalf("Parsing options caused error: %s", err)
args = []string{} }
})
JustBeforeEach(func() { for _, test := range [][2]interface{}{
err = parse(&cfg, args) {cfg.Test, "blubb"},
}) {cfg.Sub.Test, "Hallo"},
} {
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) if test[0] != test[1] {
It("should have the expected values", func() { t.Errorf("Expected value does not match: %#v != %#v", test[0], test[1])
Expect(cfg.Test).To(Equal("blubb")) }
Expect(cfg.Sub.Test).To(Equal("Hallo")) }
}) }
})

View file

@ -1,12 +1,19 @@
package rconfig package rconfig
import ( import (
. "github.com/onsi/ginkgo" "testing"
. "github.com/onsi/gomega"
) )
var _ = Describe("Testing uint parsing", func() { func TestUintParsing(t *testing.T) {
type t struct { var (
args = []string{
"--int=1", "-i", "2",
"--int8=3", "-8", "4",
"--int32=5", "-3", "6",
"--int64=7", "-6", "8",
"--int16=9", "-1", "10",
}
cfg struct {
Test uint `flag:"int"` Test uint `flag:"int"`
TestP uint `flag:"intp,i"` TestP uint `flag:"intp,i"`
Test8 uint8 `flag:"int8"` Test8 uint8 `flag:"int8"`
@ -19,41 +26,28 @@ var _ = Describe("Testing uint parsing", func() {
Test64P uint64 `flag:"int64p,6"` Test64P uint64 `flag:"int64p,6"`
TestDef uint8 `default:"66"` TestDef uint8 `default:"66"`
} }
var (
err error
args []string
cfg t
) )
BeforeEach(func() { if err := parse(&cfg, args); err != nil {
cfg = t{} t.Fatalf("Parsing options caused error: %s", err)
args = []string{
"--int=1", "-i", "2",
"--int8=3", "-8", "4",
"--int32=5", "-3", "6",
"--int64=7", "-6", "8",
"--int16=9", "-1", "10",
} }
})
JustBeforeEach(func() { for _, test := range [][2]interface{}{
err = parse(&cfg, args) {cfg.Test, uint(1)},
}) {cfg.TestP, uint(2)},
{cfg.Test8, uint8(3)},
{cfg.Test8P, uint8(4)},
{cfg.Test32, uint32(5)},
{cfg.Test32P, uint32(6)},
{cfg.Test64, uint64(7)},
{cfg.Test64P, uint64(8)},
{cfg.Test16, uint16(9)},
{cfg.Test16P, uint16(10)},
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) {cfg.TestDef, uint8(66)},
It("should have the expected values", func() { } {
Expect(cfg.Test).To(Equal(uint(1))) if test[0] != test[1] {
Expect(cfg.TestP).To(Equal(uint(2))) t.Errorf("Expected value does not match: %#v != %#v", test[0], test[1])
Expect(cfg.Test8).To(Equal(uint8(3))) }
Expect(cfg.Test8P).To(Equal(uint8(4))) }
Expect(cfg.Test32).To(Equal(uint32(5))) }
Expect(cfg.Test32P).To(Equal(uint32(6)))
Expect(cfg.Test64).To(Equal(uint64(7)))
Expect(cfg.Test64P).To(Equal(uint64(8)))
Expect(cfg.Test16).To(Equal(uint16(9)))
Expect(cfg.Test16P).To(Equal(uint16(10)))
Expect(cfg.TestDef).To(Equal(uint8(66)))
})
})

View file

@ -3,14 +3,12 @@ package rconfig
import ( import (
"io/ioutil" "io/ioutil"
"os" "os"
"reflect"
. "github.com/onsi/ginkgo" "testing"
. "github.com/onsi/gomega"
) )
var _ = Describe("Testing variable defaults", func() { func TestVardefaultParsing(t *testing.T) {
type test struct {
type t struct {
MySecretValue string `default:"secret" env:"foo" vardefault:"my_secret_value"` MySecretValue string `default:"secret" env:"foo" vardefault:"my_secret_value"`
MyUsername string `default:"luzifer" vardefault:"username"` MyUsername string `default:"luzifer" vardefault:"username"`
SomeVar string `flag:"var" description:"some variable"` SomeVar string `flag:"var" description:"some variable"`
@ -18,8 +16,7 @@ var _ = Describe("Testing variable defaults", func() {
} }
var ( var (
err error cfg test
cfg t
args = []string{} args = []string{}
vardefaults = map[string]string{ vardefaults = map[string]string{
"my_secret_value": "veryverysecretkey", "my_secret_value": "veryverysecretkey",
@ -28,95 +25,62 @@ var _ = Describe("Testing variable defaults", func() {
} }
) )
BeforeEach(func() { exec := func(desc string, tests [][2]interface{}) {
cfg = t{} if err := parse(&cfg, args); err != nil {
}) t.Fatalf("Parsing options caused error: %s", err)
}
JustBeforeEach(func() { for _, test := range tests {
err = parse(&cfg, args) if !reflect.DeepEqual(reflect.ValueOf(test[0]).Elem().Interface(), test[1]) {
}) t.Errorf("%q expected value does not match: %#v != %#v", desc, test[0], test[1])
}
}
}
Context("With manually provided variables", func() {
BeforeEach(func() {
SetVariableDefaults(vardefaults) SetVariableDefaults(vardefaults)
exec("manually provided variables", [][2]interface{}{
{&cfg.IntVar, int64(42)},
{&cfg.MySecretValue, "veryverysecretkey"},
{&cfg.MyUsername, "luzifer"},
{&cfg.SomeVar, ""},
}) })
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) SetVariableDefaults(VarDefaultsFromYAML([]byte("---\nmy_secret_value: veryverysecretkey\nunknownkey: hi there\nint_var: 42\n")))
It("should have the expected values", func() { exec("defaults from YAML data", [][2]interface{}{
Expect(cfg.IntVar).To(Equal(int64(42))) {&cfg.IntVar, int64(42)},
Expect(cfg.MySecretValue).To(Equal("veryverysecretkey")) {&cfg.MySecretValue, "veryverysecretkey"},
Expect(cfg.MyUsername).To(Equal("luzifer")) {&cfg.MyUsername, "luzifer"},
Expect(cfg.SomeVar).To(Equal("")) {&cfg.SomeVar, ""},
})
}) })
Context("With defaults from YAML data", func() { tmp, _ := ioutil.TempFile("", "")
BeforeEach(func() { defer func() {
yamlData := []byte("---\nmy_secret_value: veryverysecretkey\nunknownkey: hi there\nint_var: 42\n") tmp.Close()
SetVariableDefaults(VarDefaultsFromYAML(yamlData)) os.Remove(tmp.Name())
}) }()
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
It("should have the expected values", func() {
Expect(cfg.IntVar).To(Equal(int64(42)))
Expect(cfg.MySecretValue).To(Equal("veryverysecretkey"))
Expect(cfg.MyUsername).To(Equal("luzifer"))
Expect(cfg.SomeVar).To(Equal(""))
})
})
Context("With defaults from YAML file", func() {
var tmp *os.File
BeforeEach(func() {
tmp, _ = ioutil.TempFile("", "")
yamlData := "---\nmy_secret_value: veryverysecretkey\nunknownkey: hi there\nint_var: 42\n" yamlData := "---\nmy_secret_value: veryverysecretkey\nunknownkey: hi there\nint_var: 42\n"
tmp.WriteString(yamlData) tmp.WriteString(yamlData)
SetVariableDefaults(VarDefaultsFromYAMLFile(tmp.Name())) SetVariableDefaults(VarDefaultsFromYAMLFile(tmp.Name()))
exec("defaults from YAML file", [][2]interface{}{
{&cfg.IntVar, int64(42)},
{&cfg.MySecretValue, "veryverysecretkey"},
{&cfg.MyUsername, "luzifer"},
{&cfg.SomeVar, ""},
}) })
AfterEach(func() { SetVariableDefaults(VarDefaultsFromYAML([]byte("---\nmy_secret_value = veryverysecretkey\nunknownkey = hi there\nint_var = 42\n")))
tmp.Close() exec("defaults from invalid YAML data", [][2]interface{}{
os.Remove(tmp.Name()) {&cfg.IntVar, int64(23)},
}) {&cfg.MySecretValue, "secret"},
{&cfg.MyUsername, "luzifer"},
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) }) {&cfg.SomeVar, ""},
It("should have the expected values", func() {
Expect(cfg.IntVar).To(Equal(int64(42)))
Expect(cfg.MySecretValue).To(Equal("veryverysecretkey"))
Expect(cfg.MyUsername).To(Equal("luzifer"))
Expect(cfg.SomeVar).To(Equal(""))
})
})
Context("With defaults from invalid YAML data", func() {
BeforeEach(func() {
yamlData := []byte("---\nmy_secret_value = veryverysecretkey\nunknownkey = hi there\nint_var = 42\n")
SetVariableDefaults(VarDefaultsFromYAML(yamlData))
})
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
It("should have the expected values", func() {
Expect(cfg.IntVar).To(Equal(int64(23)))
Expect(cfg.MySecretValue).To(Equal("secret"))
Expect(cfg.MyUsername).To(Equal("luzifer"))
Expect(cfg.SomeVar).To(Equal(""))
})
})
Context("With defaults from non existent YAML file", func() {
BeforeEach(func() {
file := "/tmp/this_file_should_not_exist_146e26723r"
SetVariableDefaults(VarDefaultsFromYAMLFile(file))
})
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
It("should have the expected values", func() {
Expect(cfg.IntVar).To(Equal(int64(23)))
Expect(cfg.MySecretValue).To(Equal("secret"))
Expect(cfg.MyUsername).To(Equal("luzifer"))
Expect(cfg.SomeVar).To(Equal(""))
})
}) })
SetVariableDefaults(VarDefaultsFromYAMLFile("/tmp/this_file_should_not_exist_146e26723r"))
exec("defaults from non-existing YAML file", [][2]interface{}{
{&cfg.IntVar, int64(23)},
{&cfg.MySecretValue, "secret"},
{&cfg.MyUsername, "luzifer"},
{&cfg.SomeVar, ""},
}) })
}