Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle empty sections when using configure set command #1806

Merged
merged 1 commit into from
Feb 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions awscli/customizations/configure/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,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 @@ -131,13 +137,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
Original file line number Diff line number Diff line change
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
16 changes: 15 additions & 1 deletion tests/unit/customizations/configure/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,18 @@ def test_update_nested_attr_no_prior_nesting(self):
's3 =\n'
' signature_version = newval\n'
'[profile foo]\n'
'foo = bar\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'
)