Skip to content

Commit 49995aa

Browse files
authored
Merge pull request #5377 from stsewd/use-relative-paths-in-config-module
Use relative paths in config module
2 parents 79e6151 + 7c7c064 commit 49995aa

File tree

6 files changed

+51
-50
lines changed

6 files changed

+51
-50
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
@@ -604,7 +603,7 @@ def test_validates_conda_file(tmpdir):
604603
)
605604
build.validate()
606605
assert isinstance(build.conda, Conda)
607-
assert build.conda.environment == str(tmpdir.join('environment.yml'))
606+
assert build.conda.environment == 'environment.yml'
608607

609608

610609
def test_file_is_required_when_using_conda(tmpdir):
@@ -640,7 +639,7 @@ def test_requirements_file_repects_default_value(tmpdir):
640639
build.validate()
641640
install = build.python.install
642641
assert len(install) == 1
643-
assert install[0].requirements == str(tmpdir.join('myrequirements.txt'))
642+
assert install[0].requirements == 'myrequirements.txt'
644643

645644

646645
def test_requirements_file_respects_configuration(tmpdir):
@@ -652,7 +651,7 @@ def test_requirements_file_respects_configuration(tmpdir):
652651
build.validate()
653652
install = build.python.install
654653
assert len(install) == 1
655-
assert install[0].requirements == str(tmpdir.join('requirements.txt'))
654+
assert install[0].requirements == 'requirements.txt'
656655

657656

658657
def test_requirements_file_is_null(tmpdir):
@@ -744,7 +743,7 @@ def test_as_dict(tmpdir):
744743
'python': {
745744
'version': 3.7,
746745
'install': [{
747-
'requirements': str(tmpdir.join('requirements.txt')),
746+
'requirements': 'requirements.txt',
748747
}],
749748
'use_system_site_packages': False,
750749
},
@@ -859,7 +858,7 @@ def test_conda_check_valid(self, tmpdir):
859858
source_file=str(tmpdir.join('readthedocs.yml')),
860859
)
861860
build.validate()
862-
assert build.conda.environment == str(tmpdir.join('environment.yml'))
861+
assert build.conda.environment == 'environment.yml'
863862

864863
def test_conda_check_invalid(self, tmpdir):
865864
apply_fs(tmpdir, {'environment.yml': ''})
@@ -1054,7 +1053,7 @@ def test_python_install_check_default(self, tmpdir):
10541053
install = build.python.install
10551054
assert len(install) == 1
10561055
assert isinstance(install[0], PythonInstall)
1057-
assert install[0].path == str(tmpdir)
1056+
assert install[0].path == '.'
10581057
assert install[0].method == PIP
10591058
assert install[0].extra_requirements == []
10601059

@@ -1107,7 +1106,7 @@ def test_python_install_requirements_check_valid(self, tmpdir):
11071106
install = build.python.install
11081107
assert len(install) == 1
11091108
assert isinstance(install[0], PythonInstallRequirements)
1110-
assert install[0].requirements == str(tmpdir.join('requirements.txt'))
1109+
assert install[0].requirements == 'requirements.txt'
11111110

11121111
def test_python_install_requirements_check_invalid(self, tmpdir):
11131112
apply_fs(tmpdir, {'requirements.txt': ''})
@@ -1187,7 +1186,7 @@ def test_python_install_requirements_priority_over_default(self, tmpdir):
11871186
build.validate()
11881187
install = build.python.install
11891188
assert len(install) == 1
1190-
assert install[0].requirements == str(tmpdir.join('requirements.txt'))
1189+
assert install[0].requirements == 'requirements.txt'
11911190

11921191
@pytest.mark.parametrize('value', [3, [], {}])
11931192
def test_python_install_requirements_check_invalid_types(self, value, tmpdir):
@@ -1237,7 +1236,7 @@ def test_python_install_pip_check_valid(self, tmpdir):
12371236
build.validate()
12381237
install = build.python.install
12391238
assert len(install) == 1
1240-
assert install[0].path == str(tmpdir)
1239+
assert install[0].path == '.'
12411240
assert install[0].method == PIP
12421241

12431242
def test_python_install_pip_have_priority_over_default(self, tmpdir):
@@ -1256,7 +1255,7 @@ def test_python_install_pip_have_priority_over_default(self, tmpdir):
12561255
build.validate()
12571256
install = build.python.install
12581257
assert len(install) == 1
1259-
assert install[0].path == str(tmpdir)
1258+
assert install[0].path == '.'
12601259
assert install[0].method == PIP
12611260

12621261
def test_python_install_setuptools_check_valid(self, tmpdir):
@@ -1274,7 +1273,7 @@ def test_python_install_setuptools_check_valid(self, tmpdir):
12741273
build.validate()
12751274
install = build.python.install
12761275
assert len(install) == 1
1277-
assert install[0].path == str(tmpdir)
1276+
assert install[0].path == '.'
12781277
assert install[0].method == SETUPTOOLS
12791278

12801279
def test_python_install_setuptools_ignores_default(self):
@@ -1301,7 +1300,7 @@ def test_python_install_setuptools_priority_over_default(self, tmpdir):
13011300
build.validate()
13021301
install = build.python.install
13031302
assert len(install) == 1
1304-
assert install[0].path == str(tmpdir)
1303+
assert install[0].path == '.'
13051304
assert install[0].method == SETUPTOOLS
13061305

13071306
def test_python_install_allow_empty_list(self):
@@ -1419,14 +1418,14 @@ def test_python_install_several_respects_order(self, tmpdir):
14191418
install = build.python.install
14201419
assert len(install) == 3
14211420

1422-
assert install[0].path == str(tmpdir.join('one'))
1421+
assert install[0].path == 'one'
14231422
assert install[0].method == PIP
14241423
assert install[0].extra_requirements == []
14251424

1426-
assert install[1].path == str(tmpdir.join('two'))
1425+
assert install[1].path == 'two'
14271426
assert install[1].method == SETUPTOOLS
14281427

1429-
assert install[2].requirements == str(tmpdir.join('three.txt'))
1428+
assert install[2].requirements == 'three.txt'
14301429

14311430
def test_python_install_reports_correct_invalid_index(self, tmpdir):
14321431
apply_fs(tmpdir, {
@@ -1564,7 +1563,7 @@ def test_sphinx_configuration_check_valid(self, tmpdir):
15641563
source_file=str(tmpdir.join('readthedocs.yml')),
15651564
)
15661565
build.validate()
1567-
assert build.sphinx.configuration == str(tmpdir.join('conf.py'))
1566+
assert build.sphinx.configuration == 'conf.py'
15681567

15691568
def test_sphinx_configuration_check_invalid(self, tmpdir):
15701569
apply_fs(tmpdir, {'conf.py': ''})
@@ -1607,7 +1606,7 @@ def test_sphinx_configuration_respects_default(self, tmpdir):
16071606
source_file=str(tmpdir.join('readthedocs.yml')),
16081607
)
16091608
build.validate()
1610-
assert build.sphinx.configuration == str(tmpdir.join('conf.py'))
1609+
assert build.sphinx.configuration == 'conf.py'
16111610

16121611
def test_sphinx_configuration_default_can_be_none(self, tmpdir):
16131612
apply_fs(tmpdir, {'conf.py': ''})
@@ -1627,7 +1626,7 @@ def test_sphinx_configuration_priorities_over_default(self, tmpdir):
16271626
source_file=str(tmpdir.join('readthedocs.yml')),
16281627
)
16291628
build.validate()
1630-
assert build.sphinx.configuration == str(tmpdir.join('conf.py'))
1629+
assert build.sphinx.configuration == 'conf.py'
16311630

16321631
@pytest.mark.parametrize('value', [[], True, 0, {}])
16331632
def test_sphinx_configuration_validate_type(self, value):
@@ -1674,7 +1673,7 @@ def test_mkdocs_configuration_check_valid(self, tmpdir):
16741673
source_file=str(tmpdir.join('readthedocs.yml')),
16751674
)
16761675
build.validate()
1677-
assert build.mkdocs.configuration == str(tmpdir.join('mkdocs.yml'))
1676+
assert build.mkdocs.configuration == 'mkdocs.yml'
16781677
assert build.doctype == 'mkdocs'
16791678
assert build.sphinx is None
16801679

@@ -2032,7 +2031,7 @@ def test_as_dict(self, tmpdir):
20322031
'python': {
20332032
'version': 3.6,
20342033
'install': [{
2035-
'requirements': str(tmpdir.join('requirements.txt')),
2034+
'requirements': 'requirements.txt',
20362035
}],
20372036
'use_system_site_packages': False,
20382037
},

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

@@ -81,28 +79,33 @@ def validate_bool(value):
8179

8280
def validate_directory(value, base_path):
8381
"""Check that ``value`` is a directory."""
84-
path = validate_path(value, base_path)
82+
path = os.path.join(
83+
base_path,
84+
validate_path(value, base_path)
85+
)
8586
if not os.path.isdir(path):
8687
raise ValidationError(value, INVALID_DIRECTORY)
87-
return path
88+
return os.path.relpath(path, base_path)
8889

8990

9091
def validate_file(value, base_path):
9192
"""Check that ``value`` is a file."""
92-
path = validate_path(value, base_path)
93+
path = os.path.join(
94+
base_path,
95+
validate_path(value, base_path)
96+
)
9397
if not os.path.isfile(path):
9498
raise ValidationError(value, INVALID_FILE)
95-
return path
99+
return os.path.relpath(path, base_path)
96100

97101

98102
def validate_path(value, base_path):
99103
"""Check that ``value`` is an existent file in ``base_path``."""
100104
string_value = validate_string(value)
101105
pathed_value = os.path.join(base_path, string_value)
102-
final_value = os.path.abspath(pathed_value)
103-
if not os.path.exists(final_value):
106+
if not os.path.exists(pathed_value):
104107
raise ValidationError(value, INVALID_PATH)
105-
return final_value
108+
return os.path.relpath(pathed_value, base_path)
106109

107110

108111
def validate_string(value):

readthedocs/doc_builder/backends/sphinx.py

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

readthedocs/doc_builder/python_environments.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,10 @@ def install_package(self, install):
8282
:param install: A install object from the config module.
8383
:type install: readthedocs.config.models.PythonInstall
8484
"""
85-
rel_path = os.path.relpath(install.path, self.checkout_path)
8685
if install.method == PIP:
8786
# Prefix ./ so pip installs from a local path rather than pypi
8887
local_path = (
89-
os.path.join('.', rel_path) if rel_path != '.' else rel_path
88+
os.path.join('.', install.path) if install.path != '.' else install.path
9089
)
9190
extra_req_param = ''
9291
if install.extra_requirements:
@@ -111,7 +110,7 @@ def install_package(self, install):
111110
elif install.method == SETUPTOOLS:
112111
self.build_env.run(
113112
self.venv_bin(filename='python'),
114-
os.path.join(rel_path, 'setup.py'),
113+
os.path.join(install.path, 'setup.py'),
115114
'install',
116115
'--force',
117116
cwd=self.checkout_path,
@@ -364,7 +363,10 @@ def install_requirements_file(self, install):
364363
for path, req_file in itertools.product(paths, req_files):
365364
test_path = os.path.join(self.checkout_path, path, req_file)
366365
if os.path.exists(test_path):
367-
requirements_file_path = test_path
366+
requirements_file_path = os.path.relpath(
367+
test_path,
368+
self.checkout_path,
369+
)
368370
break
369371

370372
if requirements_file_path:
@@ -381,10 +383,7 @@ def install_requirements_file(self, install):
381383
'--cache-dir',
382384
self.project.pip_cache_path,
383385
'-r',
384-
os.path.relpath(
385-
requirements_file_path,
386-
self.checkout_path
387-
),
386+
requirements_file_path,
388387
]
389388
self.build_env.run(
390389
*args,

readthedocs/rtd_tests/tests/test_config_integration.py

+5-7
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')
@@ -954,7 +952,7 @@ def test_mkdocs_configuration(
954952

955953
args, kwargs = run.call_args
956954
assert '--config-file' in args
957-
assert path.join(str(tmpdir), 'docx/mkdocs.yml') in args
955+
assert 'docx/mkdocs.yml' in args
958956
append_conf.assert_called_once()
959957
move.assert_called_once()
960958

0 commit comments

Comments
 (0)