File: test_examples.py

package info (click to toggle)
mkdocs-include-markdown-plugin 7.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 1,316 kB
  • sloc: python: 5,360; makefile: 6
file content (60 lines) | stat: -rw-r--r-- 1,700 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
59
60
import os
import subprocess
import sys

import pytest
from mkdocs import config
from mkdocs.commands.build import build
from mkdocs.exceptions import Abort

from mkdocs_include_markdown_plugin.cache import is_platformdirs_installed
from testing_helpers import rootdir


EXAMPLES_DIR = os.path.join(rootdir, 'examples')


def config_is_using_cache_setting(config_file_path):
    with open(config_file_path, encoding='utf-8') as f:
        return 'cache:' in f.read()


@pytest.mark.parametrize('dirname', os.listdir(EXAMPLES_DIR))
def test_examples_subprocess(dirname):
    example_dir = os.path.join(EXAMPLES_DIR, dirname)
    config_file = os.path.join(example_dir, 'mkdocs.yml')
    expected_returncode = 1 if config_is_using_cache_setting(
        config_file,
    ) and not is_platformdirs_installed() else 0

    proc = subprocess.Popen(
        [sys.executable, '-mmkdocs', 'build'],
        cwd=example_dir,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    stdout, stderr = proc.communicate()

    assert proc.returncode == expected_returncode, (
        f'{stdout.decode("utf-8")}\n{stderr.decode("utf-8")}'
    )


@pytest.mark.parametrize('dirname', os.listdir(EXAMPLES_DIR))
def test_examples_api(dirname):
    example_dir = os.path.join(EXAMPLES_DIR, dirname)
    config_file = os.path.join(example_dir, 'mkdocs.yml')
    expected_to_raise_exc = (
        config_is_using_cache_setting(config_file) and
        not is_platformdirs_installed()
    )

    def run():
        cfg = config.load_config(config_file=config_file)
        build(cfg, dirty=False)

    if expected_to_raise_exc:
        with pytest.raises(Abort):
            run()
    else:
        run()