forked from paulovn/python-aiml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__main__.py
More file actions
133 lines (110 loc) · 3.74 KB
/
__main__.py
File metadata and controls
133 lines (110 loc) · 3.74 KB
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
# A script to enable execution of any python unit test in this directory.
# It will be called when Python tries to 'execute' this directory
"""
Usage:
* execute all unit tests in this dir
python <dir> [options] [-a]
* execute a certain set of tests
python <dir> [options] <name1> <name2> ...
Options:
-f: failfast (abort with the first test that fails)
-v, -vv: increase verbosity
-q: reduce verbosity
-d LEVEL: set debug level
"""
from __future__ import print_function
import re
import unittest
import os.path
import sys
# --------------------------------------------------------------------
def script_list( path ):
"""
Get all the python scripts in a directory. Scripts are taken as all
files ending in ``.py`` that do not start with an underscore or dot.
"""
regexp = re.compile( '^([^_\.].+)\.py$' )
r = []
for f in os.listdir(path):
if os.path.isdir( os.path.join(path,f) ):
continue
m = regexp.match(f)
if m:
r.append( m.group(1) )
return sorted(r)
# --------------------------------------------------------------------
def default_opts():
'''Default options'''
return type( "options", (),
{ 'help' : False,
'all' : False,
'quiet' : False,
'failfast': False,
'verbosity': 1 } )
def read_opts():
'''Read command-line options'''
# Default options
opt = default_opts()
# Detect options
while len(sys.argv) > 1 and sys.argv[1].startswith('-'):
if sys.argv[1] == '-h':
opt.help = True
elif sys.argv[1] == '-q':
opt.quiet = True
elif sys.argv[1] == '-v':
opt.verbosity = 2
elif sys.argv[1] == '-vv':
opt.verbosity = 3
elif sys.argv[1] in ('-a','-all','--all'):
opt.all = True
elif sys.argv[1] in ('-f'):
opt.failfast = True
elif sys.argv[1] == '-d':
import logging
logging.basicConfig( level=sys.argv[2],
format='%(levelname)s %(filename)s %(funcName)s: %(message)s' )
sys.argv.pop(1)
sys.argv.pop(1) # remove the just-used argument
return opt
# --------------------------------------------------------------------
def load_tests( opt=None ):
'''Load the test suite by adding all python files in this directory'''
if opt is None:
opt = default_opts()
thisdir = os.path.dirname(__file__)
# Select the test(s) to run
if __name__ != '__main__' or len(sys.argv) < 2 or opt.all:
test_list = script_list( thisdir )
else:
test_list = [ t if t.startswith('test_') else 'test_' + t
for t in sys.argv[1:] ]
loader = unittest.defaultTestLoader
suite = unittest.TestSuite()
# Add the base path of python-aiml sources
sys.path.insert( 0, os.path.dirname(thisdir) )
# Add all requested unit tests
if not opt.quiet:
print( "\n******************************\n" )
for test in test_list:
if not opt.quiet:
print( "Loading unit test '" + test + "'" )
suite.addTest( loader.loadTestsFromName( 'test.' + test ) )
return suite
if __name__ == '__main__':
# Usage message
opt = read_opts()
if opt.help:
print( __doc__ )
sys.exit(1)
# Load the tests
suite = load_tests( opt )
# Run the tests
if not opt.quiet:
print( "\n\nRunning test suite\n" )
try:
testrunner = unittest.TextTestRunner( verbosity=opt.verbosity )
testrunner.failfast = opt.failfast
testrunner.run( suite )
except KeyboardInterrupt as err:
print( "Interrupted!! :", err )
sys.exit(1)