File: test_happydoc.py

package info (click to toggle)
python-happydoc 2.0-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,176 kB
  • ctags: 3,347
  • sloc: python: 11,321; makefile: 88; sh: 77
file content (677 lines) | stat: -rwxr-xr-x 21,425 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
#
# $Id: test_happydoc.py,v 1.74 2001/12/16 12:13:53 doughellmann Exp $
#
# Copyright Doug Hellmann 2000
#
#                         All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby
# granted, provided that the above copyright notice appear in all
# copies and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of Doug
# Hellmann not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.
#
# DOUG HELLMANN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL DOUG HELLMANN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

"""Driver for unit tests for HappyDoc.

"""

__rcs_info__ = {
    #
    #  Creation Information
    #
    'module_name'  : '$RCSfile: test_happydoc.py,v $',
    'rcs_id'       : '$Id: test_happydoc.py,v 1.74 2001/12/16 12:13:53 doughellmann Exp $',
    'creator'      : 'Doug Hellmann <doug@hellfly.net>',
    'project'      : 'HappyDoc',
    'created'      : 'Sun, 13-Aug-2000 10:16:13 EDT',

    #
    #  Current Information
    #
    'author'       : '$Author: doughellmann $',
    'version'      : '$Revision: 1.74 $',
    'date'         : '$Date: 2001/12/16 12:13:53 $',
}
try:
    __version__ = __rcs_info__['version'].split(' ')[1]
except:
    __version__ = '0.0'

#
# Import system modules
#
import sys
import os
import tempfile
import string
from glob import glob
import pprint
import unittest
    
#
# Local Modules
#
import happydoclib
from happydoclib.appclass import HappyDoc
from happydoclib.StreamFlushTest import StreamFlushTest, DEFAULT_OUTPUT_DIR
from happydoclib.StreamFlushTest import verboseLevel as globalVerboseLevel

#
# Modules with tests
#
import happydoclib.docset.docset_MultipleFile
import happydoclib.happydocset
import happydoclib.happydocstring
import happydoclib.docstring
import happydoclib.docstring.docstring_ClassicStructuredText
import happydoclib.docstring.docstring_StructuredText
import happydoclib.docstring.docstring_RawText
import happydoclib.docstring.docstring_PlainText
import happydoclib.formatter.openoffice
import happydoclib.parseinfo
import happydoclib.trace

#
# Module
#

    
    
class HappyDocTestBase(StreamFlushTest):

    def setUp(self):
        self.happydoc = './happydoc'
        return

    def runHappyDoc(self, modules=(), extraArgs=()):
        #
        # Fix up output directory variable
        #
        output_dir = self.output_dir
        happydoc = self.happydoc
        test_name = self.name
        output_dir = '%(output_dir)s/%(test_name)s' % locals()
        #
        # Verbose level setting
        #
        if self.verboseLevel.get():
            verboseArgs = '-%s' % ('v' * self.verboseLevel.get())
        else:
            verboseArgs = '-q'
        #
        # Build the argument list for the command.
        #
        # We have to pay some attention to the
        # order in which values are added to the
        # list, to ensure all options are presented
        # before arguments (so getopt will not
        # interpret arguments as filenames)
        #
        argv = [ verboseArgs,
                 '-t', self.name,
                 '-d', output_dir,
                 '-i', 'TestCases',
                 '-i', 'tmp',
                 ] + \
                 \
                 list(extraArgs) + \
                 \
                 [ 'formatter_dateStampFiles=0',  # always different, breaks diff
                   ] + \
                   \
                   list(modules)

        #
        # Set up for the test.
        #
        
        try:
            
            if self.verboseLevel.get():
                print
                print
                print 'Arguments: ',
                pprint.pprint(argv)
                print
                
            exit_code = HappyDoc(argv).run()
            
        except HappyDoc.HelpRequested, msg:
            if msg:
                exit_code = 1
        
        #
        # Flush the output so watchers can keep up
        #
        sys.stdout.flush()
        sys.stderr.flush()
        return exit_code
    
class ExecuteHappyDocTest(HappyDocTestBase):

    """Tests involving external executions of HappyDoc.

    Separated the definitions of the test from the base class because
    unittest appears to duplicate tests if they are inherited.
    """

    #
    # Application tests
    #
    
    def testHelpSyntax(self):
        assert self.runHappyDoc( (),
                                 extraArgs=('-h',)
                                 ), 'Basic help syntax test failed.'
        return
 
    def testHelpManual(self):
        assert self.runHappyDoc( (),
                                 extraArgs=('--help',)
                                 ), 'Help manual generation test failed.'
        return
 
    def testUsageWhenNoArgs(self):
        assert self.runHappyDoc( (),
                                 extraArgs=()
                                 ), 'Usage message test failed.'
        return

    #
    # Formatter and docset tests
    #
    
    def testHTMLSingleFile(self):
        assert (not self.runHappyDoc( ('TestCases/test.py',),
                                      extraArgs=('-p', '-', '-T', 'SingleFile')
                                      )
                ), 'Basic single-file docset test failed.'
        return
    
    def testHTMLSingleFileCompact(self):
        assert (not self.runHappyDoc( ('TestCases/test.py',),
                                      extraArgs=('-p', '-', '-T', 'SingleFile',
                                                 'formatter_compactHTML=yes',
                                                 )
                                      )
                ), 'Basic single-file docset test failed.'
        return

    def testHTMLStdout(self):
        assert (not self.runHappyDoc( ('TestCases/test.py',),
                                      extraArgs=('-T', 'StdOut')
                                      )
                ), 'HTML to standard-output docset test failed.'
        return

    def testText(self):
        assert (not self.runHappyDoc( ('TestCases/test.py',),
                                      extraArgs=('-F', 'Text')
                                      )
                ), 'Text formatter test failed.'
        return

    def testTextStdout(self):
        assert (not self.runHappyDoc( ('TestCases/test.py',),
                                      extraArgs=('-F', 'Text', '-T', 'StdOut')
                                      )
                ), 'Text to standard-output docset test failed.'
        return

    def testDia(self):
        assert (not self.runHappyDoc( ('TestCases/test_dia.py', ),
                                      extraArgs=('--dia',)
                                      )
                ), 'Dia output test failed.'
        return

    def testImportsFromPackages(self):
        assert (not self.runHappyDoc( ('TestCases/test_import_packages_basic',),
                                      )
                ), 'Import from packages test failed.'
        return

    def testBasicImportsFromPackagesIgnorePackages(self):
        assert (not self.runHappyDoc( ('TestCases/test_import_packages',),
                                      extraArgs=('docset_usePackages=0',),
                                      )
                ), 'Import from packages while ignoring package special handling test failed.'
        return

    def testOutputWithPrefix(self):
        assert (not self.runHappyDoc( ('TestCases/test_import_packages_basic',),
                                      extraArgs=('-p',
                                                 '',
                                                 'formatter_filenamePrefix=TESTPREFIX_')
                                      )
                ), 'Formatter output prefix test failed.'
        return 

    ##
    ## Full self documentation tests
    ##
    
#     def testSelfDia(self):
#         assert (not self.runHappyDoc( ('../HappyDoc',),
#                                       extraArgs=('--dia',))
#                 ), 'Full self documentation test in dia format failed.'
#         return
   
    def testSelfHTML(self):
        assert (not self.runHappyDoc( ('../HappyDoc',),
                                      )
                ), 'Full self documentation test failed.'
        return
    
#     def testSelfHTMLCompact(self):
#         assert (not self.runHappyDoc( ('../HappyDoc',),
#                                       extraArgs=('formatter_compactHTML=yes',))
#                 ), 'Full self documentation with compact output test failed.'
#         return
    
    def testSelfDocBookSingleFile(self):
        assert (not self.runHappyDoc( ('../HappyDoc',),
                                      extraArgs=('-F',
                                                 'SGMLDocBook',
                                                 '-T',
                                                 'SingleFile'))
                ), 'Full self documentation in DocBook format output test failed.'
        return

#     def testSelfDocBookXML(self):
#         assert (not self.runHappyDoc( ('../HappyDoc',),
#                                       extraArgs=('-F',
#                                                  'docbookx',
#                                                  '-T',
#                                                  'mstruct'))
#                 ), 'Full self documentation in DocBookX format output test failed.'
#         return 
    
#     def testSelfPlainText(self):
#         assert (not self.runHappyDoc( ('../HappyDoc',),
#                                       extraArgs=('-F', 'Text'))
#                 ), 'Full self documentation-as-text test failed.'
#         return
    

class OtherWorkingDirTest(HappyDocTestBase):

    def __init__(self,
                 workingDir='.',
                 outputDir='DefaultTestOutputDir',
                 **nargs
                 ):
        #
        # Base class
        #
        output_dir = happydoclib.path.join(os.pardir, 'HappyDoc', outputDir)
        nargs['outputDir'] = output_dir
        apply(HappyDocTestBase.__init__, (self,), nargs)
        #
        # This class
        #
        self.dir_stack = None
        self.working_dir = workingDir
        return

    def setUp(self):
        self.happydoc = '../HappyDoc/happydoc'
        return
    
    def runHappyDoc(self, *args, **nargs):
        self.pushDir()
        apply(HappyDocTestBase.runHappyDoc, (self,) + args, nargs)
        self.popDir()
        return

    def pushDir(self):
        self.dir_stack = (os.getcwd(), self.dir_stack)
        os.chdir(self.working_dir)
        return

    def popDir(self):
        if self.dir_stack:
            top, self.dir_stack = self.dir_stack
            os.chdir(top)
        return


    
class ExternalTest(HappyDocTestBase):

    def externalApp(self, command):
        ret = os.system('python %s' % command)
        assert not ret, 'External test command "%s" failed' % command

    def testPluginLoader(self):
        self.externalApp('./TestCases/test_plugin_loader/runtest.py')
        return


    
class ZopeTest(OtherWorkingDirTest):
    
    def testZopeFull(self):
        assert (not self.runHappyDoc( ('../Zope-2-CVS-src',),
                                      )
                ), 'Zope full documentation test failed.'
        return
    
    def testZopeRoot(self):
        assert (not self.runHappyDoc( ('../Zope-2-CVS-src',),
                                      extraArgs=('-r',))
                ), 'Zope full documentation test failed.'
        return

    def testGadflyParseError(self):
        assert (not self.runHappyDoc( ('../Zope-2-CVS-src/lib/python/Products/ZGadflyDA/gadfly/gfdb0.py',),
                                      extraArgs=('-r',))
                ), 'Gadfly test with parse-error failed.'
        return

    def testZEOParseError(self):
        assert (not self.runHappyDoc( ('../Zope-2-CVS-src/lib/python/ZEO/zrpc.py',),
                                      extraArgs=('-r',))
                ), 'ZEO test with parse-error failed.'
        return

    def testZopeWithSafePrefix(self):
        assert (not self.runHappyDoc( ('../Zope-2-CVS-src',),
                                      extraArgs=('formatter_filenamePrefix=zsd_',))
                ), 'Zope test with output prefix failed.'
        return
        
        
        
def ZopeTestFactory(**nargs):
    nargs['workingDir'] = nargs.get('workingDir', '../Zope-2-CVS-src')
    return apply(ZopeTest, (), nargs)



class HappyDocBugRegressionTest(HappyDocTestBase):

    def __init__(self,
                 methodName='',
                 outputDir='DefaultTestOutputDir',
                 ):
        HappyDocTestBase.__init__(self,
                              outputDir=outputDir,
                              methodName='testBugReport%s' % methodName)
        return

    def checkBugReport(self, bugId):
        print '\n%s: %s' % (bugId, os.getcwd())
        assert not self.runHappyDoc( ('./TestCases/test_bug%s.py' % bugId, ),
                                     extraArgs=('-p', '-'),
                                     ), 'Check for bug %s failed.' % bugId

    def __getattr__(self, name):
        prefix = 'testBugReport'
        prefix_len = len(prefix)
        if name[:prefix_len] == prefix:
            id = name[prefix_len:]
            test_func = lambda bug=id, s=self: s.checkBugReport(bug)
            test_func.__doc__ = 'Regression test for bug %s' % id
            return test_func
        raise AttributeError(name)


class HappyDocTestLoader(unittest.TestLoader):
    """Special TestLoader for HappyDoc

    This TestLoader subclass tell the TestCases it loads to write to a
    specific output directory, thereby letting us differentiate
    between standard test output and regression test output.
    """

    def __init__(self, outputDir):
        self.output_dir = outputDir
        return

    def loadTestsFromTestCase(self, testCaseClass):
        """Return a suite of all tests cases contained in testCaseClass"""
        names = self.getTestCaseNames(testCaseClass)
        tests = []
        for n in names:
            tests.append( testCaseClass(n, self.output_dir) )
        return self.suiteClass(tests)
        

class TestCaseDriver(happydoclib.CommandLineApp.CommandLineApp):
    "Drive the test cases for HappyDoc."

    LIST = 'list'
    RUNTEST = 'run'

    _include_zope = 0
    _output_dir = 'DefaultTestOutputDir'
    _operation = RUNTEST

    #
    # Use the verbosity manager from the StreamFlushTest
    # module so that all verbosity is managed the same way.
    #
    verboseLevel = globalVerboseLevel

    ##
    ## OPTION HANDLERS
    ##

    def optionHandler_d(self, outputDir):
        "Specify the output directory for the tests."
        self.statusMessage('Setting output directory to "%s"' % outputDir, 2)
        self._output_dir = outputDir
        return

    def optionHandler_list(self):
        "List the tests available."
        self._operation = self.LIST
        return
    
    def optionHandler_q(self):
        "Disable visible output."
        self.verboseLevel.unset()
        return

    def optionHandler_v(self):
        "Increase verbose level by one.  Can be repeated."
        self.verboseLevel.set(self.verboseLevel.get() + 1)
        return

    def optionHandler_withzope(self):
        "Add the Zope tests to the set."
        self.statusMessage('Including Zope testes', 2)
        self._include_zope = 1
        return


    ##
    ## APPLICATION
    ##
    
    def appInit(self):
        self._desired_tests = []
        self.optionHandler_d(DEFAULT_OUTPUT_DIR)
        return

    def listTests(self, suite):
        "List the available test cases."
        self.statusMessage('Available tests', 2)
        for test in suite._tests:
            if issubclass(test.__class__, unittest.TestSuite):
                self.listTests(test)
            else:
                try:
                    description = test.shortDescription()
                    if not description:
                        description = test.id()
                    print '%s : %s' % (test.name, description)
                except AttributeError:
                    print dir(test)
                    raise
        return

    def runTests(self, suite):
        "Run the required test cases."
        #
        # Run the test suite
        #

        verbosity = self.verboseLevel.get()

        if verbosity > 1:
            print '=' * 80
            print 'START'
            print '-' * 80
            print
            
        runner = unittest.TextTestRunner(stream=sys.stdout, verbosity=verbosity)
        runner.run(suite)

        if verbosity > 1:
            print
            print '-' * 80
            print 'FINISH'
            print '=' * 80
            
        return
    
    def main(self, *args):
        "Run the requested test suites."

        #
        # Create the output directory
        #
        self.statusMessage('Creating %s' % self._output_dir, 2)
        happydoclib.path.rmkdir(self._output_dir)
        
        #
        # Figure out what tests to process
        #
        actual_test_suite = unittest.TestSuite()

        test_loader = HappyDocTestLoader(self._output_dir)

        get_all_tests = 0
        if 'all' in args:
            # We will define "all" of the tests later
            get_all_tests = 1
        elif args:
            # Run the tests specified by the user
            for a in args:
                actual_test_suite.addTest( test_loader.loadTestsFromName(a) )
        else:
            # Default to running all tests
            get_all_tests = 0
            actual_test_suite.addTest( test_loader.loadTestsFromName('__main__.ExecuteHappyDocTest.testSelfHTML') )

        if get_all_tests:
            #
            # Load tests from modules we know contain tests
            #
            for m in (
                #
                # Supporting tools
                #
                happydoclib.CommandLineApp,
                happydoclib.indentstring,
                happydoclib.optiontools,
                happydoclib.trace,
                happydoclib.path,

                #
                # Foundation modules
                #
                happydoclib.parsecomments,
                happydoclib.parseinfo,
                happydoclib.happydocstring,
                happydoclib.happydocset,

                #
                # Docstring converters
                #
                happydoclib.docstring,
                happydoclib.docstring.docstring_ClassicStructuredText,
                happydoclib.docstring.docstring_StructuredText,
                happydoclib.docstring.docstring_RawText,
                happydoclib.docstring.docstring_PlainText,

                #
                # Formatters
                #
                happydoclib.formatter.openoffice,
                happydoclib.formatter.formatter_HTMLFile,

                #
                # Docsets
                #
                happydoclib.docset.docset_MultipleFile
                
                ):
                actual_test_suite.addTest(test_loader.loadTestsFromModule(m))
            #
            # Load tests from classes in this module
            #
            for c in ( ExecuteHappyDocTest,
                       ExternalTest,
                       ):
                actual_test_suite.addTest(test_loader.loadTestsFromTestCase(c))
            #
            # Check tests related to bug reports
            #
            bug_ids = map(lambda x:x[18:-3], glob('TestCases/test_bug*.py'))
            bug_ids.sort()
            for bug in bug_ids:
                #test_definitions.append( (bug, HappyDocBugRegressionTest) )
                actual_test_suite.addTest(
                    HappyDocBugRegressionTest( methodName=bug,
                                               outputDir=self._output_dir,
                                               )
                    )
            #
            # Optionally include the Zope tests
            #
            if self._include_zope:
                actual_test_suite.addTest(test_loader.loadTestsFromTestCase(ZopeTest))
                
        #
        # Figure out what action to take
        #
        if self._operation == self.RUNTEST:
            self.runTests(actual_test_suite)
        elif self._operation == self.LIST:
            self.listTests(actual_test_suite)
        else:
            raise ValueError('Operation (%s) must be one of RUNTEST or LIST.' % \
                             self._operation)
        
        return
    

def main(argv=()):
    try:
        TestCaseDriver(argv).run()
    except TestCaseDriver.HelpRequested:
        pass


def debug():
    main( ('-t', 'checkHelpSyntax') )
    return
    

if __name__ == '__main__':
    main(sys.argv[1:])