Skip to content

Commit

Permalink
Fix legacy_rst_sections detection when heading directly follows comment
Browse files Browse the repository at this point in the history
  • Loading branch information
ypid committed Oct 29, 2016
1 parent c80ae13 commit 4246bd1
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 14 deletions.
69 changes: 69 additions & 0 deletions tests/input_files/debops.swapfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
# Default variables
# =================

# .. contents:: Sections
# :local:
#
# ---------------------------
# Swap file configuration
# ---------------------------

# .. envvar:: swapfile__size
#
# Default size of swap files, in MB.
swapfile__size: '{{ ((ansible_memtotal_mb|int * 2)
if (ansible_memtotal_mb|int <= 2048)
else "512") }}'


# .. envvar:: swapfile__priority
#
# Default swap file priority, from ``-1`` to ``32767``.
swapfile__priority: '-1'


# .. envvar:: swapfile__use_dd
#
# By default ``debops.swapfile`` uses the ``fallocate`` command to create the swap
# files. If you use unsupported filesystems (``ext4`` is supported) you can
# enable this variable to use ``dd`` command to create the files.
swapfile__use_dd: False


# .. envvar:: swapfile__files
#
# List of swap files. Specify either a path to a file on a filesystem, or
# a YAML dict. See :ref:`swapfile__files` for more details.
swapfile__files: [ '/swapfile' ]


# ---------------------
# Kernel parameters
# ---------------------

# .. envvar:: swapfile__swappiness
#
# Set ``vm.swappiness`` kernel parameter.
swapfile__swappiness: '60'


# .. envvar:: swapfile__cache_pressure
#
# Set ``vm.vfs_cache_pressure`` kernel parameter.
swapfile__cache_pressure: '100'


# .. envvar:: swapfile__sysctl_file
#
# Name of the file with kernel parameters.
swapfile__sysctl_file: '/etc/sysctl.d/30-debops.swapfile.conf'


# .. envvar:: swapfile__sysctl_map
#
# YAML dictionary with kernel parameters to set. Change it to an empty dict
# to disable the kernel configuration.
swapfile__sysctl_map:
'vm.swappiness': '{{ swapfile__swappiness }}'
'vm.vfs_cache_pressure': '{{ swapfile__cache_pressure }}'
5 changes: 3 additions & 2 deletions tests/output_files/debops.apt_install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
# .. include:: includes/all.rst


# Role configuration [[[
# ----------------------

# ]]]
# .. envvar:: apt_install__enabled [[[
#
# Enable or disable support for ``debops.apt_install`` role.
Expand Down Expand Up @@ -102,7 +102,7 @@ apt_install__cache_valid_time: '{{ ansible_local.core.cache_valid_time
if (ansible_local|d() and ansible_local.core|d() and
ansible_local.core.cache_valid_time)
else (60 * 60 * 24 * 7) }}'

# ]]]
# ]]]
# APT package lists [[[
# ---------------------
Expand Down Expand Up @@ -311,3 +311,4 @@ apt_install__apt_preferences__dependent_list:
by_role: 'debops.apt_install'
# ]]]
# ]]]
# ]]]
75 changes: 75 additions & 0 deletions tests/output_files/debops.swapfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
# .. vim: foldmarker=[[[,]]]:foldmethod=marker

# debops.swapfile default variables [[[
# =====================================

# .. contents:: Sections
# :local:
#
# .. include:: includes/all.rst


# Swap file configuration [[[
# ---------------------------

# .. envvar:: swapfile__size [[[
#
# Default size of swap files, in MB.
swapfile__size: '{{ ((ansible_memtotal_mb|int * 2)
if (ansible_memtotal_mb|int <= 2048)
else "512") }}'

# ]]]
# .. envvar:: swapfile__priority [[[
#
# Default swap file priority, from ``-1`` to ``32767``.
swapfile__priority: '-1'

# ]]]
# .. envvar:: swapfile__use_dd [[[
#
# By default ``debops.swapfile`` uses the ``fallocate`` command to create the swap
# files. If you use unsupported filesystems (``ext4`` is supported) you can
# enable this variable to use ``dd`` command to create the files.
swapfile__use_dd: False

# ]]]
# .. envvar:: swapfile__files [[[
#
# List of swap files. Specify either a path to a file on a filesystem, or
# a YAML dict. See :ref:`swapfile__files` for more details.
swapfile__files: [ '/swapfile' ]
# ]]]
# ]]]
# Kernel parameters [[[
# ---------------------

# .. envvar:: swapfile__swappiness [[[
#
# Set ``vm.swappiness`` kernel parameter.
swapfile__swappiness: '60'

# ]]]
# .. envvar:: swapfile__cache_pressure [[[
#
# Set ``vm.vfs_cache_pressure`` kernel parameter.
swapfile__cache_pressure: '100'

# ]]]
# .. envvar:: swapfile__sysctl_file [[[
#
# Name of the file with kernel parameters.
swapfile__sysctl_file: '/etc/sysctl.d/30-debops.swapfile.conf'

# ]]]
# .. envvar:: swapfile__sysctl_map [[[
#
# YAML dictionary with kernel parameters to set. Change it to an empty dict
# to disable the kernel configuration.
swapfile__sysctl_map:
'vm.swappiness': '{{ swapfile__swappiness }}'
'vm.vfs_cache_pressure': '{{ swapfile__cache_pressure }}'
# ]]]
# ]]]
# ]]]
97 changes: 97 additions & 0 deletions tests/test_reformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,58 @@ def test_update_header_empty(self):
print('#######\n' + self.r.get_content() + '\n#######')
assert_equal(expected_lines, self.r._lines)

def test_update_header_no_empty_line_to_content(self):
self.r._lines = textwrap.dedent("""
---
# Default variables
# =================
# .. contents:: Sections
# :local:
#
# ---------------------------
# Swap file configuration
# ---------------------------
# .. envvar:: swapfile__size
#
# Default size of swap files, in MB.
swapfile__size: '{{ ((ansible_memtotal_mb|int * 2)
if (ansible_memtotal_mb|int <= 2048)
else "512") }}'
""").strip().split('\n')
self.r._update_header()

print('#######\n' + self.r.get_content() + '\n#######')
assert_equal(
textwrap.dedent("""
---
# .. vim: foldmarker=[[[,]]]:foldmethod=marker
# role_owner.role_name default variables [[[
# ==========================================
# .. contents:: Sections
# :local:
#
# .. include:: includes/all.rst
# ---------------------------
# Swap file configuration
# ---------------------------
# .. envvar:: swapfile__size
#
# Default size of swap files, in MB.
swapfile__size: '{{ ((ansible_memtotal_mb|int * 2)
if (ansible_memtotal_mb|int <= 2048)
else "512") }}'
# ]]]
""").strip().split('\n'),
self.r._lines,
)

def test_get_rendered_template_missing_vars(self):
self.r = YamlRstReformatter()
assert_raises_regexp(
Expand Down Expand Up @@ -1783,6 +1835,51 @@ def test_reformat_legacy_rst_sections_recursive(self):
self.r._lines,
)

def test_reformat_legacy_rst_sections_no_empty_line_to_content(self):
self.r._sections = [
{
"lines": textwrap.dedent("""
---
# Default variables
# =================
# .. contents:: Sections
# :local:
#
# ---------------------------
# Swap file configuration
# ---------------------------
# .. envvar:: swapfile__size
#
# Default size of swap files, in MB.
swapfile__size: '{{ ((ansible_memtotal_mb|int * 2)
if (ansible_memtotal_mb|int <= 2048)
else "512") }}'
""").strip().split('\n'),
},
]
self.r._reformat_legacy_rst_sections(self.r._sections)

pprint.pprint(self.r._sections)
assert_equal(
[{'lines': ['---',
'# Default variables',
'# =================',
'',
'# .. contents:: Sections',
'# :local:']},
{'fold_name': 'Swap file configuration',
'lines': ['# ---------------------------',
'',
'# .. envvar:: swapfile__size',
'#',
'# Default size of swap files, in MB.',
"swapfile__size: '{{ ((ansible_memtotal_mb|int * 2)",
' if (ansible_memtotal_mb|int <= 2048)',
' else "512") }}\'']}],
self.r._sections)

@log_capture(level=logging.WARNING)
def test_add_fixmes(self, log):
self.r = YamlRstReformatter(
Expand Down
21 changes: 9 additions & 12 deletions yaml4rst/reformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ def _reformat_legacy_rst_sections(self, sections):

eof = ind+1 not in range(len(section['lines']))
if len(heading_char_inds) >= 1 and heading is not None and (eof or state == 'before'):
section_start = min([heading_ind] + heading_char_inds[1:])
LOG.debug('Deleting lines at: {}'.format([heading_ind] + heading_char_inds[1:]))
ind_offset = 0
for del_ind in [heading_ind] + heading_char_inds[1:]:
Expand All @@ -662,18 +663,12 @@ def _reformat_legacy_rst_sections(self, sections):

priv_fold_name = section.get('fold_name', None)

last_section_end = get_last_index(
section['lines'][:ind],
[''],
fallback=ind,
)

if last_section_end is None or ind < 2:
if section_start is None or ind < 2:
section['fold_name'] = heading
else:
LOG.debug('Creating new fold')
new_section = {
'lines': section['lines'][last_section_end + 1:],
'lines': strip_list(section['lines'][section_start - 1:]),
'fold_name': heading,
}
section_level = self._get_section_level(heading_char)
Expand All @@ -682,10 +677,12 @@ def _reformat_legacy_rst_sections(self, sections):
if 'subsections' in section:
new_section['subsections'] = section['subsections']
del section['subsections']
LOG.debug('section start: {}'.format(last_section_end))
LOG.debug('section level: {}'.format(section_level))
LOG.debug('new section: {}'.format(new_section))
section['lines'] = section['lines'][:last_section_end]
if LOG.isEnabledFor(logging.DEBUG):
LOG.debug('section start: {}'.format(section_start))
LOG.debug('section level: {}'.format(section_level))
LOG.debug('sec_ind: {}'.format(sec_ind))
LOG.debug("new section: \n{}".format(pprint.pformat(new_section)))
section['lines'] = section['lines'][:section_start - 1]
sections.insert(sec_ind + 1, new_section)

if (priv_fold_name is not None and
Expand Down

0 comments on commit 4246bd1

Please sign in to comment.