/unit_tests/test_config_defaults.rst
ReStructuredText | 146 lines | 106 code | 40 blank | 0 comment | 0 complexity | fddc19cabf43a4ef9025084c61366263 MD5 | raw file
1 >>> from optparse import OptionParser 2 >>> import os 3 >>> from cStringIO import StringIO 4 5 >>> import nose.config 6 7All commandline options to fall back to values configured in 8configuration files. The configuration lives in a single section 9("nosetests") in each configuration file. 10 11 >>> support = os.path.join(os.path.dirname(__file__), "support", 12 ... "config_defaults") 13 14 >>> def error(msg): 15 ... print "error: %s" % msg 16 17 >>> def get_parser(): 18 ... parser = OptionParser() 19 ... parser.add_option( 20 ... "-v", "--verbose", 21 ... action="count", dest="verbosity", 22 ... default=1) 23 ... parser.add_option( 24 ... "--verbosity", action="store", dest="verbosity", 25 ... type="int") 26 ... return nose.config.ConfiguredDefaultsOptionParser(parser, 27 ... "nosetests", 28 ... error) 29 30 >>> def parse(args, config_files): 31 ... argv = ["nosetests"] + list(args) 32 ... return get_parser().parseArgsAndConfigFiles(argv, config_files) 33 34 35Options on the command line combine with the defaults from the config 36files and the options' own defaults (here, -v adds 1 to verbosity of 3 37from a.cfg). Config file defaults take precedence over options' 38defaults. 39 40 >>> options, args = parse([], []) 41 >>> options.verbosity 42 1 43 >>> options, args = parse([], os.path.join(support, "a.cfg")) 44 >>> options.verbosity 45 3 46 >>> options, args = parse(["-v"], os.path.join(support, "a.cfg")) 47 >>> options.verbosity 48 4 49 50Command line arguments take precedence 51 52 >>> options, args = parse(["--verbosity=7"], os.path.join(support, "a.cfg")) 53 >>> options.verbosity 54 7 55 56Where options appear in several config files, the last config file wins 57 58 >>> files = [os.path.join(support, "b.cfg"), os.path.join(support, "a.cfg")] 59 >>> options, args = parse([], files) 60 >>> options.verbosity 61 3 62 63 64Invalid values should cause an error specifically about configuration 65files (not about a commandline option) 66 67 >>> options, arguments = parse([], StringIO("""\ 68 ... [nosetests] 69 ... verbosity = spam 70 ... """)) 71 error: Error reading config file '<???>': option 'verbosity': invalid integer value: 'spam' 72 73Unrecognised option in nosetests config section 74 75 >>> options, args = parse([], StringIO("[nosetests]\nspam=eggs\n")) 76 error: Error reading config file '<???>': no such option 'spam' 77 78If there were multiple config files, the error message tells us which 79file contains the bad option name or value 80 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' 86 87 88Invalid config files 89 90(file-like object) 91 92 >>> options, args = parse([], StringIO("spam")) 93 error: Error reading config file '<???>': File contains no section headers. 94 file: <???>, line: 1 95 'spam' 96 97(filename) 98 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 103 'spam\n' 104 105(filenames, length == 1) 106 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 111 'spam\n' 112 113(filenames, length > 1) 114 115If there were multiple config files, the error message tells us which 116file is bad 117 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 124 'spam\n' 125 126 127Missing config files don't deserve an error or warning 128 129(filename) 130 131 >>> options, args = parse([], os.path.join(support, "nonexistent.cfg")) 132 >>> print options.__dict__ 133 {'verbosity': 1} 134 135(filenames) 136 137 >>> options, args = parse([], [os.path.join(support, "nonexistent.cfg")]) 138 >>> print options.__dict__ 139 {'verbosity': 1} 140 141 142The same goes for missing config file section ("nosetests") 143 144 >>> options, args = parse([], StringIO("[spam]\nfoo=bar\n")) 145 >>> print options.__dict__ 146 {'verbosity': 1}