Skip to content

Commit

Permalink
dotenv: fix linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
zimbatm committed Jun 21, 2022
1 parent f730b1b commit 803d689
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
9 changes: 3 additions & 6 deletions pkg/dotenv/parse.go
Expand Up @@ -124,9 +124,8 @@ func expandEnv(value string, dotenv map[string]string) string {

if found {
return expanded
} else {
return getFromEnvOrDefault(envKey, defaultValue, hasDefault)
}
return getFromEnvOrDefault(envKey, defaultValue, hasDefault)
}

return os.Expand(value, expander)
Expand All @@ -137,9 +136,8 @@ func splitKeyAndDefault(value string, sep string) (string, string, bool) {

if i == -1 {
return value, "", false
} else {
return value[0:i], value[i+len(sep):], true
}
return value[0:i], value[i+len(sep):], true
}

func lookupDotenv(value string, dotenv map[string]string) (string, bool) {
Expand All @@ -152,7 +150,6 @@ func getFromEnvOrDefault(envKey string, defaultValue string, hasDefault bool) st

if len(envValue) == 0 && hasDefault {
return defaultValue
} else {
return envValue
}
return envValue
}
32 changes: 16 additions & 16 deletions pkg/dotenv/parse_test.go
Expand Up @@ -23,15 +23,15 @@ func envShouldContain(t *testing.T, env map[string]string, key string, value str
// https://github.com/bkeepers/dotenv/blob/master/lib/dotenv/environment.rb
// TODO: support shell variable expansions

const TEST_EXPORTED = `export OPTION_A=2
const TestExportedEnv = `export OPTION_A=2
export OPTION_B='\n' # foo
#export OPTION_C=3
export OPTION_D=
export OPTION_E="foo"
`

func TestDotEnvExported(t *testing.T) {
env := dotenv.MustParse(TEST_EXPORTED)
env := dotenv.MustParse(TestExportedEnv)
shouldNotHaveEmptyKey(t, env)

if env["OPTION_A"] != "2" {
Expand All @@ -51,7 +51,7 @@ func TestDotEnvExported(t *testing.T) {
}
}

const TEST_PLAIN = `OPTION_A=1
const TestPlainEnv = `OPTION_A=1
OPTION_B=2
OPTION_C= 3
OPTION_D =4
Expand All @@ -62,7 +62,7 @@ SMTP_ADDRESS=smtp # This is a comment
`

func TestDotEnvPlain(t *testing.T) {
env := dotenv.MustParse(TEST_PLAIN)
env := dotenv.MustParse(TestPlainEnv)
shouldNotHaveEmptyKey(t, env)

if env["OPTION_A"] != "1" {
Expand Down Expand Up @@ -91,10 +91,10 @@ func TestDotEnvPlain(t *testing.T) {
}
}

const TEST_SOLO_EMPTY = "SOME_VAR="
const TestSoloEmptyEnv = "SOME_VAR="

func TestSoloEmpty(t *testing.T) {
env := dotenv.MustParse(TEST_SOLO_EMPTY)
env := dotenv.MustParse(TestSoloEmptyEnv)
shouldNotHaveEmptyKey(t, env)

v, ok := env["SOME_VAR"]
Expand All @@ -106,7 +106,7 @@ func TestSoloEmpty(t *testing.T) {
}
}

const TEST_QUOTED = `OPTION_A='1'
const TestQuotedEnv = `OPTION_A='1'
OPTION_B='2'
OPTION_C=''
OPTION_D='\n'
Expand All @@ -118,7 +118,7 @@ OPTION_H="\n"
`

func TestDotEnvQuoted(t *testing.T) {
env := dotenv.MustParse(TEST_QUOTED)
env := dotenv.MustParse(TestQuotedEnv)
shouldNotHaveEmptyKey(t, env)

if env["OPTION_A"] != "1" {
Expand Down Expand Up @@ -150,7 +150,7 @@ func TestDotEnvQuoted(t *testing.T) {
}
}

const TEST_YAML = `OPTION_A: 1
const TestYAMLEnv = `OPTION_A: 1
OPTION_B: '2'
OPTION_C: ''
OPTION_D: '\n'
Expand All @@ -159,7 +159,7 @@ OPTION_F:
`

func TestDotEnvYAML(t *testing.T) {
env := dotenv.MustParse(TEST_YAML)
env := dotenv.MustParse(TestYAMLEnv)
shouldNotHaveEmptyKey(t, env)

if env["OPTION_A"] != "1" {
Expand Down Expand Up @@ -192,21 +192,21 @@ func TestFailingMustParse(t *testing.T) {
dotenv.MustParse("...")
}

const TEST_COMMENT_OVERRIDE = `
const TestCommentOverrideEnv = `
VARIABLE=value
#VARIABLE=disabled_value
`

func TestCommentOverride(t *testing.T) {
env := dotenv.MustParse(TEST_COMMENT_OVERRIDE)
env := dotenv.MustParse(TestCommentOverrideEnv)
shouldNotHaveEmptyKey(t, env)

if env["VARIABLE"] != "value" {
t.Error("VARIABLE should == value, not", env["VARIABLE"])
}
}

const TEST_VARIABLE_EXPANSION = `
const TestVariableExpansionEnv = `
OPTION_A=$FOO
OPTION_B="$FOO"
OPTION_C=${FOO}
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestVariableExpansion(t *testing.T) {
t.Fatalf("unable to set environment variable for testing: %s", err)
}

env := dotenv.MustParse(TEST_VARIABLE_EXPANSION)
env := dotenv.MustParse(TestVariableExpansionEnv)
shouldNotHaveEmptyKey(t, env)

envShouldContain(t, env, "OPTION_A", "foo")
Expand Down Expand Up @@ -274,7 +274,7 @@ func TestVariableExpansion(t *testing.T) {
envShouldContain(t, env, "OPTION_A1", "foo/bar/foo/bar/foo")
}

const TEST_VARIABLE_EXPANSION_WITH_DEFAULTS = `
const TestVariableExpansionWithDefaultsEnv = `
OPTION_A="${FOO:-}"
OPTION_B="${FOO:-default}"
OPTION_C='${FOO:-default}'
Expand Down Expand Up @@ -302,7 +302,7 @@ func TestVariableExpansionWithDefaults(t *testing.T) {
t.Fatalf("unable to set environment variable for testing: %s", err)
}

env := dotenv.MustParse(TEST_VARIABLE_EXPANSION_WITH_DEFAULTS)
env := dotenv.MustParse(TestVariableExpansionWithDefaultsEnv)
shouldNotHaveEmptyKey(t, env)

envShouldContain(t, env, "OPTION_A", "foo")
Expand Down

0 comments on commit 803d689

Please sign in to comment.