Skip to content

Commit

Permalink
fix #235 - do not fail if env sh declared but not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
m.kindritskiy committed Jun 25, 2024
1 parent 0edc10b commit f83dd6b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 20 deletions.
40 changes: 36 additions & 4 deletions config/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,44 @@ func (e *Envs) UnmarshalYAML(node *yaml.Node) error {
keyNode := node.Content[i]
valueNode := node.Content[i+1]

var env Env
if err := valueNode.Decode(&env); err != nil {
envAsStr := ""

if err := valueNode.Decode(&envAsStr); err == nil {
e.Set(keyNode.Value, Env{Name: keyNode.Value, Value: envAsStr})
continue
}

envAsMap := struct {
Sh *string
Checksum *Checksum
}{}

if err := valueNode.Decode(&envAsMap); err != nil {
return err
}
env.Name = keyNode.Value
e.Set(keyNode.Value, env)

env := Env{
Name: keyNode.Value,
Value: "",
Sh: "",
Checksum: Checksum{},
}

if envAsMap.Sh == nil && envAsMap.Checksum == nil {
return fmt.Errorf("lets: environment variable '%s' must have value or 'sh' or 'checksum'", keyNode.Value)
}

if envAsMap.Sh != nil && envAsMap.Checksum != nil {
return fmt.Errorf("lets: environment variable '%s' must have only 'sh' or 'checksum'", keyNode.Value)
}

if envAsMap.Sh != nil {
env.Sh = *envAsMap.Sh
} else if envAsMap.Checksum != nil {
env.Checksum = *envAsMap.Checksum
}

e.Set(env.Name, env)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func (e *ExecuteError) ExitCode() int {
}

type Executor struct {
cfg *config.Config
out io.Writer
cfg *config.Config
out io.Writer
initCalled bool
}

Expand Down
13 changes: 7 additions & 6 deletions tests/global_env.bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ setup() {
@test "global_env: should provide env to command" {
run lets global-env
assert_success
assert_line --index 0 "ONE=1"
assert_line --index 1 "TWO=two"
assert_line --index 2 "THREE=3"
assert_line --index 3 "FOUR=4"
assert_line --index 4 "BAR=Bar"
assert_line --index 5 "FOO=bb1da47569d9fbe3b5f2216fdbd4c9b040ccb5c1"
assert_line --index 0 "INT=1"
assert_line --index 1 "STR=hi"
assert_line --index 2 "STR_INT=1"
assert_line --index 3 "BOOL=true"
assert_line --index 4 "ORIGINAL=b"
assert_line --index 5 "BAR=Bar"
assert_line --index 6 "FOO=bb1da47569d9fbe3b5f2216fdbd4c9b040ccb5c1"
}
19 changes: 11 additions & 8 deletions tests/global_env/lets.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
shell: bash

env:
ONE: 1
TWO: two
THREE: "3"
INT: 1
STR: "hi"
STR_INT: "1"
BOOL: true
ORIGINAL: "a"
BAR:
sh: echo Bar
FOO:
Expand All @@ -13,11 +15,12 @@ commands:
global-env:
description: Test global env
env:
FOUR: "4"
ORIGINAL: "b"
cmd: |
echo ONE=${ONE}
echo TWO=${TWO}
echo THREE=${THREE}
echo FOUR=${FOUR}
echo INT=${INT}
echo STR=${STR}
echo STR_INT=${STR_INT}
echo BOOL=${BOOL}
echo ORIGINAL=${ORIGINAL}
echo BAR=${BAR}
echo FOO=${FOO}

0 comments on commit f83dd6b

Please sign in to comment.