Skip to content

Commit

Permalink
Add more env variable tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Dec 6, 2022
1 parent 6f01f51 commit 083787e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
6 changes: 4 additions & 2 deletions cmd/pint/tests/0113_config_env_expand.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
env FOO=BAR
env AUTH_KEY=1234
env FAKE_ENV=KEY=VAL
pint.ok --no-color config
! stdout .
cmp stderr stderr.txt
Expand Down Expand Up @@ -64,7 +65,8 @@ level=info msg="Loading configuration file" path=.pint.hcl
{
"name": ".+",
"keep": [
"BAR"
"BAR",
"KEY=VAL"
]
}
]
Expand All @@ -86,6 +88,6 @@ rule {
kind = "recording"
}
aggregate ".+" {
keep = [ "${ENV_FOO}" ]
keep = [ "${ENV_FOO}", "${ENV_FAKE_ENV}" ]
}
}
26 changes: 26 additions & 0 deletions cmd/pint/tests/0114_config_env_expand_error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
env FOO=BAR
pint.error --no-color config
! stdout .
cmp stderr stderr.txt

-- stderr.txt --
level=info msg="Loading configuration file" path=.pint.hcl
level=fatal msg="Fatal error" error="failed to load config file \".pint.hcl\": .pint.hcl:7,17-29: Unknown variable; There is no variable named \"ENV_AUTH_KEY\"., and 1 other diagnostic(s)"
-- .pint.hcl --
parser {
relaxed = [".*"]
}
prometheus "prod" {
uri = "http://localhost"
headers = {
X-Auth = "${ENV_AUTH_KEY}"
}
}
rule {
match {
kind = "recording"
}
aggregate ".+" {
keep = [ "${ENV_FOO}" ]
}
}
4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ func (cfg *Config) GetChecksForRule(ctx context.Context, path string, r parser.R
func getContext() *hcl.EvalContext {
vars := map[string]cty.Value{}
for _, e := range os.Environ() {
if i := strings.Index(e, "="); i >= 0 {
vars[fmt.Sprintf("ENV_%s", e[:i])] = cty.StringVal(e[i+1:])
if k, v, ok := strings.Cut(e, "="); ok {
vars[fmt.Sprintf("ENV_%s", k)] = cty.StringVal(v)
}
}
return &hcl.EvalContext{Variables: vars}
Expand Down

0 comments on commit 083787e

Please sign in to comment.