Skip to content

Commit

Permalink
FIX: extend RTD post_install (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer authored Mar 13, 2024
1 parent 8f6f65e commit 053a7ac
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 22 deletions.
13 changes: 5 additions & 8 deletions src/compwa_policy/check_dev_files/readthedocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down
14 changes: 14 additions & 0 deletions tests/check_dev_files/readthedocs/extend/.readthedocs-bad.yml
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions tests/check_dev_files/readthedocs/extend/.readthedocs-good.yml
Original file line number Diff line number Diff line change
@@ -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
50 changes: 36 additions & 14 deletions tests/check_dev_files/readthedocs/test_readthedocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 053a7ac

Please sign in to comment.