/unit_tests/test_config_defaults.rst

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