Skip to content

Commit 6f97c51

Browse files
authored
Use os.LookupEnv instead of os.Getenv (#151)
* Use os.LookupEnv instead of os.Getenv * Make linter happy * ffenv: support 'KEY=' with no val
1 parent 3499750 commit 6f97c51

5 files changed

Lines changed: 15 additions & 15 deletions

File tree

ffenv/ffenv.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ func Parse(r io.Reader, set func(name, value string) error) error {
4646
return fmt.Errorf("%w: %s", ErrInvalidLine, line)
4747
}
4848

49-
if len(value) <= 0 {
50-
return fmt.Errorf("%w: %s", ErrInvalidLine, line)
51-
}
52-
5349
if unquoted, err := strconv.Unquote(value); err == nil {
5450
value = unquoted
5551
}

ffenv/ffenv_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"github.com/peterbourgon/ff/v4"
9-
"github.com/peterbourgon/ff/v4/ffenv"
109
"github.com/peterbourgon/ff/v4/fftest"
1110
)
1211

@@ -38,7 +37,7 @@ func TestEnvFileParser(t *testing.T) {
3837
},
3938
{
4039
ConfigFile: "testdata/no-value.env",
41-
Want: fftest.Vars{WantParseErrorIs: ffenv.ErrInvalidLine},
40+
Want: fftest.Vars{WantParseErrorString: "D: parse error"},
4241
},
4342
{
4443
ConfigFile: "testdata/spaces.env",

fftest/test_case.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package fftest
22

33
import (
4-
"os"
54
"path/filepath"
65
"strings"
76
"testing"
@@ -76,8 +75,7 @@ func (tc *ParseTest) Run(t *testing.T) {
7675
// be run in parallel.
7776
if len(tc.Environment) > 0 {
7877
for k, v := range tc.Environment {
79-
defer os.Setenv(k, os.Getenv(k))
80-
os.Setenv(k, v)
78+
t.Setenv(k, v)
8179
}
8280
}
8381

parse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ func parse(fs Flags, args []string, options ...Option) error {
9191
key := getEnvVarKey(name, pc.envVarPrefix)
9292

9393
// Look up the value from the environment.
94-
val := os.Getenv(key)
95-
if val == "" {
94+
val, ok := os.LookupEnv(key)
95+
if !ok {
9696
continue
9797
}
9898

parse_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
var testdataConfigFS embed.FS
1717

1818
func TestParse(t *testing.T) {
19-
t.Parallel()
19+
// t.Parallel() not possible
2020

2121
testcases := fftest.TestCases{
2222
{
@@ -134,13 +134,20 @@ func TestParse(t *testing.T) {
134134
Options: []ff.Option{ff.WithEnvVarPrefix("TEST_PARSE"), ff.WithEnvVarSplit("xx")},
135135
Want: fftest.Vars{S: `axxb`, X: []string{`one`, `twoxxthree`}},
136136
},
137+
{
138+
Name: "env var provided but empty",
139+
Default: fftest.Vars{S: "non-empty-default", I: 42, F: 1.23},
140+
Environment: map[string]string{"TEST_PARSE_S": ""},
141+
Options: []ff.Option{ff.WithEnvVarPrefix("TEST_PARSE")},
142+
Want: fftest.Vars{S: "", I: 42, F: 1.23},
143+
},
137144
}
138145

139146
testcases.Run(t)
140147
}
141148

142149
func TestParse_FlagSet(t *testing.T) {
143-
t.Parallel()
150+
// t.Parallel() not possible
144151

145152
testcases := fftest.TestCases{
146153
{
@@ -271,7 +278,7 @@ func TestParse_FlagSet(t *testing.T) {
271278
}
272279

273280
func TestParse_StdFlagSetAdapter(t *testing.T) {
274-
t.Parallel()
281+
// t.Parallel() not possible
275282

276283
testcases := fftest.TestCases{
277284
{
@@ -312,7 +319,7 @@ func TestParse_StdFlagSetAdapter(t *testing.T) {
312319
}
313320

314321
func TestParse_PlainParser(t *testing.T) {
315-
t.Parallel()
322+
// t.Parallel() not possible
316323

317324
testcases := fftest.TestCases{
318325
{

0 commit comments

Comments
 (0)