Use jsoncheck in doctest

This patch makes doctest use jsoncheck.

It offers:
- more helpful syntax error messages (courtesy of demjson)
- additional niceness checks for standardized formatting

Change-Id: Id92c43daeb6ee17ed03052b3c9b92cbf60f3f4a5
blueprint modularize-doctest
This commit is contained in:
Roger Luethi 2014-08-16 11:19:39 +02:00
parent 3fd52a5a0b
commit 1192da60ee
3 changed files with 24 additions and 6 deletions

View File

@ -31,7 +31,6 @@ Requires:
'''
import gzip
import json
import multiprocessing
import operator
import os
@ -47,6 +46,7 @@ from oslo.config import cfg
import os_doc_tools
from os_doc_tools.common import check_output # noqa
from os_doc_tools import jsoncheck
from os_doc_tools.openstack.common import log
@ -486,6 +486,15 @@ def check_deleted_files(rootdir, file_exceptions, verbose):
return 0
def indent(text, spaces):
"""Indent each line of a string by given number of spaces.
The text argument is passed through str() first to turn objects such
as exceptions into strings.
"""
return ''.join((spaces * ' ') + i for i in str(text).splitlines(True))
def validate_one_json_file(rootdir, path, verbose, check_syntax,
check_niceness):
"""Validate a single JSON file."""
@ -496,12 +505,20 @@ def validate_one_json_file(rootdir, path, verbose, check_syntax,
try:
if check_syntax:
json_file = open(path, 'rb')
json.load(json_file)
jsoncheck.check_syntax(path)
except ValueError as e:
any_failures = True
print(" Invalid JSON file %s: %s" %
(os.path.relpath(path, rootdir), e))
print(" Invalid JSON file %s:\n%s" %
(os.path.relpath(path, rootdir), indent(e, 4)))
else:
try:
if check_niceness:
jsoncheck.check_formatting(path)
except ValueError as e:
any_failures = True
print(" %s:\n%s" % (os.path.relpath(path, rootdir),
indent(e, 4)))
try:
if check_niceness:
verify_whitespace_niceness(path)

View File

@ -68,7 +68,7 @@ def _indent_note(note):
indented_note = []
# Split into single lines in case the argument is pre-formatted.
for line in note.splitlines():
indented_note.append(textwrap.fill(line, initial_indent=8 * ' ',
indented_note.append(textwrap.fill(line, initial_indent=4 * ' ',
subsequent_indent=12 * ' ',
width=80))
return "\n".join(indented_note)

View File

@ -9,3 +9,4 @@ iso8601>=0.1.9
lxml>=2.3
oslo.config>=1.4.0 # Apache-2.0
sphinx>=1.1.2,!=1.2.0,!=1.3b1,<1.3
demjson