Skip to content

Commit d373487

Browse files
committed
Use relative paths in config module
Closes readthedocs#5364
1 parent a98aac4 commit d373487

File tree

7 files changed

+55
-53
lines changed

7 files changed

+55
-53
lines changed

readthedocs/config/tests/test_config.py

+20-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
import os
32
import re
43
import textwrap
@@ -571,7 +570,7 @@ def test_validates_conda_file(tmpdir):
571570
)
572571
build.validate()
573572
assert isinstance(build.conda, Conda)
574-
assert build.conda.environment == str(tmpdir.join('environment.yml'))
573+
assert build.conda.environment == 'environment.yml'
575574

576575

577576
def test_file_is_required_when_using_conda(tmpdir):
@@ -607,7 +606,7 @@ def test_requirements_file_repects_default_value(tmpdir):
607606
build.validate()
608607
install = build.python.install
609608
assert len(install) == 1
610-
assert install[0].requirements == str(tmpdir.join('myrequirements.txt'))
609+
assert install[0].requirements == 'myrequirements.txt'
611610

612611

613612
def test_requirements_file_respects_configuration(tmpdir):
@@ -619,7 +618,7 @@ def test_requirements_file_respects_configuration(tmpdir):
619618
build.validate()
620619
install = build.python.install
621620
assert len(install) == 1
622-
assert install[0].requirements == str(tmpdir.join('requirements.txt'))
621+
assert install[0].requirements == 'requirements.txt'
623622

624623

625624
def test_requirements_file_is_null(tmpdir):
@@ -711,7 +710,7 @@ def test_as_dict(tmpdir):
711710
'python': {
712711
'version': 3.5,
713712
'install': [{
714-
'requirements': str(tmpdir.join('requirements.txt')),
713+
'requirements': 'requirements.txt',
715714
}],
716715
'use_system_site_packages': False,
717716
},
@@ -826,7 +825,7 @@ def test_conda_check_valid(self, tmpdir):
826825
source_file=str(tmpdir.join('readthedocs.yml')),
827826
)
828827
build.validate()
829-
assert build.conda.environment == str(tmpdir.join('environment.yml'))
828+
assert build.conda.environment == 'environment.yml'
830829

831830
def test_conda_check_invalid(self, tmpdir):
832831
apply_fs(tmpdir, {'environment.yml': ''})
@@ -1021,7 +1020,7 @@ def test_python_install_check_default(self, tmpdir):
10211020
install = build.python.install
10221021
assert len(install) == 1
10231022
assert isinstance(install[0], PythonInstall)
1024-
assert install[0].path == str(tmpdir)
1023+
assert install[0].path == '.'
10251024
assert install[0].method == PIP
10261025
assert install[0].extra_requirements == []
10271026

@@ -1074,7 +1073,7 @@ def test_python_install_requirements_check_valid(self, tmpdir):
10741073
install = build.python.install
10751074
assert len(install) == 1
10761075
assert isinstance(install[0], PythonInstallRequirements)
1077-
assert install[0].requirements == str(tmpdir.join('requirements.txt'))
1076+
assert install[0].requirements == 'requirements.txt'
10781077

10791078
def test_python_install_requirements_check_invalid(self, tmpdir):
10801079
apply_fs(tmpdir, {'requirements.txt': ''})
@@ -1154,7 +1153,7 @@ def test_python_install_requirements_priority_over_default(self, tmpdir):
11541153
build.validate()
11551154
install = build.python.install
11561155
assert len(install) == 1
1157-
assert install[0].requirements == str(tmpdir.join('requirements.txt'))
1156+
assert install[0].requirements == 'requirements.txt'
11581157

11591158
@pytest.mark.parametrize('value', [3, [], {}])
11601159
def test_python_install_requirements_check_invalid_types(self, value, tmpdir):
@@ -1204,7 +1203,7 @@ def test_python_install_pip_check_valid(self, tmpdir):
12041203
build.validate()
12051204
install = build.python.install
12061205
assert len(install) == 1
1207-
assert install[0].path == str(tmpdir)
1206+
assert install[0].path == '.'
12081207
assert install[0].method == PIP
12091208

12101209
def test_python_install_pip_have_priority_over_default(self, tmpdir):
@@ -1223,7 +1222,7 @@ def test_python_install_pip_have_priority_over_default(self, tmpdir):
12231222
build.validate()
12241223
install = build.python.install
12251224
assert len(install) == 1
1226-
assert install[0].path == str(tmpdir)
1225+
assert install[0].path == '.'
12271226
assert install[0].method == PIP
12281227

12291228
def test_python_install_setuptools_check_valid(self, tmpdir):
@@ -1241,7 +1240,7 @@ def test_python_install_setuptools_check_valid(self, tmpdir):
12411240
build.validate()
12421241
install = build.python.install
12431242
assert len(install) == 1
1244-
assert install[0].path == str(tmpdir)
1243+
assert install[0].path == '.'
12451244
assert install[0].method == SETUPTOOLS
12461245

12471246
def test_python_install_setuptools_ignores_default(self):
@@ -1268,7 +1267,7 @@ def test_python_install_setuptools_priority_over_default(self, tmpdir):
12681267
build.validate()
12691268
install = build.python.install
12701269
assert len(install) == 1
1271-
assert install[0].path == str(tmpdir)
1270+
assert install[0].path == '.'
12721271
assert install[0].method == SETUPTOOLS
12731272

12741273
def test_python_install_allow_empty_list(self):
@@ -1386,14 +1385,14 @@ def test_python_install_several_respects_order(self, tmpdir):
13861385
install = build.python.install
13871386
assert len(install) == 3
13881387

1389-
assert install[0].path == str(tmpdir.join('one'))
1388+
assert install[0].path == 'one'
13901389
assert install[0].method == PIP
13911390
assert install[0].extra_requirements == []
13921391

1393-
assert install[1].path == str(tmpdir.join('two'))
1392+
assert install[1].path == 'two'
13941393
assert install[1].method == SETUPTOOLS
13951394

1396-
assert install[2].requirements == str(tmpdir.join('three.txt'))
1395+
assert install[2].requirements == 'three.txt'
13971396

13981397
def test_python_install_reports_correct_invalid_index(self, tmpdir):
13991398
apply_fs(tmpdir, {
@@ -1531,7 +1530,7 @@ def test_sphinx_configuration_check_valid(self, tmpdir):
15311530
source_file=str(tmpdir.join('readthedocs.yml')),
15321531
)
15331532
build.validate()
1534-
assert build.sphinx.configuration == str(tmpdir.join('conf.py'))
1533+
assert build.sphinx.configuration == 'conf.py'
15351534

15361535
def test_sphinx_configuration_check_invalid(self, tmpdir):
15371536
apply_fs(tmpdir, {'conf.py': ''})
@@ -1574,7 +1573,7 @@ def test_sphinx_configuration_respects_default(self, tmpdir):
15741573
source_file=str(tmpdir.join('readthedocs.yml')),
15751574
)
15761575
build.validate()
1577-
assert build.sphinx.configuration == str(tmpdir.join('conf.py'))
1576+
assert build.sphinx.configuration == 'conf.py'
15781577

15791578
def test_sphinx_configuration_default_can_be_none(self, tmpdir):
15801579
apply_fs(tmpdir, {'conf.py': ''})
@@ -1594,7 +1593,7 @@ def test_sphinx_configuration_priorities_over_default(self, tmpdir):
15941593
source_file=str(tmpdir.join('readthedocs.yml')),
15951594
)
15961595
build.validate()
1597-
assert build.sphinx.configuration == str(tmpdir.join('conf.py'))
1596+
assert build.sphinx.configuration == 'conf.py'
15981597

15991598
@pytest.mark.parametrize('value', [[], True, 0, {}])
16001599
def test_sphinx_configuration_validate_type(self, value):
@@ -1641,7 +1640,7 @@ def test_mkdocs_configuration_check_valid(self, tmpdir):
16411640
source_file=str(tmpdir.join('readthedocs.yml')),
16421641
)
16431642
build.validate()
1644-
assert build.mkdocs.configuration == str(tmpdir.join('mkdocs.yml'))
1643+
assert build.mkdocs.configuration == 'mkdocs.yml'
16451644
assert build.doctype == 'mkdocs'
16461645
assert build.sphinx is None
16471646

@@ -1999,7 +1998,7 @@ def test_as_dict(self, tmpdir):
19991998
'python': {
20001999
'version': 3.6,
20012000
'install': [{
2002-
'requirements': str(tmpdir.join('requirements.txt')),
2001+
'requirements': 'requirements.txt',
20032002
}],
20042003
'use_system_site_packages': False,
20052004
},

readthedocs/config/tests/test_validation.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# -*- coding: utf-8 -*-
2-
import os
3-
41
from mock import patch
52
from pytest import raises
63

@@ -133,10 +130,10 @@ def test_it_accepts_absolute_path(self, tmpdir):
133130
path = str(tmpdir.mkdir('a directory'))
134131
validate_path(path, 'does not matter')
135132

136-
def test_it_returns_absolute_path(self, tmpdir):
133+
def test_it_returns_relative_path(self, tmpdir):
137134
tmpdir.mkdir('a directory')
138135
path = validate_path('a directory', str(tmpdir))
139-
assert path == os.path.abspath(path)
136+
assert path == 'a directory'
140137

141138
def test_it_only_accepts_strings(self):
142139
with raises(ValidationError) as excinfo:

readthedocs/config/validation.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# -*- coding: utf-8 -*-
2-
31
"""Validations for the RTD configuration file."""
42
import os
53

@@ -90,28 +88,33 @@ def validate_bool(value):
9088

9189
def validate_directory(value, base_path):
9290
"""Check that ``value`` is a directory."""
93-
path = validate_path(value, base_path)
91+
path = os.path.join(
92+
base_path,
93+
validate_path(value, base_path)
94+
)
9495
if not os.path.isdir(path):
9596
raise ValidationError(value, INVALID_DIRECTORY)
96-
return path
97+
return os.path.relpath(path, base_path)
9798

9899

99100
def validate_file(value, base_path):
100101
"""Check that ``value`` is a file."""
101-
path = validate_path(value, base_path)
102+
path = os.path.join(
103+
base_path,
104+
validate_path(value, base_path)
105+
)
102106
if not os.path.isfile(path):
103107
raise ValidationError(value, INVALID_FILE)
104-
return path
108+
return os.path.relpath(path, base_path)
105109

106110

107111
def validate_path(value, base_path):
108112
"""Check that ``value`` is an existent file in ``base_path``."""
109113
string_value = validate_string(value)
110114
pathed_value = os.path.join(base_path, string_value)
111-
final_value = os.path.abspath(pathed_value)
112-
if not os.path.exists(final_value):
115+
if not os.path.exists(pathed_value):
113116
raise ValidationError(value, INVALID_PATH)
114-
return final_value
117+
return os.path.relpath(pathed_value, base_path)
115118

116119

117120
def validate_string(value):

readthedocs/doc_builder/backends/mkdocs.py

+5
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ def get_yaml_config(self):
7878
)
7979
if not os.path.exists(mkdoc_path):
8080
return None
81+
else:
82+
mkdoc_path = os.path.join(
83+
self.project.checkout_path(self.version.slug),
84+
self.config.mkdocs.configuration
85+
)
8186
return mkdoc_path
8287

8388
def load_yaml_config(self):

readthedocs/doc_builder/backends/sphinx.py

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ def __init__(self, *args, **kwargs):
4343
try:
4444
if not self.config_file:
4545
self.config_file = self.project.conf_file(self.version.slug)
46+
else:
47+
self.config_file = os.path.join(
48+
self.project.checkout_path(self.version.slug),
49+
self.config_file,
50+
)
4651
self.old_artifact_path = os.path.join(
4752
os.path.dirname(self.config_file),
4853
self.sphinx_build_dir,

readthedocs/doc_builder/python_environments.py

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# -*- coding: utf-8 -*-
2-
31
"""An abstraction over virtualenv and Conda environments."""
42

53
import copy
@@ -9,8 +7,6 @@
97
import os
108
import shutil
119

12-
from django.conf import settings
13-
1410
from readthedocs.config import PIP, SETUPTOOLS
1511
from readthedocs.config.models import PythonInstall, PythonInstallRequirements
1612
from readthedocs.doc_builder.config import load_yaml_config
@@ -83,11 +79,10 @@ def install_package(self, install):
8379
:param install: A install object from the config module.
8480
:type install: readthedocs.config.models.PythonInstall
8581
"""
86-
rel_path = os.path.relpath(install.path, self.checkout_path)
8782
if install.method == PIP:
8883
# Prefix ./ so pip installs from a local path rather than pypi
8984
local_path = (
90-
os.path.join('.', rel_path) if rel_path != '.' else rel_path
85+
os.path.join('.', install.path) if install.path != '.' else install.path
9186
)
9287
extra_req_param = ''
9388
if install.extra_requirements:
@@ -112,7 +107,7 @@ def install_package(self, install):
112107
elif install.method == SETUPTOOLS:
113108
self.build_env.run(
114109
self.venv_bin(filename='python'),
115-
os.path.join(rel_path, 'setup.py'),
110+
os.path.join(install.path, 'setup.py'),
116111
'install',
117112
'--force',
118113
cwd=self.checkout_path,
@@ -346,7 +341,10 @@ def install_requirements_file(self, install):
346341
for path, req_file in itertools.product(paths, req_files):
347342
test_path = os.path.join(self.checkout_path, path, req_file)
348343
if os.path.exists(test_path):
349-
requirements_file_path = test_path
344+
requirements_file_path = os.path.relpath(
345+
test_path,
346+
self.checkout_path,
347+
)
350348
break
351349

352350
if requirements_file_path:
@@ -363,10 +361,7 @@ def install_requirements_file(self, install):
363361
'--cache-dir',
364362
self.project.pip_cache_path,
365363
'-r',
366-
os.path.relpath(
367-
requirements_file_path,
368-
self.checkout_path
369-
),
364+
requirements_file_path,
370365
]
371366
self.build_env.run(
372367
*args,

readthedocs/rtd_tests/tests/test_config_integration.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def test_conda_with_cofig(self, checkout_path):
277277
)
278278
config = load_yaml_config(self.version)
279279
self.assertTrue(config.conda is not None)
280-
self.assertEqual(config.conda.environment, full_conda_file)
280+
self.assertEqual(config.conda.environment, conda_file)
281281

282282
@mock.patch('readthedocs.projects.models.Project.checkout_path')
283283
def test_conda_without_cofig(self, checkout_path):
@@ -303,7 +303,7 @@ def test_requirements_file_from_project_setting(self, checkout_path):
303303
self.assertEqual(len(config.python.install), 1)
304304
self.assertEqual(
305305
config.python.install[0].requirements,
306-
full_requirements_file
306+
requirements_file
307307
)
308308

309309
@mock.patch('readthedocs.projects.models.Project.checkout_path')
@@ -328,7 +328,7 @@ def test_requirements_file_from_yml(self, checkout_path):
328328
self.assertEqual(len(config.python.install), 1)
329329
self.assertEqual(
330330
config.python.install[0].requirements,
331-
full_requirements_file
331+
requirements_file
332332
)
333333

334334

@@ -459,7 +459,6 @@ def test_conda_environment(self, build_failed, checkout_path, tmpdir):
459459
update_docs = self.get_update_docs_task()
460460
update_docs.run_build(docker=False, record=False)
461461

462-
conda_file = path.join(str(base_path), conda_file)
463462
assert update_docs.config.conda.environment == conda_file
464463
assert isinstance(update_docs.python_env, Conda)
465464

@@ -530,11 +529,10 @@ def test_python_install_requirements(self, run, checkout_path, tmpdir):
530529
update_docs.python_env.install_requirements()
531530

532531
args, kwargs = run.call_args
533-
full_requirements_file = str(base_path.join(requirements_file))
534532
install = config.python.install
535533

536534
assert len(install) == 1
537-
assert install[0].requirements == full_requirements_file
535+
assert install[0].requirements == requirements_file
538536
assert requirements_file in args
539537

540538
@patch('readthedocs.doc_builder.environments.BuildEnvironment.run')

0 commit comments

Comments
 (0)