/nose/config.py

https://bitbucket.org/jpellerin/nose/ · Python · 638 lines · 521 code · 64 blank · 53 comment · 73 complexity · 6edb344923487e916d5f3cacf84a35cd MD5 · raw file

  1. import logging
  2. import optparse
  3. import os
  4. import re
  5. import sys
  6. import ConfigParser
  7. from optparse import OptionParser
  8. from nose.util import absdir, tolist
  9. from nose.plugins.manager import NoPlugins
  10. from warnings import warn
  11. log = logging.getLogger(__name__)
  12. # not allowed in config files
  13. option_blacklist = ['help', 'verbose']
  14. config_files = [
  15. # Linux users will prefer this
  16. "~/.noserc",
  17. # Windows users will prefer this
  18. "~/nose.cfg"
  19. ]
  20. # plaforms on which the exe check defaults to off
  21. # Windows and IronPython
  22. exe_allowed_platforms = ('win32', 'cli')
  23. class NoSuchOptionError(Exception):
  24. def __init__(self, name):
  25. Exception.__init__(self, name)
  26. self.name = name
  27. class ConfigError(Exception):
  28. pass
  29. class ConfiguredDefaultsOptionParser(object):
  30. """
  31. Handler for options from commandline and config files.
  32. """
  33. def __init__(self, parser, config_section, error=None, file_error=None):
  34. self._parser = parser
  35. self._config_section = config_section
  36. if error is None:
  37. error = self._parser.error
  38. self._error = error
  39. if file_error is None:
  40. file_error = lambda msg, **kw: error(msg)
  41. self._file_error = file_error
  42. def _configTuples(self, cfg, filename):
  43. config = []
  44. if self._config_section in cfg.sections():
  45. for name, value in cfg.items(self._config_section):
  46. config.append((name, value, filename))
  47. return config
  48. def _readFromFilenames(self, filenames):
  49. config = []
  50. for filename in filenames:
  51. cfg = ConfigParser.RawConfigParser()
  52. try:
  53. cfg.read(filename)
  54. except ConfigParser.Error, exc:
  55. raise ConfigError("Error reading config file %r: %s" %
  56. (filename, str(exc)))
  57. config.extend(self._configTuples(cfg, filename))
  58. return config
  59. def _readFromFileObject(self, fh):
  60. cfg = ConfigParser.RawConfigParser()
  61. try:
  62. filename = fh.name
  63. except AttributeError:
  64. filename = '<???>'
  65. try:
  66. cfg.readfp(fh)
  67. except ConfigParser.Error, exc:
  68. raise ConfigError("Error reading config file %r: %s" %
  69. (filename, str(exc)))
  70. return self._configTuples(cfg, filename)
  71. def _readConfiguration(self, config_files):
  72. try:
  73. config_files.readline
  74. except AttributeError:
  75. filename_or_filenames = config_files
  76. if isinstance(filename_or_filenames, basestring):
  77. filenames = [filename_or_filenames]
  78. else:
  79. filenames = filename_or_filenames
  80. config = self._readFromFilenames(filenames)
  81. else:
  82. fh = config_files
  83. config = self._readFromFileObject(fh)
  84. return config
  85. def _processConfigValue(self, name, value, values, parser):
  86. opt_str = '--' + name
  87. option = parser.get_option(opt_str)
  88. if option is None:
  89. raise NoSuchOptionError(name)
  90. else:
  91. option.process(opt_str, value, values, parser)
  92. def _applyConfigurationToValues(self, parser, config, values):
  93. for name, value, filename in config:
  94. if name in option_blacklist:
  95. continue
  96. try:
  97. self._processConfigValue(name, value, values, parser)
  98. except NoSuchOptionError, exc:
  99. self._file_error(
  100. "Error reading config file %r: "
  101. "no such option %r" % (filename, exc.name),
  102. name=name, filename=filename)
  103. except optparse.OptionValueError, exc:
  104. msg = str(exc).replace('--' + name, repr(name), 1)
  105. self._file_error("Error reading config file %r: "
  106. "%s" % (filename, msg),
  107. name=name, filename=filename)
  108. def parseArgsAndConfigFiles(self, args, config_files):
  109. values = self._parser.get_default_values()
  110. try:
  111. config = self._readConfiguration(config_files)
  112. except ConfigError, exc:
  113. self._error(str(exc))
  114. else:
  115. self._applyConfigurationToValues(self._parser, config, values)
  116. return self._parser.parse_args(args, values)
  117. class Config(object):
  118. """nose configuration.
  119. Instances of Config are used throughout nose to configure
  120. behavior, including plugin lists. Here are the default values for
  121. all config keys::
  122. self.env = env = kw.pop('env', {})
  123. self.args = ()
  124. self.testMatch = re.compile(r'(?:^|[\\b_\\.%s-])[Tt]est' % os.sep)
  125. self.addPaths = not env.get('NOSE_NOPATH', False)
  126. self.configSection = 'nosetests'
  127. self.debug = env.get('NOSE_DEBUG')
  128. self.debugLog = env.get('NOSE_DEBUG_LOG')
  129. self.exclude = None
  130. self.getTestCaseNamesCompat = False
  131. self.includeExe = env.get('NOSE_INCLUDE_EXE',
  132. sys.platform in exe_allowed_platforms)
  133. self.ignoreFiles = (re.compile(r'^\.'),
  134. re.compile(r'^_'),
  135. re.compile(r'^setup\.py$')
  136. )
  137. self.include = None
  138. self.loggingConfig = None
  139. self.logStream = sys.stderr
  140. self.options = NoOptions()
  141. self.parser = None
  142. self.plugins = NoPlugins()
  143. self.srcDirs = ('lib', 'src')
  144. self.runOnInit = True
  145. self.stopOnError = env.get('NOSE_STOP', False)
  146. self.stream = sys.stderr
  147. self.testNames = ()
  148. self.verbosity = int(env.get('NOSE_VERBOSE', 1))
  149. self.where = ()
  150. self.py3where = ()
  151. self.workingDir = None
  152. """
  153. def __init__(self, **kw):
  154. self.env = env = kw.pop('env', {})
  155. self.args = ()
  156. self.testMatchPat = env.get('NOSE_TESTMATCH',
  157. r'(?:^|[\b_\.%s-])[Tt]est' % os.sep)
  158. self.testMatch = re.compile(self.testMatchPat)
  159. self.addPaths = not env.get('NOSE_NOPATH', False)
  160. self.configSection = 'nosetests'
  161. self.debug = env.get('NOSE_DEBUG')
  162. self.debugLog = env.get('NOSE_DEBUG_LOG')
  163. self.exclude = None
  164. self.getTestCaseNamesCompat = False
  165. self.includeExe = env.get('NOSE_INCLUDE_EXE',
  166. sys.platform in exe_allowed_platforms)
  167. self.ignoreFilesDefaultStrings = [r'^\.',
  168. r'^_',
  169. r'^setup\.py$',
  170. ]
  171. self.ignoreFiles = map(re.compile, self.ignoreFilesDefaultStrings)
  172. self.include = None
  173. self.loggingConfig = None
  174. self.logStream = sys.stderr
  175. self.options = NoOptions()
  176. self.parser = None
  177. self.plugins = NoPlugins()
  178. self.srcDirs = ('lib', 'src')
  179. self.runOnInit = True
  180. self.stopOnError = env.get('NOSE_STOP', False)
  181. self.stream = sys.stderr
  182. self.testNames = []
  183. self.verbosity = int(env.get('NOSE_VERBOSE', 1))
  184. self.where = ()
  185. self.py3where = ()
  186. self.workingDir = os.getcwd()
  187. self.traverseNamespace = False
  188. self.firstPackageWins = False
  189. self.parserClass = OptionParser
  190. self.worker = False
  191. self._default = self.__dict__.copy()
  192. self.update(kw)
  193. self._orig = self.__dict__.copy()
  194. def __getstate__(self):
  195. state = self.__dict__.copy()
  196. del state['stream']
  197. del state['_orig']
  198. del state['_default']
  199. del state['env']
  200. del state['logStream']
  201. # FIXME remove plugins, have only plugin manager class
  202. state['plugins'] = self.plugins.__class__
  203. return state
  204. def __setstate__(self, state):
  205. plugincls = state.pop('plugins')
  206. self.update(state)
  207. self.worker = True
  208. # FIXME won't work for static plugin lists
  209. self.plugins = plugincls()
  210. self.plugins.loadPlugins()
  211. # needed so .can_configure gets set appropriately
  212. dummy_parser = self.parserClass()
  213. self.plugins.addOptions(dummy_parser, {})
  214. self.plugins.configure(self.options, self)
  215. def __repr__(self):
  216. d = self.__dict__.copy()
  217. # don't expose env, could include sensitive info
  218. d['env'] = {}
  219. keys = [ k for k in d.keys()
  220. if not k.startswith('_') ]
  221. keys.sort()
  222. return "Config(%s)" % ', '.join([ '%s=%r' % (k, d[k])
  223. for k in keys ])
  224. __str__ = __repr__
  225. def _parseArgs(self, argv, cfg_files):
  226. def warn_sometimes(msg, name=None, filename=None):
  227. if (hasattr(self.plugins, 'excludedOption') and
  228. self.plugins.excludedOption(name)):
  229. msg = ("Option %r in config file %r ignored: "
  230. "excluded by runtime environment" %
  231. (name, filename))
  232. warn(msg, RuntimeWarning)
  233. else:
  234. raise ConfigError(msg)
  235. parser = ConfiguredDefaultsOptionParser(
  236. self.getParser(), self.configSection, file_error=warn_sometimes)
  237. return parser.parseArgsAndConfigFiles(argv[1:], cfg_files)
  238. def configure(self, argv=None, doc=None):
  239. """Configure the nose running environment. Execute configure before
  240. collecting tests with nose.TestCollector to enable output capture and
  241. other features.
  242. """
  243. env = self.env
  244. if argv is None:
  245. argv = sys.argv
  246. cfg_files = getattr(self, 'files', [])
  247. options, args = self._parseArgs(argv, cfg_files)
  248. # If -c --config has been specified on command line,
  249. # load those config files and reparse
  250. if getattr(options, 'files', []):
  251. options, args = self._parseArgs(argv, options.files)
  252. self.options = options
  253. if args:
  254. self.testNames = args
  255. if options.testNames is not None:
  256. self.testNames.extend(tolist(options.testNames))
  257. if options.py3where is not None:
  258. if sys.version_info >= (3,):
  259. options.where = options.py3where
  260. # `where` is an append action, so it can't have a default value
  261. # in the parser, or that default will always be in the list
  262. if not options.where:
  263. options.where = env.get('NOSE_WHERE', None)
  264. # include and exclude also
  265. if not options.ignoreFiles:
  266. options.ignoreFiles = env.get('NOSE_IGNORE_FILES', [])
  267. if not options.include:
  268. options.include = env.get('NOSE_INCLUDE', [])
  269. if not options.exclude:
  270. options.exclude = env.get('NOSE_EXCLUDE', [])
  271. self.addPaths = options.addPaths
  272. self.stopOnError = options.stopOnError
  273. self.verbosity = options.verbosity
  274. self.includeExe = options.includeExe
  275. self.traverseNamespace = options.traverseNamespace
  276. self.debug = options.debug
  277. self.debugLog = options.debugLog
  278. self.loggingConfig = options.loggingConfig
  279. self.firstPackageWins = options.firstPackageWins
  280. self.configureLogging()
  281. if options.where is not None:
  282. self.configureWhere(options.where)
  283. if options.testMatch:
  284. self.testMatch = re.compile(options.testMatch)
  285. if options.ignoreFiles:
  286. self.ignoreFiles = map(re.compile, tolist(options.ignoreFiles))
  287. log.info("Ignoring files matching %s", options.ignoreFiles)
  288. else:
  289. log.info("Ignoring files matching %s", self.ignoreFilesDefaultStrings)
  290. if options.include:
  291. self.include = map(re.compile, tolist(options.include))
  292. log.info("Including tests matching %s", options.include)
  293. if options.exclude:
  294. self.exclude = map(re.compile, tolist(options.exclude))
  295. log.info("Excluding tests matching %s", options.exclude)
  296. # When listing plugins we don't want to run them
  297. if not options.showPlugins:
  298. self.plugins.configure(options, self)
  299. self.plugins.begin()
  300. def configureLogging(self):
  301. """Configure logging for nose, or optionally other packages. Any logger
  302. name may be set with the debug option, and that logger will be set to
  303. debug level and be assigned the same handler as the nose loggers, unless
  304. it already has a handler.
  305. """
  306. if self.loggingConfig:
  307. from logging.config import fileConfig
  308. fileConfig(self.loggingConfig)
  309. return
  310. format = logging.Formatter('%(name)s: %(levelname)s: %(message)s')
  311. if self.debugLog:
  312. handler = logging.FileHandler(self.debugLog)
  313. else:
  314. handler = logging.StreamHandler(self.logStream)
  315. handler.setFormatter(format)
  316. logger = logging.getLogger('nose')
  317. logger.propagate = 0
  318. # only add our default handler if there isn't already one there
  319. # this avoids annoying duplicate log messages.
  320. if handler not in logger.handlers:
  321. logger.addHandler(handler)
  322. # default level
  323. lvl = logging.WARNING
  324. if self.verbosity >= 5:
  325. lvl = 0
  326. elif self.verbosity >= 4:
  327. lvl = logging.DEBUG
  328. elif self.verbosity >= 3:
  329. lvl = logging.INFO
  330. logger.setLevel(lvl)
  331. # individual overrides
  332. if self.debug:
  333. # no blanks
  334. debug_loggers = [ name for name in self.debug.split(',')
  335. if name ]
  336. for logger_name in debug_loggers:
  337. l = logging.getLogger(logger_name)
  338. l.setLevel(logging.DEBUG)
  339. if not l.handlers and not logger_name.startswith('nose'):
  340. l.addHandler(handler)
  341. def configureWhere(self, where):
  342. """Configure the working directory or directories for the test run.
  343. """
  344. from nose.importer import add_path
  345. self.workingDir = None
  346. where = tolist(where)
  347. warned = False
  348. for path in where:
  349. if not self.workingDir:
  350. abs_path = absdir(path)
  351. if abs_path is None:
  352. raise ValueError("Working directory %s not found, or "
  353. "not a directory" % path)
  354. log.info("Set working dir to %s", abs_path)
  355. self.workingDir = abs_path
  356. if self.addPaths and \
  357. os.path.exists(os.path.join(abs_path, '__init__.py')):
  358. log.info("Working directory %s is a package; "
  359. "adding to sys.path" % abs_path)
  360. add_path(abs_path)
  361. continue
  362. if not warned:
  363. warn("Use of multiple -w arguments is deprecated and "
  364. "support may be removed in a future release. You can "
  365. "get the same behavior by passing directories without "
  366. "the -w argument on the command line, or by using the "
  367. "--tests argument in a configuration file.",
  368. DeprecationWarning)
  369. self.testNames.append(path)
  370. def default(self):
  371. """Reset all config values to defaults.
  372. """
  373. self.__dict__.update(self._default)
  374. def getParser(self, doc=None):
  375. """Get the command line option parser.
  376. """
  377. if self.parser:
  378. return self.parser
  379. env = self.env
  380. parser = self.parserClass(doc)
  381. parser.add_option(
  382. "-V","--version", action="store_true",
  383. dest="version", default=False,
  384. help="Output nose version and exit")
  385. parser.add_option(
  386. "-p", "--plugins", action="store_true",
  387. dest="showPlugins", default=False,
  388. help="Output list of available plugins and exit. Combine with "
  389. "higher verbosity for greater detail")
  390. parser.add_option(
  391. "-v", "--verbose",
  392. action="count", dest="verbosity",
  393. default=self.verbosity,
  394. help="Be more verbose. [NOSE_VERBOSE]")
  395. parser.add_option(
  396. "--verbosity", action="store", dest="verbosity",
  397. metavar='VERBOSITY',
  398. type="int", help="Set verbosity; --verbosity=2 is "
  399. "the same as -v")
  400. parser.add_option(
  401. "-q", "--quiet", action="store_const", const=0, dest="verbosity",
  402. help="Be less verbose")
  403. parser.add_option(
  404. "-c", "--config", action="append", dest="files",
  405. metavar="FILES",
  406. help="Load configuration from config file(s). May be specified "
  407. "multiple times; in that case, all config files will be "
  408. "loaded and combined")
  409. parser.add_option(
  410. "-w", "--where", action="append", dest="where",
  411. metavar="WHERE",
  412. help="Look for tests in this directory. "
  413. "May be specified multiple times. The first directory passed "
  414. "will be used as the working directory, in place of the current "
  415. "working directory, which is the default. Others will be added "
  416. "to the list of tests to execute. [NOSE_WHERE]"
  417. )
  418. parser.add_option(
  419. "--py3where", action="append", dest="py3where",
  420. metavar="PY3WHERE",
  421. help="Look for tests in this directory under Python 3.x. "
  422. "Functions the same as 'where', but only applies if running under "
  423. "Python 3.x or above. Note that, if present under 3.x, this "
  424. "option completely replaces any directories specified with "
  425. "'where', so the 'where' option becomes ineffective. "
  426. "[NOSE_PY3WHERE]"
  427. )
  428. parser.add_option(
  429. "-m", "--match", "--testmatch", action="store",
  430. dest="testMatch", metavar="REGEX",
  431. help="Files, directories, function names, and class names "
  432. "that match this regular expression are considered tests. "
  433. "Default: %s [NOSE_TESTMATCH]" % self.testMatchPat,
  434. default=self.testMatchPat)
  435. parser.add_option(
  436. "--tests", action="store", dest="testNames", default=None,
  437. metavar='NAMES',
  438. help="Run these tests (comma-separated list). This argument is "
  439. "useful mainly from configuration files; on the command line, "
  440. "just pass the tests to run as additional arguments with no "
  441. "switch.")
  442. parser.add_option(
  443. "-l", "--debug", action="store",
  444. dest="debug", default=self.debug,
  445. help="Activate debug logging for one or more systems. "
  446. "Available debug loggers: nose, nose.importer, "
  447. "nose.inspector, nose.plugins, nose.result and "
  448. "nose.selector. Separate multiple names with a comma.")
  449. parser.add_option(
  450. "--debug-log", dest="debugLog", action="store",
  451. default=self.debugLog, metavar="FILE",
  452. help="Log debug messages to this file "
  453. "(default: sys.stderr)")
  454. parser.add_option(
  455. "--logging-config", "--log-config",
  456. dest="loggingConfig", action="store",
  457. default=self.loggingConfig, metavar="FILE",
  458. help="Load logging config from this file -- bypasses all other"
  459. " logging config settings.")
  460. parser.add_option(
  461. "-I", "--ignore-files", action="append", dest="ignoreFiles",
  462. metavar="REGEX",
  463. help="Completely ignore any file that matches this regular "
  464. "expression. Takes precedence over any other settings or "
  465. "plugins. "
  466. "Specifying this option will replace the default setting. "
  467. "Specify this option multiple times "
  468. "to add more regular expressions [NOSE_IGNORE_FILES]")
  469. parser.add_option(
  470. "-e", "--exclude", action="append", dest="exclude",
  471. metavar="REGEX",
  472. help="Don't run tests that match regular "
  473. "expression [NOSE_EXCLUDE]")
  474. parser.add_option(
  475. "-i", "--include", action="append", dest="include",
  476. metavar="REGEX",
  477. help="This regular expression will be applied to files, "
  478. "directories, function names, and class names for a chance "
  479. "to include additional tests that do not match TESTMATCH. "
  480. "Specify this option multiple times "
  481. "to add more regular expressions [NOSE_INCLUDE]")
  482. parser.add_option(
  483. "-x", "--stop", action="store_true", dest="stopOnError",
  484. default=self.stopOnError,
  485. help="Stop running tests after the first error or failure")
  486. parser.add_option(
  487. "-P", "--no-path-adjustment", action="store_false",
  488. dest="addPaths",
  489. default=self.addPaths,
  490. help="Don't make any changes to sys.path when "
  491. "loading tests [NOSE_NOPATH]")
  492. parser.add_option(
  493. "--exe", action="store_true", dest="includeExe",
  494. default=self.includeExe,
  495. help="Look for tests in python modules that are "
  496. "executable. Normal behavior is to exclude executable "
  497. "modules, since they may not be import-safe "
  498. "[NOSE_INCLUDE_EXE]")
  499. parser.add_option(
  500. "--noexe", action="store_false", dest="includeExe",
  501. help="DO NOT look for tests in python modules that are "
  502. "executable. (The default on the windows platform is to "
  503. "do so.)")
  504. parser.add_option(
  505. "--traverse-namespace", action="store_true",
  506. default=self.traverseNamespace, dest="traverseNamespace",
  507. help="Traverse through all path entries of a namespace package")
  508. parser.add_option(
  509. "--first-package-wins", "--first-pkg-wins", "--1st-pkg-wins",
  510. action="store_true", default=False, dest="firstPackageWins",
  511. help="nose's importer will normally evict a package from sys."
  512. "modules if it sees a package with the same name in a different "
  513. "location. Set this option to disable that behavior.")
  514. self.plugins.loadPlugins()
  515. self.pluginOpts(parser)
  516. self.parser = parser
  517. return parser
  518. def help(self, doc=None):
  519. """Return the generated help message
  520. """
  521. return self.getParser(doc).format_help()
  522. def pluginOpts(self, parser):
  523. self.plugins.addOptions(parser, self.env)
  524. def reset(self):
  525. self.__dict__.update(self._orig)
  526. def todict(self):
  527. return self.__dict__.copy()
  528. def update(self, d):
  529. self.__dict__.update(d)
  530. class NoOptions(object):
  531. """Options container that returns None for all options.
  532. """
  533. def __getstate__(self):
  534. return {}
  535. def __setstate__(self, state):
  536. pass
  537. def __getnewargs__(self):
  538. return ()
  539. def __getattr__(self, attr):
  540. return None
  541. def __nonzero__(self):
  542. return False
  543. def user_config_files():
  544. """Return path to any existing user config files
  545. """
  546. return filter(os.path.exists,
  547. map(os.path.expanduser, config_files))
  548. def all_config_files():
  549. """Return path to any existing user config files, plus any setup.cfg
  550. in the current working directory.
  551. """
  552. user = user_config_files()
  553. if os.path.exists('setup.cfg'):
  554. return user + ['setup.cfg']
  555. return user
  556. # used when parsing config files
  557. def flag(val):
  558. """Does the value look like an on/off flag?"""
  559. if val == 1:
  560. return True
  561. elif val == 0:
  562. return False
  563. val = str(val)
  564. if len(val) > 5:
  565. return False
  566. return val.upper() in ('1', '0', 'F', 'T', 'TRUE', 'FALSE', 'ON', 'OFF')
  567. def _bool(val):
  568. return str(val).upper() in ('1', 'T', 'TRUE', 'ON')