Skip to content

Commit

Permalink
Reverting 9c4dfae and adding test to cover reason
Browse files Browse the repository at this point in the history
The refactoring in 9c4dfae was a bit too clever - I changed behaviour
and started considering _empty_ envvars as valid. This _may_ be expected
in some use-cases, but `getenv` didn't work that way before, and it's
probably a surprising behaviour anyway.

Signed-off-by: Dave Henderson <[email protected]>
  • Loading branch information
hairyhenderson committed May 1, 2017
1 parent 4645843 commit 9c9d105
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ type Env struct {
// It returns the value, or the default (or an emptry string) if the variable is
// not set.
func (e *Env) Getenv(key string, def ...string) string {
val, ok := os.LookupEnv(key)
if !ok && len(def) > 0 {
val := os.Getenv(key)
if val == "" && len(def) > 0 {
return def[0]
}

Expand Down
6 changes: 6 additions & 0 deletions test/integration/envvars.bats
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ load helper
[[ "${output}" == "foo" ]]
}

@test "default string with empty env var using getenv" {
FOO="" gomplate -i '{{getenv "FOO" "foo"}}'
[ "$status" -eq 0 ]
[[ "${output}" == "foo" ]]
}

@test "existant env var using .Env" {
gomplate -i '{{.Env.HOME}}'
[ "$status" -eq 0 ]
Expand Down

0 comments on commit 9c9d105

Please sign in to comment.