Skip to content

Commit

Permalink
Handle empty sections when using configure set command
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesls committed Feb 19, 2016
1 parent bde11c6 commit 2fe5785
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
17 changes: 8 additions & 9 deletions awscli/customizations/configure/__init__.py
Expand Up @@ -162,11 +162,17 @@ def _update_section_contents(self, contents, section_name, new_values):
# to figure out if we're updating a value or adding a new value.
# There's 2 cases. Either we're setting a normal scalar value
# of, we're setting a nested value.
section_start_line_num += 1
last_matching_line = section_start_line_num
j = section_start_line_num
j = last_matching_line + 1
while j < len(contents):
line = contents[j]
if self.SECTION_REGEX.search(line) is not None:
# We've hit a new section which means the config key is
# not in the section. We need to add it here.
self._insert_new_values(line_number=last_matching_line,
contents=contents,
new_values=new_values)
return
match = self.OPTION_REGEX.search(line)
if match is not None:
last_matching_line = j
Expand All @@ -185,13 +191,6 @@ def _update_section_contents(self, contents, section_name, new_values):
j, contents, new_values[key_name],
len(match.group(1)) - len(match.group(1).lstrip()))
return
elif self.SECTION_REGEX.search(line) is not None:
# We've hit a new section which means the config key is
# not in the section. We need to add it here.
self._insert_new_values(line_number=last_matching_line,
contents=contents,
new_values=new_values)
return
j += 1

if new_values:
Expand Down
16 changes: 16 additions & 0 deletions tests/integration/customizations/test_configure.py
Expand Up @@ -298,6 +298,22 @@ def test_get_nested_attribute(self):
self.assertEqual(p.rc, 1)
self.assertEqual(p.stdout, '')

def test_can_handle_empty_section(self):
self.set_config_file_contents(
'[default]\n'
)
p = aws('configure set preview.cloudfront true',
env_vars=self.env_vars)
p = aws('configure set region us-west-2',
env_vars=self.env_vars)
self.assertEqual(
'[default]\n'
'region = us-west-2\n'
'[preview]\n'
'cloudfront = true\n',
self.get_config_file_contents(),
)


class TestConfigureHasArgTable(unittest.TestCase):
def test_configure_command_has_arg_table(self):
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/customizations/configure/test_configure.py
Expand Up @@ -596,6 +596,20 @@ def test_update_nested_attr_no_prior_nesting(self):
'[profile foo]\n'
'foo = bar\n')

def test_can_handle_empty_section(self):
original = (
'[default]\n'
'[preview]\n'
'cloudfront = true\n'
)
self.assert_update_config(
original, {'region': 'us-west-2', '__section__': 'default'},
'[default]\n'
'region = us-west-2\n'
'[preview]\n'
'cloudfront = true\n'
)


class TestConfigureListCommand(unittest.TestCase):

Expand Down

0 comments on commit 2fe5785

Please sign in to comment.