File: config_tests.py

package info (click to toggle)
python-mkdocs 0.15.3-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,476 kB
  • ctags: 1,313
  • sloc: python: 3,840; makefile: 22; xml: 20
file content (213 lines) | stat: -rw-r--r-- 7,490 bytes parent folder | download
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python
# coding: utf-8

from __future__ import unicode_literals
import os
import shutil
import tempfile
import unittest

from mkdocs import config
from mkdocs import utils
from mkdocs.config import config_options
from mkdocs.exceptions import ConfigurationError
from mkdocs.tests.base import dedent


def ensure_utf(string):
    return string.encode('utf-8') if not utils.PY3 else string


class ConfigTests(unittest.TestCase):
    def test_missing_config_file(self):

        def load_missing_config():
            config.load_config(config_file='bad_filename.yaml')
        self.assertRaises(ConfigurationError, load_missing_config)

    def test_missing_site_name(self):
        c = config.Config(schema=config.DEFAULT_SCHEMA)
        c.load_dict({})
        errors, warnings = c.validate()
        self.assertEqual(len(errors), 1)
        self.assertEqual(errors[0][0], 'site_name')
        self.assertEqual(str(errors[0][1]), 'Required configuration not provided.')

        self.assertEqual(len(warnings), 0)

    def test_empty_config(self):
        def load_empty_config():
            config.load_config(config_file='/dev/null')
        self.assertRaises(ConfigurationError, load_empty_config)

    def test_nonexistant_config(self):
        def load_empty_config():
            config.load_config(config_file='/path/that/is/not/real')
        self.assertRaises(ConfigurationError, load_empty_config)

    def test_invalid_config(self):
        file_contents = dedent("""
        - ['index.md', 'Introduction']
        - ['index.md', 'Introduction']
        - ['index.md', 'Introduction']
        """)
        config_file = tempfile.NamedTemporaryFile('w', delete=False)
        try:
            config_file.write(ensure_utf(file_contents))
            config_file.flush()
            config_file.close()

            self.assertRaises(
                ConfigurationError,
                config.load_config, config_file=open(config_file.name, 'rb')
            )
        finally:
            os.remove(config_file.name)

    def test_config_option(self):
        """
        Users can explicitly set the config file using the '--config' option.
        Allows users to specify a config other than the default `mkdocs.yml`.
        """
        expected_result = {
            'site_name': 'Example',
            'pages': [
                {'Introduction': 'index.md'}
            ],
        }
        file_contents = dedent("""
        site_name: Example
        pages:
        - ['index.md', 'Introduction']
        """)
        config_file = tempfile.NamedTemporaryFile('w', delete=False)
        try:
            config_file.write(ensure_utf(file_contents))
            config_file.flush()
            config_file.close()

            result = config.load_config(config_file=config_file.name)
            self.assertEqual(result['site_name'], expected_result['site_name'])
            self.assertEqual(result['pages'], expected_result['pages'])
        finally:
            os.remove(config_file.name)

    def test_theme(self):

        mytheme = tempfile.mkdtemp()
        custom = tempfile.mkdtemp()

        configs = [
            dict(),  # default theme
            {"theme": "readthedocs"},  # builtin theme
            {"theme_dir": mytheme},  # custom only
            {"theme": "readthedocs", "theme_dir": custom},  # builtin and custom
        ]

        abs_path = os.path.abspath(os.path.dirname(__file__))
        mkdocs_dir = os.path.abspath(os.path.join(abs_path, '..', '..'))
        theme_dir = os.path.abspath(os.path.join(mkdocs_dir, 'themes'))
        search_asset_dir = os.path.abspath(os.path.join(
            mkdocs_dir, 'assets', 'search'))

        results = (
            [os.path.join(theme_dir, 'mkdocs'), search_asset_dir],
            [os.path.join(theme_dir, 'readthedocs'), search_asset_dir],
            [mytheme, search_asset_dir],
            [custom, os.path.join(theme_dir, 'readthedocs'), search_asset_dir],
        )

        for config_contents, result in zip(configs, results):

            c = config.Config(schema=(
                ('theme', config_options.Theme(default='mkdocs')),
                ('theme_dir', config_options.ThemeDir(exists=True)),
            ))
            c.load_dict(config_contents)
            c.validate()
            self.assertEqual(c['theme_dir'], result)

    def test_default_pages(self):
        tmp_dir = tempfile.mkdtemp()
        try:
            open(os.path.join(tmp_dir, 'index.md'), 'w').close()
            open(os.path.join(tmp_dir, 'about.md'), 'w').close()
            conf = config.Config(schema=config.DEFAULT_SCHEMA)
            conf.load_dict({
                'site_name': 'Example',
                'docs_dir': tmp_dir
            })
            conf.validate()
            self.assertEqual(['index.md', 'about.md'], conf['pages'])
        finally:
            shutil.rmtree(tmp_dir)

    def test_default_pages_nested(self):
        tmp_dir = tempfile.mkdtemp()
        try:
            open(os.path.join(tmp_dir, 'index.md'), 'w').close()
            open(os.path.join(tmp_dir, 'getting-started.md'), 'w').close()
            open(os.path.join(tmp_dir, 'about.md'), 'w').close()
            os.makedirs(os.path.join(tmp_dir, 'subA'))
            open(os.path.join(tmp_dir, 'subA', 'index.md'), 'w').close()
            os.makedirs(os.path.join(tmp_dir, 'subA', 'subA1'))
            open(os.path.join(tmp_dir, 'subA', 'subA1', 'index.md'), 'w').close()
            os.makedirs(os.path.join(tmp_dir, 'subC'))
            open(os.path.join(tmp_dir, 'subC', 'index.md'), 'w').close()
            os.makedirs(os.path.join(tmp_dir, 'subB'))
            open(os.path.join(tmp_dir, 'subB', 'index.md'), 'w').close()
            conf = config.Config(schema=config.DEFAULT_SCHEMA)
            conf.load_dict({
                'site_name': 'Example',
                'docs_dir': tmp_dir
            })
            conf.validate()
            self.assertEqual([
                'index.md',
                'about.md',
                'getting-started.md',
                {'subA': [
                    os.path.join('subA', 'index.md'),
                    {'subA1': [
                        os.path.join('subA', 'subA1', 'index.md')
                    ]}
                ]},
                {'subB': [
                    os.path.join('subB', 'index.md')
                ]},
                {'subC': [
                    os.path.join('subC', 'index.md')
                ]}
            ], conf['pages'])
        finally:
            shutil.rmtree(tmp_dir)

    def test_doc_dir_in_site_dir(self):

        j = os.path.join

        test_configs = (
            {'docs_dir': j('site', 'docs'), 'site_dir': 'site'},
            {'docs_dir': 'docs', 'site_dir': '.'},
            {'docs_dir': '.', 'site_dir': '.'},
            {'docs_dir': 'docs', 'site_dir': ''},
            {'docs_dir': '', 'site_dir': ''},
        )

        conf = {
            'site_name': 'Example',
        }

        for test_config in test_configs:

            patch = conf.copy()
            patch.update(test_config)

            # Same as the default schema, but don't verify the docs_dir exists.
            c = config.Config(schema=(
                ('docs_dir', config_options.Dir(default='docs')),
                ('site_dir', config_options.SiteDir(default='site')),
            ))
            c.load_dict(patch)

            self.assertRaises(config_options.ValidationError, c.validate)