def test_theme(self): mytheme = tempfile.mkdtemp() custom = tempfile.mkdtemp() configs = [ dict(), # default theme {"theme": "readthedocs"}, # builtin theme {"theme_dir": mytheme}, # custom only {"theme": "cosmo", "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, 'cosmo'), search_asset_dir], ) for config_contents, result in six.moves.zip(configs, results): c = base.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_doc_dir_in_site_dir(self): j = os.path.join test_configs = ( {'docs_dir': 'docs', 'site_dir': j('docs', 'site')}, {'docs_dir': j('site', 'docs'), 'site_dir': 'site'}, {'docs_dir': 'docs', 'site_dir': '.'}, {'docs_dir': '.', 'site_dir': 'site'}, {'docs_dir': '.', 'site_dir': '.'}, {'docs_dir': 'docs', 'site_dir': ''}, {'docs_dir': '', 'site_dir': 'site'}, {'docs_dir': '', 'site_dir': ''}, {'docs_dir': j('..', 'mkdocs', 'docs'), 'site_dir': 'docs'}, ) 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 = base.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)
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, 'about.md'), 'w').close() os.makedirs(os.path.join(tmp_dir, 'sub')) open(os.path.join(tmp_dir, 'sub', 'sub.md'), 'w').close() os.makedirs(os.path.join(tmp_dir, 'sub', 'sub2')) open(os.path.join(tmp_dir, 'sub', 'sub2', 'sub2.md'), 'w').close() conf = base.Config(schema=defaults.DEFAULT_SCHEMA) conf.load_dict({ 'site_name': 'Example', 'docs_dir': tmp_dir }) conf.validate() self.assertEqual([ 'index.md', 'about.md', {'Sub': [ os.path.join('sub', 'sub.md'), {'Sub2': [ os.path.join('sub', 'sub2', 'sub2.md'), ]} ]} ], conf['pages']) finally: shutil.rmtree(tmp_dir)
def test_missing_site_name(self): c = base.Config(schema=defaults.DEFAULT_SCHEMA) c.load_dict({}) errors, warings = c.validate() self.assertEqual([ ('site_name', 'Required configuration not provided.') ], errors) self.assertEqual([], warings)
def test_unrecognised_keys(self): c = base.Config(schema=defaults.get_schema()) c.load_dict({'not_a_valid_config_option': "test"}) failed, warnings = c.validate() self.assertIn( ('not_a_valid_config_option', 'Unrecognised configuration name: not_a_valid_config_option'), warnings)
def test_unrecognised_keys(self): c = base.Config(schema=defaults.DEFAULT_SCHEMA) c.load_dict({'not_a_valid_config_option': "test"}) failed, warnings = c.validate() self.assertEqual( warnings, [('not_a_valid_config_option', 'Unrecognised configuration name: not_a_valid_config_option')])
def test_missing_required(self): c = base.Config(schema=defaults.DEFAULT_SCHEMA) 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_extra_context(self): # Same as the default schema, but don't verify the docs_dir exists. config = config_base.Config(schema=config_defaults.DEFAULT_SCHEMA) config.load_dict({'site_name': "Site", 'extra': {'a': 1}}) self.assertEqual(config.validate(), ([], [])) context = build.get_global_context(mock.Mock(), config) self.assertEqual(context['extra'], {'a': 1})
def test_missing_required(self): c = base.Config(schema=defaults.DEFAULT_SCHEMA) failed, warnings = c.validate() self.assertEqual(failed, [ ('site_name', 'Required configuration not provided.') ]) self.assertEqual(warnings, [])
def test_post_validation_error(self): class InvalidConfigOption(BaseConfigOption): def post_validation(self, config, key_name): raise base.ValidationError('post_validation error') c = base.Config(schema=(('invalid_option', InvalidConfigOption()), )) errors, warnings = c.validate() self.assertEqual(len(errors), 1) self.assertEqual(errors[0][0], 'invalid_option') self.assertEqual(str(errors[0][1]), 'post_validation error') self.assertTrue(isinstance(errors[0][1], base.ValidationError)) self.assertEqual(len(warnings), 0)
def test_copying_media(self): docs_dir = tempfile.mkdtemp() site_dir = tempfile.mkdtemp() try: # Create a non-empty markdown file, image, dot file and dot directory. f = open(os.path.join(docs_dir, 'index.md'), 'w') f.write( dedent(""" page_title: custom title # Heading 1 This is some text. # Heading 2 And some more text. """)) f.close() open(os.path.join(docs_dir, 'img.jpg'), 'w').close() open(os.path.join(docs_dir, '.hidden'), 'w').close() os.mkdir(os.path.join(docs_dir, '.git')) open(os.path.join(docs_dir, '.git/hidden'), 'w').close() conf = config_base.Config(schema=config_defaults.DEFAULT_SCHEMA) conf.load_dict({ 'site_name': 'Example', 'docs_dir': docs_dir, 'site_dir': site_dir }) conf.validate() build.build(conf) # Verify only the markdown (coverted to html) and the image are copied. self.assertTrue( os.path.isfile(os.path.join(site_dir, 'index.html'))) self.assertTrue(os.path.isfile(os.path.join(site_dir, 'img.jpg'))) self.assertFalse(os.path.isfile(os.path.join(site_dir, '.hidden'))) self.assertFalse( os.path.isfile(os.path.join(site_dir, '.git/hidden'))) finally: shutil.rmtree(docs_dir) shutil.rmtree(site_dir)
def test_validation_warnings(self): class InvalidConfigOption(BaseConfigOption): def pre_validation(self, config, key_name): self.warnings.append('pre_validation warning') def run_validation(self, value): self.warnings.append('run_validation warning') def post_validation(self, config, key_name): self.warnings.append('post_validation warning') c = base.Config(schema=(('invalid_option', InvalidConfigOption()), )) errors, warnings = c.validate() self.assertEqual(len(errors), 0) self.assertEqual(warnings, [ ('invalid_option', 'pre_validation warning'), ('invalid_option', 'run_validation warning'), ('invalid_option', 'post_validation warning'), ])
def test_pre_and_run_validation_errors(self): """ A pre_validation error does not stop run_validation from running. """ class InvalidConfigOption(BaseConfigOption): def pre_validation(self, config, key_name): raise base.ValidationError('pre_validation error') def run_validation(self, value): raise base.ValidationError('run_validation error') c = base.Config(schema=(('invalid_option', InvalidConfigOption()), )) errors, warnings = c.validate() self.assertEqual(len(errors), 2) self.assertEqual(errors[0][0], 'invalid_option') self.assertEqual(str(errors[0][1]), 'pre_validation error') self.assertTrue(isinstance(errors[0][1], base.ValidationError)) self.assertEqual(errors[1][0], 'invalid_option') self.assertEqual(str(errors[1][1]), 'run_validation error') self.assertTrue(isinstance(errors[1][1], base.ValidationError)) self.assertEqual(len(warnings), 0)