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:
parent
cd793303df
commit
37f53184c3
15 changed files with 393 additions and 634 deletions
79
bool_test.go
79
bool_test.go
|
@ -1,70 +1,37 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var _ = Describe("Testing bool parsing", func() {
|
||||
type t struct {
|
||||
Test1 bool `default:"true"`
|
||||
Test2 bool `default:"false" flag:"test2"`
|
||||
Test3 bool `default:"true" flag:"test3,t"`
|
||||
Test4 bool `flag:"test4"`
|
||||
}
|
||||
|
||||
func TestBoolParsing(t *testing.T) {
|
||||
var (
|
||||
err error
|
||||
args []string
|
||||
cfg t
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
args = []string{
|
||||
"--test2",
|
||||
"-t",
|
||||
}
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
|
||||
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"`
|
||||
cfg struct {
|
||||
Test1 bool `default:"true"`
|
||||
Test2 bool `default:"false" flag:"test2"`
|
||||
Test3 bool `default:"true" flag:"test3,t"`
|
||||
Test4 bool `flag:"test4"`
|
||||
TestEnvDefault bool `default:"true" env:"AN_ENV_VARIABLE_HOPEFULLY_NEVER_SET_DSFGDF"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
args []string
|
||||
cfg t
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
args = []string{}
|
||||
})
|
||||
if err := parse(&cfg, args); err != nil {
|
||||
t.Fatalf("Parsing options caused error: %s", err)
|
||||
}
|
||||
|
||||
JustBeforeEach(func() {
|
||||
os.Unsetenv("TEST1")
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have the expected values", func() {
|
||||
Expect(cfg.Test1).To(Equal(true))
|
||||
})
|
||||
})
|
||||
for _, test := range [][2]interface{}{
|
||||
{cfg.Test1, true},
|
||||
{cfg.Test2, true},
|
||||
{cfg.Test3, true},
|
||||
{cfg.Test4, false},
|
||||
{cfg.TestEnvDefault, true},
|
||||
} {
|
||||
if test[0] != test[1] {
|
||||
t.Errorf("Expected value does not match: %#v != %#v", test[0], test[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,41 +1,34 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Duration", func() {
|
||||
type t struct {
|
||||
func TestDurationParsing(t *testing.T) {
|
||||
var (
|
||||
args = []string{
|
||||
"--duration=23s", "-o", "45m",
|
||||
}
|
||||
cfg struct {
|
||||
Test time.Duration `flag:"duration"`
|
||||
TestS time.Duration `flag:"other-duration,o"`
|
||||
TestDef time.Duration `default:"30h"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
args []string
|
||||
cfg t
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
args = []string{
|
||||
"--duration=23s", "-o", "45m",
|
||||
if err := parse(&cfg, args); err != nil {
|
||||
t.Fatalf("Parsing options caused error: %s", err)
|
||||
}
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
for _, test := range [][2]interface{}{
|
||||
{cfg.Test, 23 * time.Second},
|
||||
{cfg.TestS, 45 * time.Minute},
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have the expected values", func() {
|
||||
Expect(cfg.Test).To(Equal(23 * time.Second))
|
||||
Expect(cfg.TestS).To(Equal(45 * time.Minute))
|
||||
|
||||
Expect(cfg.TestDef).To(Equal(30 * time.Hour))
|
||||
})
|
||||
})
|
||||
{cfg.TestDef, 30 * time.Hour},
|
||||
} {
|
||||
if test[0] != test[1] {
|
||||
t.Errorf("Expected value does not match: %#v != %#v", test[0], test[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,56 +1,41 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var _ = Describe("Testing errors", func() {
|
||||
|
||||
It("should not accept string as int", func() {
|
||||
Expect(parse(&struct {
|
||||
func TestErrors(t *testing.T) {
|
||||
for test, parsable := range map[string]interface{}{
|
||||
"use string as default to int": struct {
|
||||
A int `default:"a"`
|
||||
}{}, []string{})).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("should not accept string as float", func() {
|
||||
Expect(parse(&struct {
|
||||
}{},
|
||||
"use string as default to float": struct {
|
||||
A float32 `default:"a"`
|
||||
}{}, []string{})).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("should not accept string as uint", func() {
|
||||
Expect(parse(&struct {
|
||||
}{},
|
||||
"use string as default to uint": struct {
|
||||
A uint `default:"a"`
|
||||
}{}, []string{})).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("should not accept string as uint in sub-struct", func() {
|
||||
Expect(parse(&struct {
|
||||
}{},
|
||||
"use string as default to uint in sub-struct": struct {
|
||||
B struct {
|
||||
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() {
|
||||
Expect(parse(&struct {
|
||||
A []int `default:"a,bn"`
|
||||
}{}, []string{})).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("should not accept variables not being pointers", func() {
|
||||
cfg := struct {
|
||||
if err := parse(struct {
|
||||
A string `default:"a"`
|
||||
}{}
|
||||
}{}, nil); err == nil {
|
||||
t.Errorf("Expected error when feeding non-pointer struct to parse")
|
||||
}
|
||||
|
||||
Expect(parse(cfg, []string{})).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("should not accept variables not being pointers to structs", func() {
|
||||
cfg := "test"
|
||||
|
||||
Expect(parse(cfg, []string{})).To(HaveOccurred())
|
||||
})
|
||||
|
||||
})
|
||||
if err := parse("test", nil); err == nil {
|
||||
t.Errorf("Expected error when feeding non-pointer string to parse")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,44 +1,38 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var _ = Describe("Testing float parsing", func() {
|
||||
type t struct {
|
||||
func TestFloatParsing(t *testing.T) {
|
||||
var (
|
||||
args = []string{
|
||||
"--float32=5.5", "-3", "6.6",
|
||||
"--float64=7.7", "-6", "8.8",
|
||||
}
|
||||
cfg struct {
|
||||
Test32 float32 `flag:"float32"`
|
||||
Test32P float32 `flag:"float32p,3"`
|
||||
Test64 float64 `flag:"float64"`
|
||||
Test64P float64 `flag:"float64p,6"`
|
||||
TestDef float32 `default:"66.256"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
args []string
|
||||
cfg t
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
args = []string{
|
||||
"--float32=5.5", "-3", "6.6",
|
||||
"--float64=7.7", "-6", "8.8",
|
||||
if err := parse(&cfg, args); err != nil {
|
||||
t.Fatalf("Parsing options caused error: %s", err)
|
||||
}
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
for _, test := range [][2]interface{}{
|
||||
{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()) })
|
||||
It("should have the expected values", func() {
|
||||
Expect(cfg.Test32).To(Equal(float32(5.5)))
|
||||
Expect(cfg.Test32P).To(Equal(float32(6.6)))
|
||||
Expect(cfg.Test64).To(Equal(float64(7.7)))
|
||||
Expect(cfg.Test64P).To(Equal(float64(8.8)))
|
||||
|
||||
Expect(cfg.TestDef).To(Equal(float32(66.256)))
|
||||
})
|
||||
})
|
||||
{cfg.TestDef, float32(66.256)},
|
||||
} {
|
||||
if test[0] != test[1] {
|
||||
t.Errorf("Expected value does not match: %#v != %#v", test[0], test[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
135
general_test.go
135
general_test.go
|
@ -2,127 +2,90 @@ package rconfig
|
|||
|
||||
import (
|
||||
"os"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var _ = Describe("Testing general parsing", func() {
|
||||
type t struct {
|
||||
func TestGeneralExecution(t *testing.T) {
|
||||
type test struct {
|
||||
Test string `default:"foo" env:"shell" flag:"shell" description:"Test"`
|
||||
Test2 string `default:"blub" env:"testvar" flag:"testvar,t" description:"Test"`
|
||||
DefaultFlag string `default:"goo"`
|
||||
SadFlag string
|
||||
}
|
||||
|
||||
type tValidated struct {
|
||||
Test string `flag:"test" default:"" validate:"nonzero"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
args []string
|
||||
cfg t
|
||||
cfg test
|
||||
)
|
||||
|
||||
Context("with defined arguments", func() {
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
exec := func(desc string, tests [][2]interface{}) {
|
||||
if err := parse(&cfg, args); err != nil {
|
||||
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{
|
||||
"--shell=test23",
|
||||
"-t", "bla",
|
||||
}
|
||||
exec("defined arguments", [][2]interface{}{
|
||||
{&cfg.Test, "test23"},
|
||||
{&cfg.Test2, "bla"},
|
||||
{&cfg.SadFlag, ""},
|
||||
{&cfg.DefaultFlag, "goo"},
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
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{}
|
||||
cfg = test{}
|
||||
args = []string{}
|
||||
exec("no arguments", [][2]interface{}{
|
||||
{&cfg.Test, "foo"},
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
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{}
|
||||
cfg = test{}
|
||||
args = []string{}
|
||||
os.Setenv("shell", "test546")
|
||||
exec("no arguments and set env", [][2]interface{}{
|
||||
{&cfg.Test, "test546"},
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
os.Unsetenv("shell")
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
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{}
|
||||
cfg = test{}
|
||||
args = []string{
|
||||
"--shell=test23",
|
||||
"-t", "bla",
|
||||
"positional1", "positional2",
|
||||
}
|
||||
exec("additional arguments", [][2]interface{}{
|
||||
{&cfg.Test, "test23"},
|
||||
{&cfg.Test2, "bla"},
|
||||
{&cfg.SadFlag, ""},
|
||||
{&cfg.DefaultFlag, "goo"},
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
if !reflect.DeepEqual(Args(), []string{"positional1", "positional2"}) {
|
||||
t.Errorf("expected positional arguments to match")
|
||||
}
|
||||
}
|
||||
|
||||
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"))
|
||||
})
|
||||
It("should have detected the positional arguments", func() {
|
||||
Expect(Args()).To(Equal([]string{"positional1", "positional2"}))
|
||||
})
|
||||
})
|
||||
func TestValidationIntegration(t *testing.T) {
|
||||
type tValidated struct {
|
||||
Test string `flag:"test" default:"" validate:"nonzero"`
|
||||
}
|
||||
|
||||
Context("making use of the validator package", func() {
|
||||
var cfgValidated tValidated
|
||||
|
||||
BeforeEach(func() {
|
||||
var (
|
||||
cfgValidated = tValidated{}
|
||||
args = []string{}
|
||||
})
|
||||
)
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parseAndValidate(&cfgValidated, args)
|
||||
})
|
||||
|
||||
It("should have errored", func() { Expect(err).To(HaveOccurred()) })
|
||||
})
|
||||
|
||||
})
|
||||
if err := parseAndValidate(&cfgValidated, args); err == nil {
|
||||
t.Errorf("Expected error, got none")
|
||||
}
|
||||
}
|
||||
|
|
2
go.mod
2
go.mod
|
@ -1,6 +1,6 @@
|
|||
module github.com/Luzifer/rconfig/v2
|
||||
|
||||
go 1.16
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/spf13/pflag v1.0.5
|
||||
|
|
7
go.sum
7
go.sum
|
@ -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/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/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/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/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
|
|
63
int_test.go
63
int_test.go
|
@ -1,12 +1,18 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var _ = Describe("Testing int parsing", func() {
|
||||
type t struct {
|
||||
func TestIntParsing(t *testing.T) {
|
||||
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"`
|
||||
TestP int `flag:"intp,i"`
|
||||
Test8 int8 `flag:"int8"`
|
||||
|
@ -17,38 +23,25 @@ var _ = Describe("Testing int parsing", func() {
|
|||
Test64P int64 `flag:"int64p,6"`
|
||||
TestDef int8 `default:"66"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
args []string
|
||||
cfg t
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
args = []string{
|
||||
"--int=1", "-i", "2",
|
||||
"--int8=3", "-8", "4",
|
||||
"--int32=5", "-3", "6",
|
||||
"--int64=7", "-6", "8",
|
||||
if err := parse(&cfg, args); err != nil {
|
||||
t.Fatalf("Parsing options caused error: %s", err)
|
||||
}
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have the expected values", func() {
|
||||
Expect(cfg.Test).To(Equal(1))
|
||||
Expect(cfg.TestP).To(Equal(2))
|
||||
Expect(cfg.Test8).To(Equal(int8(3)))
|
||||
Expect(cfg.Test8P).To(Equal(int8(4)))
|
||||
Expect(cfg.Test32).To(Equal(int32(5)))
|
||||
Expect(cfg.Test32P).To(Equal(int32(6)))
|
||||
Expect(cfg.Test64).To(Equal(int64(7)))
|
||||
Expect(cfg.Test64P).To(Equal(int64(8)))
|
||||
|
||||
Expect(cfg.TestDef).To(Equal(int8(66)))
|
||||
})
|
||||
})
|
||||
for _, test := range [][2]interface{}{
|
||||
{cfg.Test, int(1)},
|
||||
{cfg.TestP, 2},
|
||||
{cfg.Test8, int8(3)},
|
||||
{cfg.Test8P, int8(4)},
|
||||
{cfg.Test32, int32(5)},
|
||||
{cfg.Test32P, int32(6)},
|
||||
{cfg.Test64, int64(7)},
|
||||
{cfg.Test64P, int64(8)},
|
||||
{cfg.TestDef, int8(66)},
|
||||
} {
|
||||
if test[0] != test[1] {
|
||||
t.Errorf("Expected value does not match: %#v != %#v", test[0], test[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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"))
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
})
|
|
@ -2,86 +2,66 @@ package rconfig
|
|||
|
||||
import (
|
||||
"os"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var _ = Describe("Precedence", func() {
|
||||
|
||||
type t struct {
|
||||
func TestPrecedence(t *testing.T) {
|
||||
type testcfg struct {
|
||||
A int `default:"1" vardefault:"a" env:"a" flag:"avar,a" description:"a"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
cfg t
|
||||
cfg testcfg
|
||||
args []string
|
||||
vardefaults map[string]string
|
||||
)
|
||||
|
||||
JustBeforeEach(func() {
|
||||
cfg = t{}
|
||||
exec := func(desc string, fn func() interface{}, exp interface{}) {
|
||||
cfg = testcfg{}
|
||||
SetVariableDefaults(vardefaults)
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
|
||||
Context("Provided: Flag, Env, Default, VarDefault", func() {
|
||||
BeforeEach(func() {
|
||||
if err != nil {
|
||||
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"}
|
||||
os.Setenv("a", "8")
|
||||
vardefaults = map[string]string{
|
||||
"a": "3",
|
||||
}
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have used the flag value", func() {
|
||||
Expect(cfg.A).To(Equal(5))
|
||||
})
|
||||
})
|
||||
exec("Provided: Flag, Env, Default, VarDefault", func() interface{} { return cfg.A }, 5)
|
||||
|
||||
Context("Provided: Env, Default, VarDefault", func() {
|
||||
BeforeEach(func() {
|
||||
// Provided: Env, Default, VarDefault
|
||||
args = []string{}
|
||||
os.Setenv("a", "8")
|
||||
vardefaults = map[string]string{
|
||||
"a": "3",
|
||||
}
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have used the env value", func() {
|
||||
Expect(cfg.A).To(Equal(8))
|
||||
})
|
||||
})
|
||||
exec("Provided: Env, Default, VarDefault", func() interface{} { return cfg.A }, 8)
|
||||
|
||||
Context("Provided: Default, VarDefault", func() {
|
||||
BeforeEach(func() {
|
||||
// Provided: Default, VarDefault
|
||||
args = []string{}
|
||||
os.Unsetenv("a")
|
||||
vardefaults = map[string]string{
|
||||
"a": "3",
|
||||
}
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have used the vardefault value", func() {
|
||||
Expect(cfg.A).To(Equal(3))
|
||||
})
|
||||
})
|
||||
exec("Provided: Default, VarDefault", func() interface{} { return cfg.A }, 3)
|
||||
|
||||
Context("Provided: Default", func() {
|
||||
BeforeEach(func() {
|
||||
// Provided: Default
|
||||
args = []string{}
|
||||
os.Unsetenv("a")
|
||||
vardefaults = map[string]string{}
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have used the default value", func() {
|
||||
Expect(cfg.A).To(Equal(1))
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
exec("Provided: Default", func() interface{} { return cfg.A }, 1)
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
|
@ -1,55 +1,45 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var _ = Describe("Testing slices", func() {
|
||||
type t struct {
|
||||
func TestSliceParsing(t *testing.T) {
|
||||
var (
|
||||
args = []string{
|
||||
"--int=4,5", "-s", "hallo,welt",
|
||||
}
|
||||
cfg struct {
|
||||
Int []int `default:"1,2,3" flag:"int"`
|
||||
String []string `default:"a,b,c" flag:"string"`
|
||||
IntP []int `default:"1,2,3" flag:"intp,i"`
|
||||
StringP []string `default:"a,b,c" flag:"stringp,s"`
|
||||
EmptyString []string `default:""`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
args []string
|
||||
cfg t
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
args = []string{
|
||||
"--int=4,5", "-s", "hallo,welt",
|
||||
if err := parse(&cfg, args); err != nil {
|
||||
t.Fatalf("Parsing options caused error: %s", err)
|
||||
}
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
for _, test := range [][2]interface{}{
|
||||
{len(cfg.Int), 2},
|
||||
{cfg.Int, []int{4, 5}},
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have the expected values for int-slice", func() {
|
||||
Expect(len(cfg.Int)).To(Equal(2))
|
||||
Expect(cfg.Int).To(Equal([]int{4, 5}))
|
||||
Expect(cfg.Int).NotTo(Equal([]int{5, 4}))
|
||||
})
|
||||
It("should have the expected values for int-shorthand-slice", func() {
|
||||
Expect(len(cfg.IntP)).To(Equal(3))
|
||||
Expect(cfg.IntP).To(Equal([]int{1, 2, 3}))
|
||||
})
|
||||
It("should have the expected values for string-slice", func() {
|
||||
Expect(len(cfg.String)).To(Equal(3))
|
||||
Expect(cfg.String).To(Equal([]string{"a", "b", "c"}))
|
||||
})
|
||||
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))
|
||||
})
|
||||
})
|
||||
{len(cfg.IntP), 3},
|
||||
{cfg.IntP, []int{1, 2, 3}},
|
||||
|
||||
{len(cfg.String), 3},
|
||||
{cfg.String, []string{"a", "b", "c"}},
|
||||
|
||||
{len(cfg.StringP), 2},
|
||||
{cfg.StringP, []string{"hallo", "welt"}},
|
||||
|
||||
{len(cfg.EmptyString), 0},
|
||||
} {
|
||||
if !reflect.DeepEqual(test[0], test[1]) {
|
||||
t.Errorf("Expected value does not match: %#v != %#v", test[0], test[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,36 +1,30 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var _ = Describe("Testing sub-structs", func() {
|
||||
type t struct {
|
||||
func TestSubStructParsing(t *testing.T) {
|
||||
var (
|
||||
args = []string{}
|
||||
cfg struct {
|
||||
Test string `default:"blubb"`
|
||||
Sub struct {
|
||||
Test string `default:"Hallo"`
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
args []string
|
||||
cfg t
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
args = []string{}
|
||||
})
|
||||
if err := parse(&cfg, args); err != nil {
|
||||
t.Fatalf("Parsing options caused error: %s", err)
|
||||
}
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have the expected values", func() {
|
||||
Expect(cfg.Test).To(Equal("blubb"))
|
||||
Expect(cfg.Sub.Test).To(Equal("Hallo"))
|
||||
})
|
||||
})
|
||||
for _, test := range [][2]interface{}{
|
||||
{cfg.Test, "blubb"},
|
||||
{cfg.Sub.Test, "Hallo"},
|
||||
} {
|
||||
if test[0] != test[1] {
|
||||
t.Errorf("Expected value does not match: %#v != %#v", test[0], test[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
68
uint_test.go
68
uint_test.go
|
@ -1,12 +1,19 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var _ = Describe("Testing uint parsing", func() {
|
||||
type t struct {
|
||||
func TestUintParsing(t *testing.T) {
|
||||
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"`
|
||||
TestP uint `flag:"intp,i"`
|
||||
Test8 uint8 `flag:"int8"`
|
||||
|
@ -19,41 +26,28 @@ var _ = Describe("Testing uint parsing", func() {
|
|||
Test64P uint64 `flag:"int64p,6"`
|
||||
TestDef uint8 `default:"66"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
args []string
|
||||
cfg t
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
args = []string{
|
||||
"--int=1", "-i", "2",
|
||||
"--int8=3", "-8", "4",
|
||||
"--int32=5", "-3", "6",
|
||||
"--int64=7", "-6", "8",
|
||||
"--int16=9", "-1", "10",
|
||||
if err := parse(&cfg, args); err != nil {
|
||||
t.Fatalf("Parsing options caused error: %s", err)
|
||||
}
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
for _, test := range [][2]interface{}{
|
||||
{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()) })
|
||||
It("should have the expected values", func() {
|
||||
Expect(cfg.Test).To(Equal(uint(1)))
|
||||
Expect(cfg.TestP).To(Equal(uint(2)))
|
||||
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)))
|
||||
})
|
||||
})
|
||||
{cfg.TestDef, uint8(66)},
|
||||
} {
|
||||
if test[0] != test[1] {
|
||||
t.Errorf("Expected value does not match: %#v != %#v", test[0], test[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,14 +3,12 @@ package rconfig
|
|||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var _ = Describe("Testing variable defaults", func() {
|
||||
|
||||
type t struct {
|
||||
func TestVardefaultParsing(t *testing.T) {
|
||||
type test struct {
|
||||
MySecretValue string `default:"secret" env:"foo" vardefault:"my_secret_value"`
|
||||
MyUsername string `default:"luzifer" vardefault:"username"`
|
||||
SomeVar string `flag:"var" description:"some variable"`
|
||||
|
@ -18,8 +16,7 @@ var _ = Describe("Testing variable defaults", func() {
|
|||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
cfg t
|
||||
cfg test
|
||||
args = []string{}
|
||||
vardefaults = map[string]string{
|
||||
"my_secret_value": "veryverysecretkey",
|
||||
|
@ -28,95 +25,62 @@ var _ = Describe("Testing variable defaults", func() {
|
|||
}
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
})
|
||||
exec := func(desc string, tests [][2]interface{}) {
|
||||
if err := parse(&cfg, args); err != nil {
|
||||
t.Fatalf("Parsing options caused error: %s", err)
|
||||
}
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
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])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Context("With manually provided variables", func() {
|
||||
BeforeEach(func() {
|
||||
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()) })
|
||||
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(""))
|
||||
})
|
||||
SetVariableDefaults(VarDefaultsFromYAML([]byte("---\nmy_secret_value: veryverysecretkey\nunknownkey: hi there\nint_var: 42\n")))
|
||||
exec("defaults from YAML data", [][2]interface{}{
|
||||
{&cfg.IntVar, int64(42)},
|
||||
{&cfg.MySecretValue, "veryverysecretkey"},
|
||||
{&cfg.MyUsername, "luzifer"},
|
||||
{&cfg.SomeVar, ""},
|
||||
})
|
||||
|
||||
Context("With defaults from 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(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("", "")
|
||||
tmp, _ := ioutil.TempFile("", "")
|
||||
defer func() {
|
||||
tmp.Close()
|
||||
os.Remove(tmp.Name())
|
||||
}()
|
||||
yamlData := "---\nmy_secret_value: veryverysecretkey\nunknownkey: hi there\nint_var: 42\n"
|
||||
tmp.WriteString(yamlData)
|
||||
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() {
|
||||
tmp.Close()
|
||||
os.Remove(tmp.Name())
|
||||
SetVariableDefaults(VarDefaultsFromYAML([]byte("---\nmy_secret_value = veryverysecretkey\nunknownkey = hi there\nint_var = 42\n")))
|
||||
exec("defaults from invalid YAML data", [][2]interface{}{
|
||||
{&cfg.IntVar, int64(23)},
|
||||
{&cfg.MySecretValue, "secret"},
|
||||
{&cfg.MyUsername, "luzifer"},
|
||||
{&cfg.SomeVar, ""},
|
||||
})
|
||||
|
||||
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(""))
|
||||
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, ""},
|
||||
})
|
||||
})
|
||||
|
||||
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(""))
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue