File: test_doc_examples.py

package info (click to toggle)
moviepy 2.1.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 96,920 kB
  • sloc: python: 10,566; makefile: 150; sh: 6
file content (58 lines) | stat: -rw-r--r-- 1,522 bytes parent folder | download | duplicates (2)
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
"""Try to run all the documentation examples with runpy and check they don't raise
exceptions.
"""

import os
import pathlib
import runpy
import shutil
from contextlib import contextmanager

import pytest

from moviepy.tools import no_display_available


@contextmanager
def cwd(path):
    oldpwd = os.getcwd()
    os.chdir(path)
    try:
        yield
    finally:
        os.chdir(oldpwd)


# Dir for doc code examples to run
DOC_EXAMPLES_DIR = "docs/_static/code"

# List of examples script to ignore, mostly scripts that are too long
DOC_EXAMPLES_IGNORE = ["trailer.py", "display_in_notebook.py"]

# If no display, also remove all examples using preview
if no_display_available():
    DOC_EXAMPLES_IGNORE.append("preview.py")

scripts = list(pathlib.Path(DOC_EXAMPLES_DIR).resolve().rglob("*.py"))
scripts = dict(zip(map(str, scripts), scripts))  # This make test name more readable


@pytest.mark.parametrize("script", scripts)
def test_doc_examples(util, tmp_path, script):
    if os.path.basename(script) == "preview.py":
        pytest.skip("Skipping preview.py because no display is available")
    print("Try script: ", script)

    if os.path.basename(script) in DOC_EXAMPLES_IGNORE:
        return

    # Lets build a test dir with all medias needed to run our test in
    shutil.copytree(util.DOC_EXAMPLES_MEDIAS_DIR, os.path.join(tmp_path, "doc_tests"))
    test_dir = os.path.join(tmp_path, "doc_tests")

    with cwd(test_dir):
        runpy.run_path(script)


if __name__ == "__main__":
    pytest.main()