Skip to content

Commit

Permalink
Remove unused validations from v1 config
Browse files Browse the repository at this point in the history
  • Loading branch information
stsewd committed Nov 9, 2018
1 parent 1648006 commit 9d787aa
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 163 deletions.
73 changes: 0 additions & 73 deletions readthedocs/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from __future__ import division, print_function, unicode_literals

import os
import re
from contextlib import contextmanager

import six
Expand All @@ -22,7 +21,6 @@
validate_bool,
validate_choice,
validate_dict,
validate_directory,
validate_file,
validate_list,
validate_string,
Expand All @@ -43,12 +41,8 @@

CONFIG_NOT_SUPPORTED = 'config-not-supported'
VERSION_INVALID = 'version-invalid'
BASE_INVALID = 'base-invalid'
BASE_NOT_A_DIR = 'base-not-a-directory'
CONFIG_SYNTAX_INVALID = 'config-syntax-invalid'
CONFIG_REQUIRED = 'config-required'
NAME_REQUIRED = 'name-required'
NAME_INVALID = 'name-invalid'
CONF_FILE_REQUIRED = 'conf-file-required'
PYTHON_INVALID = 'python-invalid'
SUBMODULES_INVALID = 'submodules-invalid'
Expand Down Expand Up @@ -263,12 +257,6 @@ class BuildConfigV1(BuildConfigBase):

"""Version 1 of the configuration file."""

BASE_INVALID_MESSAGE = 'Invalid value for base: {base}'
BASE_NOT_A_DIR_MESSAGE = '"base" is not a directory: {base}'
NAME_REQUIRED_MESSAGE = 'Missing key "name"'
NAME_INVALID_MESSAGE = (
'Invalid name "{name}". Valid values must match {name_re}'
)
CONF_FILE_REQUIRED_MESSAGE = 'Missing key "conf_file"'
PYTHON_INVALID_MESSAGE = '"python" section must be a mapping.'
PYTHON_EXTRA_REQUIREMENTS_INVALID_MESSAGE = (
Expand Down Expand Up @@ -306,63 +294,17 @@ def validate(self):
``readthedocs.yml`` config file if not set
"""
# Validate env_config.
# TODO: this isn't used
self._config['output_base'] = self.validate_output_base()

# Validate the build environment first
# Must happen before `validate_python`!
self._config['build'] = self.validate_build()

# Validate raw_config. Order matters.
# TODO: this isn't used
self._config['name'] = self.validate_name()
# TODO: this isn't used
self._config['base'] = self.validate_base()
self._config['python'] = self.validate_python()
self._config['formats'] = self.validate_formats()

self._config['conda'] = self.validate_conda()
self._config['requirements_file'] = self.validate_requirements_file()

def validate_output_base(self):
"""Validates that ``output_base`` exists and set its absolute path."""
assert 'output_base' in self.env_config, (
'"output_base" required in "env_config"')
output_base = os.path.abspath(
os.path.join(
self.env_config.get('output_base', self.base_path),
)
)
return output_base

def validate_name(self):
"""Validates that name exists."""
name = self.raw_config.get('name', None)
if not name:
name = self.env_config.get('name', None)
if not name:
self.error('name', self.NAME_REQUIRED_MESSAGE, code=NAME_REQUIRED)
name_re = r'^[-_.0-9a-zA-Z]+$'
if not re.match(name_re, name):
self.error(
'name',
self.NAME_INVALID_MESSAGE.format(
name=name,
name_re=name_re),
code=NAME_INVALID)

return name

def validate_base(self):
"""Validates that path is a valid directory."""
if 'base' in self.raw_config:
base = self.raw_config['base']
else:
base = self.base_path
with self.catch_validation_error('base'):
base = validate_directory(base, self.base_path)
return base

def validate_build(self):
"""
Validate the build config settings.
Expand Down Expand Up @@ -542,21 +484,6 @@ def validate_formats(self):

return formats

@property
def name(self):
"""The project name."""
return self._config['name']

@property
def base(self):
"""The base directory."""
return self._config['base']

@property
def output_base(self):
"""The output base."""
return self._config['output_base']

@property
def formats(self):
"""The documentation formats to be built."""
Expand Down
91 changes: 1 addition & 90 deletions readthedocs/config/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
CONFIG_NOT_SUPPORTED,
CONFIG_REQUIRED,
INVALID_KEY,
NAME_INVALID,
NAME_REQUIRED,
PYTHON_INVALID,
VERSION_INVALID,
)
Expand All @@ -34,8 +32,6 @@
INVALID_BOOL,
INVALID_CHOICE,
INVALID_LIST,
INVALID_PATH,
INVALID_STRING,
VALUE_NOT_FOUND,
ValidationError,
)
Expand Down Expand Up @@ -90,10 +86,7 @@ def get_build_config(config, env_config=None, source_file='readthedocs.yml'):

def get_env_config(extra=None):
"""Get the minimal env_config for the configuration object."""
defaults = {
'output_base': '',
'name': 'name',
}
defaults = {}
if extra is None:
extra = {}
defaults.update(extra)
Expand Down Expand Up @@ -186,30 +179,6 @@ def test_build_config_has_list_with_single_empty_value(tmpdir):
assert build.formats == []


def test_config_requires_name():
build = BuildConfigV1(
{'output_base': ''},
{},
source_file='readthedocs.yml',
)
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'name'
assert excinfo.value.code == NAME_REQUIRED


def test_build_requires_valid_name():
build = BuildConfigV1(
{'output_base': ''},
{'name': 'with/slashes'},
source_file='readthedocs.yml',
)
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'name'
assert excinfo.value.code == NAME_INVALID


def test_version():
build = get_build_config({}, get_env_config())
assert build.version == '1'
Expand Down Expand Up @@ -528,62 +497,10 @@ def test_valid_build_config():
source_file='readthedocs.yml',
)
build.validate()
assert build.name == 'docs'
assert build.base
assert build.python
assert build.python.install_with_setup is False
assert build.python.install_with_pip is False
assert build.python.use_system_site_packages is False
assert build.output_base


class TestValidateBase(object):

def test_it_validates_to_abspath(self, tmpdir):
apply_fs(tmpdir, {'configs': minimal_config, 'docs': {}})
with tmpdir.as_cwd():
source_file = str(tmpdir.join('configs', 'readthedocs.yml'))
build = BuildConfigV1(
get_env_config(),
{'base': '../docs'},
source_file=source_file,
)
build.validate()
assert build.base == str(tmpdir.join('docs'))

@patch('readthedocs.config.config.validate_directory')
def test_it_uses_validate_directory(self, validate_directory):
validate_directory.return_value = 'path'
build = get_build_config({'base': '../my-path'}, get_env_config())
build.validate()
# Test for first argument to validate_directory
args, kwargs = validate_directory.call_args
assert args[0] == '../my-path'

def test_it_fails_if_base_is_not_a_string(self, tmpdir):
apply_fs(tmpdir, minimal_config)
with tmpdir.as_cwd():
build = BuildConfigV1(
get_env_config(),
{'base': 1},
source_file=str(tmpdir.join('readthedocs.yml')),
)
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'base'
assert excinfo.value.code == INVALID_STRING

def test_it_fails_if_base_does_not_exist(self, tmpdir):
apply_fs(tmpdir, minimal_config)
build = BuildConfigV1(
get_env_config(),
{'base': 'docs'},
source_file=str(tmpdir.join('readthedocs.yml')),
)
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'base'
assert excinfo.value.code == INVALID_PATH


class TestValidateBuild(object):
Expand Down Expand Up @@ -752,16 +669,10 @@ def test_build_validate_calls_all_subvalidators(tmpdir):
)
with patch.multiple(
BuildConfigV1,
validate_base=DEFAULT,
validate_name=DEFAULT,
validate_python=DEFAULT,
validate_output_base=DEFAULT,
):
build.validate()
BuildConfigV1.validate_base.assert_called_with()
BuildConfigV1.validate_name.assert_called_with()
BuildConfigV1.validate_python.assert_called_with()
BuildConfigV1.validate_output_base.assert_called_with()


def test_load_calls_validate(tmpdir):
Expand Down

0 comments on commit 9d787aa

Please sign in to comment.