From 4100336fba3a362ac45def3aa8e79e9af76eed7e Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Fri, 22 Aug 2014 11:57:48 -0400 Subject: Added `--extension_configs` option to the CLI. The `--extension_configs` option must point to a YAML or JSON file. The contents of the file must parse to a Python Dict which will be passed to the `extension_configs` keyword of the `markdown.Markdown` class. Also added tests for all of the CLI option parsing options and updated documentation. --- tests/test_apis.py | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) (limited to 'tests') diff --git a/tests/test_apis.py b/tests/test_apis.py index 7a0147a..5117ccd 100644 --- a/tests/test_apis.py +++ b/tests/test_apis.py @@ -10,9 +10,14 @@ Tests of the various APIs with the python markdown lib. from __future__ import unicode_literals import unittest import sys +import os import types import markdown import warnings +from markdown.__main__ import parse_options +from logging import DEBUG, INFO, CRITICAL +import yaml +import tempfile PY3 = sys.version_info[0] == 3 @@ -433,3 +438,139 @@ class TestConfigParsing(unittest.TestCase): def testInvalidBooleansParsing(self): self.assertRaises(ValueError, markdown.util.parseBoolValue, 'novalue') + +class TestCliOptionParsing(unittest.TestCase): + """ Test parsing of Command Line Interface Options. """ + + def setUp(self): + self.default_options = { + 'input': None, + 'output': None, + 'encoding': None, + 'safe_mode': False, + 'output_format': 'xhtml1', + 'lazy_ol': True, + 'extensions': [], + 'extension_configs': {}, + } + self.tempfile = '' + + def tearDown(self): + if os.path.isfile(self.tempfile): + os.remove(self.tempfile) + + def testNoOptions(self): + options, logging_level = parse_options([]) + self.assertEqual(options, self.default_options) + self.assertEqual(logging_level, CRITICAL) + + def testQuietOption(self): + options, logging_level = parse_options(['-q']) + self.assertTrue(logging_level > CRITICAL) + + def testVerboseOption(self): + options, logging_level = parse_options(['-v']) + self.assertEqual(logging_level, INFO) + + def testNoisyOption(self): + options, logging_level = parse_options(['--noisy']) + self.assertEqual(logging_level, DEBUG) + + def testInputFileOption(self): + options, logging_level = parse_options(['foo.txt']) + self.default_options['input'] = 'foo.txt' + self.assertEqual(options, self.default_options) + + def testOutputFileOption(self): + options, logging_level = parse_options(['-f', 'foo.html']) + self.default_options['output'] = 'foo.html' + self.assertEqual(options, self.default_options) + + def testInputAndOutputFileOptions(self): + options, logging_level = parse_options(['-f', 'foo.html', 'foo.txt']) + self.default_options['output'] = 'foo.html' + self.default_options['input'] = 'foo.txt' + self.assertEqual(options, self.default_options) + + def testEncodingOption(self): + options, logging_level = parse_options(['-e', 'utf-8']) + self.default_options['encoding'] = 'utf-8' + self.assertEqual(options, self.default_options) + + def testSafeModeOption(self): + options, logging_level = parse_options(['-s', 'escape']) + self.default_options['safe_mode'] = 'escape' + self.assertEqual(options, self.default_options) + + def testOutputFormatOption(self): + options, logging_level = parse_options(['-o', 'html5']) + self.default_options['output_format'] = 'html5' + self.assertEqual(options, self.default_options) + + def testNoLazyOlOption(self): + options, logging_level = parse_options(['-n']) + self.default_options['lazy_ol'] = False + self.assertEqual(options, self.default_options) + + def testExtensionOption(self): + options, logging_level = parse_options(['-x', 'footnotes']) + self.default_options['extensions'] = ['footnotes'] + self.assertEqual(options, self.default_options) + + def testMultipleExtensionOptions(self): + options, logging_level = parse_options(['-x', 'footnotes', '-x', 'smarty']) + self.default_options['extensions'] = ['footnotes', 'smarty'] + self.assertEqual(options, self.default_options) + + def create_config_file(self, config): + """ Helper to create temp config files. """ + if not isinstance(config, markdown.util.string_type): + # convert to string + config = yaml.dump(config) + fd, self.tempfile = tempfile.mkstemp('.yml') + with os.fdopen(fd, 'w') as fp: + fp.write(config) + + def testExtensonConfigOption(self): + config = { + 'wikilinks': { + 'base_url': 'http://example.com/', + 'end_url': '.html', + 'html_class': 'test', + }, + 'footnotes': { + 'PLACE_MARKER': '~~~footnotes~~~' + } + } + self.create_config_file(config) + options, logging_level = parse_options(['-c', self.tempfile]) + self.default_options['extension_configs'] = config + self.assertEqual(options, self.default_options) + + def testExtensonConfigOptionAsJSON(self): + config = { + 'wikilinks': { + 'base_url': 'http://example.com/', + 'end_url': '.html', + 'html_class': 'test', + }, + 'footnotes': { + 'PLACE_MARKER': '~~~footnotes~~~' + } + } + import json + self.create_config_file(json.dumps(config)) + options, logging_level = parse_options(['-c', self.tempfile]) + self.default_options['extension_configs'] = config + self.assertEqual(options, self.default_options) + + def testExtensonConfigOptionMissingFile(self): + self.assertRaises(IOError, parse_options, ['-c', 'missing_file.yaml']) + + def testExtensonConfigOptionBadFormat(self): + config = """ +[footnotes] +PLACE_MARKER= ~~~footnotes~~~ +""" + self.create_config_file(config) + self.assertRaises(yaml.YAMLError, parse_options, ['-c', self.tempfile]) \ No newline at end of file -- cgit v1.2.3