1
+ import re
2
+ import yaml
3
+ import shutil
4
+ import pytest
5
+
6
+ from mkdocs_git_revision_date_localized_plugin .util import Util
7
+
8
+ from click .testing import CliRunner
9
+ from mkdocs .__main__ import build_command
10
+
11
+ def load_config (mkdocs_path ):
12
+ return yaml .load (open (mkdocs_path , 'rb' ), Loader = yaml .Loader )
13
+
14
+ def build_docs_setup (mkdocs_path , output_path ):
15
+ runner = CliRunner ()
16
+ return runner .invoke (build_command ,
17
+ ['--config-file' ,
18
+ mkdocs_path ,
19
+ '--site-dir' ,
20
+ str (output_path )])
21
+
22
+ def built_site_tests (path ):
23
+ # Make sure there is proper output
24
+ index_file = path / 'index.html'
25
+ assert index_file .exists (), f"{ index_file } does not exist"
26
+
27
+ # Make sure there is some output for the tag
28
+ contents = index_file .read_text ()
29
+ assert re .search (r"Markdown tag\:\s[\w].+" , contents )
30
+
31
+
32
+ def test_date_formats ():
33
+ u = Util ()
34
+ assert u ._date_formats (1582397529 ) == {
35
+ 'date' : 'February 22, 2020' ,
36
+ 'datetime' : 'February 22, 2020 18:52:09' ,
37
+ 'iso_date' : '2020-02-22' ,
38
+ 'iso_datetime' : '2020-02-22 18:52:09' ,
39
+ 'timeago' : "<span class='timeago' datetime='1582397529000' locale='en'></span>"
40
+ }
41
+
42
+ def test_basic_locale_builds (tmp_path ):
43
+ """
44
+ Test some different settings in mkdocs.yml
45
+ """
46
+ # No config
47
+ result = build_docs_setup ('tests/basic_setup/mkdocs.yml' , tmp_path )
48
+ assert result .exit_code == 0 , "'mkdocs build' command failed"
49
+ built_site_tests (tmp_path )
50
+
51
+ # Plugin locale set
52
+ result = build_docs_setup ('tests/basic_setup/mkdocs_plugin_locale.yml' , tmp_path )
53
+ assert result .exit_code == 0 , "'mkdocs build' command failed"
54
+ built_site_tests (tmp_path )
55
+
56
+ # Mkdocs locale set
57
+ result = build_docs_setup ('tests/basic_setup/mkdocs_locale.yml' , tmp_path )
58
+ assert result .exit_code == 0 , "'mkdocs build' command failed"
59
+ built_site_tests (tmp_path )
60
+
61
+
62
+ def test_material_theme (tmp_path ):
63
+ """
64
+ Test correct working of mkdocs material theme
65
+ """
66
+ # With the mkdocs-material theme:
67
+ result = build_docs_setup ('tests/basic_setup/mkdocs_theme_locale.yml' , tmp_path )
68
+ assert result .exit_code == 0 , "'mkdocs build' command failed"
69
+ built_site_tests (tmp_path )
70
+
71
+ # In mkdocs-material, a 'last update' should appear
72
+ # in German because locale is set to 'de'
73
+ index_file = tmp_path / 'index.html'
74
+ built_site_tests (tmp_path )
75
+ contents = index_file .read_text ()
76
+ assert re .search (r"Letztes Update\:\s[\w].+" , contents )
77
+
78
+
79
+ def test_type_builds (tmp_path ):
80
+ """
81
+ Test the different 'type' parameters
82
+ """
83
+
84
+ # type: 'timeago'
85
+ result = build_docs_setup ('tests/basic_setup/mkdocs_timeago.yml' , tmp_path )
86
+ assert result .exit_code == 0 , "'mkdocs build' command failed but should have succeeded"
87
+ index_file = tmp_path / 'index.html'
88
+ contents = index_file .read_text ()
89
+ assert re .search ("<span class='timeago'" , contents )
90
+
91
+ # type: 'datetime'
92
+ result = build_docs_setup ('tests/basic_setup/mkdocs_datetime.yml' , tmp_path )
93
+ assert result .exit_code == 0 , "'mkdocs build' command failed but should have succeeded"
94
+ built_site_tests (tmp_path )
95
+
96
+ # unknown type
97
+ result = build_docs_setup ('tests/basic_setup/mkdocs_unknown_type.yml' , tmp_path )
98
+ assert result .exit_code == 1 , "'mkdocs build' command succeeded but should have failed"
99
+
100
+
101
+ def test_low_fetch_depth (tmp_path ):
102
+ """
103
+ On gitlab and github runners, a GIT might have a low fetch
104
+ depth, which means commits are not available.
105
+ This should throw informative errors.
106
+ """
107
+
108
+ pass
109
+
110
+ # Test correct error messages when GIT is not available
111
+ #target_dir = os.path.join(tmp_path, 'nogit')
112
+ #shutil.copytree(os.path.join(os.getcwd(), 'test/basic_setup'),
113
+ # target_dir)
114
+
115
+ # ...
116
+ #result = build_docs_setup(os.path.join(target_dir,'mkdocs.yml'), target_dir)
117
+
118
+ #with pytest.warns(UserWarning):
119
+ # assert result.exit_code == 0, "'mkdocs build' command failed"
120
+
0 commit comments