Skip to content

Commit ea058d5

Browse files
stsewdagjohnson
authored andcommitted
Remove hardcoded constant from config module (#4704)
* Test * Allow source_file to be a dict * Move docstrings
1 parent 345935c commit ea058d5

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

readthedocs/config/config.py

+31-20
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,17 @@ class BuildConfigBase(object):
129129
"""
130130
Config that handles the build of one particular documentation.
131131
132-
You need to call ``validate`` before the config is ready to use. Also
133-
setting the ``output_base`` is required before using it for a build.
132+
.. note::
133+
134+
You need to call ``validate`` before the config is ready to use.
135+
136+
:param env_config: A dict that cointains additional information
137+
about the environment.
138+
:param raw_config: A dict with all configuration without validation.
139+
:param source_file: The file that contains the configuration.
140+
All paths are relative to this file.
141+
If a dir is given, the configuration was loaded
142+
from another source (like the web admin).
134143
"""
135144

136145
version = None
@@ -140,21 +149,27 @@ def __init__(self, env_config, raw_config, source_file, source_position):
140149
self.raw_config = raw_config
141150
self.source_file = source_file
142151
self.source_position = source_position
143-
self.base_path = os.path.dirname(self.source_file)
152+
if os.path.isdir(self.source_file):
153+
self.base_path = self.source_file
154+
else:
155+
self.base_path = os.path.dirname(self.source_file)
144156
self.defaults = self.env_config.get('defaults', {})
145157

146158
self._config = {}
147159

148160
def error(self, key, message, code):
149161
"""Raise an error related to ``key``."""
150-
source = '{file} [{pos}]'.format(
151-
file=os.path.relpath(self.source_file, self.base_path),
152-
pos=self.source_position,
153-
)
154-
error_message = '{source}: {message}'.format(
155-
source=source,
156-
message=message,
157-
)
162+
if not os.path.isdir(self.source_file):
163+
source = '{file} [{pos}]'.format(
164+
file=os.path.relpath(self.source_file, self.base_path),
165+
pos=self.source_position,
166+
)
167+
error_message = '{source}: {message}'.format(
168+
source=source,
169+
message=message,
170+
)
171+
else:
172+
error_message = message
158173
raise InvalidConfig(
159174
key=key,
160175
code=code,
@@ -307,10 +322,9 @@ def validate_output_base(self):
307322
"""Validates that ``output_base`` exists and set its absolute path."""
308323
assert 'output_base' in self.env_config, (
309324
'"output_base" required in "env_config"')
310-
base_path = os.path.dirname(self.source_file)
311325
output_base = os.path.abspath(
312326
os.path.join(
313-
self.env_config.get('output_base', base_path),
327+
self.env_config.get('output_base', self.base_path),
314328
)
315329
)
316330
return output_base
@@ -338,10 +352,9 @@ def validate_base(self):
338352
if 'base' in self.raw_config:
339353
base = self.raw_config['base']
340354
else:
341-
base = os.path.dirname(self.source_file)
355+
base = self.base_path
342356
with self.catch_validation_error('base'):
343-
base_path = os.path.dirname(self.source_file)
344-
base = validate_directory(base, base_path)
357+
base = validate_directory(base, self.base_path)
345358
return base
346359

347360
def validate_build(self):
@@ -488,9 +501,8 @@ def validate_conda(self):
488501
conda_environment = None
489502
if 'file' in raw_conda:
490503
with self.catch_validation_error('conda.file'):
491-
base_path = os.path.dirname(self.source_file)
492504
conda_environment = validate_file(
493-
raw_conda['file'], base_path
505+
raw_conda['file'], self.base_path
494506
)
495507
conda['environment'] = conda_environment
496508

@@ -505,9 +517,8 @@ def validate_requirements_file(self):
505517
requirements_file = self.raw_config['requirements_file']
506518
if not requirements_file:
507519
return None
508-
base_path = os.path.dirname(self.source_file)
509520
with self.catch_validation_error('requirements_file'):
510-
validate_file(requirements_file, base_path)
521+
validate_file(requirements_file, self.base_path)
511522
return requirements_file
512523

513524
def validate_formats(self):

readthedocs/config/tests/test_config.py

+8
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,14 @@ def test_version(self):
856856
build = self.get_build_config({})
857857
assert build.version == '2'
858858

859+
def test_correct_error_when_source_is_dir(self, tmpdir):
860+
build = self.get_build_config({}, source_file=str(tmpdir))
861+
with raises(InvalidConfig) as excinfo:
862+
build.error(key='key', message='Message', code='code')
863+
# We don't have any extra information about
864+
# the source_file.
865+
assert str(excinfo.value) == 'Invalid "key": Message'
866+
859867
def test_formats_check_valid(self):
860868
build = self.get_build_config({'formats': ['htmlzip', 'pdf', 'epub']})
861869
build.validate()

readthedocs/doc_builder/config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def load_yaml_config(version):
7373
config = BuildConfigV1(
7474
env_config=env_config,
7575
raw_config={},
76-
source_file=path.join(checkout_path, 'empty'),
76+
source_file=checkout_path,
7777
source_position=0,
7878
)
7979
config.validate()

0 commit comments

Comments
 (0)