Skip to content

Commit 22ee56e

Browse files
authored
Merge pull request #246 from lets-cli/fix-env-override
fix env (-E) override
2 parents 10fc44a + cf49331 commit 22ee56e

5 files changed

Lines changed: 53 additions & 41 deletions

File tree

cmd/subcommand.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ func isHidden(cmdName string, showAll bool) bool {
156156
// newSubcommand creates new cobra root subcommand from config.Command.
157157
func newSubcommand(command *config.Command, conf *config.Config, showAll bool, out io.Writer) *cobra.Command {
158158
subCmd := &cobra.Command{
159-
Use: command.Name,
160-
Short: short(command.Description),
159+
Use: command.Name,
160+
Short: short(command.Description),
161161
Hidden: isHidden(command.Name, showAll),
162162
RunE: func(cmd *cobra.Command, args []string) error {
163163
command.Args = append(command.Args, prepareArgs(command.Name, os.Args)...)

config/config/command.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ func (c *Command) UnmarshalYAML(unmarshal func(interface{}) error) error {
7878
c.Cmds = cmd.Cmd
7979
c.Description = cmd.Description
8080
c.Env = cmd.Env
81+
if c.Env == nil {
82+
c.Env = &Envs{}
83+
}
8184

8285
// support for deprecated eval_env
8386
if !cmd.EvalEnv.Empty() {

tests/override_env.bats

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
load test_helpers
2+
3+
setup() {
4+
load "${BATS_UTILS_PATH}/bats-support/load.bash"
5+
load "${BATS_UTILS_PATH}/bats-assert/load.bash"
6+
cd ./tests/override_env
7+
}
8+
9+
@test "override_env: should use default global env for running command" {
10+
run lets say_hello_global_env
11+
assert_success
12+
assert_line --index 0 "Hello John"
13+
}
14+
15+
@test "override_env: should use default command env for running command" {
16+
run lets say_hello_command_env
17+
assert_success
18+
assert_line --index 0 "Hello Rick"
19+
}
20+
21+
@test "override_env: should override global env for running command with -E" {
22+
run lets -E NAME=Morty say_hello_global_env
23+
assert_success
24+
assert_line --index 0 "Hello Morty"
25+
}
26+
27+
@test "override_env: should override command env for running command with -E" {
28+
run lets -E NAME=Morty say_hello_command_env
29+
assert_success
30+
assert_line --index 0 "Hello Morty"
31+
}
32+
33+
@test "override_env: should override command env for running command with --env" {
34+
run lets --env NAME=Morty say_hello_command_env
35+
assert_success
36+
assert_line --index 0 "Hello Morty"
37+
}
38+
39+
@test "override_env: should set env variable for command with -E even if there is no either global or command env var" {
40+
run lets -E FOO=BAR print-foo
41+
assert_success
42+
assert_line --index 0 "FOO=BAR"
43+
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ env:
44
NAME: "John"
55

66
commands:
7-
say_hello_global:
7+
say_hello_global_env:
88
description: Say hello
99
cmd: echo "Hello ${NAME}"
1010

11-
say_hello:
11+
say_hello_command_env:
1212
description: Say hello
1313
env:
1414
NAME: Rick
@@ -17,3 +17,6 @@ commands:
1717
say_command:
1818
description: Say command name
1919
cmd: echo $LETS_COMMAND_NAME
20+
21+
print-foo:
22+
cmd: echo FOO=${FOO}

tests/set_env.bats

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)