forked from readthedocs/readthedocs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_config.py
2537 lines (2277 loc) · 81.2 KB
/
test_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import re
import textwrap
from collections import OrderedDict
from unittest.mock import DEFAULT, patch
import pytest
from django.conf import settings
from django.test import override_settings
from pytest import raises
from readthedocs.config import (
ALL,
PIP,
SETUPTOOLS,
BuildConfigV1,
BuildConfigV2,
ConfigError,
ConfigOptionNotSupportedError,
DefaultConfigFileNotFound,
InvalidConfig,
load,
)
from readthedocs.config.config import (
CONFIG_FILE_REQUIRED,
CONFIG_FILENAME_REGEX,
CONFIG_NOT_SUPPORTED,
CONFIG_REQUIRED,
CONFIG_SYNTAX_INVALID,
INVALID_KEY,
INVALID_NAME,
PYTHON_INVALID,
VERSION_INVALID,
)
from readthedocs.config.models import (
Build,
BuildJobs,
BuildWithTools,
Conda,
PythonInstall,
PythonInstallRequirements,
)
from readthedocs.config.validation import (
INVALID_BOOL,
INVALID_CHOICE,
INVALID_LIST,
VALUE_NOT_FOUND,
ValidationError,
)
from .utils import apply_fs
yaml_config_dir = {
'readthedocs.yml': textwrap.dedent(
'''
formats:
- pdf
'''
),
}
def get_build_config(config, env_config=None, source_file='readthedocs.yml'):
return BuildConfigV1(
env_config or {},
config,
source_file=source_file,
)
@pytest.mark.parametrize(
'files', [
{'readthedocs.ymlmore': ''}, {'first': {'readthedocs.yml': ''}},
{'startreadthedocs.yml': ''}, {'second': {'confuser.txt': 'content'}},
{'noroot': {'readthedocs.ymlmore': ''}}, {'third': {'readthedocs.yml': 'content', 'Makefile': ''}},
{'noroot': {'startreadthedocs.yml': ''}}, {'fourth': {'samplefile.yaml': 'content'}},
{'readthebots.yaml': ''}, {'fifth': {'confuser.txt': '', 'readthedocs.yml': 'content'}},
],
)
def test_load_no_config_file(tmpdir, files):
apply_fs(tmpdir, files)
base = str(tmpdir)
with raises(DefaultConfigFileNotFound) as e:
with override_settings(DOCROOT=tmpdir):
load(base, {})
assert e.value.code == CONFIG_FILE_REQUIRED
def test_load_empty_config_file(tmpdir):
apply_fs(
tmpdir, {
'readthedocs.yml': '',
},
)
base = str(tmpdir)
with raises(ConfigError):
with override_settings(DOCROOT=tmpdir):
load(base, {})
def test_minimal_config(tmpdir):
apply_fs(tmpdir, yaml_config_dir)
base = str(tmpdir)
with override_settings(DOCROOT=tmpdir):
build = load(base, {})
assert isinstance(build, BuildConfigV1)
def test_load_version1(tmpdir):
apply_fs(
tmpdir, {
'readthedocs.yml': textwrap.dedent('''
version: 1
'''),
},
)
base = str(tmpdir)
with override_settings(DOCROOT=tmpdir):
build = load(base, {})
assert isinstance(build, BuildConfigV1)
def test_load_version2(tmpdir):
apply_fs(
tmpdir, {
'readthedocs.yml': textwrap.dedent('''
version: 2
'''),
},
)
base = str(tmpdir)
with override_settings(DOCROOT=tmpdir):
build = load(base, {})
assert isinstance(build, BuildConfigV2)
def test_load_unknow_version(tmpdir):
apply_fs(
tmpdir, {
'readthedocs.yml': textwrap.dedent('''
version: 9
'''),
},
)
base = str(tmpdir)
with raises(ConfigError) as excinfo:
with override_settings(DOCROOT=tmpdir):
load(base, {})
assert excinfo.value.code == VERSION_INVALID
def test_load_raise_exception_invalid_syntax(tmpdir):
apply_fs(
tmpdir, {
'readthedocs.yml': textwrap.dedent('''
version: 2
python:
install:
- method: pip
path: .
# bad indentation here
extra_requirements:
- build
'''),
},
)
base = str(tmpdir)
with raises(ConfigError) as excinfo:
with override_settings(DOCROOT=tmpdir):
load(base, {})
assert excinfo.value.code == CONFIG_SYNTAX_INVALID
def test_yaml_extension(tmpdir):
"""Make sure loading the 'readthedocs' file with a 'yaml' extension."""
apply_fs(
tmpdir, {
'readthedocs.yaml': textwrap.dedent(
'''
python:
version: 3
'''
),
},
)
base = str(tmpdir)
with override_settings(DOCROOT=tmpdir):
config = load(base, {})
assert isinstance(config, BuildConfigV1)
def test_build_config_has_source_file(tmpdir):
base = str(apply_fs(tmpdir, yaml_config_dir))
with override_settings(DOCROOT=tmpdir):
build = load(base, {})
assert build.source_file == os.path.join(base, 'readthedocs.yml')
def test_load_non_default(tmpdir):
"""
Load a config file name from non-default path
Verifies that we can load a custom config path and that an existing default config file is
correctly ignored.
"""
non_default_filename = "myconfig.yaml"
apply_fs(
tmpdir,
{
non_default_filename: textwrap.dedent(
"""
version: 2
"""
),
".readthedocs.yaml": "illegal syntax but should not load",
},
)
base = str(tmpdir)
with override_settings(DOCROOT=tmpdir):
build = load(base, {}, config_file="myconfig.yaml")
assert isinstance(build, BuildConfigV2)
assert build.source_file == os.path.join(base, non_default_filename)
def test_load_non_default_with_strange_extension(tmpdir):
"""
Load a config file name from non-default path
In this version, we verify that we can handle non-yaml extensions
because we allow the user to do that.
"""
non_default_filename = "myconfig.unconventional"
apply_fs(
tmpdir,
{
non_default_filename: textwrap.dedent(
"""
version: 2
"""
),
".readthedocs.yaml": "illegal syntax but should not load",
},
)
base = str(tmpdir)
with override_settings(DOCROOT=tmpdir):
build = load(base, {}, config_file="myconfig.unconventional")
assert isinstance(build, BuildConfigV2)
assert build.source_file == os.path.join(base, non_default_filename)
def test_build_config_has_list_with_single_empty_value(tmpdir):
base = str(apply_fs(
tmpdir, {
'readthedocs.yml': textwrap.dedent(
'''
formats: []
'''
),
},
))
with override_settings(DOCROOT=tmpdir):
build = load(base, {})
assert isinstance(build, BuildConfigV1)
assert build.formats == []
def test_version():
build = get_build_config({})
assert build.version == '1'
def test_doc_type():
build = get_build_config(
{},
{
'defaults': {
'doctype': 'sphinx',
},
},
)
build.validate()
assert build.doctype == 'sphinx'
def test_empty_python_section_is_valid():
build = get_build_config({'python': {}})
build.validate()
assert build.python
def test_python_section_must_be_dict():
build = get_build_config({'python': 123})
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'python'
assert excinfo.value.code == PYTHON_INVALID
def test_use_system_site_packages_defaults_to_false():
build = get_build_config({'python': {}})
build.validate()
# Default is False.
assert not build.python.use_system_site_packages
@pytest.mark.parametrize('value', [True, False])
def test_use_system_site_packages_repects_default_value(value):
defaults = {
'use_system_packages': value,
}
build = get_build_config({}, {'defaults': defaults})
build.validate()
assert build.python.use_system_site_packages is value
class TestValidatePythonExtraRequirements:
def test_it_defaults_to_install_requirements_as_none(self):
build = get_build_config({'python': {}})
build.validate()
install = build.python.install
assert len(install) == 1
assert isinstance(install[0], PythonInstallRequirements)
assert install[0].requirements is None
def test_it_validates_is_a_list(self):
build = get_build_config(
{'python': {'extra_requirements': 'invalid'}},
)
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'python.extra_requirements'
assert excinfo.value.code == PYTHON_INVALID
@patch('readthedocs.config.config.validate_string')
def test_it_uses_validate_string(self, validate_string):
validate_string.return_value = True
build = get_build_config(
{
'python': {
'pip_install': True,
'extra_requirements': ['tests'],
},
},
)
build.validate()
validate_string.assert_any_call('tests')
class TestValidateUseSystemSitePackages:
def test_it_defaults_to_false(self):
build = get_build_config({'python': {}})
build.validate()
assert build.python.use_system_site_packages is False
def test_it_validates_value(self):
build = get_build_config(
{'python': {'use_system_site_packages': 'invalid'}},
)
with raises(InvalidConfig) as excinfo:
build.validate()
excinfo.value.key = 'python.use_system_site_packages'
excinfo.value.code = INVALID_BOOL
@patch('readthedocs.config.config.validate_bool')
def test_it_uses_validate_bool(self, validate_bool):
validate_bool.return_value = True
build = get_build_config(
{'python': {'use_system_site_packages': 'to-validate'}},
)
build.validate()
validate_bool.assert_any_call('to-validate')
class TestValidateSetupPyInstall:
def test_it_defaults_to_false(self):
build = get_build_config({'python': {}})
build.validate()
install = build.python.install
assert len(install) == 1
assert isinstance(install[0], PythonInstallRequirements)
assert install[0].requirements is None
def test_it_validates_value(self):
build = get_build_config(
{'python': {'setup_py_install': 'this-is-string'}},
)
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'python.setup_py_install'
assert excinfo.value.code == INVALID_BOOL
@patch('readthedocs.config.config.validate_bool')
def test_it_uses_validate_bool(self, validate_bool):
validate_bool.return_value = True
build = get_build_config(
{'python': {'setup_py_install': 'to-validate'}},
)
build.validate()
validate_bool.assert_any_call('to-validate')
class TestValidatePythonVersion:
def test_it_defaults_to_a_valid_version(self):
build = get_build_config({'python': {}})
build.validate()
assert build.python.version == '2'
assert build.python_interpreter == 'python2.7'
assert build.python_full_version == '2.7'
def test_it_supports_other_versions(self):
build = get_build_config(
{'python': {'version': 3.7}},
)
build.validate()
assert build.python.version == '3.7'
assert build.python_interpreter == 'python3.7'
assert build.python_full_version == '3.7'
def test_it_supports_string_versions(self):
build = get_build_config(
{'python': {'version': 'pypy3.5'}},
)
build.validate()
assert build.python.version == 'pypy3.5'
assert build.python_interpreter == 'pypy3.5'
assert build.python_full_version == 'pypy3.5'
def test_it_validates_versions_out_of_range(self):
build = get_build_config(
{'python': {'version': 1.0}},
)
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'python.version'
assert excinfo.value.code == INVALID_CHOICE
def test_it_validates_wrong_type(self):
build = get_build_config(
{'python': {'version': 'this-is-string'}},
)
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'python.version'
assert excinfo.value.code == INVALID_CHOICE
def test_it_validates_env_supported_versions(self):
build = get_build_config(
{'python': {'version': '3.6'}},
env_config={
'python': {'supported_versions': ['3.5']},
'build': {'image': 'custom'},
},
)
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'python.version'
assert excinfo.value.code == INVALID_CHOICE
build = get_build_config(
{'python': {'version': '3.6'}},
env_config={
'python': {'supported_versions': ['3.5', '3.6']},
'build': {'image': 'custom'},
},
)
build.validate()
assert build.python.version == '3.6'
assert build.python_interpreter == 'python3.6'
assert build.python_full_version == '3.6'
@pytest.mark.parametrize('value', ['2', '3'])
def test_it_respects_default_value(self, value):
defaults = {
'python_version': value,
}
build = get_build_config(
{},
{'defaults': defaults},
)
build.validate()
assert build.python.version == value
class TestValidateFormats:
def test_it_defaults_to_empty(self):
build = get_build_config({})
build.validate()
assert build.formats == []
def test_it_gets_set_correctly(self):
build = get_build_config({'formats': ['pdf']})
build.validate()
assert build.formats == ['pdf']
def test_formats_can_be_null(self):
build = get_build_config({'formats': None})
build.validate()
assert build.formats == []
def test_formats_with_previous_none(self):
build = get_build_config({'formats': ['none']})
build.validate()
assert build.formats == []
def test_formats_can_be_empty(self):
build = get_build_config({'formats': []})
build.validate()
assert build.formats == []
def test_all_valid_formats(self):
build = get_build_config(
{'formats': ['pdf', 'htmlzip', 'epub']},
)
build.validate()
assert build.formats == ['pdf', 'htmlzip', 'epub']
def test_cant_have_none_as_format(self):
build = get_build_config(
{'formats': ['htmlzip', None]},
)
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'format'
assert excinfo.value.code == INVALID_CHOICE
def test_formats_have_only_allowed_values(self):
build = get_build_config(
{'formats': ['htmlzip', 'csv']},
)
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'format'
assert excinfo.value.code == INVALID_CHOICE
def test_only_list_type(self):
build = get_build_config({'formats': 'no-list'})
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'format'
assert excinfo.value.code == INVALID_LIST
def test_valid_build_config():
build = BuildConfigV1(
{},
{},
source_file='readthedocs.yml',
)
build.validate()
assert build.python
assert len(build.python.install) == 1
assert isinstance(build.python.install[0], PythonInstallRequirements)
assert build.python.install[0].requirements is None
class TestValidateBuild:
def test_it_fails_if_build_is_invalid_option(self, tmpdir):
apply_fs(tmpdir, yaml_config_dir)
build = BuildConfigV1(
{},
{'build': {'image': 3.2}},
source_file=str(tmpdir.join('readthedocs.yml')),
)
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'build'
assert excinfo.value.code == INVALID_CHOICE
def test_it_fails_on_python_validation(self, tmpdir):
apply_fs(tmpdir, yaml_config_dir)
build = BuildConfigV1(
{},
{
'build': {'image': 2.0},
'python': {'version': '3.8'},
},
source_file=str(tmpdir.join('readthedocs.yml')),
)
build.validate_build()
with raises(InvalidConfig) as excinfo:
build.validate_python()
assert excinfo.value.key == 'python.version'
assert excinfo.value.code == INVALID_CHOICE
def test_it_works_on_python_validation(self, tmpdir):
apply_fs(tmpdir, yaml_config_dir)
build = BuildConfigV1(
{},
{
'build': {'image': 'latest'},
'python': {'version': '3.6'},
},
source_file=str(tmpdir.join('readthedocs.yml')),
)
build.validate_build()
build.validate_python()
def test_it_works(self, tmpdir):
apply_fs(tmpdir, yaml_config_dir)
build = BuildConfigV1(
{},
{'build': {'image': 'latest'}},
source_file=str(tmpdir.join('readthedocs.yml')),
)
build.validate()
assert build.build.image == 'readthedocs/build:latest'
def test_default(self, tmpdir):
apply_fs(tmpdir, yaml_config_dir)
build = BuildConfigV1(
{},
{},
source_file=str(tmpdir.join('readthedocs.yml')),
)
build.validate()
assert build.build.image == 'readthedocs/build:latest'
@pytest.mark.parametrize(
'image', ['latest', 'readthedocs/build:3.0', 'rtd/build:latest'],
)
def test_it_priorities_image_from_env_config(self, tmpdir, image):
apply_fs(tmpdir, yaml_config_dir)
defaults = {
'build_image': image,
}
build = BuildConfigV1(
{'defaults': defaults},
{'build': {'image': 'latest'}},
source_file=str(tmpdir.join('readthedocs.yml')),
)
build.validate()
assert build.build.image == image
def test_use_conda_default_none():
build = get_build_config({})
build.validate()
assert build.conda is None
def test_validates_conda_file(tmpdir):
apply_fs(tmpdir, {'environment.yml': ''})
build = get_build_config(
{'conda': {'file': 'environment.yml'}},
source_file=str(tmpdir.join('readthedocs.yml')),
)
build.validate()
assert isinstance(build.conda, Conda)
assert build.conda.environment == 'environment.yml'
def test_file_is_required_when_using_conda(tmpdir):
apply_fs(tmpdir, {'environment.yml': ''})
build = get_build_config(
{'conda': {'foo': 'environment.yml'}},
source_file=str(tmpdir.join('readthedocs.yml')),
)
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'conda.file'
assert excinfo.value.code == VALUE_NOT_FOUND
def test_requirements_file_empty():
build = get_build_config({})
build.validate()
install = build.python.install
assert len(install) == 1
assert install[0].requirements is None
def test_requirements_file_repects_default_value(tmpdir):
apply_fs(tmpdir, {'myrequirements.txt': ''})
defaults = {
'requirements_file': 'myrequirements.txt',
}
build = get_build_config(
{},
{'defaults': defaults},
source_file=str(tmpdir.join('readthedocs.yml')),
)
build.validate()
install = build.python.install
assert len(install) == 1
assert install[0].requirements == 'myrequirements.txt'
def test_requirements_file_respects_configuration(tmpdir):
apply_fs(tmpdir, {'requirements.txt': ''})
build = get_build_config(
{'requirements_file': 'requirements.txt'},
source_file=str(tmpdir.join('readthedocs.yml')),
)
build.validate()
install = build.python.install
assert len(install) == 1
assert install[0].requirements == 'requirements.txt'
def test_requirements_file_is_null(tmpdir):
build = get_build_config(
{'requirements_file': None},
source_file=str(tmpdir.join('readthedocs.yml')),
)
build.validate()
install = build.python.install
assert len(install) == 1
assert install[0].requirements is None
def test_requirements_file_is_blank(tmpdir):
build = get_build_config(
{'requirements_file': ''},
source_file=str(tmpdir.join('readthedocs.yml')),
)
build.validate()
install = build.python.install
assert len(install) == 1
assert install[0].requirements is None
def test_build_validate_calls_all_subvalidators(tmpdir):
apply_fs(tmpdir, {})
build = BuildConfigV1(
{},
{},
source_file=str(tmpdir.join('readthedocs.yml')),
)
with patch.multiple(
BuildConfigV1,
validate_python=DEFAULT,
):
build.validate()
BuildConfigV1.validate_python.assert_called_with()
def test_load_calls_validate(tmpdir):
apply_fs(tmpdir, yaml_config_dir)
base = str(tmpdir)
with patch.object(BuildConfigV1, 'validate') as build_validate:
with override_settings(DOCROOT=tmpdir):
load(base, {})
assert build_validate.call_count == 1
def test_raise_config_not_supported():
build = get_build_config({})
build.validate()
with raises(ConfigOptionNotSupportedError) as excinfo:
build.redirects
assert excinfo.value.configuration == 'redirects'
assert excinfo.value.code == CONFIG_NOT_SUPPORTED
@pytest.mark.parametrize(
'correct_config_filename',
[prefix + 'readthedocs.' + extension for prefix in {'', '.'}
for extension in {'yml', 'yaml'}],
)
def test_config_filenames_regex(correct_config_filename):
assert re.match(CONFIG_FILENAME_REGEX, correct_config_filename)
def test_as_dict(tmpdir):
apply_fs(tmpdir, {'requirements.txt': ''})
build = get_build_config(
{
'version': 1,
'formats': ['pdf'],
'python': {
'version': 3.7,
},
'requirements_file': 'requirements.txt',
},
{
'defaults': {
'doctype': 'sphinx',
'sphinx_configuration': None,
},
},
source_file=str(tmpdir.join('readthedocs.yml')),
)
build.validate()
expected_dict = {
'version': '1',
'formats': ['pdf'],
'python': {
'version': '3.7',
'install': [{
'requirements': 'requirements.txt',
}],
'use_system_site_packages': False,
},
'build': {
'image': 'readthedocs/build:latest',
'apt_packages': [],
},
'conda': None,
'sphinx': {
'builder': 'sphinx',
'configuration': None,
'fail_on_warning': False,
},
'mkdocs': {
'configuration': None,
'fail_on_warning': False,
},
'doctype': 'sphinx',
'submodules': {
'include': ALL,
'exclude': [],
'recursive': True,
},
'search': {
'ranking': {},
'ignore': [],
},
}
assert build.as_dict() == expected_dict
class TestBuildConfigV2:
def get_build_config(
self, config, env_config=None, source_file='readthedocs.yml',
):
return BuildConfigV2(
env_config or {},
config,
source_file=source_file,
)
def test_version(self):
build = self.get_build_config({})
assert build.version == '2'
def test_correct_error_when_source_is_dir(self, tmpdir):
build = self.get_build_config({}, source_file=str(tmpdir))
with raises(InvalidConfig) as excinfo:
build.error(key='key', message='Message', code='code')
# We don't have any extra information about
# the source_file.
assert str(excinfo.value) == 'Invalid "key": Message'
def test_formats_check_valid(self):
build = self.get_build_config({'formats': ['htmlzip', 'pdf', 'epub']})
build.validate()
assert build.formats == ['htmlzip', 'pdf', 'epub']
@pytest.mark.parametrize('value', [3, 'invalid', {'other': 'value'}])
def test_formats_check_invalid_value(self, value):
build = self.get_build_config({'formats': value})
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'formats'
def test_formats_check_invalid_type(self):
build = self.get_build_config(
{'formats': ['htmlzip', 'invalid', 'epub']},
)
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'formats'
def test_formats_default_value(self):
build = self.get_build_config({})
build.validate()
assert build.formats == []
def test_formats_overrides_default_values(self):
build = self.get_build_config(
{},
{'defaults': {'formats': ['htmlzip']}},
)
build.validate()
assert build.formats == []
def test_formats_priority_over_defaults(self):
build = self.get_build_config(
{'formats': []},
{'defaults': {'formats': ['htmlzip']}},
)
build.validate()
assert build.formats == []
build = self.get_build_config(
{'formats': ['pdf']},
{'defaults': {'formats': ['htmlzip']}},
)
build.validate()
assert build.formats == ['pdf']
def test_formats_allow_empty(self):
build = self.get_build_config({'formats': []})
build.validate()
assert build.formats == []
def test_formats_allow_all_keyword(self):
build = self.get_build_config({'formats': 'all'})
build.validate()
assert build.formats == ['htmlzip', 'pdf', 'epub']
def test_conda_check_valid(self, tmpdir):
apply_fs(tmpdir, {'environment.yml': ''})
build = self.get_build_config(
{'conda': {'environment': 'environment.yml'}},
source_file=str(tmpdir.join('readthedocs.yml')),
)
build.validate()
assert build.conda.environment == 'environment.yml'
@pytest.mark.parametrize('value', [3, [], 'invalid'])
def test_conda_check_invalid_value(self, value):
build = self.get_build_config({'conda': value})
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'conda'
@pytest.mark.parametrize('value', [3, [], {}])
def test_conda_check_invalid_file_value(self, value):
build = self.get_build_config({'conda': {'file': value}})
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'conda.environment'
def test_conda_check_file_required(self):
build = self.get_build_config({'conda': {'no-file': 'other'}})
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'conda.environment'
@pytest.mark.parametrize('value', ['stable', 'latest', 'testing'])
def test_build_image_check_valid(self, value):
build = self.get_build_config({'build': {'image': value}})
build.validate()
assert build.build.image == 'readthedocs/build:{}'.format(value)
@pytest.mark.parametrize('value', ['readthedocs/build:latest', 'one'])
def test_build_image_check_invalid(self, value):
build = self.get_build_config({'build': {'image': value}})
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'build.image'
@pytest.mark.parametrize(
'image', ['latest', 'readthedocs/build:3.0', 'rtd/build:latest'],
)
def test_build_image_priorities_default(self, image):
build = self.get_build_config(
{'build': {'image': 'latest'}},
{'defaults': {'build_image': image}},
)
build.validate()
assert build.build.image == image
@pytest.mark.parametrize('image', ['', None])
def test_build_image_over_empty_default(self, image):
build = self.get_build_config(
{'build': {'image': 'latest'}},
{'defaults': {'build_image': image}},
)
build.validate()
assert build.build.image == 'readthedocs/build:latest'
def test_build_image_default_value(self):
build = self.get_build_config({})
build.validate()
assert not build.using_build_tools
assert isinstance(build.build, Build)
assert build.build.image == 'readthedocs/build:latest'
@pytest.mark.parametrize('value', [3, [], 'invalid'])
def test_build_check_invalid_type(self, value):
build = self.get_build_config({'build': value})
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'build'
@pytest.mark.parametrize('value', [3, [], {}])
def test_build_image_check_invalid_type(self, value):
build = self.get_build_config({'build': {'image': value}})
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'build.image'
@pytest.mark.parametrize('value', ['', None, 'latest'])
def test_new_build_config_invalid_os(self, value):
build = self.get_build_config(
{
'build': {
'os': value,
'tools': {'python': '3'},
},