1
>>> from optparse import OptionParser
3
>>> from cStringIO import StringIO
7
All commandline options to fall back to values configured in
8
configuration files. The configuration lives in a single section
9
("nosetests") in each configuration file.
11
>>> support = os.path.join(os.path.dirname(__file__), "support",
12
... "config_defaults")
15
... print "error: %s" % msg
18
... parser = OptionParser()
19
... parser.add_option(
20
... "-v", "--verbose",
21
... action="count", dest="verbosity",
23
... parser.add_option(
24
... "--verbosity", action="store", dest="verbosity",
26
... return nose.config.ConfiguredDefaultsOptionParser(parser,
30
>>> def parse(args, config_files):
31
... argv = ["nosetests"] + list(args)
32
... return get_parser().parseArgsAndConfigFiles(argv, config_files)
35
Options on the command line combine with the defaults from the config
36
files and the options' own defaults (here, -v adds 1 to verbosity of 3
37
from a.cfg). Config file defaults take precedence over options'
40
>>> options, args = parse([], [])
43
>>> options, args = parse([], os.path.join(support, "a.cfg"))
46
>>> options, args = parse(["-v"], os.path.join(support, "a.cfg"))
50
Command line arguments take precedence
52
>>> options, args = parse(["--verbosity=7"], os.path.join(support, "a.cfg"))
56
Where options appear in several config files, the last config file wins
58
>>> files = [os.path.join(support, "b.cfg"), os.path.join(support, "a.cfg")]
59
>>> options, args = parse([], files)
64
Invalid values should cause an error specifically about configuration
65
files (not about a commandline option)
67
>>> options, arguments = parse([], StringIO("""\
71
error: Error reading config file '<???>': option 'verbosity': invalid integer value: 'spam'
73
Unrecognised option in nosetests config section
75
>>> options, args = parse([], StringIO("[nosetests]\nspam=eggs\n"))
76
error: Error reading config file '<???>': no such option 'spam'
78
If there were multiple config files, the error message tells us which
79
file contains the bad option name or value
81
>>> options, args = parse([], [os.path.join(support, "a.cfg"),
82
... os.path.join(support, "invalid_value.cfg"),
83
... os.path.join(support, "b.cfg")])
84
... # doctest: +ELLIPSIS
85
error: Error reading config file '.../invalid_value.cfg': option 'verbosity': invalid integer value: 'spam'
92
>>> options, args = parse([], StringIO("spam"))
93
error: Error reading config file '<???>': File contains no section headers.
99
>>> options, args = parse([], os.path.join(support, "invalid.cfg"))
100
... # doctest: +ELLIPSIS
101
error: Error reading config file '.../invalid.cfg': File contains no section headers.
102
file: .../invalid.cfg, line: 1
105
(filenames, length == 1)
107
>>> options, args = parse([], [os.path.join(support, "invalid.cfg")])
108
... # doctest: +ELLIPSIS
109
error: Error reading config file '.../invalid.cfg': File contains no section headers.
110
file: .../invalid.cfg, line: 1
113
(filenames, length > 1)
115
If there were multiple config files, the error message tells us which
118
>>> options, args = parse([], [os.path.join(support, "a.cfg"),
119
... os.path.join(support, "invalid.cfg"),
120
... os.path.join(support, "b.cfg")])
121
... # doctest: +ELLIPSIS
122
error: Error reading config file '.../invalid.cfg': File contains no section headers.
123
file: .../invalid.cfg, line: 1
127
Missing config files don't deserve an error or warning
131
>>> options, args = parse([], os.path.join(support, "nonexistent.cfg"))
132
>>> print options.__dict__
137
>>> options, args = parse([], [os.path.join(support, "nonexistent.cfg")])
138
>>> print options.__dict__
142
The same goes for missing config file section ("nosetests")
144
>>> options, args = parse([], StringIO("[spam]\nfoo=bar\n"))
145
>>> print options.__dict__