Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extend test coverage #19

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion env/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ func (c *VarNameConstructor) setReplacement(oldChar, newChar string) {
}

func (c *VarNameConstructor) VarFromFlagName(name string) string {
varName := strings.ReplaceAll(name, c.varNameReplacement.old, c.varNameReplacement.new)
varName := name
if c.prefix != "" {
varName = fmt.Sprintf("%s_%s", c.prefix, varName)
}
varName = strings.ReplaceAll(varName, c.varNameReplacement.old, c.varNameReplacement.new)
if c.capitalize {
varName = strings.ToUpper(varName)
}
Expand Down
124 changes: 89 additions & 35 deletions flag/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func assertFlag[T any](t *testing.T, f flagPropertyGetter, tt flagTest) {
assert.Equal(t, tt.expected.Shared(), f.IsShared())
}

func assertGetFlag[T any](t *testing.T, f flagPropertyGetter, tt getFlagTest[T]) {
func assertGetFlagCmd[T any](t *testing.T, f flagPropertyGetter, tt getFlagTest[T]) {
assert.NotNil(t, f)
if tt.input != nil {
if tt.wanted.err {
Expand All @@ -108,6 +108,24 @@ func assertGetFlag[T any](t *testing.T, f flagPropertyGetter, tt getFlagTest[T])
assert.Equal(t, tt.wanted.some, PtrOrDie[T](f.Value()))
}

func assertGetFlagEnv[T any](t *testing.T, f flagPropertyGetter, tt getFlagTest[T]) {
assert.NotNil(t, f)
if tt.input != nil {
if tt.wanted.err {
assert.Error(t, f.FromEnvVariable(*tt.input))
return
}
if *tt.input == "" {
assert.Error(t, f.FromEnvVariable(*tt.input))
return
}
assert.NoError(t, f.FromEnvVariable(*tt.input))
assert.True(t, f.IsSetFromCmd())
}
assert.Equal(t, *tt.wanted.some, DerefOrDie[T](f.Value()))
assert.Equal(t, tt.wanted.some, PtrOrDie[T](f.Value()))
}

type custom struct {
field int
}
Expand All @@ -128,8 +146,12 @@ func (p *customParser) ParseCmd(s string) (any, error) {
return &custom{field: v}, nil
}

func (p *customParser) ParseEnv(_ string) (any, error) {
return nil, nil
func (p *customParser) ParseEnv(s string) (any, error) {
v, err := strconv.Atoi(s)
if err != nil {
return nil, err
}
return &custom{field: v}, nil
}

func TestFlag_Custom(t *testing.T) {
Expand Down Expand Up @@ -186,7 +208,8 @@ func TestFlag_GetCustom(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Typed[custom](tt.name, tt.opts...)
assertGetFlag[custom](t, f, tt)
assertGetFlagCmd[custom](t, f, tt)
assertGetFlagEnv[custom](t, f, tt)
})
}
}
Expand Down Expand Up @@ -265,7 +288,8 @@ func TestFlag_GetString(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := String(tt.name, tt.opts...)
assertGetFlag[string](t, f, tt)
assertGetFlagCmd[string](t, f, tt)
assertGetFlagEnv[string](t, f, tt)
})
}
}
Expand Down Expand Up @@ -353,7 +377,8 @@ func TestFlag_GetDuration(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Duration(tt.name, tt.opts...)
assertGetFlag[time.Duration](t, f, tt)
assertGetFlagCmd[time.Duration](t, f, tt)
assertGetFlagEnv[time.Duration](t, f, tt)
})
}
}
Expand Down Expand Up @@ -450,7 +475,8 @@ func TestFlag_GetBool(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Bool(tt.name, tt.opts...)
assertGetFlag[bool](t, f, tt)
assertGetFlagCmd[bool](t, f, tt)
assertGetFlagEnv[bool](t, f, tt)
})
}
}
Expand Down Expand Up @@ -526,7 +552,8 @@ func TestFlag_GetInt(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Int(tt.name, tt.opts...)
assertGetFlag[int](t, f, tt)
assertGetFlagCmd[int](t, f, tt)
assertGetFlagEnv[int](t, f, tt)
})
}
}
Expand Down Expand Up @@ -602,7 +629,8 @@ func TestFlag_GetInt8(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Int8(tt.name, tt.opts...)
assertGetFlag[int8](t, f, tt)
assertGetFlagCmd[int8](t, f, tt)
assertGetFlagEnv[int8](t, f, tt)
})
}
}
Expand Down Expand Up @@ -678,7 +706,8 @@ func TestFlag_GetInt16(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Int16(tt.name, tt.opts...)
assertGetFlag[int16](t, f, tt)
assertGetFlagCmd[int16](t, f, tt)
assertGetFlagEnv[int16](t, f, tt)
})
}
}
Expand Down Expand Up @@ -754,7 +783,8 @@ func TestFlag_GetInt32(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Int32(tt.name, tt.opts...)
assertGetFlag[int32](t, f, tt)
assertGetFlagCmd[int32](t, f, tt)
assertGetFlagEnv[int32](t, f, tt)
})
}
}
Expand Down Expand Up @@ -830,7 +860,8 @@ func TestFlag_GetInt64(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Int64(tt.name, tt.opts...)
assertGetFlag[int64](t, f, tt)
assertGetFlagCmd[int64](t, f, tt)
assertGetFlagEnv[int64](t, f, tt)
})
}
}
Expand Down Expand Up @@ -906,7 +937,8 @@ func TestFlag_GetUint(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Uint(tt.name, tt.opts...)
assertGetFlag[uint](t, f, tt)
assertGetFlagCmd[uint](t, f, tt)
assertGetFlagEnv[uint](t, f, tt)
})
}
}
Expand Down Expand Up @@ -982,7 +1014,8 @@ func TestFlag_GetUint8(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Uint8(tt.name, tt.opts...)
assertGetFlag[uint8](t, f, tt)
assertGetFlagCmd[uint8](t, f, tt)
assertGetFlagEnv[uint8](t, f, tt)
})
}
}
Expand Down Expand Up @@ -1058,7 +1091,8 @@ func TestFlag_GetUint16(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Uint16(tt.name, tt.opts...)
assertGetFlag[uint16](t, f, tt)
assertGetFlagCmd[uint16](t, f, tt)
assertGetFlagEnv[uint16](t, f, tt)
})
}
}
Expand Down Expand Up @@ -1134,7 +1168,8 @@ func TestFlag_GetUint32(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Uint32(tt.name, tt.opts...)
assertGetFlag[uint32](t, f, tt)
assertGetFlagCmd[uint32](t, f, tt)
assertGetFlagEnv[uint32](t, f, tt)
})
}
}
Expand Down Expand Up @@ -1210,7 +1245,8 @@ func TestFlag_GetUint64(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Uint64(tt.name, tt.opts...)
assertGetFlag[uint64](t, f, tt)
assertGetFlagCmd[uint64](t, f, tt)
assertGetFlagEnv[uint64](t, f, tt)
})
}
}
Expand Down Expand Up @@ -1286,7 +1322,8 @@ func TestFlag_GetFloat32(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Float32(tt.name, tt.opts...)
assertGetFlag[float32](t, f, tt)
assertGetFlagCmd[float32](t, f, tt)
assertGetFlagEnv[float32](t, f, tt)
})
}
}
Expand Down Expand Up @@ -1362,7 +1399,8 @@ func TestFlag_GetFloat64(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Float64(tt.name, tt.opts...)
assertGetFlag[float64](t, f, tt)
assertGetFlagCmd[float64](t, f, tt)
assertGetFlagEnv[float64](t, f, tt)
})
}
}
Expand Down Expand Up @@ -1449,7 +1487,8 @@ func TestFlag_GetIntSlice(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := IntSlice(tt.name, tt.opts...)
assertGetFlag[[]int](t, f, tt)
assertGetFlagCmd[[]int](t, f, tt)
assertGetFlagEnv[[]int](t, f, tt)
})
}
}
Expand Down Expand Up @@ -1536,7 +1575,8 @@ func TestFlag_GetInt8Slice(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Int8Slice(tt.name, tt.opts...)
assertGetFlag[[]int8](t, f, tt)
assertGetFlagCmd[[]int8](t, f, tt)
assertGetFlagEnv[[]int8](t, f, tt)
})
}
}
Expand Down Expand Up @@ -1623,7 +1663,8 @@ func TestFlag_GetInt16Slice(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Int16Slice(tt.name, tt.opts...)
assertGetFlag[[]int16](t, f, tt)
assertGetFlagCmd[[]int16](t, f, tt)
assertGetFlagEnv[[]int16](t, f, tt)
})
}
}
Expand Down Expand Up @@ -1710,7 +1751,8 @@ func TestFlag_GetInt32Slice(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Int32Slice(tt.name, tt.opts...)
assertGetFlag[[]int32](t, f, tt)
assertGetFlagCmd[[]int32](t, f, tt)
assertGetFlagEnv[[]int32](t, f, tt)
})
}
}
Expand Down Expand Up @@ -1797,7 +1839,8 @@ func TestFlag_GetInt64Slice(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Int64Slice(tt.name, tt.opts...)
assertGetFlag[[]int64](t, f, tt)
assertGetFlagCmd[[]int64](t, f, tt)
assertGetFlagEnv[[]int64](t, f, tt)
})
}
}
Expand Down Expand Up @@ -1884,7 +1927,8 @@ func TestFlag_GetUintSlice(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := UintSlice(tt.name, tt.opts...)
assertGetFlag[[]uint](t, f, tt)
assertGetFlagCmd[[]uint](t, f, tt)
assertGetFlagEnv[[]uint](t, f, tt)
})
}
}
Expand Down Expand Up @@ -1971,7 +2015,8 @@ func TestFlag_GetUint8Slice(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Uint8Slice(tt.name, tt.opts...)
assertGetFlag[[]uint8](t, f, tt)
assertGetFlagCmd[[]uint8](t, f, tt)
assertGetFlagEnv[[]uint8](t, f, tt)
})
}
}
Expand Down Expand Up @@ -2058,7 +2103,8 @@ func TestFlag_GetUint16Slice(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Uint16Slice(tt.name, tt.opts...)
assertGetFlag[[]uint16](t, f, tt)
assertGetFlagCmd[[]uint16](t, f, tt)
assertGetFlagEnv[[]uint16](t, f, tt)
})
}
}
Expand Down Expand Up @@ -2145,7 +2191,8 @@ func TestFlag_GetUint32Slice(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Uint32Slice(tt.name, tt.opts...)
assertGetFlag[[]uint32](t, f, tt)
assertGetFlagCmd[[]uint32](t, f, tt)
assertGetFlagEnv[[]uint32](t, f, tt)
})
}
}
Expand Down Expand Up @@ -2232,7 +2279,8 @@ func TestFlag_GetUint64Slice(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Uint64Slice(tt.name, tt.opts...)
assertGetFlag[[]uint64](t, f, tt)
assertGetFlagCmd[[]uint64](t, f, tt)
assertGetFlagEnv[[]uint64](t, f, tt)
})
}
}
Expand Down Expand Up @@ -2319,7 +2367,8 @@ func TestFlag_GetFloat32Slice(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Float32Slice(tt.name, tt.opts...)
assertGetFlag[[]float32](t, f, tt)
assertGetFlagCmd[[]float32](t, f, tt)
assertGetFlagEnv[[]float32](t, f, tt)
})
}
}
Expand Down Expand Up @@ -2406,7 +2455,8 @@ func TestFlag_GetFloat64Slice(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Float64Slice(tt.name, tt.opts...)
assertGetFlag[[]float64](t, f, tt)
assertGetFlagCmd[[]float64](t, f, tt)
assertGetFlagEnv[[]float64](t, f, tt)
})
}
}
Expand Down Expand Up @@ -2484,7 +2534,8 @@ func TestFlag_GetStringSlice(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := StringSlice(tt.name, tt.opts...)
assertGetFlag[[]string](t, f, tt)
assertGetFlagCmd[[]string](t, f, tt)
assertGetFlagEnv[[]string](t, f, tt)
})
}
}
Expand Down Expand Up @@ -2571,7 +2622,8 @@ func TestFlag_GetDurationSlice(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := DurationSlice(tt.name, tt.opts...)
assertGetFlag[[]time.Duration](t, f, tt)
assertGetFlagCmd[[]time.Duration](t, f, tt)
assertGetFlagEnv[[]time.Duration](t, f, tt)
})
}
}
Expand Down Expand Up @@ -2658,7 +2710,8 @@ func TestFlag_GetBoolSlice(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := BoolSlice(tt.name, tt.opts...)
assertGetFlag[[]bool](t, f, tt)
assertGetFlagCmd[[]bool](t, f, tt)
assertGetFlagEnv[[]bool](t, f, tt)
})
}
}
Expand Down Expand Up @@ -2737,7 +2790,8 @@ func TestFlag_GetCounter(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := Counter(tt.name, tt.opts...)
assertGetFlag[int](t, f, tt)
assertGetFlagCmd[int](t, f, tt)
assertGetFlagEnv[int](t, f, tt)
})
}
}
Loading
Loading