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

Move parser tests from rtd-build repo #4225

Merged
merged 13 commits into from Jun 14, 2018
Empty file added readthedocs/config/__init__.py
Empty file.
34 changes: 34 additions & 0 deletions readthedocs/config/parser.py
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
"""YAML parser for the RTD configuration file."""

from __future__ import division, print_function, unicode_literals

import yaml

__all__ = ('parse', 'ParseError')


class ParseError(Exception):

"""Parser related errors."""

pass


def parse(stream):
"""
Take file-like object and return a list of project configurations.

The files need be valid YAML and only contain mappings as documents.
Everything else raises a ``ParseError``.
"""
try:
configs = list(yaml.safe_load_all(stream))
except yaml.YAMLError as error:
raise ParseError('YAML: {message}'.format(message=error))
if not configs:
raise ParseError('Empty config')
for config in configs:
if not isinstance(config, dict):
raise ParseError('Expected mapping')
return configs
Empty file.
56 changes: 56 additions & 0 deletions readthedocs/config/tests/test_parser.py
@@ -0,0 +1,56 @@
from __future__ import division, print_function, unicode_literals

from io import StringIO

from pytest import raises

from readthedocs.config.parser import ParseError, parse


def test_parse_empty_config_file():
buf = StringIO(u'')
with raises(ParseError):
parse(buf)


def test_parse_invalid_yaml():
buf = StringIO(u'- - !asdf')
with raises(ParseError):
parse(buf)


def test_parse_bad_type():
buf = StringIO(u'Hello')
with raises(ParseError):
parse(buf)


def test_parse_single_config():
buf = StringIO(u'base: path')
config = parse(buf)
assert isinstance(config, list)
assert len(config) == 1
assert config[0]['base'] == 'path'


def test_parse_empty_list():
buf = StringIO(u'base: []')
config = parse(buf)
assert config[0]['base'] == []


def test_parse_multiple_configs_in_one_file():
buf = StringIO(
u'''
base: path
---
base: other_path
name: second
nested:
works: true
''')
configs = parse(buf)
assert isinstance(configs, list)
assert len(configs) == 2
assert configs[0]['base'] == 'path'
assert configs[1]['nested'] == {'works': True}