10
10
from __future__ import unicode_literals
11
11
import unittest
12
12
import sys
13
+ import os
13
14
import types
14
15
import markdown
15
16
import warnings
17
+ from markdown .__main__ import parse_options
18
+ from logging import DEBUG , INFO , CRITICAL
19
+ import yaml
20
+ import tempfile
16
21
17
22
PY3 = sys .version_info [0 ] == 3
18
23
@@ -433,3 +438,139 @@ def testBooleansParsing(self):
433
438
434
439
def testInvalidBooleansParsing (self ):
435
440
self .assertRaises (ValueError , markdown .util .parseBoolValue , 'novalue' )
441
+
442
+ class TestCliOptionParsing (unittest .TestCase ):
443
+ """ Test parsing of Command Line Interface Options. """
444
+
445
+ def setUp (self ):
446
+ self .default_options = {
447
+ 'input' : None ,
448
+ 'output' : None ,
449
+ 'encoding' : None ,
450
+ 'safe_mode' : False ,
451
+ 'output_format' : 'xhtml1' ,
452
+ 'lazy_ol' : True ,
453
+ 'extensions' : [],
454
+ 'extension_configs' : {},
455
+ }
456
+ self .tempfile = ''
457
+
458
+ def tearDown (self ):
459
+ if os .path .isfile (self .tempfile ):
460
+ os .remove (self .tempfile )
461
+
462
+ def testNoOptions (self ):
463
+ options , logging_level = parse_options ([])
464
+ self .assertEqual (options , self .default_options )
465
+ self .assertEqual (logging_level , CRITICAL )
466
+
467
+ def testQuietOption (self ):
468
+ options , logging_level = parse_options (['-q' ])
469
+ self .assertTrue (logging_level > CRITICAL )
470
+
471
+ def testVerboseOption (self ):
472
+ options , logging_level = parse_options (['-v' ])
473
+ self .assertEqual (logging_level , INFO )
474
+
475
+ def testNoisyOption (self ):
476
+ options , logging_level = parse_options (['--noisy' ])
477
+ self .assertEqual (logging_level , DEBUG )
478
+
479
+ def testInputFileOption (self ):
480
+ options , logging_level = parse_options (['foo.txt' ])
481
+ self .default_options ['input' ] = 'foo.txt'
482
+ self .assertEqual (options , self .default_options )
483
+
484
+ def testOutputFileOption (self ):
485
+ options , logging_level = parse_options (['-f' , 'foo.html' ])
486
+ self .default_options ['output' ] = 'foo.html'
487
+ self .assertEqual (options , self .default_options )
488
+
489
+ def testInputAndOutputFileOptions (self ):
490
+ options , logging_level = parse_options (['-f' , 'foo.html' , 'foo.txt' ])
491
+ self .default_options ['output' ] = 'foo.html'
492
+ self .default_options ['input' ] = 'foo.txt'
493
+ self .assertEqual (options , self .default_options )
494
+
495
+ def testEncodingOption (self ):
496
+ options , logging_level = parse_options (['-e' , 'utf-8' ])
497
+ self .default_options ['encoding' ] = 'utf-8'
498
+ self .assertEqual (options , self .default_options )
499
+
500
+ def testSafeModeOption (self ):
501
+ options , logging_level = parse_options (['-s' , 'escape' ])
502
+ self .default_options ['safe_mode' ] = 'escape'
503
+ self .assertEqual (options , self .default_options )
504
+
505
+ def testOutputFormatOption (self ):
506
+ options , logging_level = parse_options (['-o' , 'html5' ])
507
+ self .default_options ['output_format' ] = 'html5'
508
+ self .assertEqual (options , self .default_options )
509
+
510
+ def testNoLazyOlOption (self ):
511
+ options , logging_level = parse_options (['-n' ])
512
+ self .default_options ['lazy_ol' ] = False
513
+ self .assertEqual (options , self .default_options )
514
+
515
+ def testExtensionOption (self ):
516
+ options , logging_level = parse_options (['-x' , 'footnotes' ])
517
+ self .default_options ['extensions' ] = ['footnotes' ]
518
+ self .assertEqual (options , self .default_options )
519
+
520
+ def testMultipleExtensionOptions (self ):
521
+ options , logging_level = parse_options (['-x' , 'footnotes' , '-x' , 'smarty' ])
522
+ self .default_options ['extensions' ] = ['footnotes' , 'smarty' ]
523
+ self .assertEqual (options , self .default_options )
524
+
525
+ def create_config_file (self , config ):
526
+ """ Helper to create temp config files. """
527
+ if not isinstance (config , markdown .util .string_type ):
528
+ # convert to string
529
+ config = yaml .dump (config )
530
+ fd , self .tempfile = tempfile .mkstemp ('.yml' )
531
+ with os .fdopen (fd , 'w' ) as fp :
532
+ fp .write (config )
533
+
534
+ def testExtensonConfigOption (self ):
535
+ config = {
536
+ 'wikilinks' : {
537
+ 'base_url' : 'http://example.com/' ,
538
+ 'end_url' : '.html' ,
539
+ 'html_class' : 'test' ,
540
+ },
541
+ 'footnotes' : {
542
+ 'PLACE_MARKER' : '~~~footnotes~~~'
543
+ }
544
+ }
545
+ self .create_config_file (config )
546
+ options , logging_level = parse_options (['-c' , self .tempfile ])
547
+ self .default_options ['extension_configs' ] = config
548
+ self .assertEqual (options , self .default_options )
549
+
550
+ def testExtensonConfigOptionAsJSON (self ):
551
+ config = {
552
+ 'wikilinks' : {
553
+ 'base_url' : 'http://example.com/' ,
554
+ 'end_url' : '.html' ,
555
+ 'html_class' : 'test' ,
556
+ },
557
+ 'footnotes' : {
558
+ 'PLACE_MARKER' : '~~~footnotes~~~'
559
+ }
560
+ }
561
+ import json
562
+ self .create_config_file (json .dumps (config ))
563
+ options , logging_level = parse_options (['-c' , self .tempfile ])
564
+ self .default_options ['extension_configs' ] = config
565
+ self .assertEqual (options , self .default_options )
566
+
567
+ def testExtensonConfigOptionMissingFile (self ):
568
+ self .assertRaises (IOError , parse_options , ['-c' , 'missing_file.yaml' ])
569
+
570
+ def testExtensonConfigOptionBadFormat (self ):
571
+ config = """
572
+ [footnotes]
573
+ PLACE_MARKER= ~~~footnotes~~~
574
+ """
575
+ self .create_config_file (config )
576
+ self .assertRaises (yaml .YAMLError , parse_options , ['-c' , self .tempfile ])
0 commit comments