summaryrefslogtreecommitdiff
path: root/unit_tests/test_config_defaults.rst
blob: 944d37087d1337951fbdc6b5a994cb69f270e356 (plain)
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
    >>> from optparse import OptionParser
    >>> import os
    >>> from cStringIO import StringIO

    >>> import nose.config

All commandline options to fall back to values configured in
configuration files.  The configuration lives in a single section
("nosetests") in each configuration file.

    >>> support = os.path.join(os.path.dirname(__file__), "support",
    ...                        "config_defaults")

    >>> def error(msg):
    ...     print "error: %s" % msg

    >>> def get_parser():
    ...     parser = OptionParser()
    ...     parser.add_option(
    ...         "-v", "--verbose",
    ...         action="count", dest="verbosity",
    ...         default=1)
    ...     parser.add_option(
    ...         "--verbosity", action="store", dest="verbosity",
    ...         type="int")
    ...     return nose.config.ConfiguredDefaultsOptionParser(parser,
    ...                                                       "nosetests",
    ...                                                       error)

    >>> def parse(args, config_files):
    ...     argv = ["nosetests"] + list(args)
    ...     return get_parser().parseArgsAndConfigFiles(argv, config_files)


Options on the command line combine with the defaults from the config
files and the options' own defaults (here, -v adds 1 to verbosity of 3
from a.cfg).  Config file defaults take precedence over options'
defaults.

    >>> options, args = parse([], [])
    >>> options.verbosity
    1
    >>> options, args = parse([], os.path.join(support, "a.cfg"))
    >>> options.verbosity
    3
    >>> options, args = parse(["-v"], os.path.join(support, "a.cfg"))
    >>> options.verbosity
    4

Command line arguments take precedence

    >>> options, args = parse(["--verbosity=7"], os.path.join(support, "a.cfg"))
    >>> options.verbosity
    7

Where options appear in several config files, the last config file wins

    >>> files = [os.path.join(support, "b.cfg"), os.path.join(support, "a.cfg")]
    >>> options, args = parse([], files)
    >>> options.verbosity
    3


Invalid values should cause an error specifically about configuration
files (not about a commandline option)

    >>> options, arguments = parse([], StringIO("""\
    ... [nosetests]
    ... verbosity = spam
    ... """))
    error: Error reading config file '<???>': option 'verbosity': invalid integer value: 'spam'

Unrecognised option in nosetests config section

    >>> options, args = parse([], StringIO("[nosetests]\nspam=eggs\n"))
    error: Error reading config file '<???>': no such option 'spam'

If there were multiple config files, the error message tells us which
file contains the bad option name or value

    >>> options, args = parse([], [os.path.join(support, "a.cfg"),
    ...                            os.path.join(support, "invalid_value.cfg"),
    ...                            os.path.join(support, "b.cfg")])
    ... # doctest: +ELLIPSIS
    error: Error reading config file '...invalid_value.cfg': option 'verbosity': invalid integer value: 'spam'


Invalid config files

(file-like object)

    >>> options, args = parse([], StringIO("spam"))
    error: Error reading config file '<???>': File contains no section headers.
    file: <???>, line: 1
    'spam'

(filename)

    >>> options, args = parse([], os.path.join(support, "invalid.cfg"))
    ... # doctest: +ELLIPSIS
    error: Error reading config file '...invalid.cfg': File contains no section headers.
    file: ...invalid.cfg, line: 1
    'spam\n'

(filenames, length == 1)

    >>> options, args = parse([], [os.path.join(support, "invalid.cfg")])
    ... # doctest: +ELLIPSIS
    error: Error reading config file '...invalid.cfg': File contains no section headers.
    file: ...invalid.cfg, line: 1
    'spam\n'

(filenames, length > 1)

If there were multiple config files, the error message tells us which
file is bad

    >>> options, args = parse([], [os.path.join(support, "a.cfg"),
    ...                            os.path.join(support, "invalid.cfg"),
    ...                            os.path.join(support, "b.cfg")])
    ... # doctest: +ELLIPSIS
    error: Error reading config file '...invalid.cfg': File contains no section headers.
    file: ...invalid.cfg, line: 1
    'spam\n'


Missing config files don't deserve an error or warning

(filename)

    >>> options, args = parse([], os.path.join(support, "nonexistent.cfg"))
    >>> print options.__dict__
    {'verbosity': 1}

(filenames)

    >>> options, args = parse([], [os.path.join(support, "nonexistent.cfg")])
    >>> print options.__dict__
    {'verbosity': 1}


The same goes for missing config file section ("nosetests")

    >>> options, args = parse([], StringIO("[spam]\nfoo=bar\n"))
    >>> print options.__dict__
    {'verbosity': 1}