From 053a7ac33049b76548bc3c11b28c7da942995052 Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Wed, 13 Mar 2024 23:12:48 +0100 Subject: [PATCH] FIX: extend RTD `post_install` (#330) --- .../check_dev_files/readthedocs.py | 13 ++--- .../readthedocs/extend/.readthedocs-bad.yml | 14 ++++++ .../readthedocs/extend/.readthedocs-good.yml | 16 ++++++ .../{ => overwrite}/.readthedocs-bad1.yml | 0 .../{ => overwrite}/.readthedocs-bad2.yml | 0 .../{ => overwrite}/.readthedocs-good.yml | 0 .../readthedocs/test_readthedocs.py | 50 +++++++++++++------ 7 files changed, 71 insertions(+), 22 deletions(-) create mode 100644 tests/check_dev_files/readthedocs/extend/.readthedocs-bad.yml create mode 100644 tests/check_dev_files/readthedocs/extend/.readthedocs-good.yml rename tests/check_dev_files/readthedocs/{ => overwrite}/.readthedocs-bad1.yml (100%) rename tests/check_dev_files/readthedocs/{ => overwrite}/.readthedocs-bad2.yml (100%) rename tests/check_dev_files/readthedocs/{ => overwrite}/.readthedocs-good.yml (100%) diff --git a/src/compwa_policy/check_dev_files/readthedocs.py b/src/compwa_policy/check_dev_files/readthedocs.py index 119ada65..11354dce 100644 --- a/src/compwa_policy/check_dev_files/readthedocs.py +++ b/src/compwa_policy/check_dev_files/readthedocs.py @@ -28,7 +28,7 @@ def main( rtd = ReadTheDocs(source) _update_os(rtd) _update_python_version(rtd, python_version) - _update_install_step(rtd, python_version) + _update_post_install(rtd, python_version) rtd.finalize() @@ -60,11 +60,9 @@ def _update_python_version(config: ReadTheDocs, python_version: PythonVersion) - config.changelog.append(msg) -def _update_install_step(config: ReadTheDocs, python_version: PythonVersion) -> None: +def _update_post_install(config: ReadTheDocs, python_version: PythonVersion) -> None: jobs = get_nested_dict(config.document, ["build", "jobs"]) - if "post_install" not in jobs: - jobs["post_install"] = [] - steps: list[str] = jobs["post_install"] + steps: list[str] = jobs.get("post_install", []) if steps is None: return expected_steps = __get_install_steps(python_version) @@ -76,12 +74,11 @@ def _update_install_step(config: ReadTheDocs, python_version: PythonVersion) -> existing_steps = steps[start:end] if existing_steps == expected_steps: return - steps.clear() # update the reference in the post_install dict! - steps.extend([ + jobs["post_install"] = [ *steps[:start], *expected_steps, *steps[end:], - ]) + ] msg = "Updated pip install steps" config.changelog.append(msg) diff --git a/tests/check_dev_files/readthedocs/extend/.readthedocs-bad.yml b/tests/check_dev_files/readthedocs/extend/.readthedocs-bad.yml new file mode 100644 index 00000000..a4783463 --- /dev/null +++ b/tests/check_dev_files/readthedocs/extend/.readthedocs-bad.yml @@ -0,0 +1,14 @@ +version: 2 +build: + os: ubuntu-20.04 + tools: + python: "3.10" + jobs: + post_install: + - pip install -e .[doc] + - | + wget https://julialang-s3.julialang.org/bin/linux/x64/1.9/julia-1.9.2-linux-x86_64.tar.gz + - tar xzf julia-1.9.2-linux-x86_64.tar.gz + - mkdir bin + - ln -s $PWD/julia-1.9.2/bin/julia bin/julia + - ./bin/julia docs/InstallIJulia.jl diff --git a/tests/check_dev_files/readthedocs/extend/.readthedocs-good.yml b/tests/check_dev_files/readthedocs/extend/.readthedocs-good.yml new file mode 100644 index 00000000..6fb931ac --- /dev/null +++ b/tests/check_dev_files/readthedocs/extend/.readthedocs-good.yml @@ -0,0 +1,16 @@ +version: 2 +build: + os: ubuntu-22.04 + tools: + python: "3.9" + jobs: + post_install: + - curl -LsSf https://astral.sh/uv/install.sh | sh + - |- + /home/docs/.cargo/bin/uv pip install --system -e .[doc] + - | + wget https://julialang-s3.julialang.org/bin/linux/x64/1.9/julia-1.9.2-linux-x86_64.tar.gz + - tar xzf julia-1.9.2-linux-x86_64.tar.gz + - mkdir bin + - ln -s $PWD/julia-1.9.2/bin/julia bin/julia + - ./bin/julia docs/InstallIJulia.jl diff --git a/tests/check_dev_files/readthedocs/.readthedocs-bad1.yml b/tests/check_dev_files/readthedocs/overwrite/.readthedocs-bad1.yml similarity index 100% rename from tests/check_dev_files/readthedocs/.readthedocs-bad1.yml rename to tests/check_dev_files/readthedocs/overwrite/.readthedocs-bad1.yml diff --git a/tests/check_dev_files/readthedocs/.readthedocs-bad2.yml b/tests/check_dev_files/readthedocs/overwrite/.readthedocs-bad2.yml similarity index 100% rename from tests/check_dev_files/readthedocs/.readthedocs-bad2.yml rename to tests/check_dev_files/readthedocs/overwrite/.readthedocs-bad2.yml diff --git a/tests/check_dev_files/readthedocs/.readthedocs-good.yml b/tests/check_dev_files/readthedocs/overwrite/.readthedocs-good.yml similarity index 100% rename from tests/check_dev_files/readthedocs/.readthedocs-good.yml rename to tests/check_dev_files/readthedocs/overwrite/.readthedocs-good.yml diff --git a/tests/check_dev_files/readthedocs/test_readthedocs.py b/tests/check_dev_files/readthedocs/test_readthedocs.py index adbe3805..f58df203 100644 --- a/tests/check_dev_files/readthedocs/test_readthedocs.py +++ b/tests/check_dev_files/readthedocs/test_readthedocs.py @@ -19,39 +19,61 @@ def this_dir() -> Path: return Path(__file__).parent -@pytest.mark.parametrize("python_version", ["3.9", "3.10"]) -@pytest.mark.parametrize("suffix", ["bad1", "bad2"]) -def test_update_readthedocs_bad( - this_dir: Path, python_version: PythonVersion, suffix: str -): - with open(this_dir / f".readthedocs-{suffix}.yml") as f: +def test_update_readthedocs_extend(this_dir: Path): + with open(this_dir / "extend" / ".readthedocs-bad.yml") as f: input_stream = io.StringIO(f.read()) with pytest.raises(PrecommitError) as exception: - readthedocs.main(python_version, source=input_stream) + readthedocs.main(python_version="3.9", source=input_stream) - exception_msg = dedent(f""" + exception_msg = dedent(""" Updated .readthedocs.yml: - Set build.os to ubuntu-22.04 - - Set build.tools.python to {python_version!r} + - Set build.tools.python to '3.9' - Updated pip install steps """) assert str(exception.value).strip() == exception_msg.strip() - with open(this_dir / ".readthedocs-good.yml") as f: + with open(this_dir / "extend" / ".readthedocs-good.yml") as f: expected_output = f.read() - expected_output = expected_output.replace("3.9", python_version) input_stream.seek(0) result = input_stream.read() assert result.strip() == expected_output.strip() -def test_update_readthedocs_good(this_dir: Path): - with open(this_dir / ".readthedocs-good.yml") as f: +@pytest.mark.parametrize("example", ["extend", "overwrite"]) +def test_update_readthedocs_good(this_dir: Path, example: str): + with open(this_dir / example / ".readthedocs-good.yml") as f: input_stream = io.StringIO(f.read()) readthedocs.main(python_version="3.9", source=input_stream) - with open(this_dir / ".readthedocs-good.yml") as f: + with open(this_dir / example / ".readthedocs-good.yml") as f: expected_output = f.read() input_stream.seek(0) result = input_stream.read() assert result.strip() == expected_output.strip() + + +@pytest.mark.parametrize("python_version", ["3.9", "3.10"]) +@pytest.mark.parametrize("suffix", ["bad1", "bad2"]) +def test_update_readthedocs_overwrite( + this_dir: Path, python_version: PythonVersion, suffix: str +): + with open(this_dir / "overwrite" / f".readthedocs-{suffix}.yml") as f: + input_stream = io.StringIO(f.read()) + with pytest.raises(PrecommitError) as exception: + readthedocs.main(python_version, source=input_stream) + + exception_msg = dedent(f""" + Updated .readthedocs.yml: + - Set build.os to ubuntu-22.04 + - Set build.tools.python to {python_version!r} + - Updated pip install steps + """) + assert str(exception.value).strip() == exception_msg.strip() + + with open(this_dir / "overwrite" / ".readthedocs-good.yml") as f: + expected_output = f.read() + expected_output = expected_output.replace("3.9", python_version) + input_stream.seek(0) + result = input_stream.read() + assert result.strip() == expected_output.strip()