Skip to content

Commit efe753a

Browse files
authored
Merge pull request #682 from bashly-framework/fix/local-var-leakage
Fix local variables leaking from internal functions
2 parents d9c0812 + 97d50ca commit efe753a

File tree

10 files changed

+63
-2
lines changed

10 files changed

+63
-2
lines changed

lib/bashly/views/command/inspect_args.gtx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
= view_marker
22

33
> inspect_args() {
4+
> local k
5+
>
46
> if ((${#args[@]})); then
57
> readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
68
> echo args:
@@ -17,8 +19,8 @@ if catch_all_used_anywhere?
1719
> echo
1820
> echo other_args:
1921
> echo "- \${other_args[*]} = ${other_args[*]}"
20-
> for i in "${!other_args[@]}"; do
21-
> echo "- \${other_args[$i]} = ${other_args[$i]}"
22+
> for k in "${!other_args[@]}"; do
23+
> echo "- \${other_args[$k]} = ${other_args[$k]}"
2224
> done
2325
> fi
2426
>

lib/bashly/views/command/parse_requirements.gtx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ if root_command?
55
else
66
> {{ function_name }}_parse_requirements() {
77
end
8+
> local key
9+
>
810

911
= render(:fixed_flags_filter).indent 2
1012
= render(:environment_variables_filter).indent 2
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
+ bundle exec bashly generate
2+
creating user files in src
3+
skipped src/one_command.sh (exists)
4+
skipped src/two_command.sh (exists)
5+
skipped src/noop_command.sh (exists)
6+
created ./cli
7+
run ./cli --help to test your bash script
8+
+ ./cli 1
9+
Before key=THIS IS MY VALUE
10+
noop called
11+
After key=THIS IS MY VALUE
12+
+ ./cli 2
13+
Before k=THIS IS MY VALUE
14+
args:
15+
- ${args[--user]} = admin
16+
After k=THIS IS MY VALUE
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cli
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This fixture tests that variables that are supposed to be local, internal to
2+
bashly, are indeed defined as local and do not leak to the global scope.
3+
4+
Reference issue: https://github.com/bashly-framework/bashly/issues/681
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: cli
2+
help: Testing that bashly-defined variables do not leak to the global scope
3+
version: 0.1.0
4+
5+
commands:
6+
- name: one
7+
alias: "1"
8+
help: Testing 'key' leakage when calling 'run' internally
9+
10+
- name: two
11+
alias: "2"
12+
help: Testing 'k' leak when calling 'inspect_args'
13+
flags:
14+
- long: --user
15+
arg: name
16+
default: admin
17+
18+
- name: noop
19+
help: Does nothing except declare it was called
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
echo "noop called"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
key="THIS IS MY VALUE"
2+
echo "Before key=$key"
3+
run noop
4+
echo "After key=$key"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
k="THIS IS MY VALUE"
2+
echo "Before k=$k"
3+
inspect_args
4+
echo "After k=$k"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
set -x
4+
5+
bundle exec bashly generate
6+
7+
./cli 1
8+
./cli 2

0 commit comments

Comments
 (0)