Skip to content

Commit

Permalink
Fix bug where lines where truncated by _get_sections_for_lines
Browse files Browse the repository at this point in the history
  • Loading branch information
ypid committed Oct 22, 2016
1 parent 5e9c8dd commit bdb4cd6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 23 deletions.
74 changes: 58 additions & 16 deletions tests/test_reformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,48 @@ def test_get_sections_for_lines_trailing_section_comments_unclosed_fold(self):
self.r._sections,
)

def test_get_sections_for_lines_mixed(self):
self.r._lines = textwrap.dedent("""
# Network accessibility [[[
# -------------------------
# .. envvar:: role_name__interfaces [[[
#
# List of network interfaces from which to allow access to Apt-Cacher NG.
# If not specified, allows access from all interfaces.
role_name__interfaces: []
# ]]]
# .. envvar:: apt_cacher_ng__allow
#
# Allow access to Apt-Cacher NG from specified IP addresses or CIDR networks.
# If not specified, allows access from all networks.
role_name__allow: []
# ]]]
""").strip().split('\n')
_, self.r._sections = self.r._get_sections_for_lines(self.r._lines, 0)

pprint.pprint(self.r._sections)
assert_equal(
[{'fold_name': 'Network accessibility',
'lines': ['# -------------------------'],
'subsections': [{'fold_name': '.. envvar:: role_name__interfaces',
'lines': ['#',
'# List of network interfaces from which to '
'allow access to Apt-Cacher NG.',
'# If not specified, allows access from all '
'interfaces.',
'role_name__interfaces: []']},
{'lines': ['# .. envvar:: apt_cacher_ng__allow',
'#',
'# Allow access to Apt-Cacher NG from '
'specified IP addresses or CIDR networks.',
'# If not specified, allows access from all '
'networks.',
'role_name__allow: []']}]}],
self.r._sections,
)

def test_reformat_variables_single(self):
self.r._sections = [
{
Expand Down Expand Up @@ -2447,40 +2489,40 @@ def test_reformat_variables_mixed(self):
{'fold_name': 'Network accessibility',
'lines': ['# -------------------------',
'',
'# .. envvar:: apt_cacher_ng__allow',
'# .. envvar:: role_name__allow',
'#',
'# Allow access to Apt-Cacher NG from specified IP addresses '
'or CIDR networks.',
'# If not specified, allows access from all networks.',
'apt_cacher_ng__allow: []'],
'subsections': [{'fold_name': '.. envvar:: apt_cacher_ng__interfaces',
'role_name__allow: []'],
'subsections': [{'fold_name': '.. envvar:: role_name__interfaces',
'lines': ['#',
'# List of network interfaces from which to '
'allow access to Apt-Cacher NG.',
'# If not specified, allows access from all '
'interfaces.',
'apt_cacher_ng__interfaces: []']}]},
'role_name__interfaces: []']}]},
]
self.r._reformat_variables(self.r._sections)

pprint.pprint(self.r._sections)
assert_equal(
[{'fold_name': 'Network accessibility',
'lines': ['# -------------------------', ''],
'subsections': [{'fold_name': '.. envvar:: apt_cacher_ng__allow',
'subsections': [{'fold_name': '.. envvar:: role_name__allow',
'lines': ['#',
'# Allow access to Apt-Cacher NG from '
'specified IP addresses or CIDR networks.',
'# If not specified, allows access from all '
'networks.',
'apt_cacher_ng__allow: []']},
{'fold_name': '.. envvar:: apt_cacher_ng__interfaces',
'role_name__allow: []']},
{'fold_name': '.. envvar:: role_name__interfaces',
'lines': ['#',
'# List of network interfaces from which to '
'allow access to Apt-Cacher NG.',
'# If not specified, allows access from all '
'interfaces.',
'apt_cacher_ng__interfaces: []']}]}],
'role_name__interfaces: []']}]}],
self.r._sections,
)

Expand All @@ -2489,17 +2531,17 @@ def test_reformat_mixed_section(self):
# Network accessibility [[[
# -------------------------
# .. envvar:: apt_cacher_ng__allow
# .. envvar:: role_name__allow
#
# Allow access to Apt-Cacher NG from specified IP addresses or CIDR networks.
# If not specified, allows access from all networks.
apt_cacher_ng__allow: []
role_name__allow: []
# .. envvar:: apt_cacher_ng__interfaces [[[
# .. envvar:: role_name__interfaces [[[
#
# List of network interfaces from which to allow access to Apt-Cacher NG.
# If not specified, allows access from all interfaces.
apt_cacher_ng__interfaces: []
role_name__interfaces: []
# ]]]
# ]]]
""").strip().split('\n')
Expand All @@ -2520,18 +2562,18 @@ def test_reformat_mixed_section(self):
# Network accessibility [[[
# -------------------------
# .. envvar:: apt_cacher_ng__allow [[[
# .. envvar:: role_name__allow [[[
#
# Allow access to Apt-Cacher NG from specified IP addresses or CIDR networks.
# If not specified, allows access from all networks.
apt_cacher_ng__allow: []
role_name__allow: []
# ]]]
# .. envvar:: apt_cacher_ng__interfaces [[[
# .. envvar:: role_name__interfaces [[[
#
# List of network interfaces from which to allow access to Apt-Cacher NG.
# If not specified, allows access from all interfaces.
apt_cacher_ng__interfaces: []
role_name__interfaces: []
# ]]]
# ]]]
# ]]]
Expand Down
16 changes: 9 additions & 7 deletions yaml4rst/reformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,19 @@ def _get_sections_for_lines(self, input_lines, ind, section_lines=None, inside_f
line = input_lines[ind]
fold = self._get_line_fold(line)

if fold['change'] != 0 or not line.startswith('#'):
if fold['change'] != 0:
break

subsections.append({
'lines': input_lines[start_ind:ind],
'lines': strip_list(input_lines[start_ind:ind]),
})
continue

if fold['change'] != 0:
ind -= 1

continue # pragma: no cover
# Seems to be a known issue that coverage.py might fail with that:
# https://bitbucket.org/ned/coveragepy/issues/496/incorrect-coverage-with-branching-and

section_lines.append(line)
LOG.debug('section_lines: {}'.format(section_lines))
Expand Down Expand Up @@ -886,9 +892,5 @@ def _reformat_variables(self, sections):
LOG.debug("Splitting section into two sections. Moving variable start into new section.")
section.pop('subsections', None)

if LOG.isEnabledFor(logging.DEBUG):
LOG.debug('sections:\n{}'.format(
pprint.pformat(sections),
))
if 'subsections' in section:
self._reformat_variables(section['subsections'])

0 comments on commit bdb4cd6

Please sign in to comment.