1- import importlib
21import os
32import subprocess
43import sys
54
65import pytest
6+ from mkdocs import config
7+ from mkdocs .commands .build import build
8+ from mkdocs .exceptions import Abort
79from testing_helpers import rootdir
810
11+ from mkdocs_include_markdown_plugin .cache import CACHE_AVAILABLE
12+
913
1014EXAMPLES_DIR = os .path .join (rootdir , 'examples' )
1115
1216
13- @pytest .mark .parametrize ('dirname' , os .listdir (EXAMPLES_DIR ))
14- def test_examples (dirname ):
15- expected_returncode = 0
17+ def config_is_using_cache_setting (config_file_path ):
18+ with open (config_file_path , encoding = 'utf-8' ) as f :
19+ return 'cache:' in f .read ()
20+
1621
22+ @pytest .mark .parametrize ('dirname' , os .listdir (EXAMPLES_DIR ))
23+ def test_examples_subprocess (dirname ):
1724 example_dir = os .path .join (EXAMPLES_DIR , dirname )
1825 config_file = os .path .join (example_dir , 'mkdocs.yml' )
19- with open (config_file , encoding = 'utf-8' ) as f :
20- if 'cache:' in f .read ():
21- try :
22- importlib .import_module ('platformdirs' )
23- except ImportError :
24- expected_returncode = 1
26+ expected_returncode = 1 if config_is_using_cache_setting (
27+ config_file ,
28+ ) and not CACHE_AVAILABLE else 0
2529
2630 proc = subprocess .Popen (
2731 [sys .executable , '-mmkdocs' , 'build' ],
@@ -34,3 +38,22 @@ def test_examples(dirname):
3438 assert proc .returncode == expected_returncode , (
3539 f'{ stdout .decode ("utf-8" )} \n { stderr .decode ("utf-8" )} '
3640 )
41+
42+
43+ @pytest .mark .parametrize ('dirname' , os .listdir (EXAMPLES_DIR ))
44+ def test_examples_api (dirname ):
45+ example_dir = os .path .join (EXAMPLES_DIR , dirname )
46+ config_file = os .path .join (example_dir , 'mkdocs.yml' )
47+ expected_to_raise_exc = (
48+ config_is_using_cache_setting (config_file ) and not CACHE_AVAILABLE
49+ )
50+
51+ def run ():
52+ cfg = config .load_config (config_file = config_file )
53+ build (cfg , dirty = False )
54+
55+ if expected_to_raise_exc :
56+ with pytest .raises (Abort ):
57+ run ()
58+ else :
59+ run ()
0 commit comments