Skip to content

Commit 1fcd354

Browse files
committed
Test: add tests for build.jobs config
1 parent b4739fd commit 1fcd354

File tree

2 files changed

+135
-1
lines changed

2 files changed

+135
-1
lines changed

readthedocs/config/tests/test_config.py

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import os
2-
from django.conf import settings
32
import re
43
import textwrap
54
from collections import OrderedDict
65
from unittest.mock import DEFAULT, patch
76

87
import pytest
8+
from django.conf import settings
99
from pytest import raises
1010

1111
from readthedocs.config import (
@@ -33,6 +33,7 @@
3333
)
3434
from readthedocs.config.models import (
3535
Build,
36+
BuildJobs,
3637
BuildWithTools,
3738
Conda,
3839
PythonInstall,
@@ -1012,6 +1013,87 @@ def test_new_build_config_conflict_with_build_python_version(self):
10121013
build.validate()
10131014
assert excinfo.value.key == 'python.version'
10141015

1016+
@pytest.mark.parametrize("value", ["", None, "pre_invalid"])
1017+
def test_jobs_build_config_invalid_jobs(self, value):
1018+
build = self.get_build_config(
1019+
{
1020+
"build": {
1021+
"os": "ubuntu-20.04",
1022+
"tools": {"python": "3.8"},
1023+
"jobs": {value: ["echo 1234", "git fetch --unshallow"]},
1024+
},
1025+
},
1026+
)
1027+
with raises(InvalidConfig) as excinfo:
1028+
build.validate()
1029+
assert excinfo.value.key == "build.jobs"
1030+
1031+
@pytest.mark.parametrize("value", ["", None, "echo 123", 42])
1032+
def test_jobs_build_config_invalid_job_commands(self, value):
1033+
build = self.get_build_config(
1034+
{
1035+
"build": {
1036+
"os": "ubuntu-20.04",
1037+
"tools": {"python": "3.8"},
1038+
"jobs": {
1039+
"pre_install": value,
1040+
},
1041+
},
1042+
},
1043+
)
1044+
with raises(InvalidConfig) as excinfo:
1045+
build.validate()
1046+
assert excinfo.value.key == "build.jobs.pre_install"
1047+
1048+
def test_jobs_build_config(self):
1049+
build = self.get_build_config(
1050+
{
1051+
"build": {
1052+
"os": "ubuntu-20.04",
1053+
"tools": {"python": "3.8"},
1054+
"jobs": {
1055+
"pre_checkout": ["echo pre_checkout"],
1056+
"post_checkout": ["echo post_checkout"],
1057+
"pre_system_dependencies": ["echo pre_system_dependencies"],
1058+
"post_system_dependencies": ["echo post_system_dependencies"],
1059+
"pre_create_environment": ["echo pre_create_environment"],
1060+
"post_create_environment": ["echo post_create_environment"],
1061+
"pre_install": ["echo pre_install", "echo `date`"],
1062+
"post_install": ["echo post_install"],
1063+
"pre_build": [
1064+
"echo pre_build",
1065+
'sed -i -e "s|{VERSION}|${READTHEDOCS_VERSION_NAME}|g"',
1066+
],
1067+
"post_build": ["echo post_build"],
1068+
},
1069+
},
1070+
},
1071+
)
1072+
build.validate()
1073+
assert isinstance(build.build, BuildWithTools)
1074+
assert isinstance(build.build.jobs, BuildJobs)
1075+
assert build.build.jobs.pre_checkout == ["echo pre_checkout"]
1076+
assert build.build.jobs.post_checkout == ["echo post_checkout"]
1077+
assert build.build.jobs.pre_system_dependencies == [
1078+
"echo pre_system_dependencies"
1079+
]
1080+
assert build.build.jobs.post_system_dependencies == [
1081+
"echo post_system_dependencies"
1082+
]
1083+
assert build.build.jobs.pre_create_environment == [
1084+
"echo pre_create_environment"
1085+
]
1086+
assert build.build.jobs.post_create_environment == [
1087+
"echo post_create_environment"
1088+
]
1089+
assert build.build.jobs.pre_install == ["echo pre_install", "echo `date`"]
1090+
assert build.build.jobs.post_install == ["echo post_install"]
1091+
assert build.build.jobs.pre_build == [
1092+
"echo pre_build",
1093+
'sed -i -e "s|{VERSION}|${READTHEDOCS_VERSION_NAME}|g"',
1094+
]
1095+
assert build.build.jobs.post_build == ["echo post_build"]
1096+
10151097
@pytest.mark.parametrize(
10161098
'value',
10171099
[
@@ -2297,6 +2379,18 @@ def test_as_dict_new_build_config(self, tmpdir):
22972379
'full_version': settings.RTD_DOCKER_BUILD_SETTINGS['tools']['nodejs']['16'],
22982380
},
22992381
},
2382+
"jobs": {
2383+
"pre_checkout": [],
2384+
"post_checkout": [],
2385+
"pre_system_dependencies": [],
2386+
"post_system_dependencies": [],
2387+
"pre_create_environment": [],
2388+
"post_create_environment": [],
2389+
"pre_install": [],
2390+
"post_install": [],
2391+
"pre_build": [],
2392+
"post_build": [],
2393+
},
23002394
'apt_packages': [],
23012395
},
23022396
'conda': None,

readthedocs/projects/tests/test_build_tasks.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,46 @@ def test_build_tools(self, load_yaml_config):
732732
]
733733
)
734734

735+
@mock.patch("readthedocs.doc_builder.director.load_yaml_config")
736+
def test_build_jobs(self, load_yaml_config):
737+
config = BuildConfigV2(
738+
{},
739+
{
740+
"version": 2,
741+
"build": {
742+
"os": "ubuntu-20.04",
743+
"tools": {"python": "3.7"},
744+
"jobs": {
745+
"post_checkout": ["git fetch --unshallow"],
746+
"pre_build": ["echo `date`"],
747+
},
748+
},
749+
},
750+
source_file="readthedocs.yml",
751+
)
752+
config.validate()
753+
load_yaml_config.return_value = config
754+
755+
self._trigger_update_docs_task()
756+
757+
self.mocker.mocks["environment.run"].assert_has_calls(
758+
[
759+
mock.call(
760+
"git", "fetch", "--unshallow", escape_command=False, cwd=mock.ANY
761+
),
762+
# Don't care about the intermediate commands. They are checked
763+
# in other tests
764+
mock.ANY,
765+
mock.ANY,
766+
mock.ANY,
767+
mock.ANY,
768+
mock.ANY,
769+
mock.ANY,
770+
mock.ANY,
771+
mock.call("echo", "`date`", escape_command=False, cwd=mock.ANY),
772+
]
773+
)
774+
735775
@mock.patch("readthedocs.doc_builder.python_environments.tarfile")
736776
@mock.patch("readthedocs.doc_builder.python_environments.build_tools_storage")
737777
@mock.patch("readthedocs.doc_builder.director.load_yaml_config")

0 commit comments

Comments
 (0)