Forklift nova-specs test_titles.py
* Reports which sections are missing/extra * Checks line wrapping * Checks for literal carriage returns * Checks for trailing spaces * Handles per-release spec templates Change-Id: I5fdf2d42353dee737d0551013536adeaff6ed69a
This commit is contained in:
parent
30d6b49f1c
commit
229a556c72
|
@ -11,32 +11,12 @@
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import glob
|
import glob
|
||||||
|
import re
|
||||||
|
|
||||||
import docutils.core
|
import docutils.core
|
||||||
import testtools
|
import testtools
|
||||||
|
|
||||||
|
|
||||||
TITLES = {
|
|
||||||
'Problem Description': [],
|
|
||||||
'Proposed Change': [
|
|
||||||
'Alternatives',
|
|
||||||
'Security Impact',
|
|
||||||
'Other End User Impact',
|
|
||||||
'Performance Impact',
|
|
||||||
'Other Deployer Impact',
|
|
||||||
'Developer Impact',
|
|
||||||
],
|
|
||||||
'Implementation': [
|
|
||||||
'Assignee(s)',
|
|
||||||
'Work Items',
|
|
||||||
],
|
|
||||||
'Dependencies': [],
|
|
||||||
'Testing': [],
|
|
||||||
'Documentation Impact': [],
|
|
||||||
'References': [],
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class TestTitles(testtools.TestCase):
|
class TestTitles(testtools.TestCase):
|
||||||
def _get_title(self, section_tree):
|
def _get_title(self, section_tree):
|
||||||
section = {
|
section = {
|
||||||
|
@ -46,7 +26,6 @@ class TestTitles(testtools.TestCase):
|
||||||
if node.tagname == 'title':
|
if node.tagname == 'title':
|
||||||
section['name'] = node.rawsource
|
section['name'] = node.rawsource
|
||||||
elif node.tagname == 'section':
|
elif node.tagname == 'section':
|
||||||
# Note subsection subtitles are thrown away
|
|
||||||
subsection = self._get_title(node)
|
subsection = self._get_title(node)
|
||||||
section['subtitles'].append(subsection['name'])
|
section['subtitles'].append(subsection['name'])
|
||||||
return section
|
return section
|
||||||
|
@ -55,27 +34,75 @@ class TestTitles(testtools.TestCase):
|
||||||
titles = {}
|
titles = {}
|
||||||
for node in spec:
|
for node in spec:
|
||||||
if node.tagname == 'section':
|
if node.tagname == 'section':
|
||||||
|
# Note subsection subtitles are thrown away
|
||||||
section = self._get_title(node)
|
section = self._get_title(node)
|
||||||
titles[section['name']] = section['subtitles']
|
titles[section['name']] = section['subtitles']
|
||||||
return titles
|
return titles
|
||||||
|
|
||||||
def _check_titles(self, titles):
|
def _check_titles(self, filename, expect, actual):
|
||||||
self.assertEqual(TITLES, titles)
|
missing_sections = [x for x in expect.keys() if x not in actual.keys()]
|
||||||
|
extra_sections = [x for x in actual.keys() if x not in expect.keys()]
|
||||||
|
|
||||||
def _run_template_tests(self, release_name):
|
msgs = []
|
||||||
files = ['specs/%s-template.rst' % release_name] + \
|
if len(missing_sections) > 0:
|
||||||
glob.glob('specs/%s/*' % release_name)
|
msgs.append("Missing sections: %s" % missing_sections)
|
||||||
for filename in files:
|
if len(extra_sections) > 0:
|
||||||
self.assertTrue(filename.endswith(".rst"),
|
msgs.append("Extra sections: %s" % extra_sections)
|
||||||
"specs file must uses 'rst' extension.")
|
|
||||||
with open(filename) as f:
|
|
||||||
data = f.read()
|
|
||||||
spec = docutils.core.publish_doctree(data)
|
|
||||||
titles = self._get_titles(spec)
|
|
||||||
self._check_titles(titles)
|
|
||||||
|
|
||||||
def test_juno_templates(self):
|
for section in expect.keys():
|
||||||
self._run_template_tests('juno')
|
missing_subsections = [x for x in expect[section]
|
||||||
|
if x not in actual[section]]
|
||||||
|
# extra subsections are allowed
|
||||||
|
if len(missing_subsections) > 0:
|
||||||
|
msgs.append("Section '%s' is missing subsections: %s"
|
||||||
|
% (section, missing_subsections))
|
||||||
|
|
||||||
def test_kilo_templates(self):
|
if len(msgs) > 0:
|
||||||
self._run_template_tests('kilo')
|
self.fail("While checking '%s':\n %s"
|
||||||
|
% (filename, "\n ".join(msgs)))
|
||||||
|
|
||||||
|
def _check_lines_wrapping(self, tpl, raw):
|
||||||
|
for i, line in enumerate(raw.split("\n")):
|
||||||
|
if "http://" in line or "https://" in line:
|
||||||
|
continue
|
||||||
|
self.assertTrue(
|
||||||
|
len(line) < 80,
|
||||||
|
msg="%s:%d: Line limited to a maximum of 79 characters." %
|
||||||
|
(tpl, i+1))
|
||||||
|
|
||||||
|
def _check_no_cr(self, tpl, raw):
|
||||||
|
matches = re.findall('\r', raw)
|
||||||
|
self.assertEqual(
|
||||||
|
len(matches), 0,
|
||||||
|
"Found %s literal carriage returns in file %s" %
|
||||||
|
(len(matches), tpl))
|
||||||
|
|
||||||
|
|
||||||
|
def _check_trailing_spaces(self, tpl, raw):
|
||||||
|
for i, line in enumerate(raw.split("\n")):
|
||||||
|
trailing_spaces = re.findall(" +$", line)
|
||||||
|
self.assertEqual(len(trailing_spaces),0,
|
||||||
|
"Found trailing spaces on line %s of %s" % (i+1, tpl))
|
||||||
|
|
||||||
|
|
||||||
|
def test_template(self):
|
||||||
|
releases = [x.split('/')[1] for x in glob.glob('specs/*/')]
|
||||||
|
for release in releases:
|
||||||
|
with open("specs/%s-template.rst" % release) as f:
|
||||||
|
template = f.read()
|
||||||
|
spec = docutils.core.publish_doctree(template)
|
||||||
|
template_titles = self._get_titles(spec)
|
||||||
|
|
||||||
|
files = glob.glob("specs/%s/*/*" % release)
|
||||||
|
for filename in files:
|
||||||
|
self.assertTrue(filename.endswith(".rst"),
|
||||||
|
"spec filenames must use 'rst' extension.")
|
||||||
|
with open(filename) as f:
|
||||||
|
data = f.read()
|
||||||
|
|
||||||
|
spec = docutils.core.publish_doctree(data)
|
||||||
|
titles = self._get_titles(spec)
|
||||||
|
self._check_titles(filename, template_titles, titles)
|
||||||
|
self._check_lines_wrapping(filename, data)
|
||||||
|
self._check_no_cr(filename, data)
|
||||||
|
self._check_trailing_spaces(filename, data)
|
||||||
|
|
Loading…
Reference in New Issue